-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeSmartServo.cpp
1236 lines (1198 loc) · 29.4 KB
/
MeSmartServo.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* \par Copyright (C), 2012-2016, MakeBlock
* \class MeSmartServo
* \brief Driver for Me Smart Servo device.
* @file MeSmartServo.cpp
* @author MakeBlock
* @version V1.0.0
* @date 2016/08/23
* @brief Driver for Me Smart Servo device.
*
* \par Copyright
* This software is Copyright (C), 2012-2016, MakeBlock. Use is subject to license \n
* conditions. The main licensing options available are GPL V2 or Commercial: \n
*
* \par Open Source Licensing GPL V2
* This is the appropriate option if you want to share the source code of your \n
* application with everyone you distribute it to, and you also want to give them \n
* the right to share who uses it. If you wish to use this software under Open \n
* Source Licensing, you must contribute all your source code to the open source \n
* community in accordance with the GPL Version 2 when your application is \n
* distributed. See http://www.gnu.org/copyleft/gpl.html
*
* \par Description
* This file is a drive for Smart Servo device, The Smart Servo inherited the
* MeSerial class from SoftwareSerial.
*
*
* \par Method List:
* 1. uint8_t MeSmartServo::readByte(uint8_t *argv,int16_t idx);
* 2. short MeSmartServo::readShort(uint8_t *argv,int16_t idx,boolean ignore_high);
* 3. float MeSmartServo::readFloat(uint8_t *argv,int16_t idx);
* 4. long MeSmartServo::readLong(uint8_t *argv,int idx);
* 5. uint8_t MeSmartServo::sendByte(uint8_t val);
* 6. uint8_t MeSmartServo::sendShort(int16_t val,boolean ignore_high);
* 7. uint8_t MeSmartServo::sendFloat(float val);
* 8. uint8_t MeSmartServo::sendLong(long val);
* 9. boolean MeSmartServo::assignDevIdRequest(void);
* 10. boolean MeSmartServo::moveTo(uint8_t dev_id,long angle_value,float speed,smartServoCb callback);
* 11. boolean MeSmartServo::move(uint8_t dev_id,long angle_value,float speed,smartServoCb callback);
* 12. boolean MeSmartServo::setZero(uint8_t dev_id);
* 13. boolean MeSmartServo::setBreak(uint8_t dev_id, uint8_t breakStatus);
* 14. boolean MeSmartServo::setRGBLed(uint8_t dev_id, uint8_t r_value, uint8_t g_value, uint8_t b_value);
* 15. boolean MeSmartServo::handSharke(uint8_t dev_id);
* 16. boolean MeSmartServo::setPwmMove(uint8_t dev_id, int16_t pwm_value);
* 17. boolean MeSmartServo::setInitAngle(uint8_t dev_id,uint8_t mode,int16_t speed);
* 18. long MeSmartServo::getAngleRequest(uint8_t devId);
* 19. float MeSmartServo::getSpeedRequest(uint8_t devId);
* 20. float MeSmartServo::getVoltageRequest(uint8_t devId);
* 21. float MeSmartServo::getTempRequest(uint8_t devId);
* 22. float MeSmartServo::getCurrentRequest(uint8_t devId);
* 23. void MeSmartServo::assignDevIdResponse(void *arg);
* 24. void MeSmartServo::processSysexMessage(void);
* 25. void MeSmartServo::smartServoEventHandle(void);
* 26. void MeSmartServo::errorCodeCheckResponse(void *arg);
* 27. void MeSmartServo::smartServoCmdResponse(void *arg);
*
* \par History:
* <pre>
* `<Author>` `<Time>` `<Version>` `<Descr>`
* Mark Yan 2016/08/23 1.0.0 Build the new.
* </pre>
*
* @example SmartServoTest.ino
*/
#include "MeSmartServo.h"
#include "MeSerial.h"
#include <avr/wdt.h>
#ifdef ME_PORT_DEFINED
/**
* Alternate Constructor which can call your own function to map the Me Smart Servo to arduino port,
* no pins are used or initialized here.
* \param[in]
* None
*/
MeSmartServo::MeSmartServo() : MeSerial(0)
{
parsingSysex = false;
sysex = {0};
sysexBytesRead = 0;
servo_num_max = 0;
}
/**
* Alternate Constructor which can call your own function to map the Me Smart Servo to arduino port,
* If the hardware serial was selected, it will used the hardware serial.
* \param[in]
* port - RJ25 port from PORT_1 to M2.
*/
MeSmartServo::MeSmartServo(uint8_t port) : MeSerial(port)
{
parsingSysex = false;
sysex = {0};
sysexBytesRead = 0;
servo_num_max = 0;
}
#else // ME_PORT_DEFINED
/**
* Alternate Constructor which can call your own function to map the Me Smart Servo to arduino port,
* If the hardware serial was selected, it will used the hardware serial.
* \param[in]
* receivePin - the rx pin of serial(arduino port).
* \param[in]
* transmitPin - the tx pin of serial(arduino port).
* \param[in]
* inverse_logic - Whether the Serial level need inv.
*/
MeSmartServo::MeSmartServo(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic)\
: MeSerial(receivePin, transmitPin, inverse_logic)
{
parsingSysex = false;
sysex = {0};
sysexBytesRead = 0;
servo_num_max = 0;
}
#endif // ME_PORT_DEFINED
/**
* \par Function
* readByte
* \par Description
* change 2byte 7bit read date to 1byte byte data.
* \param[in]
* *argv - the buffer for input data.\n
* \param[in]
* idx - the index used to identify the first address of read data.\n
* \par Output
* return the converted data.
* \return
* None
* \par Others
* None
*/
uint8_t MeSmartServo::readByte(uint8_t *argv,int16_t idx)
{
uint8_t temp;
val1byte.byteVal[0] = argv[idx] & 0x7f;
temp = argv[idx+1] << 7;
val1byte.byteVal[0] |= temp;
return val1byte.charVal;
}
/**
* \par Function
* readShort
* \par Description
* change (2byte 7bit) or (3byte 7bit) read date to 2byte short data.
* \param[in]
* *argv - the buffer for input data.\n
* \param[in]
* idx - the index used to identify the first address of read data.\n
* \param[in]
* ignore_high - is there have third byte high-level data.\n
* \par Output
* return the converted data.
* \return
* None
* \par Others
* None
*/
short MeSmartServo::readShort(uint8_t *argv,int16_t idx,boolean ignore_high)
{
uint8_t temp;
val2byte.byteVal[0] = argv[idx] & 0x7f;
temp = argv[idx+1] << 7;
val2byte.byteVal[0] |= temp;
val2byte.byteVal[1] = (argv[idx+1] >> 1) & 0x7f;
//Send analog can ignored high
if(ignore_high == false)
{
temp = (argv[idx+2] << 6);
val2byte.byteVal[1] |= temp;
}
return val2byte.shortVal;
}
/**
* \par Function
* readFloat
* \par Description
* change (5byte 7bit) read date to 4byte float data.
* \param[in]
* *argv - the buffer for input data.\n
* \param[in]
* idx - the index used to identify the first address of read data.\n
* \par Output
* return the converted data.
* \return
* None
* \par Others
* None
*/
float MeSmartServo::readFloat(uint8_t *argv,int16_t idx)
{
uint8_t temp;
val4byte.byteVal[0] = argv[idx] & 0x7f;
temp = argv[idx+1] << 7;
val4byte.byteVal[0] |= temp;
val4byte.byteVal[1] = (argv[idx+1] >> 1) & 0x7f;
temp = (argv[idx+2] << 6);
val4byte.byteVal[1] += temp;
val4byte.byteVal[2] = (argv[idx+2] >> 2) & 0x7f;
temp = (argv[idx+3] << 5);
val4byte.byteVal[2] += temp;
val4byte.byteVal[3] = (argv[idx+3] >> 3) & 0x7f;
temp = (argv[idx+4] << 4);
val4byte.byteVal[3] += temp;
return val4byte.floatVal;
}
/**
* \par Function
* readLong
* \par Description
* change (5byte 7bit) read date to 4byte long data.
* \param[in]
* *argv - the buffer for input data.\n
* \param[in]
* idx - the index used to identify the first address of read data.\n
* \par Output
* return the converted data.
* \return
* None
* \par Others
* None
*/
long MeSmartServo::readLong(uint8_t *argv,int idx)
{
uint8_t temp;
val4byte.byteVal[0] = argv[idx] & 0x7f;
temp = argv[idx+1] << 7;
val4byte.byteVal[0] |= temp;
val4byte.byteVal[1] = (argv[idx+1] >> 1) & 0x7f;
temp = (argv[idx+2] << 6);
val4byte.byteVal[1] += temp;
val4byte.byteVal[2] = (argv[idx+2] >> 2) & 0x7f;
temp = (argv[idx+3] << 5);
val4byte.byteVal[2] += temp;
val4byte.byteVal[3] = (argv[idx+3] >> 3) & 0x7f;
temp = (argv[idx+4] << 4);
val4byte.byteVal[3] += temp;
return val4byte.longVal;
}
/**
* \par Function
* sendByte
* \par Description
* send (1byte 8bit) date to 2byte 7bit data.
* \param[in]
* val - the byte data to be converted.\n
* \par Output
* return the checksum data.
* \return
* None
* \par Others
* None
*/
uint8_t MeSmartServo::sendByte(uint8_t val)
{
uint8_t checksum;
uint8_t val_7bit[2]={0};
val1byte.charVal = val;
val_7bit[0] = val1byte.byteVal[0] & 0x7f;
write(val_7bit[0]);
val_7bit[1] = (val1byte.byteVal[0] >> 7) & 0x7f;
write(val_7bit[1]);
checksum = val_7bit[0] + val_7bit[1];
checksum = checksum & 0x7f;
return checksum;
}
/**
* \par Function
* sendShort
* \par Description
* send (2byte short) date to 2byte or 3byte 7bit data.
* \param[in]
* val - the short data to be converted.\n
* \param[in]
* ignore_high - is there have third byte high-level data.\n
* \par Output
* return the checksum data.
* \return
* None
* \par Others
* None
*/
uint8_t MeSmartServo::sendShort(int16_t val,boolean ignore_high)
{
uint8_t checksum;
uint8_t val_7bit[3]={0};
val2byte.shortVal = val;
val_7bit[0] = val2byte.byteVal[0] & 0x7f;
write(val_7bit[0]);
val_7bit[1] = ((val2byte.byteVal[1] << 1) | (val2byte.byteVal[0] >> 7)) & 0x7f;
write(val_7bit[1]);
checksum = val_7bit[0] + val_7bit[1];
//Send analog can ignored high
if(ignore_high == false)
{
val_7bit[2] = (val2byte.byteVal[1] >> 6) & 0x7f;
checksum += val_7bit[2];
checksum = checksum & 0x7f;
write(val_7bit[2]);
}
}
/**
* \par Function
* sendFloat
* \par Description
* send (4byte float) date to 5byte 7bit data.
* \param[in]
* val - the short data to be converted.\n
* \par Output
* return the checksum data.
* \return
* None
* \par Others
* None
*/
uint8_t MeSmartServo::sendFloat(float val)
{
uint8_t checksum;
uint8_t val_7bit[5]={0};
val4byte.floatVal = val;
val_7bit[0] = val4byte.byteVal[0] & 0x7f;
write(val_7bit[0]);
val_7bit[1] = ((val4byte.byteVal[1] << 1) | (val4byte.byteVal[0] >> 7)) & 0x7f;
write(val_7bit[1]);
val_7bit[2] = ((val4byte.byteVal[2] << 2) | (val4byte.byteVal[1] >> 6)) & 0x7f;
write(val_7bit[2]);
val_7bit[3] = ((val4byte.byteVal[3] << 3) | (val4byte.byteVal[2] >> 5)) & 0x7f;
write(val_7bit[3]);
val_7bit[4] = (val4byte.byteVal[3] >> 4) & 0x7f;
write(val_7bit[4]);
checksum = (val_7bit[0] + val_7bit[1] + val_7bit[2] + val_7bit[3] + val_7bit[4]) & 0x7f;
return checksum;
}
/**
* \par Function
* sendLong
* \par Description
* send (4byte long) date to 5byte 7bit data.
* \param[in]
* val - the short data to be converted.\n
* \par Output
* return the checksum data.
* \return
* None
* \par Others
* None
*/
uint8_t MeSmartServo::sendLong(long val)
{
uint8_t checksum;
uint8_t val_7bit[5]={0};
val4byte.longVal = val;
val_7bit[0] = val4byte.byteVal[0] & 0x7f;
write(val_7bit[0]);
val_7bit[1] = ((val4byte.byteVal[1] << 1) | (val4byte.byteVal[0] >> 7)) & 0x7f;
write(val_7bit[1]);
val_7bit[2] = ((val4byte.byteVal[2] << 2) | (val4byte.byteVal[1] >> 6)) & 0x7f;
write(val_7bit[2]);
val_7bit[3] = ((val4byte.byteVal[3] << 3) | (val4byte.byteVal[2] >> 5)) & 0x7f;
write(val_7bit[3]);
val_7bit[4] = (val4byte.byteVal[3] >> 4) & 0x7f;
write(val_7bit[4]);
checksum = (val_7bit[0] + val_7bit[1] + val_7bit[2] + val_7bit[3] + val_7bit[4]) & 0x7f;
return checksum;
}
/**
* \par Function
* assignDevIdRequest
* \par Description
* distribution device ID number to the smart servo link.
* \param[in]
* None
* \par Output
* None
* \return
* If the assignment is successful, return true.
* \par Others
* None
*/
boolean MeSmartServo::assignDevIdRequest(void)
{
write(START_SYSEX);
write(ALL_DEVICE);
write(CTL_ASSIGN_DEV_ID);
write(0x00);
write(0x0f);
write(END_SYSEX);
resFlag &= 0xfe;
cmdTimeOutValue = millis();
while(((resFlag & 0x01) != 0x01) || (millis() - cmdTimeOutValue < 150))
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
resFlag &= 0xfe;
wdt_reset();
return false;
}
}
resFlag &= 0xfe;
return true;
}
/**
* \par Function
* moveTo
* \par Description
* smart servo moves to the absolute angle.
* \param[in]
* dev_id - the device id of servo that we want to move.
* \param[in]
* angle_value - the absolute angle value we want move to.
* \param[in]
* speed - move speed value(The unit is rpm).
* \param[in]
* callback - callback function when the target position has been reached(Optional parameters).
* \par Output
* None
* \return
* If the assignment is successful, return true.
* \par Others
* None
*/
boolean MeSmartServo::moveTo(uint8_t dev_id,long angle_value,float speed,smartServoCb callback)
{
uint8_t checksum;
if((dev_id > servo_num_max) && (dev_id != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(dev_id);
write(SMART_SERVO);
write(SET_SERVO_ABSOLUTE_ANGLE_LONG);
checksum = (dev_id + SMART_SERVO + SET_SERVO_ABSOLUTE_ANGLE_LONG);
checksum += sendLong(angle_value);
checksum = checksum & 0x7f;
checksum += sendShort((int)speed,true);
checksum = checksum & 0x7f;
write(checksum);
write(END_SYSEX);
resFlag &= 0xbf;
_callback = callback;
cmdTimeOutValue = millis();
while((resFlag & 0x40) != 0x40)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
resFlag &= 0xbf;
wdt_reset();
return false;
}
}
resFlag &= 0xbf;
return true;
}
/**
* \par Function
* move
* \par Description
* smart servo moves to the relative angle.
* \param[in]
* dev_id - the device id of servo that we want to move.
* \param[in]
* angle_value - the relative angle value we want move to.
* \param[in]
* speed - move speed value(The unit is rpm).
* \param[in]
* callback - callback function when the target position has been reached(Optional parameters).
* \par Output
* None
* \return
* If the assignment is successful, return true.
* \par Others
* None
*/
boolean MeSmartServo::move(uint8_t dev_id,long angle_value,float speed,smartServoCb callback)
{
uint8_t checksum;
if((dev_id > servo_num_max) && (dev_id != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(dev_id);
write(SMART_SERVO);
write(SET_SERVO_RELATIVE_ANGLE_LONG);
checksum = (dev_id + SMART_SERVO + SET_SERVO_RELATIVE_ANGLE_LONG);
checksum += sendLong(angle_value);
checksum = checksum & 0x7f;
checksum += sendShort((int)speed,true);
checksum = checksum & 0x7f;
write(checksum);
write(END_SYSEX);
resFlag &= 0xbf;
_callback = callback;
cmdTimeOutValue = millis();
while((resFlag & 0x40) != 0x40)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
resFlag &= 0xbf;
wdt_reset();
return false;
}
}
resFlag &= 0xbf;
return true;
}
/**
* \par Function
* setZero
* \par Description
* set smart servo current angle zero postion.
* \param[in]
* dev_id - the device id of servo that we want to initialization position.
* \par Output
* None
* \return
* If the assignment is successful, return true.
* \par Others
* None
*/
boolean MeSmartServo::setZero(uint8_t dev_id)
{
uint8_t checksum;
if((dev_id > servo_num_max) && (dev_id != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(dev_id);
write(SMART_SERVO);
write(SET_SERVO_CURRENT_ANGLE_ZERO_DEGREES);
checksum = (dev_id + SMART_SERVO + SET_SERVO_CURRENT_ANGLE_ZERO_DEGREES);
write(checksum);
write(END_SYSEX);
resFlag &= 0xbf;
cmdTimeOutValue = millis();
while((resFlag & 0x40) != 0x40)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
resFlag &= 0xbf;
wdt_reset();
return false;
}
}
resFlag &= 0xbf;
return true;
}
/**
* \par Function
* setBreak
* \par Description
* set smart servo break status.
* \param[in]
* dev_id - the device id of servo that we want to set.
* \param[in]
* breakStatus - the break status of servo.
* \par Output
* None
* \return
* If the assignment is successful, return true.
* \par Others
* None
*/
boolean MeSmartServo::setBreak(uint8_t dev_id, uint8_t breakStatus)
{
uint8_t checksum;
if((dev_id > servo_num_max) && (dev_id != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(dev_id);
write(SMART_SERVO);
write(SET_SERVO_BREAK);
write(breakStatus);
checksum = (dev_id + SMART_SERVO + SET_SERVO_BREAK + breakStatus);
write(checksum);
write(END_SYSEX);
resFlag &= 0xbf;
cmdTimeOutValue = millis();
while((resFlag & 0x40) != 0x40)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
resFlag &= 0xbf;
wdt_reset();
return false;
}
}
resFlag &= 0xbf;
return true;
}
/**
* \par Function
* setRGBLed
* \par Description
* set the color of smart servo's RGB LED.
* \param[in]
* dev_id - the device id of servo that we want to set.
* \param[in]
* r_value - Red component.
* \param[in]
* g_value - green component.
* \param[in]
* B_value - Blue component.
* \par Output
* None
* \return
* If the assignment is successful, return true.
* \par Others
* None
*/
boolean MeSmartServo::setRGBLed(uint8_t dev_id, uint8_t r_value, uint8_t g_value, uint8_t b_value)
{
uint8_t checksum;
if((dev_id > servo_num_max) && (dev_id != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(dev_id);
write(SMART_SERVO);
write(SET_SERVO_RGB_LED);
checksum = (dev_id + SMART_SERVO + SET_SERVO_RGB_LED);
checksum += sendByte(r_value);
checksum += sendByte(g_value);
checksum += sendByte(b_value);
write(checksum);
write(END_SYSEX);
resFlag &= 0xbf;
cmdTimeOutValue = millis();
while((resFlag & 0x40) != 0x40)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
resFlag &= 0xbf;
wdt_reset();
return false;
}
}
resFlag &= 0xbf;
return true;
}
/**
* \par Function
* handSharke
* \par Description
* This function is used MCU and servo handshake.
* \param[in]
* dev_id - the device id of servo that we want to handsharke.
* \par Output
* None
* \return
* If the assignment is successful, return true.
* \par Others
* None
*/
boolean MeSmartServo::handSharke(uint8_t dev_id)
{
uint8_t checksum;
if((dev_id > servo_num_max) && (dev_id != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(dev_id);
write(SMART_SERVO);
write(SERVO_SHARKE_HAND);
checksum = (dev_id + SMART_SERVO + SERVO_SHARKE_HAND);
write(checksum);
write(END_SYSEX);
resFlag &= 0xbf;
cmdTimeOutValue = millis();
while((resFlag & 0x40) != 0x40)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
resFlag &= 0xbf;
wdt_reset();
return false;
}
}
resFlag &= 0xbf;
return true;
}
/**
* \par Function
* setPwmMove
* \par Description
* This function is used to set the pwm motion of smart servo.
* \param[in]
* dev_id - the device id of servo that we want to set.
* \param[in]
* pwm_value - the pwm value we wan't set the servo motor.
* \par Output
* None
* \return
* If the assignment is successful, return true.
* \par Others
* None
*/
boolean MeSmartServo::setPwmMove(uint8_t dev_id, int16_t pwm_value)
{
uint8_t checksum;
if((dev_id > servo_num_max) && (dev_id != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(dev_id);
write(SMART_SERVO);
write(SET_SERVO_PWM_MOVE);
checksum = (dev_id + SMART_SERVO + SET_SERVO_PWM_MOVE);
checksum += sendShort(pwm_value,false);
write(checksum);
write(END_SYSEX);
resFlag &= 0xbf;
cmdTimeOutValue = millis();
while((resFlag & 0x40) != 0x40)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
resFlag &= 0xbf;
wdt_reset();
return false;
}
}
resFlag &= 0xbf;
return true;
}
/**
* \par Function
* setInitAngle
* \par Description
* This function is used to move smart servo to its 0 degrees.
* \param[in]
* dev_id - the device id of servo that we want to set.
* \param[in]
* mode - the return mode, 0 is the quick return mode.
* \param[in]
* speed - the speed value return to init angle.
* \par Output
* None
* \return
* If the assignment is successful, return true.
* \par Others
* None
*/
boolean MeSmartServo::setInitAngle(uint8_t dev_id,uint8_t mode,int16_t speed)
{
uint8_t checksum;
if((dev_id > servo_num_max) && (dev_id != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(dev_id);
write(SMART_SERVO);
write(SET_SERVO_INIT_ANGLE);
write(mode);
checksum = (dev_id + SMART_SERVO + SET_SERVO_INIT_ANGLE + mode);
checksum += sendShort(abs(speed),true);
write(checksum);
write(END_SYSEX);
resFlag &= 0xbf;
cmdTimeOutValue = millis();
while((resFlag & 0x40) != 0x40)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
resFlag &= 0xbf;
wdt_reset();
return false;
}
}
resFlag &= 0xbf;
return true;
}
/**
* \par Function
* getAngleRequest
* \par Description
* This function used to get the smart servo's angle.
* \param[in]
* devId - the device id of servo that we want to read its angle.
* \par Output
* None
* \return
* the angle of smart servo.
* \par Others
* None
*/
long MeSmartServo::getAngleRequest(uint8_t devId)
{
uint8_t checksum;
if((devId > servo_num_max) && (devId != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(devId);
write(SMART_SERVO);
write(GET_SERVO_CUR_ANGLE);
write(0x00);
checksum = devId + SMART_SERVO + GET_SERVO_CUR_ANGLE + 0x00;
write(checksum);
write(END_SYSEX);
resFlag &= 0xfd;
cmdTimeOutValue = millis();
while((resFlag & 0x02) != 0x02)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
wdt_reset();
break;
}
}
resFlag &= 0xfd;
return servo_dev_list[devId - 1].angleValue;
}
/**
* \par Function
* getSpeedRequest
* \par Description
* This function used to get the smart servo's speed.
* \param[in]
* devId - the device id of servo that we want to read its speed.
* \par Output
* None
* \return
* the speed of smart servo.
* \par Others
* None
*/
float MeSmartServo::getSpeedRequest(uint8_t devId)
{
uint8_t checksum;
if((devId > servo_num_max) && (devId != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(devId);
write(SMART_SERVO);
write(GET_SERVO_SPEED);
write(0x00);
checksum = devId + SMART_SERVO + GET_SERVO_SPEED + 0x00;
write(checksum);
write(END_SYSEX);
resFlag &= 0xfb;
cmdTimeOutValue = millis();
while((resFlag & 0x04) != 0x04)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
wdt_reset();
break;
}
}
resFlag &= 0xfb;
return servo_dev_list[devId - 1].servoSpeed;
}
/**
* \par Function
* getVoltageRequest
* \par Description
* This function used to get the smart servo's voltage.
* \param[in]
* devId - the device id of servo that we want to read its voltage.
* \par Output
* None
* \return
* the voltage of smart servo.
* \par Others
* None
*/
float MeSmartServo::getVoltageRequest(uint8_t devId)
{
uint8_t checksum;
if((devId > servo_num_max) && (devId != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(devId);
write(SMART_SERVO);
write(GET_SERVO_VOLTAGE);
write(0x00);
checksum = devId + SMART_SERVO + GET_SERVO_VOLTAGE + 0x00;
write(checksum);
write(END_SYSEX);
resFlag &= 0xf7;
cmdTimeOutValue = millis();
while((resFlag & 0x08) != 0x08)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
wdt_reset();
break;
}
}
return servo_dev_list[devId - 1].voltage;
}
/**
* \par Function
* getTempRequest
* \par Description
* This function used to get the smart servo's temperature.
* \param[in]
* devId - the device id of servo that we want to read its temperature.
* \par Output
* None
* \return
* the temperature of smart servo.
* \par Others
* None
*/
float MeSmartServo::getTempRequest(uint8_t devId)
{
uint8_t checksum;
if((devId > servo_num_max) && (devId != ALL_DEVICE))
{
return false;
}
write(START_SYSEX);
write(devId);
write(SMART_SERVO);
write(GET_SERVO_TEMPERATURE);
write(0x00);
checksum = devId + SMART_SERVO + GET_SERVO_TEMPERATURE + 0x00;
write(checksum);
write(END_SYSEX);
resFlag &= 0xef;
cmdTimeOutValue = millis();
while((resFlag & 0x10) != 0x10)
{
smartServoEventHandle();
if(millis() - cmdTimeOutValue > 1200)
{
wdt_reset();
break;
}
}
resFlag &= 0xf7;
return servo_dev_list[devId - 1].temperature;
}
/**
* \par Function
* getCurrentRequest
* \par Description
* This function used to get the smart servo's current.
* \param[in]
* devId - the device id of servo that we want to read its current.
* \par Output
* None