-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathuArmService.cpp
More file actions
356 lines (272 loc) · 7.74 KB
/
uArmService.cpp
File metadata and controls
356 lines (272 loc) · 7.74 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
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
/**
******************************************************************************
* @file uArmService.cpp
* @author David.Long
* @email xiaokun.long@ufactory.cc
* @date 2016-09-28
* @license GNU
* copyright(c) 2016 UFactory Team. All right reserved
******************************************************************************
*/
#include "uArm.h"
#include "uArmComm.h"
uArmService service;
uArmService::uArmService()
{
mRecordAddr = 0;
mReportInterval = 0;
mButtonServiceDisable = false;
mReportStartTime = millis();
mTickRecorderTime = millis();
}
void uArmService::setButtonService(bool on)
{
if (on)
{
mButtonServiceDisable = false;
}
else
{
mButtonServiceDisable = true;
}
}
void uArmService::setReportInterval(unsigned int interval)
{
mReportInterval = interval;
}
void uArmService::init()
{
}
void uArmService::recorderTick()
{
//sys led function detec every 0.05s-----------------------------------------------------------------
switch(mSysStatus)//every 0.125s per point
{
case SINGLE_PLAY_MODE:
if(play() == false)
{
mSysStatus = NORMAL_MODE;
mRecordAddr = 0;
}
break;
case LOOP_PLAY_MODE:
if(play() == false)
{
mRecordAddr = 0;
}
break;
case LEARNING_MODE:
case LEARNING_MODE_STOP:
if(record() == false)
{
mSysStatus = NORMAL_MODE;
mRecordAddr = 0;
controller.attachAllServo();
}
break;
default:
break;
}
}
void uArmService::systemRun()
{
//check the button4 status------------------------------------------------------------------------
if (mButtonServiceDisable)
{
if (buttonMenu.longPressed())
{
buttonMenu.clearEvent();
reportButtonEvent(0, EVENT_LONG_PRESS);
}
else if (buttonMenu.clicked())
{
//debugPrint("btnD4 down");
buttonMenu.clearEvent();
reportButtonEvent(0, EVENT_CLICK);
}
//check the button7 status-------------------------------------------------------------------------
if (buttonPlay.longPressed())
{
buttonPlay.clearEvent();
reportButtonEvent(1, EVENT_LONG_PRESS);
}
else if (buttonPlay.clicked())
{
buttonPlay.clearEvent();
reportButtonEvent(1, EVENT_CLICK);
}
}
else
{
if (buttonMenu.clicked())
{
//debugPrint("btnD4 down");
buttonMenu.clearEvent();
switch (mSysStatus)
{
case NORMAL_MODE:
case NORMAL_BT_CONNECTED_MODE:
mSysStatus = LEARNING_MODE;
mRecordAddr = 0;//recording/playing address
controller.detachAllServo();
break;
case LEARNING_MODE:
//LEARNING_MODE_STOP is just used to notificate record() function to stop, once record() get it then change the sys_status to normal_mode
mSysStatus = LEARNING_MODE_STOP;//do not detec if BT is connected here, will do it seperatly
pumpOff();
break;
default: break;
}
}
//check the button7 status-------------------------------------------------------------------------
if (buttonPlay.longPressed())
{
buttonPlay.clearEvent();
switch(mSysStatus)
{
case NORMAL_MODE:
case NORMAL_BT_CONNECTED_MODE:
mRecordAddr = 0;
mSysStatus = LOOP_PLAY_MODE;
break;
case SINGLE_PLAY_MODE:
case LOOP_PLAY_MODE:
break;
case LEARNING_MODE:
break;
}
}
else if (buttonPlay.clicked())
{
buttonPlay.clearEvent();
//debugPrint("btnD7 down");
switch(mSysStatus)
{
case NORMAL_MODE:
case NORMAL_BT_CONNECTED_MODE:
mRecordAddr = 0;//recording/playing address
mSysStatus = SINGLE_PLAY_MODE; // or play just one time
break;
case SINGLE_PLAY_MODE:
case LOOP_PLAY_MODE:
pumpOff();
mSysStatus = NORMAL_MODE;
break;
case LEARNING_MODE:
if (getPumpStatus())
{
pumpOff();
}
else
{
pumpOn();
}
break;
}
}
}
if (mReportInterval > 0)
{
if(millis() - mReportStartTime >= mReportInterval)
{
mReportStartTime = millis();
reportPos();
}
}
}
void uArmService::btDetect()
{
#ifdef MKII
if (!buzzer.on() && ((mSysStatus == NORMAL_MODE) || (mSysStatus == NORMAL_BT_CONNECTED_MODE)))
{
pinMode(BT_DETECT_PIN, INPUT);
digitalWrite(BT_DETECT_PIN,HIGH);
if (digitalRead(BT_DETECT_PIN) == HIGH)//do it here
{
ledRed.on();
mSysStatus = NORMAL_BT_CONNECTED_MODE;
}
else
{
ledRed.off();
mSysStatus = NORMAL_MODE;
}
//pinMode(BT_DETECT_PIN, OUTPUT);
}
#endif
}
void uArmService::run()
{
systemRun();
if (millis() - mTickRecorderTime >= 50)
{
mTickRecorderTime= millis();
recorderTick();
}
}
bool uArmService::play()
{
unsigned char data[5]; // 0: L 1: R 2: Rotation 3: hand rotation 4:gripper
recorder.read(mRecordAddr, data, 5);
debugPrint("mRecordAddr = %d, data=%d, %d, %d", mRecordAddr, data[0], data[1], data[2]);
if(data[0] != 255)
{
//double x, y, z;
//controller.getXYZFromAngle(x, y, z, (double)data[2], (double)data[0], (double)data[1]);
//moveToAngle((double)data[2], (double)data[0], (double)data[1]);
controller.writeServoAngle((double)data[2], (double)data[0], (double)data[1]);
controller.writeServoAngle(SERVO_HAND_ROT_NUM, (double)data[3]);
unsigned char pumpStatus = getPumpStatus() > 0 ? 1 : 0;
if (pumpStatus != data[4])
{
if (data[4])
{
pumpOn();
}
else
{
pumpOff();
}
}
}
else
{
pumpOff();
return false;
}
mRecordAddr += 5;
return true;
}
bool uArmService::record()
{
debugPrint("mRecordAddr = %d", mRecordAddr);
if(mRecordAddr <= 65530)
{
unsigned char data[5]; // 0: L 1: R 2: Rotation 3: hand rotation 4:gripper
if((mRecordAddr != 65530) && (mSysStatus != LEARNING_MODE_STOP))
{
double rot, left, right;
//controller.updateAllServoAngle();
controller.readServoAngles(rot, left, right);
data[0] = (unsigned char)left;
data[1] = (unsigned char)right;
data[2] = (unsigned char)rot;
data[3] = (unsigned char)controller.readServoAngle(SERVO_HAND_ROT_NUM);
data[4] = getPumpStatus() > 0 ? 1 : 0;
debugPrint("l=%d, r=%d, r= %d", data[0], data[1], data[2]);
}
else
{
data[0] = 255;//255 is the ending flag
recorder.write(mRecordAddr, data, 5);
return false;
}
recorder.write(mRecordAddr, data, 5);
mRecordAddr += 5;
return true;
}
else
{
return false;
}
}