Browse Source

设置电机位置以及获取电机当前状态回执

master
zwsd 3 years ago
parent
commit
d70c001a91
  1. 39
      main/main.c
  2. 6
      main/motor_drive.c
  3. 2
      main/motor_drive.h

39
main/main.c

@ -73,12 +73,39 @@ bleuart_t ble_uart_init_struct = {
};
motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1};
void order_receipt_setPosition(int index, uint8_t setPosition_code) {
uint8_t buffer[256] = {0};
ESP_LOGI(MAIN_LOG_TAG, "Command setPosition execution complete!");
if (setPosition_code == 0) {
sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"success\", \"index\": %d }", setPosition_code, index);
bleuart_notify_send(buffer, strlen((char *)buffer));
} else {
sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"error\", \"index\": %d }", setPosition_code, index);
bleuart_notify_send(buffer, strlen((char *)buffer));
}
}
void order_receipt_getStatus(int index) {
ESP_LOGI(MAIN_LOG_TAG, "Command getStatus execution complete!");
uint8_t buffer[256] = {0};
double motor_position = motor_get_position_degree();
sprintf((char *)buffer,
"{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"running\", \"deviceException\": 0, \"deviceExceptionInfo\": "
", \"position\":%lf }",
index, motor_position);
bleuart_notify_send(buffer, strlen((char *)buffer));
}
void process_setPosition(cJSON *rxjson) {
// { "order": "setPosition", "ble_uart_index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 }
int index = 0;
int speedLevel = 0;
float position = 0;
int direction = 0;
uint8_t setPosition_code;
if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) {
position = cJSON_GetObjectItem(rxjson, "position")->valuedouble;
}
@ -92,7 +119,16 @@ void process_setPosition(cJSON *rxjson) {
if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) {
direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction"));
}
motor_run_to_postion(1, position, 0);
setPosition_code = motor_run_to_postion(1, position, 0);
order_receipt_setPosition(index, setPosition_code);
}
void process_getStatus(cJSON *rxjson) {
int index = 0;
if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
}
order_receipt_getStatus(index);
}
void processrxjson(cJSON *rxjson) {
@ -106,6 +142,7 @@ void processrxjson(cJSON *rxjson) {
process_setPosition(rxjson);
} else if (strcmp(order, "getStatus") == 0) {
process_getStatus(rxjson);
}
}

6
main/motor_drive.c

@ -109,7 +109,7 @@ double motor_get_position_degree() {
/**
* @brief
*/
void motor_run_to_postion(int rotation_direction, double position, int speed_level) {
uint8_t motor_run_to_postion(int rotation_direction, double position, int speed_level) {
int position_int = 0;
uint8_t position_remainder = 0;
uint8_t position_buffer_size = 6; //()
@ -177,7 +177,7 @@ void motor_run_to_postion(int rotation_direction, double position, int speed_lev
if (position_buffer_size != 13 || buffer[0] != 0X3E) {
ESP_LOGW(MOTOR_DRIVE, "set motor size error ,buffer_size:%d,buffer[0] = 0X%x", position_buffer_size, buffer[0]);
// bluetooth_active_notify((uint8_t *)notify_err, strlen(notify_err));
return;
return 1;
}
motor_position = position;
@ -185,7 +185,7 @@ void motor_run_to_postion(int rotation_direction, double position, int speed_lev
// Parse receive
// motor_drive_buffer_cmd_parse(buffer);
return;
return 0;
}
/**

2
main/motor_drive.h

@ -14,7 +14,7 @@ typedef void (*motor_on_event_t)(motor_event_t event);
void motor_init(motor_t* motor);
void motor_set_zero_point();
double motor_get_position_degree();
void motor_run_to_postion(int rotation_direction, double position, int speed_level);
uint8_t motor_run_to_postion(int rotation_direction, double position, int speed_level);
void motor_reg_event_cb(motor_on_event_t onevent);
void motor_module_schedule();

Loading…
Cancel
Save