-
Notifications
You must be signed in to change notification settings - Fork 0
/
servo.py
52 lines (44 loc) · 1.4 KB
/
servo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import time
import Adafruit_PCA9685
class Robot:
def __init__(self):
self.pwm = Adafruit_PCA9685.PCA9685()
self.pwm.set_pwm_freq(60)
def set_servo_pulse(self, channel, pulse):
pulse_length = 1000000
pulse_length //= 60
pulse *= 1000
pulse //= pulse_length
self.pwm.set_pwm(channel, 0, pulse)
def move_servo(self, channel, pulse):
self.pwm.set_pwm(channel, 0, pulse)
time.sleep(0.01)
def normal(self):
positions = [320, 400, 500, 440, 120, 240, 200, 280]
for i in range(8):
self.move_servo(i, positions[i])
def moveforward(self):
# Define the positions for each step
steps = [
[320, 400, 500, 400, 120, 240, 200, 250],
[280, 400, 500, 400, 100, 280, 300, 250],
[320, 400, 500, 400, 220, 350, 200, 250],
[280, 400, 500, 400, 100, 280, 300, 250]
]
# Execute each step
for step in steps:
for i in range(8):
self.move_servo(i, step[i])
time.sleep(1)
def kick(self):
positions = [320, 400, 500, 440, 120, 240, 200, 280]
for _ in range(2):
for i in range(8):
self.move_servo(i, positions[i])
def main():
robot = Robot()
print('press Ctrl-C to quit...')
while True:
robot.kick()
if __name__ == "__main__":
main()