15
15
#include " default_colors.h"
16
16
17
17
Arduino_Alvik::Arduino_Alvik ():i2c(Wire){
18
+ buffer_semaphore = xSemaphoreCreateRecursiveMutex ();
18
19
update_semaphore = xSemaphoreCreateMutex ();
19
20
uart = new HardwareSerial (UART); // &Serial0
20
21
packeter = new ucPack (200 );
@@ -30,14 +31,14 @@ Arduino_Alvik::Arduino_Alvik():i2c(Wire){
30
31
robot_vel_semaphore = xSemaphoreCreateMutex ();
31
32
robot_pos_semaphore = xSemaphoreCreateMutex ();
32
33
33
- left_led = ArduinoAlvikRgbLed (uart, packeter, " left_led" , &led_state, 2 );
34
- right_led = ArduinoAlvikRgbLed (uart, packeter," right_led" , &led_state, 5 );
34
+ left_led = ArduinoAlvikRgbLed (uart, packeter, &buffer_semaphore, " left_led" , &led_state, 2 );
35
+ right_led = ArduinoAlvikRgbLed (uart, packeter, &buffer_semaphore, " right_led" , &led_state, 5 );
35
36
36
- left_wheel = ArduinoAlvikWheel (uart, packeter, ' L' , &joints_velocity[0 ], &joints_position[0 ], WHEEL_DIAMETER_MM, *this );
37
- right_wheel = ArduinoAlvikWheel (uart, packeter, ' R' , &joints_velocity[1 ], &joints_position[1 ], WHEEL_DIAMETER_MM, *this );
37
+ left_wheel = ArduinoAlvikWheel (uart, packeter, &buffer_semaphore, ' L' , &joints_velocity[0 ], &joints_position[0 ], WHEEL_DIAMETER_MM, *this );
38
+ right_wheel = ArduinoAlvikWheel (uart, packeter, &buffer_semaphore, ' R' , &joints_velocity[1 ], &joints_position[1 ], WHEEL_DIAMETER_MM, *this );
38
39
39
- servo_A = ArduinoAlvikServo (uart, packeter, ' A' , 0 , servo_positions);
40
- servo_B = ArduinoAlvikServo (uart, packeter, ' B' , 1 , servo_positions);
40
+ servo_A = ArduinoAlvikServo (uart, packeter, &buffer_semaphore, ' A' , 0 , servo_positions);
41
+ servo_B = ArduinoAlvikServo (uart, packeter, &buffer_semaphore, ' B' , 1 , servo_positions);
41
42
}
42
43
43
44
void Arduino_Alvik::reset_hw (){ // it is private
@@ -404,8 +405,10 @@ void Arduino_Alvik::get_wheels_speed(float & left, float & right, const uint8_t
404
405
}
405
406
406
407
void Arduino_Alvik::set_wheels_speed (const float left, const float right, const uint8_t unit){
408
+ while (!xSemaphoreTakeRecursive (buffer_semaphore, TICK_TIME_SEMAPHORE));
407
409
msg_size = packeter->packetC2F (' J' , convert_rotational_speed (left, unit, RPM), convert_rotational_speed (right, unit, RPM));
408
410
uart->write (packeter->msg , msg_size);
411
+ xSemaphoreGiveRecursive (buffer_semaphore);
409
412
}
410
413
411
414
void Arduino_Alvik::get_wheels_position (float & left, float & right, const uint8_t unit){
@@ -416,12 +419,14 @@ void Arduino_Alvik::get_wheels_position(float & left, float & right, const uint8
416
419
}
417
420
418
421
void Arduino_Alvik::set_wheels_position (const float left, const float right, const uint8_t unit, const bool blocking){
422
+ while (!xSemaphoreTakeRecursive (buffer_semaphore, TICK_TIME_SEMAPHORE));
419
423
msg_size = packeter->packetC2F (' A' , convert_angle (left, unit, DEG), convert_angle (right, unit, DEG));
420
424
uart->write (packeter->msg , msg_size);
421
425
waiting_ack = ' P' ;
422
426
if (blocking){
423
427
wait_for_target (max (left, right) / MOTOR_CONTROL_DEG_S);
424
428
}
429
+ xSemaphoreGiveRecursive (buffer_semaphore);
425
430
}
426
431
427
432
void Arduino_Alvik::get_drive_speed (float & linear, float & angular, const uint8_t linear_unit, const uint8_t angular_unit){
@@ -437,6 +442,7 @@ void Arduino_Alvik::get_drive_speed(float & linear, float & angular, const uint8
437
442
}
438
443
439
444
void Arduino_Alvik::drive (const float linear, const float angular, const uint8_t linear_unit, const uint8_t angular_unit){
445
+ while (!xSemaphoreTakeRecursive (buffer_semaphore, TICK_TIME_SEMAPHORE));
440
446
if (angular_unit == PERCENTAGE){
441
447
converted_angular = (angular/ROBOT_MAX_DEG_S)*100.0 ;
442
448
}
@@ -445,6 +451,7 @@ void Arduino_Alvik::drive(const float linear, const float angular, const uint8_t
445
451
}
446
452
msg_size = packeter->packetC2F (' V' , convert_speed (linear, linear_unit, MM_S), converted_angular);
447
453
uart->write (packeter->msg , msg_size);
454
+ xSemaphoreGiveRecursive (buffer_semaphore);
448
455
}
449
456
450
457
void Arduino_Alvik::get_pose (float & x, float & y, float & theta, const uint8_t distance_unit, const uint8_t angle_unit){
@@ -456,11 +463,14 @@ void Arduino_Alvik::get_pose(float & x, float & y, float & theta, const uint8_t
456
463
}
457
464
458
465
void Arduino_Alvik::reset_pose (const float x, const float y, const float theta, const uint8_t distance_unit, const uint8_t angle_unit){
466
+ while (!xSemaphoreTakeRecursive (buffer_semaphore, TICK_TIME_SEMAPHORE));
459
467
msg_size = packeter->packetC3F (' Z' , convert_distance (x, distance_unit, MM), convert_distance (y, distance_unit, MM), convert_distance (theta, angle_unit, DEG));
460
468
uart->write (packeter->msg , msg_size);
469
+ xSemaphoreGiveRecursive (buffer_semaphore);
461
470
}
462
471
463
472
bool Arduino_Alvik::is_target_reached (){
473
+ while (!xSemaphoreTakeRecursive (buffer_semaphore, TICK_TIME_SEMAPHORE));
464
474
if (waiting_ack == NO_ACK){
465
475
return true ;
466
476
}
@@ -470,43 +480,48 @@ bool Arduino_Alvik::is_target_reached(){
470
480
waiting_ack = NO_ACK;
471
481
last_ack = 0x00 ;
472
482
delay (100 );
483
+ xSemaphoreGiveRecursive (buffer_semaphore);
473
484
return true ;
474
485
}
486
+ xSemaphoreGiveRecursive (buffer_semaphore);
475
487
return false ;
476
488
}
477
489
478
- void Arduino_Alvik::wait_for_target (const int idle_time){ // it is private
490
+ void Arduino_Alvik::wait_for_target (const int idle_time){ // it is private
479
491
unsigned long start_t = millis ();
480
492
481
493
while (true ){
482
494
if (((millis () - start_t ) >= idle_time*1000 ) && is_target_reached ()) {
483
495
break ;
484
- } else
485
- {
496
+ }
497
+ else {
486
498
delay (100 );
487
499
}
488
-
489
500
}
490
501
}
491
502
492
503
void Arduino_Alvik::rotate (const float angle, const uint8_t unit, const bool blocking){
504
+ while (!xSemaphoreTakeRecursive (buffer_semaphore, TICK_TIME_SEMAPHORE));
493
505
delay (200 );
494
506
msg_size = packeter->packetC1F (' R' , convert_angle (angle, unit, DEG));
495
507
uart->write (packeter->msg , msg_size);
496
508
waiting_ack = ' R' ;
497
509
if (blocking){
498
510
wait_for_target (round (angle/MOTOR_CONTROL_DEG_S));
499
511
}
512
+ xSemaphoreGiveRecursive (buffer_semaphore);
500
513
}
501
514
502
515
void Arduino_Alvik::move (const float distance, const uint8_t unit, const bool blocking){
516
+ while (!xSemaphoreTakeRecursive (buffer_semaphore, TICK_TIME_SEMAPHORE));
503
517
delay (200 );
504
518
msg_size = packeter->packetC1F (' G' , convert_distance (distance, unit, MM));
505
519
uart->write (packeter->msg , msg_size);
506
520
waiting_ack = ' M' ;
507
521
if (blocking){
508
522
wait_for_target (round (distance/MOTOR_CONTROL_MM_S));
509
523
}
524
+ xSemaphoreGiveRecursive (buffer_semaphore);
510
525
}
511
526
512
527
void Arduino_Alvik::brake (){
@@ -841,6 +856,10 @@ bool Arduino_Alvik::get_shake(){
841
856
return move_bits & 0b00000001 ;
842
857
}
843
858
859
+ bool Arduino_Alvik::get_lifted (){
860
+ return move_bits & 0b00000010 ;
861
+ }
862
+
844
863
String Arduino_Alvik::get_tilt (){
845
864
if (move_bits & 0b00000100 ){
846
865
return " X" ;
@@ -943,8 +962,10 @@ bool Arduino_Alvik::is_battery_charging(){
943
962
// -----------------------------------------------------------------------------------------------//
944
963
945
964
void Arduino_Alvik::set_leds (){ // it is private
965
+ while (!xSemaphoreTakeRecursive (buffer_semaphore, TICK_TIME_SEMAPHORE));
946
966
msg_size = packeter->packetC1B (' L' , led_state);
947
967
uart->write (packeter->msg , msg_size);
968
+ xSemaphoreGiveRecursive (buffer_semaphore);
948
969
}
949
970
950
971
void Arduino_Alvik::set_builtin_led (const bool value){
@@ -970,8 +991,10 @@ void Arduino_Alvik::set_illuminator(const bool value){
970
991
void Arduino_Alvik::set_servo_positions (const uint8_t a_position, const uint8_t b_position){
971
992
servo_positions[0 ] = a_position;
972
993
servo_positions[1 ] = b_position;
994
+ while (!xSemaphoreTakeRecursive (buffer_semaphore, TICK_TIME_SEMAPHORE));
973
995
msg_size = packeter->packetC2B (' S' , a_position, b_position);
974
996
uart->write (packeter->msg , msg_size);
997
+ xSemaphoreGiveRecursive (buffer_semaphore);
975
998
}
976
999
977
1000
void Arduino_Alvik::get_servo_positions (int & a_position, int & b_position){
@@ -980,8 +1003,10 @@ void Arduino_Alvik::get_servo_positions(int & a_position, int & b_position){
980
1003
}
981
1004
982
1005
void Arduino_Alvik::set_behaviour (const uint8_t behaviour){
1006
+ while (!xSemaphoreTakeRecursive (buffer_semaphore, TICK_TIME_SEMAPHORE));
983
1007
msg_size = packeter->packetC1B (' B' , behaviour);
984
1008
uart->write (packeter->msg , msg_size);
1009
+ xSemaphoreGiveRecursive (buffer_semaphore);
985
1010
}
986
1011
987
1012
void Arduino_Alvik::get_version (uint8_t & upper, uint8_t & middle, uint8_t & lower, String version){
@@ -1032,10 +1057,12 @@ bool Arduino_Alvik::check_firmware_compatibility(){
1032
1057
// RGB led class //
1033
1058
// -----------------------------------------------------------------------------------------------//
1034
1059
1035
- Arduino_Alvik::ArduinoAlvikRgbLed::ArduinoAlvikRgbLed (HardwareSerial * serial, ucPack * packeter, String label,
1060
+ Arduino_Alvik::ArduinoAlvikRgbLed::ArduinoAlvikRgbLed (HardwareSerial * serial, ucPack * packeter, SemaphoreHandle_t * buffer_semaphore,
1061
+ String label,
1036
1062
uint8_t * led_state, uint8_t offset){
1037
1063
_serial = serial;
1038
1064
_packeter = packeter;
1065
+ _buffer_semaphore = buffer_semaphore;
1039
1066
this ->label = label;
1040
1067
_led_state = led_state;
1041
1068
_offset = offset;
@@ -1044,6 +1071,7 @@ Arduino_Alvik::ArduinoAlvikRgbLed::ArduinoAlvikRgbLed(HardwareSerial * serial, u
1044
1071
void Arduino_Alvik::ArduinoAlvikRgbLed::operator =(const ArduinoAlvikRgbLed& other){
1045
1072
_serial = other._serial ;
1046
1073
_packeter = other._packeter ;
1074
+ _buffer_semaphore = other._buffer_semaphore ;
1047
1075
label = other.label ;
1048
1076
_led_state = other._led_state ;
1049
1077
_offset = other._offset ;
@@ -1072,41 +1100,49 @@ void Arduino_Alvik::ArduinoAlvikRgbLed::set_color(const bool red, const bool gre
1072
1100
else {
1073
1101
(*_led_state) = (*_led_state) & ~(1 <<(_offset+2 ));
1074
1102
}
1075
-
1103
+ while (! xSemaphoreTakeRecursive (*_buffer_semaphore, TICK_TIME_SEMAPHORE));
1076
1104
_msg_size = _packeter->packetC1B (' L' , *_led_state);
1077
1105
_serial->write (_packeter->msg , _msg_size);
1106
+ xSemaphoreGiveRecursive (*_buffer_semaphore);
1078
1107
}
1079
1108
1080
1109
1081
1110
// -----------------------------------------------------------------------------------------------//
1082
1111
// wheel class //
1083
1112
// -----------------------------------------------------------------------------------------------//
1084
1113
1085
- Arduino_Alvik::ArduinoAlvikWheel::ArduinoAlvikWheel (HardwareSerial * serial, ucPack * packeter, uint8_t label,
1114
+ Arduino_Alvik::ArduinoAlvikWheel::ArduinoAlvikWheel (HardwareSerial * serial, ucPack * packeter, SemaphoreHandle_t * buffer_semaphore,
1115
+ uint8_t label,
1086
1116
float * joint_velocity, float * joint_position, float wheel_diameter, Arduino_Alvik & alvik):_alvik(&alvik){
1087
1117
_serial = serial;
1088
1118
_packeter = packeter;
1119
+ _buffer_semaphore = buffer_semaphore;
1089
1120
_label = label;
1090
1121
_wheel_diameter = wheel_diameter;
1091
1122
_joint_velocity = joint_velocity;
1092
1123
_joint_position = joint_position;
1093
1124
}
1094
1125
1095
1126
void Arduino_Alvik::ArduinoAlvikWheel::reset (const float initial_position, const uint8_t unit){
1127
+ while (!xSemaphoreTakeRecursive (*_buffer_semaphore, TICK_TIME_SEMAPHORE));
1096
1128
_msg_size = _packeter->packetC2B1F (' W' , _label, ' Z' , convert_angle (initial_position, unit, DEG));
1097
1129
_serial->write (_packeter->msg , _msg_size);
1130
+ xSemaphoreGiveRecursive (*_buffer_semaphore);
1098
1131
}
1099
1132
1100
1133
void Arduino_Alvik::ArduinoAlvikWheel::set_pid_gains (const float kp, const float ki, const float kd){
1134
+ while (!xSemaphoreTakeRecursive (*_buffer_semaphore, TICK_TIME_SEMAPHORE));
1101
1135
_msg_size = _packeter->packetC1B3F (' P' , _label, kp, ki, kd);
1102
1136
_serial->write (_packeter->msg , _msg_size);
1137
+ xSemaphoreGiveRecursive (*_buffer_semaphore);
1103
1138
}
1104
1139
1105
1140
void Arduino_Alvik::ArduinoAlvikWheel::stop (){
1106
1141
set_speed (0 );
1107
1142
}
1108
1143
1109
1144
void Arduino_Alvik::ArduinoAlvikWheel::set_speed (const float velocity, const uint8_t unit){
1145
+ while (!xSemaphoreTakeRecursive (*_buffer_semaphore, TICK_TIME_SEMAPHORE));
1110
1146
if (unit==PERCENTAGE){
1111
1147
converted_vel = (velocity/100.0 )*MOTOR_MAX_RPM;
1112
1148
}
@@ -1115,6 +1151,7 @@ void Arduino_Alvik::ArduinoAlvikWheel::set_speed(const float velocity, const uin
1115
1151
}
1116
1152
_msg_size = _packeter->packetC2B1F (' W' , _label, ' V' , converted_vel);
1117
1153
_serial->write (_packeter->msg , _msg_size);
1154
+ xSemaphoreGiveRecursive (*_buffer_semaphore);
1118
1155
}
1119
1156
1120
1157
float Arduino_Alvik::ArduinoAlvikWheel::get_speed (const uint8_t unit){
@@ -1125,12 +1162,14 @@ float Arduino_Alvik::ArduinoAlvikWheel::get_speed(const uint8_t unit){
1125
1162
}
1126
1163
1127
1164
void Arduino_Alvik::ArduinoAlvikWheel::set_position (const float position, const uint8_t unit, const bool blocking){
1165
+ while (!xSemaphoreTakeRecursive (*_buffer_semaphore, TICK_TIME_SEMAPHORE));
1128
1166
_msg_size = _packeter->packetC2B1F (' W' , _label, ' P' , convert_angle (position, unit, DEG));
1129
1167
_serial->write (_packeter->msg , _msg_size);
1130
1168
_alvik->waiting_ack = ' P' ;
1131
1169
if (blocking){
1132
1170
_alvik->wait_for_target (position / MOTOR_CONTROL_DEG_S);
1133
1171
}
1172
+ xSemaphoreGiveRecursive (*_buffer_semaphore);
1134
1173
}
1135
1174
1136
1175
float Arduino_Alvik::ArduinoAlvikWheel::get_position (const uint8_t unit){
@@ -1141,19 +1180,23 @@ float Arduino_Alvik::ArduinoAlvikWheel::get_position(const uint8_t unit){
1141
1180
// servo class //
1142
1181
// -----------------------------------------------------------------------------------------------//
1143
1182
1144
- Arduino_Alvik::ArduinoAlvikServo::ArduinoAlvikServo (HardwareSerial * serial, ucPack * packeter, char label,
1183
+ Arduino_Alvik::ArduinoAlvikServo::ArduinoAlvikServo (HardwareSerial * serial, ucPack * packeter, SemaphoreHandle_t * buffer_semaphore,
1184
+ char label,
1145
1185
uint8_t servo_id, uint8_t * positions){
1146
1186
_serial = serial;
1147
1187
_packeter = packeter;
1188
+ _buffer_semaphore = buffer_semaphore;
1148
1189
_label = label;
1149
1190
_servo_id = servo_id;
1150
1191
_positions = positions;
1151
1192
}
1152
1193
1153
1194
void Arduino_Alvik::ArduinoAlvikServo::set_position (const uint8_t position){
1195
+ while (!xSemaphoreTakeRecursive (*_buffer_semaphore, TICK_TIME_SEMAPHORE));
1154
1196
_positions[_servo_id] = position;
1155
1197
_msg_size = _packeter->packetC2B (' S' , _positions[0 ], _positions[1 ]);
1156
1198
_serial->write (_packeter->msg , _msg_size);
1199
+ xSemaphoreGiveRecursive (*_buffer_semaphore);
1157
1200
}
1158
1201
1159
1202
int Arduino_Alvik::ArduinoAlvikServo::get_position (){
0 commit comments