Coverage for /Users/fmorton/GitHub/BirdBrain-Python-Library-2/src/birdbrain_finch_output.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-12 11:24 -0400

1import time 

2 

3from birdbrain_constant import BirdbrainConstant 

4from birdbrain_finch_input import BirdbrainFinchInput 

5from birdbrain_request import BirdbrainRequest 

6from birdbrain_utility import BirdbrainUtility 

7 

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) 

13 

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.""" 

18 

19 if not port == "all": 

20 port = int(port) + 1 # tail starts counting at 2 

21 

22 return self.tri_led_response(device, port, r_intensity, g_intensity, b_intensity, BirdbrainConstant.VALID_TAIL_PORTS, True) 

23 

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 

29 

30 if direction == BirdbrainConstant.FORWARD: calc_direction = 'Forward' 

31 if direction == BirdbrainConstant.BACKWARD: calc_direction = 'Backward' 

32 

33 calc_distance = BirdbrainUtility.bounds(distance, -10000, 10000) 

34 calc_speed = BirdbrainUtility.bounds(speed, 0, 100) 

35 

36 return self.__move_and_wait(device, wait_to_finish_movement, 'hummingbird', 'out', 'move', device, calc_direction, calc_distance, calc_speed) 

37 

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) 

45 

46 return self.__move_and_wait(device, wait_to_finish_movement, 'hummingbird', 'out', 'turn', device, calc_direction, calc_angle, calc_speed) 

47 

48 @classmethod 

49 def wait(self, device): 

50 timeout_time = time.time() + BirdbrainConstant.MOVE_TIMEOUT_SECONDS 

51 

52 while (timeout_time > time.time()) and (BirdbrainFinchInput.is_moving(device)): 

53 time.sleep(BirdbrainConstant.MOVE_CHECK_MOVING_DELAY) 

54 

55 return True 

56 

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.""" 

61 

62 left_speed = BirdbrainUtility.bounds(left_speed, -100, 100) 

63 right_speed = BirdbrainUtility.bounds(right_speed, -100, 100) 

64 

65 return BirdbrainRequest.response_status('hummingbird', 'out', 'wheels', device, left_speed, right_speed) 

66 

67 @classmethod 

68 def stop(self, device): 

69 """Stop the Finch motors.""" 

70 

71 return BirdbrainRequest.response_status('hummingbird', 'out', 'stopFinch', device) 

72 

73 @classmethod 

74 def reset_encoders(self, device): 

75 """Reset both encoder values to 0.""" 

76 

77 response = BirdbrainRequest.response_status('hummingbird', 'out', 'resetEncoders', device) 

78 

79 time.sleep(BirdbrainConstant.RESET_ENCODERS_DELAY) # finch needs a chance to actually reset 

80 

81 return response 

82 

83 @classmethod 

84 def __move_and_wait(self, device, wait_to_finish_movement, *args): 

85 response = BirdbrainRequest.response_status(*args) 

86 

87 time.sleep(BirdbrainConstant.MOVE_START_WAIT_SECONDS) # hack to give time to start before waiting 

88 

89 if wait_to_finish_movement: self.wait(device) 

90 

91 return response