This repository has been archived by the owner on Jan 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manual.py
84 lines (74 loc) · 2.29 KB
/
manual.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from move import Move
from RPi.GPIO import cleanup
import traceback
from serial import Serial
from time import sleep
import io
def main():
"Main robot logic for manual control
"
# Setting up serial over bluetooth
ser = Serial('/dev/rfcomm0', timeout=1)
ser.reset_input_buffer()
# Setting up io stream to read unicode string
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
sio.flush()
m = Move()
m.move_set_speed(m.MIN_SPEED)
print("Ready!")
try:
while True:
ser.reset_input_buffer()
sio.flush()
input = sio.readline().strip()
if input: # if not a blank line
input = input.split(',')
joy_x = input[0]
joy_y = input[1]
joy_x = int(joy_x)
joy_y = int(joy_y)
print("x: ", joy_x)
print("y: ", joy_y)
# Joystick left
if joy_x < 50:
m.move_rotate(-90)
sleep(1)
# Joystick right
if joy_x > 200:
m.move_rotate(90)
sleep(1)
# Joystick not forward
if joy_y < 200:
# Joystick backward
if joy_y < 50:
m.move_backward()
sleep(1)
# Joystick neutral
else:
m.move_stop()
sleep(1)
# Joystick forward
if joy_y > 200:
m.move_forward()
sleep(1)
ser.reset_input_buffer()
ser.reset_output_buffer()
else:
ser.reset_input_buffer()
ser.reset_output_buffer()
except KeyboardInterrupt: # Reset by pressing CTRL + C
print("Error:", traceback.format_exc())
print("Stopping motors...:")
m.move_stop()
cleanup()
sio.flush()
ser.close()
except Exception:
print("Error:", traceback.format_exc())
print("Stopping motors...:")
m.move_stop()
cleanup()
sio.flush()
ser.close()
if __name__ == '__main__':
main()