|
|
@ -47,7 +47,7 @@ void motor_init(motor_t *motor) { |
|
|
|
} |
|
|
|
|
|
|
|
static double motor_drive_read_encoder(); |
|
|
|
static uint8_t motor_drive_set_packages_ctr(double position, int direction); |
|
|
|
// static uint8_t motor_drive_set_packages_ctr(double position, int direction); |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 设置电机零点位置,需电机转动前设置。 |
|
|
@ -235,72 +235,72 @@ static double motor_drive_read_encoder() { |
|
|
|
* @param direction 方向 |
|
|
|
* @return uint8_t |
|
|
|
*/ |
|
|
|
static uint8_t motor_drive_set_packages_ctr(double position, int direction) { |
|
|
|
int position_int = 0; |
|
|
|
uint8_t position_remainder = 0; |
|
|
|
uint8_t position_buffer_size = 5; //从第五位开始(低位) |
|
|
|
uint8_t checksum = 0; |
|
|
|
uint8_t buffer[13] = {0x3E, 0XA7, MOTOR_ID, 0X04, 0XEA, 0X00, 0X00, 0X00, 0X00, 0X00}; |
|
|
|
// char *notify_err = "set size error"; |
|
|
|
|
|
|
|
position_int = position * 100; |
|
|
|
if (direction == 2) { |
|
|
|
position_int = 0 - position_int; |
|
|
|
} |
|
|
|
|
|
|
|
if (position_int != 0) { |
|
|
|
if (position_int > 0) { // Positive number |
|
|
|
while ((position_int / 0X100) > 0) { |
|
|
|
position_remainder = position_int & 0XFF; |
|
|
|
buffer[position_buffer_size] = position_remainder; |
|
|
|
position_buffer_size += 1; |
|
|
|
position_int = position_int >> 8; |
|
|
|
checksum += position_remainder; |
|
|
|
} |
|
|
|
buffer[position_buffer_size] = position_int; |
|
|
|
checksum += position_int; |
|
|
|
checksum = checksum & 0XFF; |
|
|
|
buffer[9] = checksum; |
|
|
|
} else { // Negative |
|
|
|
while ((position_int / 0X100) < 0) { |
|
|
|
position_remainder = position_int & 0XFF; |
|
|
|
buffer[position_buffer_size] = position_remainder; |
|
|
|
position_buffer_size += 1; |
|
|
|
position_int = position_int >> 8; |
|
|
|
checksum += position_remainder; |
|
|
|
} |
|
|
|
buffer[position_buffer_size] = position_int; |
|
|
|
position_buffer_size += 1; |
|
|
|
while (position_buffer_size != 9) { |
|
|
|
buffer[position_buffer_size] = 0XFF; |
|
|
|
position_buffer_size += 1; |
|
|
|
checksum += 0XFF; |
|
|
|
} |
|
|
|
checksum += position_int; |
|
|
|
checksum = checksum & 0XFF; |
|
|
|
buffer[9] = checksum; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Send cmd |
|
|
|
uart_flush(uart_num); |
|
|
|
uart_write_bytes(uart_num, buffer, 10); |
|
|
|
|
|
|
|
position_buffer_size = 0; |
|
|
|
memset(buffer, 0, sizeof(buffer)); |
|
|
|
|
|
|
|
// Wait uart receive,if time out return error and output log |
|
|
|
position_buffer_size = uart_read_bytes(uart_num, buffer, 13, uart_read_time_ms / portTICK_RATE_MS); |
|
|
|
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 1; |
|
|
|
} |
|
|
|
|
|
|
|
// Parse receive |
|
|
|
// motor_drive_buffer_cmd_parse(buffer); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
// static uint8_t motor_drive_set_packages_ctr(double position, int direction) { |
|
|
|
// int position_int = 0; |
|
|
|
// uint8_t position_remainder = 0; |
|
|
|
// uint8_t position_buffer_size = 5; //从第五位开始(低位) |
|
|
|
// uint8_t checksum = 0; |
|
|
|
// uint8_t buffer[13] = {0x3E, 0XA7, MOTOR_ID, 0X04, 0XEA, 0X00, 0X00, 0X00, 0X00, 0X00}; |
|
|
|
// // char *notify_err = "set size error"; |
|
|
|
|
|
|
|
// position_int = position * 100; |
|
|
|
// if (direction == 2) { |
|
|
|
// position_int = 0 - position_int; |
|
|
|
// } |
|
|
|
|
|
|
|
// if (position_int != 0) { |
|
|
|
// if (position_int > 0) { // Positive number |
|
|
|
// while ((position_int / 0X100) > 0) { |
|
|
|
// position_remainder = position_int & 0XFF; |
|
|
|
// buffer[position_buffer_size] = position_remainder; |
|
|
|
// position_buffer_size += 1; |
|
|
|
// position_int = position_int >> 8; |
|
|
|
// checksum += position_remainder; |
|
|
|
// } |
|
|
|
// buffer[position_buffer_size] = position_int; |
|
|
|
// checksum += position_int; |
|
|
|
// checksum = checksum & 0XFF; |
|
|
|
// buffer[9] = checksum; |
|
|
|
// } else { // Negative |
|
|
|
// while ((position_int / 0X100) < 0) { |
|
|
|
// position_remainder = position_int & 0XFF; |
|
|
|
// buffer[position_buffer_size] = position_remainder; |
|
|
|
// position_buffer_size += 1; |
|
|
|
// position_int = position_int >> 8; |
|
|
|
// checksum += position_remainder; |
|
|
|
// } |
|
|
|
// buffer[position_buffer_size] = position_int; |
|
|
|
// position_buffer_size += 1; |
|
|
|
// while (position_buffer_size != 9) { |
|
|
|
// buffer[position_buffer_size] = 0XFF; |
|
|
|
// position_buffer_size += 1; |
|
|
|
// checksum += 0XFF; |
|
|
|
// } |
|
|
|
// checksum += position_int; |
|
|
|
// checksum = checksum & 0XFF; |
|
|
|
// buffer[9] = checksum; |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
// // Send cmd |
|
|
|
// uart_flush(uart_num); |
|
|
|
// uart_write_bytes(uart_num, buffer, 10); |
|
|
|
|
|
|
|
// position_buffer_size = 0; |
|
|
|
// memset(buffer, 0, sizeof(buffer)); |
|
|
|
|
|
|
|
// // Wait uart receive,if time out return error and output log |
|
|
|
// position_buffer_size = uart_read_bytes(uart_num, buffer, 13, uart_read_time_ms / portTICK_RATE_MS); |
|
|
|
// 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 1; |
|
|
|
// } |
|
|
|
|
|
|
|
// // Parse receive |
|
|
|
// // motor_drive_buffer_cmd_parse(buffer); |
|
|
|
// return 0; |
|
|
|
// } |
|
|
|
|
|
|
|
bool motor_stop() { |
|
|
|
static uint32_t time; |
|
|
|