-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAxelFlow.c
774 lines (735 loc) · 20.8 KB
/
AxelFlow.c
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
/*
* AX-12A.c
*
* Created on: Apr 23, 2024
* Author: Srikar Bharadwaj R
*/
#include "AxelFlow.h"
#define DEBUG_PRINT_STATUS
Instruction_Packet packet;
Servo AxelFlow_servo_init(uint8_t id, UART_HandleTypeDef *huartx,
bool jointMode)
{
Servo servo;
servo.id = id;
servo.huartx = *huartx;
servo.jointMode = jointMode;
return servo;
}
bool checkChecksum(Status_Packet packet)
{
Instruction_Packet ip;
ip.Header_1 = packet.Header_1;
ip.Header_2 = packet.Header_2;
ip.Packet_ID = packet.Packet_ID;
ip.Length = packet.Length;
ip.Instruction = packet.Error;
ip.Param = packet.Param;
ip.Checksum = packet.Checksum;
if (packet.Checksum == getChecksum(ip))
return 1;
else
return 0;
}
uint8_t* degreesToData(float degrees)
{
static uint8_t data[2];
uint16_t degrees_Hex = (uint16_t) (degrees * 3.41f); // 1023/300
data[0] = degrees_Hex;
data[1] = degrees_Hex >> 8;
if (degrees == 0.0)
{
data[0] = 0;
data[1] = 0;
}
return data;
}
float dataToDegrees(uint8_t *Param)
{
return ((uint16_t) Param[0] + (Param[1] << 8)) / 3.41;
}
uint16_t getCCWLimit(Servo servo)
{
uint8_t param_array[2] = { EEPROM_CCW_ANGLE_LIMIT_L, ALL };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return dataToDegrees(status.Param);
else
return -1;
}
Status_Packet setCCWLimit(float max_angle, Servo servo)
{
uint8_t *data = degreesToData(max_angle);
uint8_t param_array[3] = { EEPROM_CCW_ANGLE_LIMIT_L, data[0], data[1] };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
uint16_t getCWLimit(Servo servo)
{
uint8_t param_array[2] = { EEPROM_CW_ANGLE_LIMIT_L, ALL };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return dataToDegrees(status.Param);
else
return -1;
}
Status_Packet setCWLimit(float max_angle, Servo servo)
{
uint8_t *data = degreesToData(max_angle);
uint8_t param_array[3] = { EEPROM_CW_ANGLE_LIMIT_L, data[0], data[1] };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
Status_Packet setPosition(float angle, Servo servo)
{
uint8_t *data = degreesToData(angle);
uint8_t param_array[3] = { RAM_GOAL_POSITION_L, data[0], data[1] };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
Status_Packet setMaxTorque(float torque, Servo servo) // torque 0 to 100%
{
uint8_t *data = degreesToData(torque * 3); // 1023 / (100*3.41)
uint8_t param_array[3] = { EEPROM_MAX_TORQUE_L, data[0], data[1] };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
float getMaxTorque(Servo servo)
{
uint8_t param_array[2] = { EEPROM_MAX_TORQUE_L, ALL };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return (dataToDegrees(status.Param) / 3.0);
else
return 0;
}
Status_Packet setRunningTorque(float torque, Servo servo) // torque 0 to 100%
{
uint8_t *data = degreesToData(torque * 3); // 1023 / (100*3.41)
uint8_t param_array[3] = { RAM_TORQUE_LIMIT_L, data[0], data[1] };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
Status_Packet setSpeed(float speed, Servo servo)
{
uint8_t *data;
if (servo.jointMode)
{
data = (speed < 99) ? degreesToData(speed * 3) : 0; // 1023 / (100*3.41)
}
else // TODO; Add switch.
{
speed *= CLOCKWISE_SWITCH;
data = (speed >= 0 && speed <= 100) ?
degreesToData(speed * 3.0) :
((speed < 0 && speed >= -100) ?
degreesToData((-speed + 100) * 3) : 0);
}
uint8_t param_array[3] = { RAM_GOAL_SPEED_L, data[0], data[1] };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
Status_Packet setSpeedinRPM(float rpm, Servo servo)
{
uint8_t *data;
if (servo.jointMode)
{
data = (rpm < 114) ? degreesToData(rpm * 2.632) : 0; // 1023 / (114*3.41)
}
else
{
data = NULL;
}
uint8_t param_array[3] = { RAM_GOAL_SPEED_L, data[0], data[1] };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
return AxelFlow_fire(&servo.huartx, packet);
}
Status_Packet controlLED(bool state, Servo servo)
{
uint8_t param_array[2] = { RAM_LED, state };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
return AxelFlow_fire(&servo.huartx, packet);
}
uint8_t getLEDStatus(Servo servo)
{
uint8_t param_array[1] = { RAM_LED };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0];
else
return -1;
}
Status_Packet getPosition(Servo servo)
{
uint8_t param_array[2] = { RAM_PRESENT_POSITION_L, ALL };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
float getPositionAngle(Servo servo)
{
Status_Packet status = getPosition(servo);
return dataToDegrees(status.Param);
}
Status_Packet getSpeed(Servo servo)
{
uint8_t param_array[2] = { RAM_PRESENT_SPEED_L, ALL };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
float getSpeedRPM(Servo servo)
{
Status_Packet status = getSpeed(servo);
return dataToDegrees(status.Param);
}
Status_Packet reboot(Servo servo)
{
uint8_t param_array[0] = { };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_REBOOT;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
bool ping(Servo servo)
{
uint8_t param_array[0] = { };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_PING;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error
== 0&& status.Header_1 == HEADER && status.Header_2 == HEADER)
return true;
else
return false;
}
Status_Packet changeServoID(uint8_t future_ID, Servo servo)
{
uint8_t param_array[2] = { EEPROM_ID, future_ID };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
uint8_t readID(Servo servo) //useless function
{
uint8_t param_array[1] = { EEPROM_ID };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0];
else
return -1;
}
BaudRate getBaudRate(int new_Baud_Rate)
{
if (new_Baud_Rate <= 9600)
return BAUD_9600;
else if (new_Baud_Rate <= 19200)
return BAUD_19200;
else if (new_Baud_Rate <= 57600)
return BAUD_57600;
else if (new_Baud_Rate <= 115200)
return BAUD_115200;
else if (new_Baud_Rate <= 200000)
return BAUD_200000;
else if (new_Baud_Rate <= 250000)
return BAUD_250000;
else if (new_Baud_Rate <= 400000)
return BAUD_400000;
else if (new_Baud_Rate <= 500000)
return BAUD_500000;
else
return BAUD_DEFAULT;
}
Status_Packet changeBaudRate(uint32_t new_Baud_Rate, Servo servo)
{
uint8_t param_array[2] = { EEPROM_BAUD_RATE, getBaudRate(new_Baud_Rate) };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
uint32_t readBaudRate(Servo servo)
{
uint8_t param_array[1] = { EEPROM_BAUD_RATE };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
switch ((uint8_t) *status.Param)
{
case BAUD_9600:
return 9600;
case BAUD_19200:
return 19200;
case BAUD_57600:
return 57600;
case BAUD_115200:
return 115200;
case BAUD_200000:
return 200000;
case BAUD_250000:
return 250000;
case BAUD_400000:
return 400000;
case BAUD_500000:
return 500000;
case BAUD_DEFAULT:
return 1E6;
default:
return 0;
}
}
Status_Packet forceSetPosition(uint16_t angle, Servo servo)
{
uint16_t current_angle;
Status_Packet status;
do
{
status = setPosition(angle, servo);
current_angle = (uint16_t) getPositionAngle(servo);
} while (angle != current_angle && angle != current_angle + 1
&& angle != current_angle - 1);
return status;
}
uint8_t getFirmwareVersion(Servo servo)
{
uint8_t param_array[1] = { EEPROM_VERSION };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0];
else
return 0;
}
uint8_t scanID(Servo servo)
{
for (uint8_t id = ID_MIN; id < ID_MAX; id++)
{
servo.id = id;
if (ping(servo))
return id;
}
return 0xFF;
}
uint32_t scanBaudRate(Servo servo) // TODO: Test
{
uint32_t baudRates[9] = { 9600, 19200, 57600, 115200, 200000, 250000,
400000, 500000, 1000000 };
for (uint8_t baudID = 0; baudID < 9; baudID++)
{
// char ch[10];
// sprintf(ch, "%lu\n\r", baudRates[baudID]);
// AxelFlow_debug_println(ch);
UART_HandleTypeDef servo1_UART_Handle = AxelFlow_UART_Init(USART1,
getBaudRate(baudRates[baudID]));
servo.huartx = servo1_UART_Handle;
HAL_Delay(2);
if (ping(servo))
return baudRates[baudID];
}
return 0;
}
uint16_t getResponseDelayTime(Servo servo)
{
uint8_t param_array[1] = { EEPROM_RETURN_DELAY_TIME };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0] * 2;
else
return -1;
}
Status_Packet setResponseDelayTime(uint16_t response_time, Servo servo)
{
uint8_t param_array[2] = { EEPROM_RETURN_DELAY_TIME, (response_time / 2) };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
uint8_t getTemperatureLimit(Servo servo)
{
uint8_t param_array[1] = { EEPROM_LIMIT_TEMPERATURE };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0];
else
return -1;
}
Status_Packet setMinVoltageLimit(float min_voltage, Servo servo)
{
uint8_t param_array[2] = { EEPROM_LOW_LIMIT_VOLTAGE, (uint8_t) (min_voltage
* 10) };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
uint8_t getMinVoltageLimit(Servo servo)
{
uint8_t param_array[1] = { EEPROM_LOW_LIMIT_VOLTAGE };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return (status.Param[0] / 10);
else
return -1;
}
Status_Packet setMaxVoltageLimit(float max_voltage, Servo servo)
{
uint8_t param_array[2] = { EEPROM_HIGH_LIMIT_VOLTAGE, (uint8_t) (max_voltage
* 10) };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
uint8_t getMaxVoltageLimit(Servo servo)
{
uint8_t param_array[1] = { EEPROM_HIGH_LIMIT_VOLTAGE };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return (status.Param[0] / 10);
else
return -1;
}
Status_Packet setTemperatureLimit(float max_temp, Servo servo)
{
uint8_t param_array[2] = { EEPROM_LIMIT_TEMPERATURE, max_temp };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
uint8_t getStatusReturnLevel(Servo servo)
{
uint8_t param_array[1] = { EEPROM_RETURN_LEVEL };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0];
else
return -1;
}
Status_Packet setStatusReturnLevel(uint8_t status, Servo servo)
{
uint8_t param_array[2] = { EEPROM_RETURN_LEVEL, status };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
uint8_t getAlarmLEDStatus(Servo servo)
{
uint8_t param_array[1] = { EEPROM_ALARM_LED };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0];
else
return -1;
}
Status_Packet setAlarmLEDStatus(uint8_t status, Servo servo)
{
uint8_t param_array[2] = { EEPROM_ALARM_LED, status };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
uint8_t getShutdownStatus(Servo servo)
{
uint8_t param_array[1] = { EEPROM_ALARM_SHUTDOWN };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0];
else
return -1;
}
Status_Packet setShutdownStatus(uint8_t status, Servo servo)
{
uint8_t param_array[2] = { EEPROM_ALARM_SHUTDOWN, status };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
uint8_t getTorqueEnableStatus(Servo servo)
{
uint8_t param_array[1] = { RAM_TORQUE_ENABLE };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0];
else
return -1;
}
Status_Packet setTorqueEnableStatus(bool enable, Servo servo)
{
uint8_t param_array[2] = { RAM_TORQUE_ENABLE, enable };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
void setWheelMode(Servo servo)
{
setCCWLimit(1, servo);
HAL_Delay(10);
setCWLimit(1, servo);
}
float getPresentLoad(Servo servo)
{
uint8_t param_array[2] = { RAM_PRESENT_LOAD_L, ALL };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
{
float load = dataToDegrees(status.Param) * 3.41;
load = (load < 1023) ?
load * (100.0 / 1023) : (load * (-100.0 / 1023) + 100.0);
return load;
}
else
return -1;
}
float getPresentTemperature(Servo servo)
{
uint8_t param_array[1] = { RAM_PRESENT_TEMPERATURE };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
{
return status.Param[0] * 1.0;
}
else
return -1;
}
float getPresentVoltage(Servo servo)
{
uint8_t param_array[1] = { RAM_PRESENT_VOLTAGE };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
{
return status.Param[0] / 10.0;
}
else
return -1;
}
uint8_t getRegistered(Servo servo)
{
uint8_t param_array[1] = { RAM_REGISTER };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0];
else
return -1;
}
uint8_t getMoving(Servo servo)
{
uint8_t param_array[1] = { RAM_MOVING };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0];
else
return -1;
}
uint8_t getLockStatus(Servo servo)
{
uint8_t param_array[1] = { RAM_LOCK };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
return status.Param[0];
else
return -1;
}
Status_Packet setPunch(float punch, Servo servo)
{
uint8_t *data = degreesToData(punch * 3.0);
uint8_t param_array[3] = { RAM_PUNCH_L, data[0], data[1] };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_WRITE_DATA;
packet.Param = param_array;
return AxelFlow_fire(&servo.huartx, packet);
}
float getPunch(Servo servo)
{
uint8_t param_array[2] = { RAM_PUNCH_L, RAM_PUNCH_H };
packet.Packet_ID = servo.id;
packet.Length = sizeof(param_array) + 2;
packet.Instruction = COMMAND_READ_DATA;
packet.Param = param_array;
Status_Packet status = AxelFlow_fire(&servo.huartx, packet);
if (status.Error == 0)
{
float data = dataToDegrees(status.Param) / 3.0;
return data;
}
else
return -1;
}
#ifdef DEBUG_PRINT_STATUS
void print_status(Status_Packet packet, bool just_Errors)
{
if (!(just_Errors && packet.Error == 0x00))
{
if (packet.Header_1 == HEADER && packet.Header_2 == HEADER)
{
char temp[150] = { '\0' };
sprintf(temp, "Header 1: 0x%02X", packet.Header_1);
AxelFlow_debug_println(temp);
sprintf(temp, "Header 2: 0x%02X", packet.Header_2);
AxelFlow_debug_println(temp);
sprintf(temp, "Packet ID: 0x%02X", packet.Packet_ID);
AxelFlow_debug_println(temp);
sprintf(temp, "Length: 0x%02X", packet.Length);
AxelFlow_debug_println(temp);
char error[100] = { '\0' };
if (packet.Error & (1 << Input_Voltage_Error))
strcat(error, "Input Voltage Error\t");
if (packet.Error & (1 << Angle_Limit_Error))
strcat(error, "Angle Limit Error\t");
if (packet.Error & (1 << Overheating_Error))
strcat(error, "Overheating Error\t");
if (packet.Error & (1 << Range_Error))
strcat(error, "Range Error\t");
if (packet.Error & (1 << Checksum_Error))
strcat(error, "Checksum Error\t");
if (packet.Error & (1 << Overload_Error))
strcat(error, "Overload Error\t");
if (packet.Error & (1 << Instruction_Error))
strcat(error, "Instruction Error\t");
sprintf(temp, "Error: %s(0x%02X)", error, packet.Error);
AxelFlow_debug_println(temp);
AxelFlow_debug_print("Param:\t");
for (uint8_t i = 0; i < packet.Length - 2; i++)
{
sprintf(temp, "0x%02X\t", packet.Param[i]);
AxelFlow_debug_print(temp);
}
AxelFlow_debug_println("");
sprintf(temp, "Checksum: 0x%02X", packet.Checksum);
AxelFlow_debug_println(temp);
AxelFlow_debug_println("");
}
else
{
AxelFlow_debug_println("No Servo Connected");
}
}
}
#endif