Skip to content

Commit cb48450

Browse files
committed
Added servo Calibration
1 parent 26d137f commit cb48450

File tree

7 files changed

+157
-116
lines changed

7 files changed

+157
-116
lines changed

bubo/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Bubo Init
2+
3+
from .bubo_library import Bubo2t

bubo/bubo_library.py

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Bubo-2T the tooting robot
2+
# 2T will take a photo if you do specific hand gestures in front of it
3+
# It will then toot those pictures to mastodon.social
4+
5+
from time import sleep
6+
# from gpiozero import Servo
7+
from .toot_randomiser import RandomToots
8+
from servo import servo2040, Servo
9+
import plasma
10+
from plasma import WS2812
11+
import math
12+
13+
GREEN = (0, 255, 0)
14+
RED = (255, 0, 0)
15+
BLUE = (0, 0, 255)
16+
WHITE = (255, 255, 255)
17+
BLACK = (0, 0, 0)
18+
YELLOW = (255, 255, 0)
19+
CYAN = (0, 255, 255)
20+
MAGENTA = (255, 0, 255)
21+
22+
# Set GPIO pins for the servos
23+
LEFT_EYE_PIN = servo2040.SERVO_3 # GPIO pin
24+
RIGHT_EYE_PIN = servo2040.SERVO_2 # GPIO pin
25+
MOUTH_PIN = servo2040.SERVO_1 # GPIO pin
26+
27+
# Calibration allows us to limit the angle of the servo between and upper and lower value
28+
# We need to provide the lower pulse width, upper pulse width and the lower and upper angles
29+
MOUTH_MIN_PULSE = 1000
30+
MOUTH_MAX_PULSE = 2000
31+
MOUTH_MIN_ANGLE = -45
32+
MOUTH_MAX_ANGLE = 45
33+
34+
EYE_MIN_PULSE = 1000
35+
EYE_MAX_PULSE = 2000
36+
EYE_MIN_ANGLE = -45
37+
EYE_MAX_ANGLE = 45
38+
39+
class Bubo2t():
40+
__mouth_open = -45 # open
41+
__mouth_close = 45 # closed
42+
__eyes_open = -45 # open
43+
__eyes_close = 45 # closed
44+
45+
def __init__(self, left_eye_pin=LEFT_EYE_PIN, right_eye_pin=RIGHT_EYE_PIN, mouth_pin=MOUTH_PIN):
46+
self.toot_randomiser = RandomToots()
47+
48+
# Setup the Servos
49+
self.left_eye = Servo(left_eye_pin)
50+
self.right_eye = Servo(right_eye_pin)
51+
self.mouth = Servo(mouth_pin)
52+
53+
print('Bubo2t init')
54+
print(f'mouth pin = {self.mouth.pin()}')
55+
print(f'left eye pin = {self.left_eye.pin()}')
56+
print(f'right eye pin = {self.right_eye.pin()}')
57+
self.left_eye.value(0)
58+
self.right_eye.value(0)
59+
self.mouth.value(0)
60+
print('-'*10)
61+
62+
# Apply the calibration to Mouth, Left Eye and Right Eye
63+
mouth_cal.apply_two_pairs(MOUTH_MIN_PULSE, MOUTH_MAX_PULSE, MOUTH_MIN_ANGLE, MOUTH_MAX_ANGLE)
64+
mouth_cal = self.mouth.calibration()
65+
self.mouth.calibration(mouth_cal)
66+
67+
eye_cal = self.left_eye.calibration()
68+
eye_cal.apply_two_pairs(EYE_MIN_PULSE, EYE_MAX_PULSE, EYE_MIN_ANGLE, EYE_MAX_ANGLE)
69+
self.left_eye.calibration(eye_cal)
70+
71+
eye_cal = self.right_eye.calibration()
72+
eye_cal.apply_two_pairs(EYE_MIN_PULSE, EYE_MAX_PULSE, EYE_MIN_ANGLE, EYE_MAX_ANGLE)
73+
self.right_eye.calibration(eye_cal)
74+
75+
def tick(self):
76+
""" Perform a tick """
77+
print('tick')
78+
79+
# TODO perform current action
80+
# TODO check for new action
81+
sleep(1)
82+
83+
def mouth_open(self):
84+
""" Open the mouth """
85+
86+
print('mouth_open')
87+
88+
# Move the servos to different angles
89+
from_val = float(self.mouth.value())
90+
val = float(self.__mouth_open)
91+
print(f'moving mouth from {from_val} to {val}')
92+
self.mouth.value(val)
93+
sleep(0.1)
94+
95+
def mouth_close(self):
96+
""" Close the mouth """
97+
98+
print('mouth_close')
99+
100+
# Move the servos to different angles
101+
from_val = float(self.mouth.value())
102+
val = float(self.__mouth_close)
103+
print(f'moving mouth from {from_val} to {val}')
104+
self.mouth.value(val)
105+
sleep(0.1)
106+
107+
def eyes_open(self, speed:None):
108+
""" Open the eyes """
109+
110+
self.right_eye.value(self.__eyes_open)
111+
self.left_eye.value(self.__eyes_open)
112+
sleep(0.1)
113+
114+
def eyes_close(self):
115+
""" Close the eyes """
116+
117+
self.right_eye.value(self.__eyes_close)
118+
self.left_eye.value(self.__eyes_close)
119+
sleep(0.1)
120+
121+
def open_left_eye(self):
122+
""" Open the left eye """
123+
self.left_eye.value(self.__eyes_open)
124+
sleep(0.1)
125+
126+
def close_left_eye(self):
127+
""" Close the left eye """
128+
self.left_eye.value(self.__eyes_close)
129+
sleep(0.1)
130+
131+
def open_right_eye(self):
132+
""" Open the right eye """
133+
self.right_eye.value(self.__eyes_open)
134+
sleep(0.1)
135+
136+
def close_right_eye(self):
137+
""" Close the right eye """
138+
self.right_eye.value(self.__eyes_close)
139+
sleep(0.1)
140+
141+
def eyes_colour(self, colour):
142+
""" Set the colour of the eyes """
143+
# colour is a tuple of RGB values
144+
# Set the neopixel strip to the colour using set rgb
145+
pass

mood.py renamed to bubo/mood.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ def __init__(self):
6666
def tick(self):
6767
super().tick()
6868

69-
class Bubo():
69+
# class Bubo():
7070

71-
angry_mood = Mood("angry")
71+
# angry_mood = Mood("angry")
7272

73-
def __init__(self):
74-
self.mood = "neutral"
73+
# def __init__(self):
74+
# self.mood = "neutral"
File renamed without changes.

bubo.py renamed to bubo2t.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from bubo_library import Bubo2t
1+
from bubo import Bubo2t
22
from time import sleep
33

44
bubo = Bubo2t()
55

66
bubo.mouth_open()
7-
sleep(1)
7+
sleep(0.5)
88

99
bubo.mouth_close()
10-
sleep(1)
10+
sleep(0.5)
1111

12+
bubo.mouth.to_mid()

bubo_library.py

-108
This file was deleted.

mood_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Mood test
22

3-
from mood import Bubo, Mood
3+
from bubo.mood import Bubo, Mood
44

55
bubo = Bubo()
66
bubo.mood = "angry"

0 commit comments

Comments
 (0)