33#include " ServoController.h"
44#include " SensorPolling.h"
55
6- TaskHandle_t Protocol::task_handle = NULL ;
7- TimerHandle_t Protocol::inform_timer_handle = NULL ;
6+ Protocol& Protocol::getInstance () {
7+ static Protocol _instance;
8+ return _instance;
9+ }
810
9- void motor_control_request_handle (
10- const uint8_t * request_data, uint32_t request_data_size, uint8_t * response_data,
11- uint32_t & response_data_size) {
12- if (request_data_size != sizeof (protocol_request_motor)) {
13- int32_t status = 0 ;
14- protocol_request_motor request = *(protocol_request_motor*)request_data;
15- // MotorControl& MT = MotorControl::getInstance();
16- // status |= MT.enable(request.is_enable);
17- // status |= MT.set_speed(request.target_speed);
18- protocol_response_motor response = {.status = status};
19- memcpy (response_data, &response, sizeof (protocol_response_motor));
20- response_data_size = sizeof (protocol_response_motor);
21- }
11+ void Protocol::on_new_request () {
12+ uint32_t task_notify = TASK_NOTIFY_RX_DATA_SIGNAL;
13+ BaseType_t task_woken = pdFALSE;
14+ xTaskNotifyFromISR (task_handle, task_notify, eSetBits, &task_woken);
2215}
2316
2417void servo_control_request_handle (
@@ -28,7 +21,6 @@ void servo_control_request_handle(
2821 int32_t status = 0 ;
2922 protocol_request_servo request = *(protocol_request_servo*)request_data;
3023 ServoController& SC = ServoController::getInstance ();
31- status |= SC.enable (request.is_enable );
3224 status |= SC.set_angle (request.left_servo_angle , request.right_servo_angle );
3325 protocol_response_servo response = {.status = status};
3426 memcpy (response_data, &response, sizeof (protocol_response_servo));
@@ -39,14 +31,12 @@ void servo_control_request_handle(
3931void Protocol::init () {
4032 BaseType_t status = xTaskCreate (startTaskImpl, " protocol_task" , 512 , this , 3 , &task_handle);
4133 if (status == pdTRUE) {
42- bind (MOTOR_TAG, motor_control_request_handle);
43- bind (SERVO_TAG, servo_control_request_handle);
44- inform_timer_handle = xTimerCreate (
45- " inform_tim" , pdMS_TO_TICKS (20 ), pdTRUE, NULL , Protocol::send_inform_callback);
34+ bind (SERVO_TARGET_ANGLES_MSG_TAG, servo_control_request_handle);
35+
4636 communication_unit.set_rx_complete_callback (Protocol::on_new_request);
4737 communication_unit.init ();
38+
4839 vTaskResume (task_handle);
49- xTimerStart (inform_timer_handle, 10 );
5040 } else {
5141 printf (" Error сreating protocol task\n " );
5242 }
@@ -68,11 +58,7 @@ void Protocol::task() {
6858 if ((tag >= 0 ) && (tag < CALLBACKS_MAX_NUM) && (request_callback[tag]) != nullptr ) {
6959 request_callback[tag](
7060 request_data, request_data_size, response_data, response_data_size);
71- communication_unit.transmit (
72- tag + RESPONSE_BASE_ID_OFFSET, response_data, response_data_size);
7361 }
74- } else if (task_notify == TASK_NOTIFY_INFORM_SIGNAL) {
75- transmit_inform_messages ();
7662 }
7763 task_notify = 0 ;
7864 }
@@ -85,23 +71,27 @@ void Protocol::bind(uint32_t tag, request_callback_t callback) {
8571 }
8672}
8773
88- void Protocol::transmit_inform_messages () {
89- protocol_inform_imu_data imu_data;
90- protocol_inform_motion_data move_data;
91- // static MotorControl &MT = MotorControl::getInstance();
92- static SensorPolling& SP = SensorPolling::getInstance ();
93-
94- int32_t status = 0 ;
95-
96- status |= SP.get_magn_data (imu_data.magn_x_axis , imu_data.magn_y_axis , imu_data.magn_z_axis );
97- imu_data.data_updated = (status == 0 );
98- communication_unit.transmit (
99- IMU_TAG + INFORM_BASE_ID_OFFSET, (uint8_t *)&imu_data, sizeof (imu_data));
74+ void Protocol::transmit_encoders_speed (float left_fr_speed, float right_fr_speed,
75+ float left_rear_speed, float right_rear_speed) {
76+ uint8_t msg_buff[16 ] = {0 };
77+ memcpy (&(msg_buff[0 ]), &left_fr_speed, sizeof (left_fr_speed));
78+ memcpy (&(msg_buff[4 ]), &right_fr_speed, sizeof (right_fr_speed));
79+ memcpy (&(msg_buff[8 ]), &left_rear_speed, sizeof (left_rear_speed));
80+ memcpy (&(msg_buff[12 ]), &right_rear_speed, sizeof (right_rear_speed));
81+ communication_unit.transmit (ENCODERS_SPEED_MSG_TAG, msg_buff, 16 );
82+ }
10083
101- // move_data.motor_velocity = MT.get_speed();
102- move_data.front_left_wheel_velocity = 0 .0f ;
103- move_data.front_right_wheel_velocity = 0 .0f ;
104- move_data.data_updated = true ;
105- communication_unit.transmit (
106- MOTION_TAG + INFORM_BASE_ID_OFFSET, (uint8_t *)&move_data, sizeof (move_data));
84+ void Protocol::transmit_servo_angles (float left_angle, float right_angle) {
85+ uint8_t msg_buff[8 ] = {0 };
86+ memcpy (&(msg_buff[0 ]), &left_angle, sizeof (left_angle));
87+ memcpy (&(msg_buff[4 ]), &right_angle, sizeof (right_angle));
88+ communication_unit.transmit (SERVO_MEASURED_ANGLES_MSG_TAG, msg_buff, 8 );
10789}
90+
91+ void Protocol::transmit_magn_data (uint16_t x_axis, uint16_t y_axis, uint16_t z_axis) {
92+ uint8_t msg_buff[6 ] = {0 };
93+ memcpy (&(msg_buff[0 ]), &x_axis, sizeof (x_axis));
94+ memcpy (&(msg_buff[2 ]), &y_axis, sizeof (y_axis));
95+ memcpy (&(msg_buff[4 ]), &z_axis, sizeof (z_axis));
96+ communication_unit.transmit (MAGN_Z_MSG_TAG, msg_buff, 6 );
97+ }
0 commit comments