diff --git a/main/ble_parse_data.c b/main/ble_parse_data.c index 4bc2f64..5104a40 100644 --- a/main/ble_parse_data.c +++ b/main/ble_parse_data.c @@ -7,6 +7,7 @@ #define cmd_length_set_position 5 #define cmd_length_get_status 2 #define cmd_length_set_motor_current_size 2 +#define cmd_length_set_motor_to_position 5 static double encoder_befor_num; static bluetooth_processer_t *parse_bluetooth_processer; @@ -37,19 +38,17 @@ void bluetooth_gatts_try_process_data() { parse_bluetooth_processer->auto_report_flag = true; ESP_LOGI(BLE_PARSE_DATA_TAG, set_position); - motor_drive_set_motor_to_angle(1,180,0); - - // encoder_befor_num = motor_drive_read_encoder(); - // if (encoder_befor_num >= 0) { - // if (motor_drive_set_packages_ctr(parse_bluetooth_processer->position, parse_bluetooth_processer->direction) == 0) { - // ets_delay_us(50000); - // if (encoder_befor_num == motor_drive_read_encoder()) { - // ESP_LOGW(BLE_PARSE_DATA_TAG, "motor no turning"); - // } else { - // ESP_LOGI(BLE_PARSE_DATA_TAG, "motor turning"); - // } - // } - // } + encoder_befor_num = motor_drive_read_encoder(); + if (encoder_befor_num >= 0) { + if (motor_drive_set_packages_ctr(parse_bluetooth_processer->position, parse_bluetooth_processer->direction) == 0) { + ets_delay_us(50000); + if (encoder_befor_num == motor_drive_read_encoder()) { + ESP_LOGW(BLE_PARSE_DATA_TAG, "motor no turning"); + } else { + ESP_LOGI(BLE_PARSE_DATA_TAG, "motor turning"); + } + } + } // receipt_json_set_position(); } @@ -62,6 +61,12 @@ void bluetooth_gatts_try_process_data() { motor_drive_set_motor_current_size(); // receipt_json_get_status(); } + if (strcmp(parse_bluetooth_processer->order, set_motor_to_position) == 0) { + ESP_LOGI(BLE_PARSE_DATA_TAG, set_motor_to_position); + motor_drive_set_motor_to_angle(parse_bluetooth_processer->direction, parse_bluetooth_processer->position, // + parse_bluetooth_processer->speedLevel); + // receipt_json_get_status(); + } } } @@ -121,6 +126,9 @@ bool parse_json_to_struct(cJSON *ch) { if (strcmp(ch->valuestring, set_motor_current_size) == 0) { cmd_length = cmd_length_set_motor_current_size; } + if (strcmp(ch->valuestring, set_motor_to_position) == 0) { + cmd_length = cmd_length_set_motor_to_position; + } cmd_length--; } diff --git a/main/ble_parse_data.h b/main/ble_parse_data.h index c702bc6..4db9f8a 100644 --- a/main/ble_parse_data.h +++ b/main/ble_parse_data.h @@ -15,6 +15,7 @@ #define set_position "setPosition" #define get_status "getStatus" #define set_motor_current_size "setMotorCurrentSize" +#define set_motor_to_position "setMotorToPosition" typedef struct bluetooth_processer { char *bluetooth_processer_rx_buf; diff --git a/main/motor_drive.c b/main/motor_drive.c index a3fcb0e..20bf418 100644 --- a/main/motor_drive.c +++ b/main/motor_drive.c @@ -278,9 +278,16 @@ void motor_drive_set_motor_to_angle(int rotation_direction, double position, int uint8_t buffer[14] = {0x3E, 0XA6, MOTOR_ID, 0X08, 0XED, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00}; char *notify_err = "set size error"; - if ((rotation_direction >= 0) && (rotation_direction < 2)) { - buffer[5] = rotation_direction; - checksum += rotation_direction; + // forward + if ((rotation_direction == 1)) { + buffer[5] = 0; + checksum += 0; + } + + // reverse + if ((rotation_direction == 2)) { + buffer[5] = 1; + checksum += 1; } position_int = position * 100; @@ -303,11 +310,6 @@ void motor_drive_set_motor_to_angle(int rotation_direction, double position, int position_buffer_size = 9; } - for (uint8_t i = 0; i < 14; i++) { - /* code */ - ESP_LOGI(MOTOR_DRIVE, "buffer[%d] = %d", i, buffer[i]); - } - if (limit_speed != 0) { while ((limit_speed / 0X100) > 0) { position_remainder = limit_speed & 0XFF;