MCPcopy Create free account
hub / github.com/DexterInd/GoPiGo3 / read_mm

Method read_mm

Software/Python/easysensors.py:844–900  ·  view source on GitHub ↗

Measures the distance from a target in millimeters. :returns: The distance from a target in millimeters. :rtype: int :raises gopigo3.ValueError: If trying to read an invalid value. :raises Exception: If any other error occurs. .. important::

(self)

Source from the content-addressed store, hash-verified

842 return self.safe_distance
843
844 def read_mm(self):
845 """
846 Measures the distance from a target in millimeters.
847
848 :returns: The distance from a target in millimeters.
849 :rtype: int
850 :raises gopigo3.ValueError: If trying to read an invalid value.
851 :raises Exception: If any other error occurs.
852
853 .. important::
854
855 * This method can read distances between **15-4300** millimeters.
856 * This method will read the data for 3 times and it'll discard anything that's smaller than 15 millimeters and bigger than 4300 millimeters.
857 * If data is discarded 5 times (due to a communication error with the sensor), then the method returns **5010**.
858
859 """
860 return_reading = 0
861 readings = []
862 skip = 0
863 value=0
864
865 while len(readings) < 3 and skip < 5:
866 try:
867 value = self.gpg.get_grove_value(self.get_port_ID())
868 print ("raw {}".format(value))
869 except gopigo3.ValueError as e:
870 # print("Value Error")
871 # print(e)
872 value = 5010 # assume open road ahead
873 time.sleep(0.05)
874
875 except Exception as e:
876 print(e)
877 skip += 1
878 time.sleep(0.05)
879 continue
880
881 if value <= 4300 and value >= 15:
882 readings.append(value)
883 # debug (readings)
884 else:
885 skip += 1
886
887 if skip >= 5:
888 # if value = 0 it means Ultrasonic Sensor wasn't found
889 if value == 0:
890 return(0)
891
892 # no special meaning to the number 5010
893 return(5010)
894
895 for reading in readings:
896 return_reading += reading
897
898 return_reading = int(return_reading / len(readings))
899
900 return (return_reading)
901

Callers 5

readMethod · 0.95
MainFunction · 0.80

Calls 2

get_port_IDMethod · 0.80
get_grove_valueMethod · 0.45

Tested by

no test coverage detected