def test_send_packet_empty(self): """ Tests json conversion of no keys. """ send_packet = web.ServerSendPacket() json_string = send_packet.get_json() self.assertDictEqual({}, json.loads(json_string))
def test_send_packet_partial(self): """ Tests a json conversion with only a few keys. """ send_packet = web.ServerSendPacket(debug_string=DEBUG_STRING_PARTIAL) json_string = send_packet.get_json() self.assertDictEqual(EXPECTED_PARTIAL, json.loads(json_string))
def test_send_packet_normal_json(self): """ Tests a normal json conversion with all of the possible keys. """ sensor_data_packet = avr_communication.SensorDataPacket(*SENSOR_ARGS) send_packet = web.ServerSendPacket(sensor_data_packet, AUTO_NORMAL, DEBUG_STRING_NORMAL) json_string = send_packet.get_json() self.assertDictEqual(EXPECTED_NORMAL, json.loads(json_string))
def do_auto_mode_iteration(sensor_spi, motor_spi, send_queue, receive_queue, decision_packet, prev_speed, prev_x, prev_y, prev_rot): """ Does one iteration of the auto mode """ sensor_data = avr_communication.get_sensor_data(sensor_spi) decision_making.get_decision(sensor_data, decision_packet, motor_spi) print("Decision: ", decision_making.int_to_string_command(decision_packet.decision)) pid_controller.regulate(sensor_data, decision_packet) send_decision_avr(motor_spi, decision_packet, prev_speed, prev_x, prev_y, prev_rot) # Send decision to server send_queue.put(web.ServerSendPacket(sensor_data_packet=sensor_data)) send_queue.put( web.ServerSendPacket(debug_string=decision_making. int_to_string_command(decision_packet.decision))) auto = True packet = receive_server_packet(receive_queue) if packet is not None: if packet.auto is not None: auto = packet.auto avr_communication.walk(motor_spi, 0, 0, 0, False, 100) # Regulate algorithm parameters if packet.angle_scaledown is not None: decision_packet.regulate_angle_scaledown = packet.angle_scaledown if packet.movement_scaledown is not None: decision_packet.regulate_movement_scaledown = packet.movement_scaledown if packet.angle_adjustment_border is not None: decision_packet.regulate_angular_adjustment_border = packet.angle_adjustment_border return auto, prev_speed, prev_x, prev_y, prev_rot
def check_auto_toggle_button(button_temp, auto, send_queue): """ Checks whether the user pressed the auto mode toggle button and updates button_temp and auto accordingly. """ button_input = GPIO.input(AUTO_BUTTON_PIN) if button_input == 1: if (button_temp != button_input): auto = not auto button_temp = 1 send_queue.put(web.ServerSendPacket(auto_mode=auto)) else: button_temp = 0 return button_temp, auto
def do_manual_mode_iteration(sensor_spi, motor_spi, send_queue, receive_queue, prev_speed, prev_x, prev_y, prev_rot): """ Does one iteration of the manual mode """ try: sensor_data = avr_communication.get_sensor_data(sensor_spi) send_queue.put(web.ServerSendPacket(sensor_data)) except avr_communication.CommunicationError as e: print("Could not read sensor data: " + str(e)) auto = False packet = receive_server_packet(receive_queue) if packet is not None: if packet.auto is not None: auto = packet.auto avr_communication.walk(motor_spi, 0, 0, 0, False, 100) elif packet.return_to_neutral is not None and packet.return_to_neutral: avr_communication.back_to_neutral(motor_spi) elif packet.has_motion_command(): servo_speed = (int)(packet.thrust * constants.MAX_16BIT_SIZE / SERVO_SPEED_SCALER) return_to_neutral = packet.return_to_neutral if prev_speed != servo_speed: # x_speed from server = -y_speed and y_speed from server = -x_speed avr_communication.set_servo_speed(motor_spi, servo_speed, 100) x_speed = convert_to_sendable_byte(-packet.y) y_speed = convert_to_sendable_byte(-packet.x) rotation = convert_to_sendable_byte(packet.rotation) if x_speed != prev_x or y_speed != prev_y or rotation != prev_rot: avr_communication.walk(motor_spi, x_speed, y_speed, rotation, False, 100) prev_x = x_speed prev_y = y_speed prev_rot = rotation prev_speed = servo_speed return auto, prev_speed, prev_x, prev_y, prev_rot