def test_get_multiple_measurements(self): distance_sensor = DistanceToPillarSensor() height_sensor = HeightSensor() distance1 = distance_sensor.get_distance() distance2 = distance_sensor.get_distance() distance3 = distance_sensor.get_distance() height1 = height_sensor.get_height() height2 = height_sensor.get_height() height3 = height_sensor.get_height() self.__log.write( str(distance1) + ", " + str(distance2) + ", " + str(distance3) + ", " + str(height1) + ", " + str( height2) + ", " + str(height3)) if distance1 is None or distance2 is None or distance3 is None: average_distance = None else: average_distance = (distance1 + distance2 + distance3) / 3 print "Distance: " + str(average_distance) + " " if height1 is None or height2 is None or height3 is None: average_height = None else: average_height = (height1 + height2 + height3) / 3 print "Height: " + str(average_height) + " "
def test_get_multiple_measurements(self): distance_sensor = DistanceToPillarSensor() height_sensor = HeightSensor() distance1 = distance_sensor.get_distance() distance2 = distance_sensor.get_distance() distance3 = distance_sensor.get_distance() height1 = height_sensor.get_height() height2 = height_sensor.get_height() height3 = height_sensor.get_height() print "Distance 1: " + str(distance1) print "Distance 2: " + str(distance2) print "Distance 3: " + str(distance3) print "Height 1: " + str(height1) print "Height 2: " + str(height2) print "Height 3: " + str(height3) average_distance = (distance1 + distance2 + distance3)/3 distance_difference = distance1 - average_distance print "Difference: " + str(distance_difference) average_height = (height1 + height2 + height3)/3 height_difference = height1 - average_height print "Difference: " + str(height_difference) assert -2.5 < distance_difference < 2.5 assert -2.5 < height_difference < 2.5
def test_get_multiple_measurements(self): sensor = HeightSensor() height1 = sensor.get_height() height2 = sensor.get_height() height3 = sensor.get_height() print "Height 1: " + str(height1) print "Height 2: " + str(height2) print "Height 3: " + str(height3) average_height = (height1 + height2 + height3) / 3 difference = height1 - average_height print "Difference: " + str(difference) assert -2.5 < difference < 2.5
def __init__(self): super(PiController, self).__init__() self.not_stopped = True self.__height = 0.0 self.__distance = 0.0 self.__target_recognised = False self.__distance_to_target = 0.0 self.__logger = LogFileWriter() self.__communication_interface_listener = CommunicatorFactory.get_communication_interface_listener( ) self.__communication_interface_sender = CommunicatorFactory.get_communication_interface_sender( ) self.__camera_sensor_factory = CameraSensorFactory() self.__camera_sensor = CameraSensorFactory.get_sensor_controller() self.__height_sensor = HeightSensor() self.__distance_sensor = DistanceToPillarSensor() if isinstance(self.__communication_interface_listener, CommunicationInterfaceListener): self.__logger.info("Setting up listener") self.__communication_interface_listener.start() self.__communication_interface_listener.attach(self) else: self.__logger.warn("Listener not started!") warnings.warn("Listener not started", RuntimeWarning) if not isinstance(self.__communication_interface_sender, CommunicationInterfaceSender): self.__logger.warn( "Sender is not subtype of CommunicationInterfaceSender") warnings.warn( "Sender is not subtype of CommunicationInterfaceSender")
log_file.write("Distance 1: " + str(distance1) + '\n') log_file.write("Distance 2: " + str(distance2) + '\n') log_file.write("Distance 3: " + str(distance3) + '\n') log_file.write("Height 1: " + str(height1) + '\n') log_file.write("Height 2: " + str(height2) + '\n') log_file.write("Height 3: " + str(height3) + '\n') if not (distance1 is None or distance2 is None or distance3 is None): average_distance = (distance1 + distance2 + distance3) / 3 log_file.write(str(average_distance) + '\n') if not (height1 is None or height2 is None or height3 is None): average_height = (height1 + height2 + height3) / 3 log_file.write(str(average_height) + '\n') log_file.close() if __name__ == '__main__': distance_sensor = DistanceToPillarSensor() height_sensor = HeightSensor() file = '/home/pi/logs/us_log' + str(datetime.datetime.now()) + '.log' with open(file, 'w') as logfile: logfile.write(str(datetime.datetime.now()) + '\n') index = 1 while True: test_get_multiple_measurements(file, index) index += 1
def test_get_height(self): sensor = HeightSensor() height = sensor.get_height() self.assertIsInstance(height, float) print height sensor.close()
class PiController(CommunicationInterfaceObserver): # Implementation of SerialInterfaceObserver methods def update(self, received_message): if received_message == "SendData": self.__logger.info("Received request for data from ET board") data_to_send = '' usb, lsb = self.__transform_to_char_ints(self.__height * 10) data_to_send = usb + lsb # Add 14cm, as the sensor is not in the middle actual_distance = self.__distance + 14 usb, lsb = self.__transform_to_char_ints(actual_distance * 10) data_to_send = data_to_send + usb + lsb if self.__target_recognised: # Add 18 cm, as the camera is in front distance_to_send = self.__distance_to_target + 18.0 usb, lsb = self.__transform_to_char_ints(distance_to_send * 10) data_to_send = data_to_send + usb + lsb else: data_to_send = data_to_send + chr(255) + chr(255) self.__logger.info(data_to_send) self.__communication_interface_sender.send_message(data_to_send) def __transform_to_char_ints(self, value): result_value = int(round(value)) integer_as_bitstring = '{0:016b}'.format(result_value) upper_significant_byte_as_char = chr(int(integer_as_bitstring[:8], 2)) lower_significant_byte_as_char = chr(int(integer_as_bitstring[8:], 2)) return upper_significant_byte_as_char, lower_significant_byte_as_char def print_debug_information(self): self.__logger.info("Height: " + str(self.__height) + "; Distance: " + str(self.__distance) + "; Target: " + str(self.__distance_to_target)) def __init__(self): super(PiController, self).__init__() self.not_stopped = True self.__height = 0.0 self.__distance = 0.0 self.__target_recognised = False self.__distance_to_target = 0.0 self.__logger = LogFileWriter() self.__communication_interface_listener = CommunicatorFactory.get_communication_interface_listener( ) self.__communication_interface_sender = CommunicatorFactory.get_communication_interface_sender( ) self.__camera_sensor_factory = CameraSensorFactory() self.__camera_sensor = CameraSensorFactory.get_sensor_controller() self.__height_sensor = HeightSensor() self.__distance_sensor = DistanceToPillarSensor() if isinstance(self.__communication_interface_listener, CommunicationInterfaceListener): self.__logger.info("Setting up listener") self.__communication_interface_listener.start() self.__communication_interface_listener.attach(self) else: self.__logger.warn("Listener not started!") warnings.warn("Listener not started", RuntimeWarning) if not isinstance(self.__communication_interface_sender, CommunicationInterfaceSender): self.__logger.warn( "Sender is not subtype of CommunicationInterfaceSender") warnings.warn( "Sender is not subtype of CommunicationInterfaceSender") def close(self): self.__logger.info("Cleaning up PiController object") self.__logger.close() self.__communication_interface_listener.stop() self.__height_sensor.close() self.__distance_sensor.close() self.__camera_sensor.close() def control(self): measured_height = self.__height_sensor.get_height() if 0 < measured_height < 500: self.__height = measured_height measured_distance = self.__distance_sensor.get_distance() if 0 < measured_distance < 500: self.__distance = measured_distance self.__camera_sensor.set_height(self.__height) self.__target_recognised, distance = self.__camera_sensor.get_target_and_distance( )