-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoArm-M2_advance.h
361 lines (309 loc) · 9.11 KB
/
RoArm-M2_advance.h
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// advance funcs for RoArm-M2 ctrl
// place holder.
void jsonCmdReceiveHandler();
bool moveToStep(String inputName, int inputStepNum);
// mission abort after serial received anything.
bool serialMissionAbort() {
if (Serial.available()) {
if (InfoPrint == 1) {Serial.println("[missionPlay abort.]");}
return true;
} else {
return false;
}
}
// input the mission name and the intro to create a mission file.
bool createMission(String inputName, String inputIntro) {
jsonInfoSend.clear();
jsonInfoSend["name"] = inputName;
jsonInfoSend["intro"] = inputIntro;
String contentBuffer;
serializeJson(jsonInfoSend, contentBuffer);
jsonInfoSend.clear();
return createFile(inputName + ".mission", contentBuffer);
}
// input the mission name and get the total content
int missionContent(String inputName) {
File file = LittleFS.open("/" + inputName + ".mission", "r");
if (!file) {
Serial.println("file not found.");
return -1;
}
Serial.println("---=== File Content ===---");
Serial.println("reading file: [" + inputName + "] starts:\n");
String mission_intro = file.readStringUntil('\n');
Serial.println(mission_intro);
jsonInfoHttp.clear();
jsonInfoHttp["info"] = "reading mission.";
jsonInfoHttp["first_line"] = mission_intro;
int _LineNum = 0;
while (file.available()) {
_LineNum++;
String line = file.readStringUntil('\n');
Serial.print("[StepNum: ");Serial.print(_LineNum);Serial.print(" ] - ");
Serial.println(line);
jsonInfoHttp["StepNum_"+String(_LineNum)] = line;
}
Serial.println("^^^ ^^^ ^^^ reading file: " + inputName + ".mission ends. ^^^ ^^^ ^^^");
file.close();
return _LineNum;
}
// input the mission name and the step to append
// a new step at the end of the mission.
// using inputStep(String)
bool appendStepJson(String inputName, String inputStep) {
DeserializationError err = deserializeJson(jsonInfoSend, inputStep);
if (err == DeserializationError::Ok) {
if (InfoPrint == 1) {
Serial.println("[json parsing succeed.]");
}
appendLine(inputName + ".mission", inputStep);
jsonInfoSend.clear();
return true;
} else {
jsonInfoSend.clear();
if (InfoPrint == 1) {
Serial.println("[deserializeJson err]");
}
return false;
}
}
// input the mission name and the step to append
// a new step at the end of the mission.
// using feedback.
void appendStepFB(String inputName, float inputSpd) {
RoArmM2_infoFeedback();
jsonInfoSend.clear();
jsonInfoSend["T"] = 104;
jsonInfoSend["x"] = lastX;
jsonInfoSend["y"] = lastY;
jsonInfoSend["z"] = lastZ;
jsonInfoSend["t"] = lastT;
jsonInfoSend["spd"] = inputSpd;
String contentBuffer;
serializeJson(jsonInfoSend, contentBuffer);
appendLine(inputName + ".mission", contentBuffer);
}
// append a new delay(ms) at the end of the mission.
void appendDelayCmd(String inputName, int delayTime) {
jsonInfoSend.clear();
jsonInfoSend["T"] = 111;
jsonInfoSend["cmd"] = delayTime;
String contentBuffer;
serializeJson(jsonInfoSend, contentBuffer);
appendLine(inputName + ".mission", contentBuffer);
}
// insert a new step as the stepNum
// using the json string input.
bool insertStepJson(String inputName, int inputStepNum, String inputStep) {
DeserializationError err = deserializeJson(jsonInfoSend, inputStep);
if (err == DeserializationError::Ok) {
if (InfoPrint == 1) {
Serial.println("[json parsing succeed.]");
}
insertLine(inputName + ".mission", inputStepNum + 1, inputStep);
jsonInfoSend.clear();
return true;
} else {
jsonInfoSend.clear();
if (InfoPrint == 1) {
Serial.println("[deserializeJson err]");
}
return false;
}
}
// insert a new step as the stepNum
// using the feedback.
void insertStepFB(String inputName, int inputStepNum, float inputSpd) {
RoArmM2_infoFeedback();
jsonInfoSend.clear();
jsonInfoSend["T"] = 104;
jsonInfoSend["x"] = lastX;
jsonInfoSend["y"] = lastY;
jsonInfoSend["z"] = lastZ;
jsonInfoSend["t"] = lastT;
jsonInfoSend["spd"] = inputSpd;
String contentBuffer;
serializeJson(jsonInfoSend, contentBuffer);
insertLine(inputName + ".mission", inputStepNum + 1, contentBuffer);
}
// insert a new delayCmd as the stepNum
void insertDelayCmd(String inputName, int inputStepNum, int delayTime) {
jsonInfoSend.clear();
jsonInfoSend["T"] = 111;
jsonInfoSend["cmd"] = delayTime;
String contentBuffer;
serializeJson(jsonInfoSend, contentBuffer);
insertLine(inputName + ".mission", inputStepNum + 1, contentBuffer);
}
// replace the cmd at stepNum.
// using the step json string input.
bool replaceStepJson(String inputName, int inputStepNum, String inputStep) {
DeserializationError err = deserializeJson(jsonInfoSend, inputStep);
if (err == DeserializationError::Ok) {
if (InfoPrint == 1) {
Serial.println("[json parsing succeed.]");
}
replaceLine(inputName + ".mission", inputStepNum + 1, inputStep);
jsonInfoSend.clear();
return true;
} else {
jsonInfoSend.clear();
if (InfoPrint == 1) {
Serial.println("[deserializeJson err]");
}
return false;
}
}
// replace the cmd at stepNum.
// using feedback.
void replaceStepFB(String inputName, int inputStepNum, float inputSpd) {
RoArmM2_infoFeedback();
jsonInfoSend.clear();
jsonInfoSend["T"] = 104;
jsonInfoSend["x"] = lastX;
jsonInfoSend["y"] = lastY;
jsonInfoSend["z"] = lastZ;
jsonInfoSend["t"] = lastT;
jsonInfoSend["spd"] = inputSpd;
String contentBuffer;
serializeJson(jsonInfoSend, contentBuffer);
replaceLine(inputName + ".mission", inputStepNum + 1, contentBuffer);
}
// replace the cmd at stepNum with delay cmd.
void replaceDelayCmd(String inputName, int inputStepNum, int delayTime) {
jsonInfoSend.clear();
jsonInfoSend["T"] = 111;
jsonInfoSend["cmd"] = delayTime;
String contentBuffer;
serializeJson(jsonInfoSend, contentBuffer);
replaceLine(inputName + ".mission", inputStepNum + 1, contentBuffer);
}
// delete a step
void deleteStep(String inputName, int inputStepNum) {
deleteSingleLine(inputName + ".mission", inputStepNum + 1);
}
// input the mission name and the stepNum.
// it will process the cmd.
bool moveToStep(String inputName, int inputStepNum) {
String stepStringBuffer = readSingleLine(inputName + ".mission", inputStepNum + 1);
DeserializationError err = deserializeJson(jsonCmdReceive, stepStringBuffer);
if (err == DeserializationError::Ok) {
if (InfoPrint == 1) {
Serial.println("[json parsing succeed.]");
Serial.println("[import a step]");
Serial.print("[mission name]: ");Serial.println(inputName);
Serial.print("[stepNum]: ");Serial.println(inputStepNum);
Serial.print("[cmd]: ");Serial.println(stepStringBuffer);
}
jsonCmdReceiveHandler();
if (InfoPrint == 1) {
Serial.println("[step finished]");
}
jsonInfoSend.clear();
return true;
} else {
jsonInfoSend.clear();
if (InfoPrint == 1) {
Serial.println("[deserializeJson err]");
}
return false;
}
}
// input the mission name and the repeat times.
// when repeatTimes = -1, it will loop forever.
// play a mission file.
void missionPlay(String inputName, int repeatTimes) {
int _LineNum = missionContent(inputName);
int currentTimes = 0;
while (1) {
currentTimes++;
if (currentTimes > repeatTimes && repeatTimes != -1) {
if (InfoPrint == 1) {Serial.println("[missionPlay finished.]");}
return;
}
if (InfoPrint == 1) {
Serial.print("---\n[currentTimes: ");Serial.print(currentTimes);
Serial.println(" ]");
}
for (int i = 1; i<=_LineNum; i++) {
if (serialMissionAbort()) {
return;
}
moveToStep(inputName, i);
}
}
}
// change EEmode.
void configEEmodeType(byte inputMode) {
EEMode = inputMode;
if (inputMode == 0){
l3A = ARM_L3_LENGTH_MM_A_0;
l3B = ARM_L3_LENGTH_MM_B_0;
l3 = sqrt(l3A * l3A + l3B * l3B);
t3rad = atan2(l3B, l3A);
initX = l3A + l2B;
initY = 0;
initZ = l2A - l3B;
initT = M_PI;
}
else if (inputMode == 1){
l3A = ARM_L3_LENGTH_MM_A_1;
l3B = ARM_L3_LENGTH_MM_B_1;
l3 = sqrt(l3A * l3A + l3B * l3B);
t3rad = atan2(l3B, l3A);
EoAT_A = EoAT_A;
EoAT_B = EoAT_B;
l4A = ARM_L4_LENGTH_MM_A;
l4B = ARM_L4_LENGTH_MM_B;
lEA = EoAT_A + ARM_L4_LENGTH_MM_A;
lEB = EoAT_B + ARM_L4_LENGTH_MM_B;
lE = sqrt(lEA * lEA + lEB * lEB);
tErad = atan2(lEB, lEA);
initX = l3A + l2B + l4A + EoAT_A;
initY = 0;
initZ = l2A - l3B - l4B - EoAT_B;
initT = M_PI;
}
goalX = initX;
goalY = initY;
goalZ = initZ;
goalT = initT;
lastX = goalX;
lastY = goalY;
lastZ = goalZ;
lastT = goalT;
RoArmM2_baseCoordinateCtrl(initX, initY, initZ, initT);
RoArmM2_goalPosMove();
}
// config the siza of EoAT.
void configEoAT(byte mountPos, double inputEA, double inputEB) {
switch (mountPos) {
case 0: ARM_L4_LENGTH_MM_A = 67.85;break;
case 1: ARM_L4_LENGTH_MM_A = 64.16;break;
case 2: ARM_L4_LENGTH_MM_A = 59.07;break;
case 3: ARM_L4_LENGTH_MM_A = 51.07;break;
}
EoAT_A = inputEA;
EoAT_B = inputEB;
l4A = ARM_L4_LENGTH_MM_A;
l4B = ARM_L4_LENGTH_MM_B;
lEA = EoAT_A + ARM_L4_LENGTH_MM_A;
lEB = EoAT_B + ARM_L4_LENGTH_MM_B;
lE = sqrt(lEA * lEA + lEB * lEB);
tErad = atan2(lEB, lEA);
initX = l3A + l2B + l4A + EoAT_A;
initY = 0;
initZ = l2A - l3B - l4B - EoAT_B;
initT = M_PI;
}
// set the InfoPrint.
void configInfoPrint(byte inputCmd) {
switch (inputCmd) {
case 0: InfoPrint = 0;
break;
case 1: InfoPrint = 1;
break;
case 2: InfoPrint = 2;
break;
}
}