From b7679c3a179857d7718746d40a6afda62e4be5a7 Mon Sep 17 00:00:00 2001 From: zwsd Date: Mon, 8 Aug 2022 11:56:17 +0800 Subject: [PATCH] update --- main/main.c | 113 -------------------------------------------- main/motor_drive.c | 134 ++++++++++++++++++++++++++--------------------------- main/port.c | 68 +++++++++++++++++++++++++++ main/port.h | 23 +++++++++ 4 files changed, 158 insertions(+), 180 deletions(-) diff --git a/main/main.c b/main/main.c index b457f52..ab90e3c 100644 --- a/main/main.c +++ b/main/main.c @@ -17,41 +17,9 @@ #define MAIN_LOG_TAG "MAIN" -/** - * @brief 按键相关宏配置 - */ -#define SUPPORT_KEY_INT 1 //是否支持按键 -#define KEY_INT1 27 -#define KEY_INT2 14 -// #define GPIO_KEY_INPUT_PIN_SEL ((1ULL << KEY_INT1) | (1ULL << KEY_INT2)) - -#define SUPPORT_ELECTRIC_RELAY 1 -#define SUPPORT_DEBUG_LIGHT 1 -#define SUPPORT_TEMPER_LINE 1 -#define SUPPORT_FAN 1 - #define ble_uart_tx_size 128 #define ble_uart_rx_size 128 -#define ELECTRIC_RELAY1 25 // -#define ELECTRIC_RELAY2 26 // -#define GPIO_ELECTRIC_RELAY_OUTPUT_PIN_SEL ((1ULL << ELECTRIC_RELAY1) | (1ULL << ELECTRIC_RELAY2)) - -#define DEBUG_LIGHT 12 -#define GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL ((1ULL << DEBUG_LIGHT)) - -#if SUPPORT_TEMPER_LINE -#define TEMPER_LINE 18 -#define GPIO_TEMPER_LINE_INPUT_PIN_SEL ((1ULL << TEMPER_LINE)) - -#endif - -#if SUPPORT_FAN -#define FAN 19 -#define GPIO_FAN_OUTPUT_PIN_SEL ((1ULL << FAN)) - -#endif - typedef struct { int index; const char *order; @@ -85,18 +53,6 @@ void order_receipt_setPosition(int index, uint8_t setPosition_code) { 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 construct_get_status_packet_and_report(int index, double motor_position) { uint8_t buffer[256] = {0}; @@ -212,71 +168,6 @@ void motor_on_event(motor_event_t event) { } } -#if SUPPORT_KEY_INT -void gpio_input_key_init() { - { // Key1 - gpio_config_t gpio_grb_led_structer; - - gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE; - gpio_grb_led_structer.mode = GPIO_MODE_INPUT; - gpio_grb_led_structer.pin_bit_mask = 1ULL << KEY_INT1; - gpio_grb_led_structer.pull_down_en = 0; - gpio_grb_led_structer.pull_up_en = 0; - - gpio_config(&gpio_grb_led_structer); - } - - { // Key2 - gpio_config_t gpio_grb_led_structer; - gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE; - gpio_grb_led_structer.mode = GPIO_MODE_INPUT; - gpio_grb_led_structer.pin_bit_mask = 1ULL << KEY_INT2; - gpio_grb_led_structer.pull_down_en = 0; - gpio_grb_led_structer.pull_up_en = 0; - gpio_config(&gpio_grb_led_structer); - } -} - -#endif - -#if SUPPORT_ELECTRIC_RELAY -void gpio_electric_relay_init() { - gpio_config_t gpio_grb_led_structer; - - gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE; - gpio_grb_led_structer.mode = GPIO_MODE_OUTPUT; - gpio_grb_led_structer.pin_bit_mask = GPIO_ELECTRIC_RELAY_OUTPUT_PIN_SEL; - gpio_grb_led_structer.pull_down_en = 0; - gpio_grb_led_structer.pull_up_en = 0; - - gpio_config(&gpio_grb_led_structer); -} - -#endif - -#if SUPPORT_DEBUG_LIGHT -void gpio_output_debug_light_init() { - gpio_config_t gpio_grb_led_structer; - - gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE; - gpio_grb_led_structer.mode = GPIO_MODE_INPUT_OUTPUT; - gpio_grb_led_structer.pin_bit_mask = GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL; - gpio_grb_led_structer.pull_down_en = 0; - gpio_grb_led_structer.pull_up_en = 0; - - gpio_config(&gpio_grb_led_structer); -} - -void port_do_debug_light_state() { - static uint32_t debug_light_time; - if (port_haspassedms(debug_light_time) > 500) { - gpio_set_level(DEBUG_LIGHT, !gpio_get_level(DEBUG_LIGHT)); - debug_light_time = port_get_ticket(); - } -} - -#endif - void app_main(void) { bleuart_init(&ble_uart_init_struct); bleuart_reg_cb(blerxcb); @@ -285,14 +176,10 @@ void app_main(void) { motor_init(&ble_uart_motor_structer); motor_reg_event_cb(motor_on_event); -#if SUPPORT_DEBUG_LIGHT gpio_output_debug_light_init(); -#endif while (true) { -#if SUPPORT_DEBUG_LIGHT port_do_debug_light_state(); -#endif bleuart_schedule(); motor_module_schedule(); diff --git a/main/motor_drive.c b/main/motor_drive.c index e18ff6c..cd6ef3c 100644 --- a/main/motor_drive.c +++ b/main/motor_drive.c @@ -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; diff --git a/main/port.c b/main/port.c index 04b3a32..04381b7 100644 --- a/main/port.c +++ b/main/port.c @@ -1,5 +1,6 @@ #include "port.h" +#include "driver/gpio.h" #include "esp_log.h" uint32_t port_get_ticket() { return esp_log_timestamp(); } @@ -11,3 +12,70 @@ uint32_t port_delay_ms(uint32_t ms) { return 0; } + +void gpio_electric_relay_init() { + { // ELECTRIC_RELAY1 + gpio_config_t gpio_grb_led_structer; + gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE; + gpio_grb_led_structer.mode = GPIO_MODE_OUTPUT; + gpio_grb_led_structer.pin_bit_mask = (1ULL << GPIO_ELECTRIC_RELAY1); + gpio_grb_led_structer.pull_down_en = 0; + gpio_grb_led_structer.pull_up_en = 0; + + gpio_config(&gpio_grb_led_structer); + } + { // ELECTRIC_RELAY2 + gpio_config_t gpio_grb_led_structer; + gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE; + gpio_grb_led_structer.mode = GPIO_MODE_OUTPUT; + gpio_grb_led_structer.pin_bit_mask = (1ULL << GPIO_ELECTRIC_RELAY2); + gpio_grb_led_structer.pull_down_en = 0; + gpio_grb_led_structer.pull_up_en = 0; + + gpio_config(&gpio_grb_led_structer); + } +} + +void gpio_output_debug_light_init() { + gpio_config_t gpio_grb_led_structer; + + gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE; + gpio_grb_led_structer.mode = GPIO_MODE_INPUT_OUTPUT; + gpio_grb_led_structer.pin_bit_mask = (1ULL << GPIO_DEBUG_LIGHT); + gpio_grb_led_structer.pull_down_en = 0; + gpio_grb_led_structer.pull_up_en = 0; + + gpio_config(&gpio_grb_led_structer); +} + +void port_do_debug_light_state() { + static uint32_t debug_light_time; + if (port_haspassedms(debug_light_time) > 500) { + gpio_set_level(GPIO_DEBUG_LIGHT, !gpio_get_level(GPIO_DEBUG_LIGHT)); + debug_light_time = port_get_ticket(); + } +} + +void gpio_input_key_init() { + { // Key1 + gpio_config_t gpio_grb_led_structer; + + gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE; + gpio_grb_led_structer.mode = GPIO_MODE_INPUT; + gpio_grb_led_structer.pin_bit_mask = (1ULL << GPIO_KEY_INT1); + gpio_grb_led_structer.pull_down_en = 0; + gpio_grb_led_structer.pull_up_en = 0; + + gpio_config(&gpio_grb_led_structer); + } + + { // Key2 + gpio_config_t gpio_grb_led_structer; + gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE; + gpio_grb_led_structer.mode = GPIO_MODE_INPUT; + gpio_grb_led_structer.pin_bit_mask = (1ULL << GPIO_KEY_INT2); + gpio_grb_led_structer.pull_down_en = 0; + gpio_grb_led_structer.pull_up_en = 0; + gpio_config(&gpio_grb_led_structer); + } +} diff --git a/main/port.h b/main/port.h index edfba9a..bda7a13 100644 --- a/main/port.h +++ b/main/port.h @@ -1,10 +1,33 @@ #pragma once #include +/** + * @brief 按键相关宏配置 + */ +#define GPIO_KEY_INT1 27 +#define GPIO_KEY_INT2 14 +// #define GPIO_KEY_INPUT_PIN_SEL ((1ULL << KEY_INT1) | (1ULL << KEY_INT2)) +/** + * @brief 继电器相关宏配置 + */ +#define GPIO_ELECTRIC_RELAY1 25 // +#define GPIO_ELECTRIC_RELAY2 26 // +// #define GPIO_ELECTRIC_RELAY_OUTPUT_PIN_SEL ((1ULL << ELECTRIC_RELAY1) | (1ULL << ELECTRIC_RELAY2)) + +/** + * @brief 程序是否正常运行检测灯相关宏配置 + */ +#define GPIO_DEBUG_LIGHT 12 +// #define GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL ((1ULL << DEBUG_LIGHT)) uint32_t port_get_ticket(); uint32_t port_haspassedms(uint32_t ticket); uint32_t port_delay_ms(uint32_t ms); + +void gpio_electric_relay_init(); +void gpio_output_debug_light_init(); +void port_do_debug_light_state(); +void gpio_input_key_init(); \ No newline at end of file