Coverage for /Users/fmorton/GitHub/BirdBrain-Python-Library-2/src/birdbrain_finch_output.py: 100%
53 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-04 20:20 -0500
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-04 20:20 -0500
1import time
3from birdbrain_constant import BirdbrainConstant
4from birdbrain_finch_input import BirdbrainFinchInput
5from birdbrain_request import BirdbrainRequest
6from birdbrain_utility import BirdbrainUtility
8class BirdbrainFinchOutput(BirdbrainRequest):
9 @classmethod
10 def beak(self, device, r_intensity, g_intensity, b_intensity):
11 """Set beak to a valid intensity. Each intensity should be an integer from 0 to 100."""
12 return self.tri_led_response(device, 1, r_intensity, g_intensity, b_intensity, BirdbrainConstant.VALID_BEAK_PORTS)
14 @classmethod
15 def tail(self, device, port, r_intensity, g_intensity, b_intensity):
16 """Set tail to a valid intensity. Port can be specified as 1, 2, 3, 4, or all.
17 Each intensity should be an integer from 0 to 100."""
19 if not port == "all":
20 port = int(port) + 1 # tail starts counting at 2
22 return self.tri_led_response(device, port, r_intensity, g_intensity, b_intensity, BirdbrainConstant.VALID_TAIL_PORTS, True)
24 @classmethod
25 def move(self, device, direction, distance, speed, wait_to_finish_movement = True):
26 """Move the Finch forward or backward for a given distance at a given speed.
27 Direction should be specified as 'F' or 'B'."""
28 calc_direction = None
30 if direction == BirdbrainConstant.FORWARD: calc_direction = 'Forward'
31 if direction == BirdbrainConstant.BACKWARD: calc_direction = 'Backward'
33 calc_distance = BirdbrainUtility.bounds(distance, -10000, 10000)
34 calc_speed = BirdbrainUtility.bounds(speed, 0, 100)
36 return self.__move_and_wait(device, wait_to_finish_movement, 'hummingbird', 'out', 'move', device, calc_direction, calc_distance, calc_speed)
38 @classmethod
39 def turn(self, device, direction, angle, speed, wait_to_finish_movement = True):
40 """Turn the Finch right or left to a given angle at a given speed.
41 Direction should be specified as 'R' or 'L'."""
42 calc_direction = BirdbrainRequest.calculate_left_or_right(direction)
43 calc_angle = BirdbrainUtility.bounds(angle, 0, 360)
44 calc_speed = BirdbrainUtility.bounds(speed, 0, 100)
46 return self.__move_and_wait(device, wait_to_finish_movement, 'hummingbird', 'out', 'turn', device, calc_direction, calc_angle, calc_speed)
48 @classmethod
49 def wait(self, device):
50 timeout_time = time.time() + BirdbrainConstant.MOVE_TIMEOUT_SECONDS
52 while (timeout_time > time.time()) and (BirdbrainFinchInput.is_moving(device)):
53 time.sleep(BirdbrainConstant.MOVE_CHECK_MOVING_DELAY)
55 return True
57 @classmethod
58 def motors(self, device, left_speed, right_speed):
59 """Set the speed of each motor individually. Speed should be in
60 the range of -100 to 100."""
62 left_speed = BirdbrainUtility.bounds(left_speed, -100, 100)
63 right_speed = BirdbrainUtility.bounds(right_speed, -100, 100)
65 return BirdbrainRequest.response_status('hummingbird', 'out', 'wheels', device, left_speed, right_speed)
67 @classmethod
68 def stop(self, device):
69 """Stop the Finch motors."""
71 return BirdbrainRequest.response_status('hummingbird', 'out', 'stopFinch', device)
73 @classmethod
74 def reset_encoders(self, device):
75 """Reset both encoder values to 0."""
77 response = BirdbrainRequest.response_status('hummingbird', 'out', 'resetEncoders', device)
79 time.sleep(BirdbrainConstant.RESET_ENCODERS_DELAY) # finch needs a chance to actually reset
81 return response
83 @classmethod
84 def __move_and_wait(self, device, wait_to_finish_movement, *args):
85 response = BirdbrainRequest.response_status(*args)
87 time.sleep(BirdbrainConstant.MOVE_START_WAIT_SECONDS) # hack to give time to start before waiting
89 if wait_to_finish_movement: self.wait(device)
91 return response