-
Notifications
You must be signed in to change notification settings - Fork 0
/
movement.py
executable file
·64 lines (44 loc) · 1.3 KB
/
movement.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
53
54
55
56
57
58
59
60
61
62
63
64
import gopigo3
from time import sleep
from easygopigo3 import EasyGoPiGo3
gpg = gopigo3.GoPiGo3()
GPG = EasyGoPiGo3()
motor_right = gpg.MOTOR_RIGHT
motor_left = gpg.MOTOR_LEFT
port = "AD1"
sensor = GPG.init_ultrasonic_sensor(port)
sensor.set_safe_distance(127)
def direction(speed=0.5, turn=0, t=2):
speed *= 100
turn *= 70
left_speed = speed + turn
right_speed = speed - turn
if left_speed > 100:
left_speed = 100
elif left_speed < -100:
left_speed = -100
if right_speed > 100:
right_speed = 100
elif right_speed < -100:
right_speed = -100
gpg.set_motor_power(motor_right, right_speed)
gpg.set_motor_power(motor_left, left_speed)
sleep(t)
def forward(speed=50):
gpg.set_motor_power(motor_right + motor_left, speed)
def reverse(speed=50):
gpg.set_motor_power(motor_right + motor_left, -speed)
def right_turn(speed=50):
gpg.set_motor_power(motor_left, speed)
gpg.set_motor_power(motor_right, 0)
def left_turn(speed=50):
gpg.set_motor_power(motor_right, speed)
gpg.set_motor_power(motor_left, 0)
def stop(t=0):
gpg.set_motor_power(motor_left + motor_right, 0)
sleep(t)
def main(): # main in this class used for isolated motor testing
stop(2)
if __name__ == '__main__':
while True:
main()