-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.py
More file actions
173 lines (139 loc) · 4.55 KB
/
create.py
File metadata and controls
173 lines (139 loc) · 4.55 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# run as super user
# port: /dev/ttyUSB0
import struct
import sys, glob # for listing serial ports
import time
try:
import serial
except ImportError:
tkMessageBox.showerror('Import error', 'Please install pyserial.')
raise
connection = None
TEXTWIDTH = 40 # window width, in characters
TEXTHEIGHT = 16 # window height, in lines
VELOCITYCHANGE = 100
ROTATIONCHANGE = 200
class TetheredDriveApp():
# sendCommandASCII takes a string of whitespace-separated,
# ASCII-encoded base 10 values to send
def sendCommandASCII(self, command):
cmd = ""
for v in command.split():
cmd += chr(int(v))
self.sendCommandRaw(cmd)
# sendCommandRaw takes a string interpreted as a byte array
def sendCommandRaw(self, command):
global connection
try:
if connection is not None:
connection.write(command)
else:
print "Not connected."
except serial.SerialException:
print "Lost connection"
connection = None
print ' '.join([ str(ord(c)) for c in command ])
# getDecodedBytes returns a n-byte value decoded using a format string.
# Whether it blocks is based on how the connection was set up.
def getDecodedBytes(self, n, fmt):
global connection
try:
print('return')
return struct.unpack(fmt, connection.read(n))[0]
except serial.SerialException:
print "Lost connection"
connection = None
return None
except struct.error:
print "Got unexpected data from serial port."
return None
# get8Unsigned returns an 8-bit unsigned value.
def get8Unsigned(self):
return getDecodedBytes(1, "B")
# get8Signed returns an 8-bit signed value.
def get8Signed(self):
return getDecodedBytes(1, "b")
# get16Unsigned returns a 16-bit unsigned value.
def get16Unsigned(self):
return getDecodedBytes(2, ">H")
# get16Signed returns a 16-bit signed value.
def get16Signed(self):
return getDecodedBytes(2, ">h")
def connect(self):
global connection
if connection is not None:
print('Oops', "You're already connected!")
return
port = '/dev/tty.usbserial-DA01NPT0'
try:
connection = serial.Serial(port, baudrate=115200, timeout=1)
print "Connected!"
self.sendCommandASCII('128') # P: Passive
self.sendCommandASCII('131') # S: Safe
self.sendCommandASCII('140 3 1 64 16 141 3') # Beep
except:
print "Failed."
def rotate(self,degrees, leftOrRight):
if leftOrRight.lower() == "right":
self.rotateRight()
time.sleep((0.018185)*degrees)
self.stop()
else:
self.rotateLeft()
time.sleep((0.018185)*degrees)
self.stop()
def move(self, cm, forwardOrBackward):
if forwardOrBackward.lower() == "forward":
self.moveForward()
time.sleep((0.1)*cm)
self.stop()
else:
self.moveBackward()
time.sleep((0.1)*cm)
self.stop()
def testDrive(self):
#self.moveForward()
#time.sleep(1)
#self.stop()
self.rotate(40,"right")
#self.rotateRight()
#time.sleep(1.08)
#self.stop()
# self.rotateLeft()
# time.sleep(1.08)
# self.stop()
# self.moveBackward()
# time.sleep(1)
# self.stop()
def moveForward(self):
# compute left and right wheel velocities
vr = VELOCITYCHANGE
vl = VELOCITYCHANGE
# create drive command
cmd = struct.pack(">Bhh", 145, vr, vl)
self.sendCommandRaw(cmd)
def moveBackward(self):
vr = -VELOCITYCHANGE
vl = -VELOCITYCHANGE
cmd = struct.pack(">Bhh", 145, vr, vl)
self.sendCommandRaw(cmd)
def rotateRight(self):
vr = -ROTATIONCHANGE/2
vl = ROTATIONCHANGE/2
cmd = struct.pack(">Bhh", 145, vr, vl)
self.sendCommandRaw(cmd)
def rotateLeft(self):
vr = ROTATIONCHANGE/2
vl = -ROTATIONCHANGE/2
cmd = struct.pack(">Bhh", 145, vr, vl)
self.sendCommandRaw(cmd)
def stop(self):
vr = 0
vl = 0
cmd = struct.pack(">Bhh", 145, vr, vl)
self.sendCommandRaw(cmd)
if __name__ == "__main__":
app = TetheredDriveApp()
app.connect()
time.sleep(0.5)
app.testDrive()