-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommanderModule.py
More file actions
169 lines (129 loc) · 4.99 KB
/
CommanderModule.py
File metadata and controls
169 lines (129 loc) · 4.99 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
import time
import serial
from multiprocessing import Queue
def envia(ser, missatge,temps=0.1, show_time = False):
first_time=time.time()
rbuffer = ''
resp = ''
ser.write(missatge + chr(10))
time.sleep(temps) # giving a breath to pi
while ser.inWaiting() > 0:
resp = ser.readline()
rbuffer = rbuffer + resp
if show_time:
t = time.time() - first_time
print("Round time: ",t)
return rbuffer
verboseCommands = False;
verboseSending = False;
def printVerbose(str,verbose = False):
if verbose:
print "Commander: "+str
def func(input,output):
print 'Commander process running...'
global ser
#Open the Serial Port.
ser = serial.Serial(port='/dev/ttyACM0', baudrate=115200, timeout=1)
print 'Test Mode On'
envia(ser,'TestMode On', 0.2)
print 'Set Motos Enable'
envia(ser,'SetMotor RWheelEnable LWheelEnable', 0.2)
print 'Play Sound 1'
envia(ser,'PlaySound 1', 0.3)
#Parametros Robot.
S = 121.5 # en mm
distancia_L = 0 # en mm
distancia_R = 0 # en mm
speed = 0 # en mm/s
tita_dot = 0
tiempo = 5
direccion = 0
output.put(S)
quite = False
while not quite:
msg = input.get()
if msg == "q": #recibinos orden de salir
printVerbose("quite",verboseCommands)
quite = True
elif msg == "forward": #recibimos acelerar hacia delante
printVerbose(msg,verboseCommands)
speed = speed+50
if speed >= 0:
direccion = 0
if speed == 0:
comando = 'SetMotor LWheelDisable RWheelDisable'
printVerbose(comando,verboseSending)
envia(ser, comando, 0.2)
comando = 'SetMotor RWheelEnable LWheelEnable'
printVerbose(comando,verboseSending)
envia(ser, comando, 0.2)
else:
distancia_R = (((speed * pow(-1, direccion) ) + (S * tita_dot)) * tiempo) * pow(-1, direccion)
distancia_L = (((speed * pow(-1, direccion) ) + (-S * tita_dot)) * tiempo) * pow(-1, direccion)
comando = 'SetMotor LWheelDist ' + str(distancia_L) + ' RWheelDist ' + str(distancia_R) + ' Speed ' + str(speed * pow(-1, direccion))
printVerbose(comando,verboseSending)
envia(ser,comando, 0.2)
elif msg == "back": #recibimos acelerar hacia atras
printVerbose(msg,verboseCommands)
speed = speed-50
if speed < 0:
direccion = 1
if speed == 0:
comando = 'SetMotor LWheelDisable RWheelDisable'
printVerbose(comando,verboseSending)
envia(ser, comando, 0.2)
comando = 'SetMotor RWheelEnable LWheelEnable'
printVerbose(comando,verboseSending)
envia(ser, comando, 0.2)
else:
distancia_R = (((speed * pow(-1, direccion) ) + (S * tita_dot)) * tiempo) * pow(-1, direccion)
distancia_L = (((speed * pow(-1, direccion) ) + (-S * tita_dot)) * tiempo) * pow(-1, direccion)
comando = 'SetMotor LWheelDist ' + str(distancia_L) + ' RWheelDist ' + str(distancia_R) + ' Speed ' + str(speed * pow(-1, direccion))
printVerbose(comando,verboseSending)
envia(ser,comando, 0.2)
elif msg == "left": #recibimos girar a la izquierda
printVerbose(msg,verboseCommands)
tita_dot = tita_dot + (3.1415/10)
distancia_R = (((speed * pow(-1, direccion) ) + (S * tita_dot)) * tiempo) * pow(-1, direccion)
distancia_L = (((speed * pow(-1, direccion) ) + (-S * tita_dot)) * tiempo) * pow(-1, direccion)
comando = 'SetMotor LWheelDist ' + str(distancia_L) + ' RWheelDist ' + str(distancia_R) + ' Speed ' + str(speed * pow(-1, direccion))
printVerbose(comando,verboseSending)
envia(ser,comando, 0.2)
elif msg == "right": #recibimos girar a la derecha
printVerbose(msg,verboseCommands)
tita_dot = tita_dot - (3.1415/10)
distancia_R = (((speed * pow(-1, direccion) ) + (S * tita_dot)) * tiempo) * pow(-1, direccion)
distancia_L = (((speed * pow(-1, direccion) ) + (-S * tita_dot)) * tiempo) * pow(-1, direccion)
comando = 'SetMotor LWheelDist ' + str(distancia_L) + ' RWheelDist ' + str(distancia_R) + ' Speed ' + str(speed * pow(-1, direccion))
printVerbose(comando,verboseSending)
envia(ser,comando, 0.2)
elif msg == "stop": #recibimos parar
printVerbose(msg,verboseCommands)
direccion = 0
speed = 0
tita_dot = 0
distancia_L = 0
distancia_R = 0
comando = 'SetMotor LWheelDisable RWheelDisable'
printVerbose(comando,verboseSending)
envia(ser, comando, 0.2)
comando = 'SetMotor RWheelEnable LWheelEnable'
printVerbose(comando,verboseSending)
envia(ser, comando, 0.2)
elif msg == "backbase": #recibimos orden de volver a la base
printVerbose(msg,verboseCommands)
elif msg == "laser": #recibimos leer laser
printVerbose(msg,verboseCommands)
comando = 'GetLDSScan'
printVerbose(comando,verboseSending)
ret = envia(ser, comando, 0.1)
output.put('l' + ret)
elif msg == "motors": #recibimos leer motores
printVerbose(msg,verboseCommands)
comando = 'GetMotors LeftWheel RightWheel'
printVerbose(comando,verboseSending)
ret = envia(ser, comando, 0.2)
output.put('m' + ret)
envia(ser, 'SetLDSRotation Off', 0.2)
envia(ser, 'TestMode Off', 0.2)
ser.close()