-
Notifications
You must be signed in to change notification settings - Fork 20
/
raspirobotboard.py
135 lines (102 loc) · 3.02 KB
/
raspirobotboard.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#raspirobotboard.py Library
import RPi.GPIO as GPIO
import serial
import time
DEVICE = '/dev/ttyAMA0'
BAUD = 9600
LEFT_GO_PIN = 17
LEFT_DIR_PIN = 4
RIGHT_GO_PIN = 10
RIGHT_DIR_PIN = 25
SW1_PIN = 11
SW2_PIN = 9
LED1_PIN = 7
LED2_PIN = 8
OC1_PIN = 22
OC2_PIN = 21
class RaspiRobot:
def __init__(self):
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEFT_GO_PIN, GPIO.OUT)
GPIO.setup(LEFT_DIR_PIN, GPIO.OUT)
GPIO.setup(RIGHT_GO_PIN, GPIO.OUT)
GPIO.setup(RIGHT_DIR_PIN, GPIO.OUT)
GPIO.setup(LED1_PIN, GPIO.OUT)
GPIO.setup(LED2_PIN, GPIO.OUT)
GPIO.setup(OC1_PIN, GPIO.OUT)
GPIO.setup(OC2_PIN, GPIO.OUT)
GPIO.setup(SW1_PIN, GPIO.IN)
GPIO.setup(SW2_PIN, GPIO.IN)
self.ser = None
def set_motors(self, left_go, left_dir, right_go, right_dir):
GPIO.output(LEFT_GO_PIN, left_go)
GPIO.output(LEFT_DIR_PIN, left_dir)
GPIO.output(RIGHT_GO_PIN, right_go)
GPIO.output(RIGHT_DIR_PIN, right_dir)
def forward(self, seconds=0):
self.set_motors(1, 0, 1, 0)
if seconds > 0:
time.sleep(seconds)
self.stop()
def stop(self):
self.set_motors(0, 0, 0, 0)
def reverse(self, seconds=0):
self.set_motors(1, 1, 1, 1)
if seconds > 0:
time.sleep(seconds)
self.stop()
def left(self, seconds=0):
self.set_motors(1, 0, 1, 1)
if seconds > 0:
time.sleep(seconds)
self.stop()
def right(self, seconds=0):
self.set_motors(1, 1, 1, 0)
if seconds > 0:
time.sleep(seconds)
self.stop()
def sw1_closed(self):
return not GPIO.input(SW1_PIN)
def sw2_closed(self):
return not GPIO.input(SW2_PIN)
def set_led1(self, state):
GPIO.output(LED1_PIN, state)
def set_led2(self, state):
GPIO.output(LED2_PIN, state)
def set_oc1(self, state):
GPIO.output(OC1_PIN, state)
def set_oc2(self, state):
GPIO.output(OC2_PIN, state)
def get_range_inch_raw(self):
msg = 'R000'
if self.ser == None:
self.ser = serial.Serial(DEVICE, BAUD)
if self.ser.inWaiting() > 4:
msg = self.ser.read(5)
reading = int(msg[1:4])
return reading
def get_range_inch(self):
readings = []
for i in range(0, 9):
reading = self.get_range_inch_raw()
if reading > 0:
readings.append(reading)
self.ser.flushInput()
if len(readings) == 0:
return 0
else:
return sum(readings) / len(readings)
def get_range_cm(self):
return int(self.get_range_inch() * 2.5)
def test(self):
raw_input("forward")
self.forward(2)
raw_input("left")
self.left(2)
raw_input("right")
self.right(2)
raw_input("reverse")
self.reverse(2)
raw_input("stop")
self.stop()
raw_input("End of test")