diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 22126f9..6f9e306 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -19,7 +19,7 @@ set(SRCS "./stm32/led.c" "./communication_pad/wifi_softap.c" - "./communication_pad/tcp_server.c" + "./communication_pad/udp_server.c" "./communication_pad/ble_gatts_server.c" "./communication_pad/communication_pad.c" diff --git a/main/communication_pad/ble_gatts_server.c b/main/communication_pad/ble_gatts_server.c index 3e753be..e673cae 100644 --- a/main/communication_pad/ble_gatts_server.c +++ b/main/communication_pad/ble_gatts_server.c @@ -441,7 +441,7 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_ ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu); break; case ESP_GATTS_CONF_EVT: - ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_CONF_EVT, status = %d, attr_handle %d", param->conf.status, param->conf.handle); + // ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_CONF_EVT, status = %d, attr_handle %d", param->conf.status, param->conf.handle); break; case ESP_GATTS_START_EVT: ESP_LOGI(GATTS_TABLE_TAG, "SERVICE_START_EVT, status %d, service_handle %d", param->start.status, param->start.service_handle); @@ -589,8 +589,8 @@ void ble_gatts_server_init(void) static uint16_t gatts_mtu_size = 23; void ble_write_data(int id, uint8_t *data, int len) { - ESP_LOGI(GATTS_TABLE_TAG, "ble_write_data %d:", len); - esp_log_buffer_hex(GATTS_TABLE_TAG, data, len); + // ESP_LOGI(GATTS_TABLE_TAG, "ble_write_data %d:", len); + // esp_log_buffer_hex(GATTS_TABLE_TAG, data, len); if (len <= gatts_mtu_size - 3) /* 包数据可以一次完整发送 */ { esp_ble_gatts_send_indicate(ble_gatts_if, ble_gatts_conn_id, ble_gatts_handle_table[id], len, data, false); diff --git a/main/communication_pad/communication_pad.c b/main/communication_pad/communication_pad.c index 432bb89..55871cb 100644 --- a/main/communication_pad/communication_pad.c +++ b/main/communication_pad/communication_pad.c @@ -7,7 +7,7 @@ #include "inc/communication_pad.h" #include "inc/wifi_softap.h" -#include "inc/tcp_server.h" +#include "inc/udp_server.h" #include "inc/ble_gatts_server.h" #include "../stm32/ModbusS.h" #include "../stm32/config.h" @@ -54,7 +54,7 @@ static void measure_data_updata(void) measure_data.current[2] = gWordVar[AC_CURRENT_REG_ADDR + 2]; } - +extern gps_message_t *gps_message; // 上报data_ctrl中的数据 static void pad_notify_data_fun(const padDataCtrl_t *data_ctrl) { @@ -73,8 +73,10 @@ static void pad_notify_data_fun(const padDataCtrl_t *data_ctrl) data_len += data_ctrl->datas[i].len; } + // ESP_LOGI(TAG, "gps message: %f, %f", gps_message->x, gps_message->y); + // 调用蓝牙和wifi的notify函数 - tcp_server_notify(data, data_len); + udp_server_notify(data, data_len); ble_server_notify(data, data_len); } @@ -85,7 +87,7 @@ static void pad_notify_task(void *pvParameters) while (1) { pad_notify_data_fun(&pad_notify_data); - vTaskDelay(pdMS_TO_TICKS(100)); + vTaskDelay(pdMS_TO_TICKS(200)); } vTaskDelete(NULL); @@ -97,7 +99,7 @@ static void pad_notify_task(void *pvParameters) void pad_communication_init(void) { wifi_init_softap(); - tcp_server_init(); + udp_server_init(); ble_gatts_server_init(); diff --git a/main/communication_pad/inc/udp_server.h b/main/communication_pad/inc/udp_server.h new file mode 100644 index 0000000..82db096 --- /dev/null +++ b/main/communication_pad/inc/udp_server.h @@ -0,0 +1,23 @@ +#ifndef __UDP_SERVER_H +#define __UDP_SERVER_H + +#include +#include "lwip/ip_addr.h" + +#define UDP_SERVER_PORT 6000 +#define UDP_NOTIFY_TIMEOUT (60 * 5) // 200ms notify一次,5s超时 + +// 链表,记录连接的客户端 +struct clientNode +{ + struct udp_pcb *upcb; + ip_addr_t addr; + uint16_t port; + int notify_timeout; // 上报数据的超时时间,客户端需定期发送上报请求 + struct clientNode *next; +}; + +void udp_server_init(void); +void udp_server_notify(uint8_t *data, int len); + +#endif \ No newline at end of file diff --git a/main/communication_pad/tcp_server.c b/main/communication_pad/tcp_server.c index 9c9c40c..87ca7f5 100644 --- a/main/communication_pad/tcp_server.c +++ b/main/communication_pad/tcp_server.c @@ -136,6 +136,7 @@ static err_t ModBusSlave_poll(void *arg, struct tcp_pcb *newpcb) { static err_t ModBusSlave_accept(void *arg, struct tcp_pcb *pcb, err_t err) { + ESP_LOGI(LOG_TAG, "TCP accept 0x%x", (unsigned int)pcb); tcp_err(pcb, ModBusSlave_conn_err); tcp_recv(pcb, ModBusSlave_recv); // tcp_poll(pcb, ModBusSlave_poll, 120); // 60 second timeout @@ -159,9 +160,10 @@ void tcp_server_notify(uint8_t *data, int len) { if (p->enable_notify == 1) // 开启上报 { - ESP_LOGI(LOG_TAG, "tcp_server_notify"); - esp_log_buffer_hex(LOG_TAG, data, len); + // ESP_LOGI(LOG_TAG, "tcp_server_notify"); + // esp_log_buffer_hex(LOG_TAG, data, len); tcp_write(p->pcb, data, len, 1); + tcp_output(p->pcb); } p = p->next; } diff --git a/main/communication_pad/udp_server.c b/main/communication_pad/udp_server.c new file mode 100644 index 0000000..15a4075 --- /dev/null +++ b/main/communication_pad/udp_server.c @@ -0,0 +1,169 @@ +#include +#include +#include "lwip/ip_addr.h" +#include "lwip/udp.h" +#include "lwip/err.h" +#include "lwip/opt.h" +#include "lwip/sockets.h" +#include "esp_log.h" + +#include "inc/udp_server.h" +#include "inc/communication_pad.h" + +#define LOG_TAG "[communication_udp_server]: " + + +struct clientNode client_list; // 客户端链表头结点 +struct clientNode *client_r_p = &client_list; // 客户端链表尾指针 + + +struct clientNode *client_list_add(struct udp_pcb * upcb, ip_addr_t *addr, uint16_t port) +{ + struct clientNode *p = client_list.next; + struct clientNode *ret = NULL; + while (p) + { + if (ip_addr_cmp(&p->addr, addr) && p->port == port) + { + ret = p; + break; + } + p = p->next; + } + + if (ret == NULL) + { + struct clientNode *client = (struct clientNode *)malloc(sizeof(struct clientNode)); + client->upcb = upcb; + ip_addr_copy(client->addr, *addr); + client->port = port; + client->notify_timeout = UDP_NOTIFY_TIMEOUT; + client->next = NULL; + client_r_p->next = client; + client_r_p = client; + ret = client; + } + + return ret; +} + +// 返回删除的前一个 +struct clientNode *client_list_delete(ip_addr_t *addr, uint16_t port) +{ + struct clientNode *p = &client_list; + while (p->next) + { + if (ip_addr_cmp(&p->next->addr, addr) && p->next->port == port) + { + struct clientNode *q = p->next; + if (q == client_r_p) + { + client_r_p = p; + } + p->next = q->next; + free(q); + return p; + } + p = p->next; + } + return NULL; +} + +void cilent_list_delete_all(void) +{ + struct clientNode *p = &client_list; + while (p->next) + { + struct clientNode *q = p->next; + p->next = q->next; + free(q); + } + client_r_p = &client_list; +} + + +void udp_server_recived(void *arg, struct udp_pcb *upcb, struct pbuf *pkt_buf, + ip_addr_t *addr, u16_t port) +{ + uint8_t *rxbuf; + uint8_t *txbuf = NULL; + int rxlen, txlen = 0; + rxbuf = (uint8_t *)pkt_buf->payload; + rxlen = pkt_buf->len; + ESP_LOGI(LOG_TAG, "recive data %d:", rxlen); + esp_log_buffer_hex(LOG_TAG, rxbuf, rxlen); + + // 接受到数据,调用pad_deal_recived_data处理,如果需要回复(读操作),则再将数据发送出去 + struct clientNode* client = client_list_add(upcb, addr, port); + int enable_notify = -1; + pad_deal_recived_data(rxbuf, rxlen, &enable_notify, &txbuf, &txlen); + if (enable_notify == 1) + { + client->notify_timeout = UDP_NOTIFY_TIMEOUT; + } + else if (enable_notify == 0) + { + client->notify_timeout = 0; + } + if (txbuf) + { + struct pbuf *pkt_txbuf = pbuf_alloc(PBUF_TRANSPORT, 1400, PBUF_RAM); + char *buf = (char *)pkt_txbuf->payload; + pkt_txbuf->len = txlen; + pkt_txbuf->tot_len = txlen; + memcpy(buf, txbuf, txlen); + err_t err = udp_sendto(upcb, pkt_txbuf, &addr, port); + pbuf_free(pkt_txbuf); + free(txbuf); + } + pbuf_free(pkt_buf); +} + +void udp_server_init(void) { + err_t err; + struct udp_pcb *UDPpcb; + UDPpcb = udp_new(); + if (!UDPpcb) + { + return; + } + err = udp_bind(UDPpcb, IP_ADDR_ANY, UDP_SERVER_PORT); + if (err != ERR_OK) + { + return; + } + udp_recv(UDPpcb, udp_server_recived, NULL); +} + +// 遍历所有客户端,如果允许上报,则将数据上报 +void udp_server_notify(uint8_t *data, int len) +{ + struct clientNode *p = client_list.next; + while (p) + { + if (p->notify_timeout > 0) + { + // ESP_LOGI(LOG_TAG, "tcp_server_notify"); + // esp_log_buffer_hex(LOG_TAG, data, len); + + struct pbuf *pkt_txbuf = pbuf_alloc(PBUF_TRANSPORT, 1400, PBUF_RAM); + char *buf = (char *)pkt_txbuf->payload; + pkt_txbuf->len = len; + pkt_txbuf->tot_len = len; + memcpy(buf, data, len); + udp_sendto(p->upcb, pkt_txbuf, &p->addr, p->port); + pbuf_free(pkt_txbuf); + p->notify_timeout--; + } + else if (p->notify_timeout == 0) // 时间耗尽,删除 + { + p = client_list_delete(&p->addr, p->port); + if (p == NULL) + { + ESP_LOGI(LOG_TAG, "err, confilct"); + return; + } + } + p = p->next; + } +} diff --git a/main/main.c b/main/main.c index abf7e86..3209462 100644 --- a/main/main.c +++ b/main/main.c @@ -119,16 +119,11 @@ void app_main(void) // *cur_pile_id = 0; // save_pile_id(); config_load();//读取保存在FRAM里的数据 - // ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); // rtc_clk_apb_freq = rtc_clk_apb_freq_get(); // ESP_LOGI(TAG, "rtc_clk_apb_freq=%u", rtc_clk_apb_freq); - // wifi_init_softap();//ok - // wifi_init_sta(); // can_init(); // PWR_4G_Init(); - // ModBusTCPSlave_init(); - // communication_tcp_init(); led_init(); ads1220_task_start(); /* 两路电流AD采样获取瞬时流量,计算累计流量,目前配置的是10HZ,取决于SPS */ @@ -138,10 +133,7 @@ void app_main(void) pile_id_init(); uart0_modbus_slave_init(); - // ble_gatts_server_init(); pad_communication_init(); - - // bt_client_init();//ok //ESP32_Uart_Receive_Data(); } diff --git a/main/stm32/ModbusS.c b/main/stm32/ModbusS.c index f790e86..b8a39c6 100644 --- a/main/stm32/ModbusS.c +++ b/main/stm32/ModbusS.c @@ -297,7 +297,7 @@ int ModbusSlaveProcess(uint8_t *txbuf, uint8_t *rxbuf, uint16_t rxLen, int is_cr break; } gWordVar[add] = (rxbuf[4]<<8)+rxbuf[5]; - // ESP_LOGI(TAG, "gWordVar[%d]=0x%04x", add, gWordVar[add]); + ESP_LOGI(TAG, "gWordVar[%d]=0x%04x", add, (rxbuf[4]<<8)+rxbuf[5]); // ESP_LOGI(TAG, "%d %d", depth_config->input_type, depth_config->port); memcpy(txbuf,rxbuf,6); ModBusWordWriteHook(add,1); @@ -328,7 +328,6 @@ int ModbusSlaveProcess(uint8_t *txbuf, uint8_t *rxbuf, uint16_t rxLen, int is_cr out_len = 6; break; case 0x10: //Write Multiple registers - ESP_LOGI(TAG, "write"); if((add+length) > gWORD_SIZE) { txbuf[1] = 0x90; diff --git a/main/stm32/depth.c b/main/stm32/depth.c index 7c64805..0adafb0 100644 --- a/main/stm32/depth.c +++ b/main/stm32/depth.c @@ -452,6 +452,7 @@ void depth_task(void *arg) int speed_calc_count = 0; int enc_value = 0; int64_t work_start_time = 0; + int64_t pause_start_time = 0; uint16_t last_work_state = 0; // depth_data->depth_offset = -depth_config->depth_offset; @@ -508,6 +509,7 @@ void depth_task(void *arg) // ESP_LOGI(TAG, "depth_data->depth:0x%x", (unsigned int)depth_data->depth); uint16_t pile_work_state = (depth_data->pile_work_state_and_direction & 0xff00); + int64_t current_work_time = esp_timer_get_time(); if (pile_work_state == PILE_STATE_WORK) { // ESP_LOGI(TAG, "pile_work_state == PILE_STATE_WORK\n"); @@ -516,7 +518,10 @@ void depth_task(void *arg) work_start_time = esp_timer_get_time(); depth_data->one_pile_work_time = 0; } - int64_t current_work_time = esp_timer_get_time(); + else if (last_work_state == PILE_STATE_PAUSE) // 暂停->开始,减去暂停所占的时间(将工作时间的开始时间后移) + { + work_start_time += current_work_time - pause_start_time; + } if(work_start_time != 0){ depth_data->one_pile_work_time = (uint16_t)((current_work_time - work_start_time)/1000000); // s // ESP_LOGI(TAG, "time : %ud", (int)depth_data->one_pile_work_time); @@ -648,7 +653,10 @@ void depth_task(void *arg) } else if((depth_data->pile_work_state_and_direction & 0xff00) == PILE_STATE_PAUSE) { - + if (last_work_state == PILE_STATE_WORK) + { + pause_start_time = current_work_time; + } } else if((depth_data->pile_work_state_and_direction & 0xff00) == PILE_STATE_STOP) { diff --git a/main/stm32/pile_id.c b/main/stm32/pile_id.c index ab689bc..4c0a85b 100644 --- a/main/stm32/pile_id.c +++ b/main/stm32/pile_id.c @@ -16,7 +16,6 @@ uint16_t *cur_pile_id = &gWordVar[LAST_PILE_ID_ADDR]; gps_message_t *gps_message = (gps_message_t *)&gWordVar[REG_GPS_MESSAGE]; double gps_xy[2]; -double inc_pile_distance = 5; int none_flag = 1; extern move_t *pMoveCtx; @@ -36,8 +35,8 @@ int gps_distance_check(void) return 0; } if (none_flag) return 0; - if (double_abs(gps_message->x, gps_xy[0]) > inc_pile_distance) return 1; - if (double_abs(gps_message->y, gps_xy[1]) > inc_pile_distance) return 1; + if (double_abs(gps_message->x, gps_xy[0]) > GPS_DISTANCE_TH) return 1; + if (double_abs(gps_message->y, gps_xy[1]) > GPS_DISTANCE_TH) return 1; return 0; } diff --git a/main/stm32/pile_id.h b/main/stm32/pile_id.h index 6f42d7e..4a86225 100644 --- a/main/stm32/pile_id.h +++ b/main/stm32/pile_id.h @@ -3,6 +3,7 @@ #define CURRENT0_TH 1000 #define CURRENT2_TIMEOUT_TH 200 // 单位100ms +#define GPS_DISTANCE_TH (0.5) void pile_id_init(void);