This repository was archived by the owner on Oct 6, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathArduinoSerial.py
More file actions
111 lines (89 loc) · 2.36 KB
/
Copy pathArduinoSerial.py
File metadata and controls
111 lines (89 loc) · 2.36 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
import serial
import time
class ArduinoController(object):
'''
self class controls each individual field unit by a unique id.
At runtime, we should make a certain number copies of self class
foreach field unit we want to play with.
'''
def __init__(self, commander, uid):
# The serial commander
self.commander = commander
# Unique uid for each field unit
self.uid = uid
def start(self, speed, delay=0):
'''
Starts the arduino field unit, there is a delay option for countdown(?)
'''
self.sendCommand("start", speed)
def stop(self, delay=0):
'''
Stops the arduino field unit, there is a delay option for countdown(?)
'''
time.sleep(delay)
self.sendCommand("stop", 0)
def setSpeedLeft(self, speed):
'''
Sets the speed of the left wheel
'''
self.sendCommand("setLeft", speed)
def setSpeedRight(self, speed):
'''
Sets the speed of the left wheel
'''
self.sendCommand("setRight", speed)
def move(self, direction):
'''
Move the field unit to a certain direction
TODO: consult with the rest of the team on the protocol
'''
self.sendCommand("move", direction)
def sendCommand(self, key, value):
'''
A general function that sends the action via the commander
The command must be a '\n' terminated.
'''
command = "uid=%s;key=%s;value=%s;\n" % (self.uid, key, value)
#print command
self.write_serial_string(command)
def write_serial_string(self, string):
print string
for i in list(string):
print i
print i
self.commander.write(i)
time.sleep(0.001)
def main():
ser = serial.Serial('COM6', baudrate=38400)
firstUnit = ArduinoController(ser, '1111')
secondUnit = ArduinoController(ser, '2222')
## thirdUnit = ArduinoController(ser, '3333')
## forthUnit = ArduinoController(ser, '4444')
## fifthUnit = ArduinoController(ser, '5555')
firstUnit.start(200 )
secondUnit.start(200)
## thirdUnit.start(200)
## forthUnit.start(200)
## fifthUnit.start(200)
time.sleep(2)
firstUnit.setSpeedRight(230)
firstUnit.setSpeedLeft(170)
time.sleep(2)
firstUnit.stop()
firstUnit.start(0)
firstUnit.setSpeedRight(-200)
firstUnit.setSpeedLeft(200)
time.sleep(2)
#firstUnit.stop()
#firstUnit.start(0)
firstUnit.setSpeedRight(200)
firstUnit.setSpeedLeft(-200)
## time.sleep(2)
firstUnit.stop()
secondUnit.stop()
## thirdUnit.stop()
## forthUnit.stop()
## fifthUnit.stop()
ser.close()
if __name__ == '__main__':
main()