-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.cpp
243 lines (220 loc) · 5.19 KB
/
command.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "command.h"
#include "parameters.h"
#include "robotstate.h"
#include "fifo.h"
#include "message.h"
#include "encoder.h"
#include "pwm.h"
#include "include_arduino.h"
/**
* Analyse le message et effectue les actions associees
*
* @param id : l'identifiant associe au message
* @param header : le type de message (en-tete)
* @param args : le tableau d'entier contenant les arguments
* */
void cmd(int id, int id_cmd, int* args, int size){
/* On analyse le message en fonction de son type */
switch(id_cmd){
case QA_ID: /* Identification */
{
sendMessage(id, ID_ASSERV, (char*)"asserv");
break;
}
case QA_PING:
{
sendMessage(id, (char*)"Pong");
break;
}
case QA_SETPID:
{
//ENC_CENTER_DIST_MM = args[0]/10.0;
break;
}
case QA_GOTO:
{
if (size < 3)
sendMessage(id, E_INVALID_PARAMETERS_NUMBERS);
else
{
pushGoalPosition(id,(double)args[0]*ENC_MM_TO_TICKS, (double)args[1]*ENC_MM_TO_TICKS, (double)args[2]);
sendMessage(id, 1);
}
break;
}
case QA_GOTOR:
{
if (size < 3)
sendMessage(id, E_INVALID_PARAMETERS_NUMBERS);
else
{
double co = cos(robot_get_angle());
double si = sin(robot_get_angle());
pushGoalPosition(id,((double)args[0]*co-(double)args[1]*si)*18+robot_get_x()*0.01, ((double)args[0]*si+(double)args[1]*co)*18+robot_get_y()*0.01, (double)args[2]);
sendMessage(id, 1);
}
break;
}
case QA_TURN:
{
if (size < 2)
sendMessage(id, E_INVALID_PARAMETERS_NUMBERS);
else
{
double angle = moduloPI(((double)args[0]) * DEG_TO_RAD);
sendMessage(-1, (int)(angle*100.0));
pushGoalOrientation(id,angle,args[1]);
sendMessage(id, 1);
}
break;
}
case QA_TURNR:
{
if (size < 2)
sendMessage(id, E_INVALID_PARAMETERS_NUMBERS);
else
{
double angle = moduloPI(((double)args[0]) * DEG_TO_RAD + robot_get_angle());
pushGoalOrientation(id,angle,args[1]);
sendMessage(id, 1);
}
break;
}
case QA_POS:
{
int x_mm = robot_get_x()*0.01*ENC_TICKS_TO_MM;
int y_mm = robot_get_y()*0.01*ENC_TICKS_TO_MM;
int a_deg = robot_get_angle()*RAD_TO_DEG;
int tab[] = {x_mm,y_mm,a_deg};
sendMessage(id,tab,3);
break;
}
case QA_SET_POS:
{
if (size < 2)
sendMessage(id, E_INVALID_PARAMETERS_NUMBERS);
else {
robot_set_mm_x(args[0]);
robot_set_mm_y(args[1]);
robot_set_deg_angle(args[2]);
value_left_enc = 0;
value_right_enc = 0;
sendMessage(id, 0);
}
break;
}
case QA_PWM:
{
if (size < 2)
sendMessage(id, E_INVALID_PARAMETERS_NUMBERS);
else
{
pushGoalPwm(id,args[0],args[1],args[2]);
sendMessage(id, 1);
}
break;
}
/*
case Q_MODIF_GOAL_ABS:
{
if (size < 3)
sendMessage(id, E_INVALID_PARAMETERS_NUMBERS);
else
{
current_goal.type = TYPE_POSITION;
current_goal.isReached = false;
current_goal.x = (double)args[0]*ENC_MM_TO_TICKS;
current_goal.y = (double)args[1]*ENC_MM_TO_TICKS;
current_goal.speed = args[2];
sendMessage(id, 1);
}
break;
}
case Q_MODIF_GOAL_REL:
{
if (size < 3)
sendMessage(id, E_INVALID_PARAMETERS_NUMBERS);
else
{
double co = cos(robot_state.angle);
double si = sin(robot_state.angle);
current_goal.type = TYPE_POSITION;
current_goal.isReached = false;
current_goal.x = (args[0]*co-args[1]*si)*18+robot_state.x;
current_goal.y = (args[0]*si+args[1]*co)*18+robot_state.y;
current_goal.speed = args[2];
sendMessage(id, 1);
}
break;
}
*/
case QA_CANCEL: /* comme stop */
{
clearGoals();
current_goal.isCanceled = true;
sendMessage(id, 0);
break;
}
case QA_STOP: /* comme pause */
{
current_goal.isPaused = true;
sendMessage(id, 0);
break;
}
case QA_RESUME: /* comme resume */
{
current_goal.isPaused = false;
sendMessage(id, 0);
break;
}
case QA_RESET:
{
clearGoals();
current_goal.isCanceled = true;
current_goal.isPaused = false;
value_left_enc = 0;
value_right_enc = 0;
robot_set_rad_angle(0.0);
robot_set_x(0);
robot_set_y(0);
sendMessage(id, 0);
break;
}
case QA_GETSENS:
{
if (value_pwm_right > 0)
sendMessage(id, AVANT);
else if (value_pwm_right < 0)
sendMessage(id, ARRIERE);
else
sendMessage(id, ARRET);
break;
}
case QA_GETENC:
{
int tab[2] = {value_left_enc,value_right_enc};
sendMessage(id, tab, 2);
break;
}
case Q_DEBUG : //TODO a degager quand tout marche
{
/*Serial.print("?,_________________§");
Serial.print("uptime: ");Serial.print(millis());
Serial.print("§angle: ");Serial.print(robot_state.angle, DEC);
Serial.print("§speed: ");Serial.print(robot_state.speed*ENC_TICKS_TO_MM, DEC);
Serial.print("§speedt: ");Serial.print(robot_state.speed, DEC);
Serial.print("§x: ");Serial.print(robot_state.x*ENC_TICKS_TO_MM, DEC);
Serial.print("§xt: ");Serial.print(robot_state.x, DEC);
Serial.print("§y: ");Serial.print(robot_state.y*ENC_TICKS_TO_MM, DEC);
Serial.print("§yt: ");Serial.print(robot_state.y, DEC);
Serial.print("§encL: ");Serial.print(value_left_enc);
Serial.print("§encR: ");Serial.println(value_right_enc);*/
break;
}
default:
{
sendMessage(id,E_INVALID_CMD);
break;
}
}
}