depth speed逻辑修改
This commit is contained in:
parent
79a0eb73b7
commit
24e496ead0
@ -1,22 +1,3 @@
|
||||
# idf_component_register(SRCS "ble_gatts_server.c" "uart_example.c" "main.c"
|
||||
# "modbus-tcp.c"
|
||||
# "can_network.c"
|
||||
# "./stm32/ads1220.c"
|
||||
# "./stm32/bl0939.c"
|
||||
# "./stm32/capture.c"
|
||||
# "./stm32/comm.c"
|
||||
# "./stm32/depth.c"
|
||||
# "./stm32/fram.c"
|
||||
# "./stm32/flow.c"
|
||||
# "./stm32/utils.c"
|
||||
# "./stm32/config.c"
|
||||
# "./stm32/ModbusS.c"
|
||||
# "./stm32/ModbusM.c"
|
||||
# "./stm32/uart0_modbus_slave.c"
|
||||
# "wifi_softap.c"
|
||||
# "bt_server.c"
|
||||
# INCLUDE_DIRS ".")
|
||||
|
||||
set(SRCS
|
||||
"uart_example.c"
|
||||
"main.c"
|
||||
@ -34,13 +15,16 @@ set(SRCS
|
||||
"./stm32/ModbusS.c"
|
||||
"./stm32/ModbusM.c"
|
||||
"./stm32/uart0_modbus_slave.c"
|
||||
"wifi_softap.c"
|
||||
|
||||
"ble_gatts_server.c"
|
||||
"./communication_pad/wifi_softap.c"
|
||||
"./communication_pad/tcp_server.c"
|
||||
"./communication_pad/ble_gatts_server.c"
|
||||
"./communication_pad/communication_pad.c"
|
||||
|
||||
#"C:\Espressif\frameworks\esp-idf-v4.4.5\components\bt\include"
|
||||
)
|
||||
|
||||
set(INCLUDE_DIRS ".")
|
||||
set(INCLUDE_DIRS "." "./communication_pad/inc")
|
||||
|
||||
idf_component_register(SRCS ${SRCS}
|
||||
INCLUDE_DIRS ${INCLUDE_DIRS})
|
@ -31,6 +31,7 @@
|
||||
|
||||
#include "stm32/config.h"
|
||||
#include "stm32/ModbusS.h"
|
||||
#include "inc/communication_pad.h"
|
||||
|
||||
#define GATTS_TABLE_TAG "BLE_GATTS_SERVER"
|
||||
|
||||
@ -280,57 +281,6 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* [0]: 标识 F0--写多个modbus内存地址
|
||||
* [2:1]: 寄存器起始地址,即寄存器下标
|
||||
* [3]: 写的字节数 0-255
|
||||
* ...: 数据内容
|
||||
*/
|
||||
void gatts_write_data_cb(uint8_t *data, uint32_t len)
|
||||
{
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "gatts_write_data_cb");
|
||||
|
||||
if (len >= 4 && data[0] == 0xF0) /* 写gWordVar寄存器 */
|
||||
{
|
||||
uint16_t addr = data[1] + (data[2] << 8);
|
||||
uint16_t addr_ = addr;
|
||||
uint8_t cnt = data[3];
|
||||
if (cnt > len - 4)
|
||||
{
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "write format is fault, cnt > len - 4");
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "write addr:%d cnt:%d", addr, cnt);
|
||||
uint8_t i;
|
||||
for (i = 0 + 4; i < cnt + 4; i+=2) /* 发送的数据要求两字节对齐 */
|
||||
{
|
||||
gWordVar[addr++] = data[i] + (data[i + 1] << 8);
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "gwordvar[%d]:0x%x", addr-1, gWordVar[addr-1]);
|
||||
}
|
||||
/* 还应该调用 ModBusWordWriteHook 来处理这些写动作带来的影响 */
|
||||
ModBusWordWriteHook(addr_, cnt);
|
||||
}
|
||||
else if (len >= 4 && data[0] == 0xF1) /* 读gWordVar,通过notify上报回去 */
|
||||
{
|
||||
uint16_t addr = data[1] + (data[2] << 8);
|
||||
uint8_t cnt = data[3];
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "read addr:%d cnt:%d", addr, cnt);
|
||||
uint8_t buf[500];
|
||||
buf[0] = data[0];
|
||||
buf[1] = data[1];
|
||||
buf[2] = data[2];
|
||||
buf[3] = data[3];
|
||||
memcpy(buf + 4, (uint8_t *)(gWordVar + addr), cnt);
|
||||
notify_data_fun(IDX_DATA_READ_WRITE_VAL, buf, cnt + 4);
|
||||
}
|
||||
else if (len == 1 && data[0] == 0xF2) /* 开启notify */
|
||||
{
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "enable notify");
|
||||
data_notify_enable = true;
|
||||
}
|
||||
else ESP_LOGI(GATTS_TABLE_TAG, "format err!");
|
||||
}
|
||||
|
||||
|
||||
void example_prepare_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param)
|
||||
{
|
||||
@ -381,7 +331,7 @@ void example_prepare_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t
|
||||
void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param){
|
||||
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC && prepare_write_env->prepare_buf){
|
||||
esp_log_buffer_hex(GATTS_TABLE_TAG, prepare_write_env->prepare_buf, prepare_write_env->prepare_len);
|
||||
gatts_write_data_cb(prepare_write_env->prepare_buf); /* 此处有隐患,有可能不是data_write特征的数据 */
|
||||
ble_read_data(prepare_write_env->prepare_buf, prepare_write_env->prepare_len);
|
||||
}else{
|
||||
ESP_LOGI(GATTS_TABLE_TAG,"ESP_GATT_PREP_WRITE_CANCEL");
|
||||
}
|
||||
@ -453,7 +403,9 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "GATT_WRITE_EVT, handle = %d, value len = %d, value :", param->write.handle, param->write.len);
|
||||
esp_log_buffer_hex(GATTS_TABLE_TAG, param->write.value, param->write.len);
|
||||
if (ble_gatts_handle_table[IDX_DATA_READ_WRITE_VAL] == param->write.handle)
|
||||
gatts_write_data_cb(param->write.value, param->write.len);
|
||||
{
|
||||
ble_read_data(param->write.value, param->write.len);
|
||||
}
|
||||
if (ble_gatts_handle_table[IDX_DATA_NOTIFY_CFG] == param->write.handle && param->write.len == 2){
|
||||
uint16_t descr_value = param->write.value[1]<<8 | param->write.value[0];
|
||||
if (descr_value == 0x0001){
|
||||
@ -571,158 +523,6 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 上报长度过长,则分包,这里限制长度最大20,分包协议:
|
||||
* [1:0] ‘#’, ‘#’,分包标识
|
||||
* [2] 总包个数
|
||||
* [3] 当前包号,从0开始
|
||||
* ... 包数据
|
||||
*/
|
||||
static uint16_t gatts_mtu_size = 23;
|
||||
void notify_data_fun(int id, uint8_t *data, uint32_t len)
|
||||
{
|
||||
// ESP_LOGI(GATTS_TABLE_TAG, "notify_data_fun:");
|
||||
// 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);
|
||||
}
|
||||
else /* 需要分包发送 */
|
||||
{
|
||||
int total_num = 0;
|
||||
uint8_t buf[gatts_mtu_size - 3];
|
||||
if ((len % (gatts_mtu_size - 7)) == 0)
|
||||
total_num = len / (gatts_mtu_size - 7);
|
||||
else
|
||||
total_num = len / (gatts_mtu_size - 7) + 1;
|
||||
// ESP_LOGI(GATTS_TABLE_TAG, "notify packet has %d", total_num);
|
||||
|
||||
int i = 0;
|
||||
buf[0] = '#';
|
||||
buf[1] = '#';
|
||||
buf[2] = total_num;
|
||||
for (i = 0; i < total_num - 1; i++)
|
||||
{
|
||||
buf[3] = i;
|
||||
memcpy(buf + 4, data + i * (gatts_mtu_size - 7), gatts_mtu_size - 7);
|
||||
esp_ble_gatts_send_indicate(ble_gatts_if, ble_gatts_conn_id, ble_gatts_handle_table[IDX_DATA_NOTIFY_VAL],gatts_mtu_size - 3, buf, false);
|
||||
}
|
||||
/* 最后一个包 */
|
||||
buf[3] = i;
|
||||
memcpy(buf + 4, data + i * (gatts_mtu_size - 7), len % (gatts_mtu_size - 7));
|
||||
esp_ble_gatts_send_indicate(ble_gatts_if, ble_gatts_conn_id, ble_gatts_handle_table[IDX_DATA_NOTIFY_VAL], (len % (gatts_mtu_size - 7)) + 4, buf, false);
|
||||
// ESP_LOGI(GATTS_TABLE_TAG, "notify packet send end!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extern flow_t *pflow;
|
||||
extern depth_t *depth_data;
|
||||
extern record_t *record;
|
||||
gps_message_t *gpsMessageP = (gps_message_t *)&gWordVar[REG_GPS_MESSAGE];
|
||||
gps_write_data_t *gpsWriteDataP = (gps_write_data_t *)&gWordVar[REG_GPS_WRITE_DATA];
|
||||
|
||||
void notify_all_data(void)
|
||||
{
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "notify_all_data");
|
||||
uint8_t buf[30];
|
||||
|
||||
buf[0] = 1; /* tag:深度:速度 时间 深度 桩号 */
|
||||
buf[1] = depth_data->speed & 0xff;
|
||||
buf[2] = (depth_data->speed >> 8) & 0xff;
|
||||
buf[3] = depth_data->one_pile_work_time & 0xff;
|
||||
buf[4] = (depth_data->one_pile_work_time >> 8) & 0xff;
|
||||
buf[5] = depth_data->depth & 0xff;
|
||||
buf[6] = (depth_data->depth >> 8) & 0xff;
|
||||
buf[7] = gWordVar[LAST_PILE_ID_ADDR] & 0xff;
|
||||
buf[8] = (gWordVar[LAST_PILE_ID_ADDR] >> 8) & 0xff;
|
||||
notify_data_fun(IDX_DATA_NOTIFY_VAL, buf, 9);
|
||||
|
||||
buf[0] = 2; /* tag:1号通道流量:瞬时,每10cm,总 */
|
||||
buf[1] = pflow[0].flow & 0xff;
|
||||
buf[2] = (pflow[0].flow >> 8) & 0xff;
|
||||
buf[3] = depth_data->depth_flow[0] & 0xff;
|
||||
buf[4] = (depth_data->depth_flow[0] >> 8) & 0xff;
|
||||
buf[5] = pflow[0].total_flow & 0xff;
|
||||
buf[6] = (pflow[0].total_flow >> 8) & 0xff;
|
||||
notify_data_fun(IDX_DATA_NOTIFY_VAL, buf, 7);
|
||||
|
||||
buf[0] = 3; /* tag:2号通道流量:瞬时,每10cm,总 */
|
||||
buf[1] = pflow[1].flow & 0xff;
|
||||
buf[2] = (pflow[1].flow >> 8) & 0xff;
|
||||
buf[3] = depth_data->depth_flow[1] & 0xff;
|
||||
buf[4] = (depth_data->depth_flow[1] >> 8) & 0xff;
|
||||
buf[5] = pflow[1].total_flow & 0xff;
|
||||
buf[6] = (pflow[1].total_flow >> 8) & 0xff;
|
||||
notify_data_fun(IDX_DATA_NOTIFY_VAL, buf, 7);
|
||||
|
||||
buf[0] = 4; /* tag:电流:三个*/
|
||||
buf[1] = gWordVar[AC_CURRENT_REG_ADDR + 0] & 0xff;
|
||||
buf[2] = (gWordVar[AC_CURRENT_REG_ADDR + 0] >> 8) & 0xff;
|
||||
buf[3] = gWordVar[AC_CURRENT_REG_ADDR + 1] & 0xff;
|
||||
buf[4] = (gWordVar[AC_CURRENT_REG_ADDR + 1] >> 8) & 0xff;
|
||||
buf[5] = gWordVar[AC_CURRENT_REG_ADDR + 2] & 0xff;
|
||||
buf[6] = (gWordVar[AC_CURRENT_REG_ADDR + 2] >> 8) & 0xff;
|
||||
notify_data_fun(IDX_DATA_NOTIFY_VAL, buf, 7);
|
||||
|
||||
|
||||
int64_t x = (int64_t)(gpsMessageP->x * 1000);
|
||||
int64_t y = (int64_t)(gpsMessageP->y * 1000);
|
||||
uint16_t dir = (uint16_t)(gpsMessageP->dir * 100);
|
||||
uint16_t pitch = (uint16_t)(gpsMessageP->pitch * 100);
|
||||
uint8_t gps_status = gpsMessageP->gps_status;
|
||||
uint32_t utc_time = gpsMessageP->utc;
|
||||
// ESP_LOGI(GATTS_TABLE_TAG, "x:%f, %f, %lld", gpsMessageP->x, gpsMessageP->x * 1000, (long long int)(gpsMessageP->x * 1000));
|
||||
// ESP_LOGI(GATTS_TABLE_TAG, "gps message x:%lld, y:%lld, dir:%ud, pitch:%ud, gps_status:%d, utc_time:%ud", (long long)x, (long long)y, (unsigned int)dir, (unsigned int)pitch, (int)gps_status, (unsigned int)utc_time);
|
||||
buf[0] = 5; /* tag:CPS信息1:x, y(int64)*/
|
||||
buf[1] = x & 0xff;
|
||||
buf[2] = (x >> 8) & 0xff;
|
||||
buf[3] = (x >> 16) & 0xff;
|
||||
buf[4] = (x >> 24) & 0xff;
|
||||
buf[5] = (x >> 32) & 0xff;
|
||||
buf[6] = (x >> 40) & 0xff;
|
||||
buf[7] = (x >> 48) & 0xff;
|
||||
buf[8] = (x >> 56) & 0xff;
|
||||
buf[9] = y & 0xff;
|
||||
buf[10] = (y >> 8) & 0xff;
|
||||
buf[11] = (y >> 16) & 0xff;
|
||||
buf[12] = (y >> 24) & 0xff;
|
||||
buf[13] = (y >> 32) & 0xff;
|
||||
buf[14] = (y >> 40) & 0xff;
|
||||
buf[15] = (y >> 48) & 0xff;
|
||||
buf[16] = (y >> 56) & 0xff;
|
||||
notify_data_fun(IDX_DATA_NOTIFY_VAL, buf, 17);
|
||||
|
||||
buf[0] = 6; /* tag:CPS信息2:方向,俯仰(uint16), gps定位状态(uint8), UTC时间(uint32)*/
|
||||
buf[1] = dir & 0xff;
|
||||
buf[2] = (dir >> 8) & 0xff;
|
||||
buf[3] = pitch & 0xff;
|
||||
buf[4] = (pitch >> 8) & 0xff;
|
||||
buf[5] = utc_time & 0xff;
|
||||
buf[6] = (utc_time >> 8) & 0xff;
|
||||
buf[7] = (utc_time >> 16) & 0xff;
|
||||
buf[8] = (utc_time >> 24) & 0xff;
|
||||
buf[9] = gps_status & 0xff;
|
||||
notify_data_fun(IDX_DATA_NOTIFY_VAL, buf, 10);
|
||||
/* 需要上报什么数据在此处添加 */
|
||||
}
|
||||
|
||||
|
||||
void notify_task(void *pvParameters)
|
||||
{
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "notify_task");
|
||||
while (1)
|
||||
{
|
||||
if (data_notify_enable && is_connected)
|
||||
{
|
||||
notify_all_data();
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void ble_gatts_server_init(void)
|
||||
{
|
||||
esp_err_t ret;
|
||||
@ -776,7 +576,112 @@ void ble_gatts_server_init(void)
|
||||
if (local_mtu_ret){
|
||||
ESP_LOGE(GATTS_TABLE_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
|
||||
}
|
||||
|
||||
/* 创建任务用于定期的数据上报notify */
|
||||
xTaskCreate(notify_task, "notify_Task", 4096, NULL, 8, NULL);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 上报长度过长,则分包,这里限制长度最大20,分包协议:
|
||||
* [0] ‘#’,分包标识
|
||||
* [1] 总包个数
|
||||
* [2] 当前包号,从0开始
|
||||
* ... 包数据
|
||||
*/
|
||||
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);
|
||||
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);
|
||||
}
|
||||
else /* 需要分包发送 */
|
||||
{
|
||||
int total_num = 0;
|
||||
uint8_t buf[gatts_mtu_size - 3];
|
||||
if ((len % (gatts_mtu_size - 6)) == 0){
|
||||
total_num = len / (gatts_mtu_size - 6);
|
||||
}
|
||||
else{
|
||||
total_num = len / (gatts_mtu_size - 6) + 1;
|
||||
}
|
||||
// ESP_LOGI(GATTS_TABLE_TAG, "notify packet has %d", total_num);
|
||||
|
||||
int i = 0;
|
||||
buf[0] = '#';
|
||||
buf[1] = total_num;
|
||||
for (i = 0; i < total_num - 1; i++)
|
||||
{
|
||||
buf[2] = i;
|
||||
memcpy(buf + 3, data + i * (gatts_mtu_size - 6), gatts_mtu_size - 6);
|
||||
esp_ble_gatts_send_indicate(ble_gatts_if, ble_gatts_conn_id, ble_gatts_handle_table[id],gatts_mtu_size - 3, buf, false);
|
||||
}
|
||||
/* 最后一个包 */
|
||||
buf[2] = i;
|
||||
memcpy(buf + 3, data + i * (gatts_mtu_size - 6), len % (gatts_mtu_size - 6));
|
||||
esp_ble_gatts_send_indicate(ble_gatts_if, ble_gatts_conn_id, ble_gatts_handle_table[id], (len % (gatts_mtu_size - 6)) + 3, buf, false);
|
||||
// ESP_LOGI(GATTS_TABLE_TAG, "notify packet send end!");
|
||||
}
|
||||
}
|
||||
|
||||
// 注意,该函数不是线程安全了,因为假设包是按顺序接受的,无法同时处理多个包的交替发送
|
||||
void ble_read_data(uint8_t *data, int len)
|
||||
{
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "ble_read_data");
|
||||
static uint8_t buf[1024]; // 注意,这里大小没处理
|
||||
static int buflen = 0;
|
||||
static int total_num = 0;
|
||||
static int next_packet_id = 0;
|
||||
uint8_t *txbuf = NULL;
|
||||
int txlen = 0;
|
||||
|
||||
if (len <= 0) return;
|
||||
if (data[0] == '#') // 分包数据
|
||||
{
|
||||
if (len < 3) return;
|
||||
if ((buflen != 0 && data[1] != total_num) || data[2] != next_packet_id) // 包错误,全部丢弃
|
||||
{
|
||||
ESP_LOGE(GATTS_TABLE_TAG, "ble packet err!");
|
||||
buflen = 0;
|
||||
next_packet_id = 0;
|
||||
return;
|
||||
}
|
||||
if (buflen == 0) total_num = data[1];
|
||||
|
||||
memcpy(buf + buflen, data + 3, len - 3);
|
||||
buflen += len - 3;
|
||||
next_packet_id++;
|
||||
if (next_packet_id == data[1]) // 接收完成
|
||||
{
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "ble paclet recoved ok, all %d packets!", next_packet_id);
|
||||
esp_log_buffer_hex(GATTS_TABLE_TAG, buf, buflen);
|
||||
pad_deal_recived_data(buf, buflen, &data_notify_enable, &txbuf, &txlen);
|
||||
if (txbuf != NULL)
|
||||
{
|
||||
ble_write_data(IDX_DATA_READ_WRITE_VAL, txbuf, txlen);
|
||||
free(txbuf);
|
||||
}
|
||||
buflen = 0;
|
||||
next_packet_id = 0;
|
||||
}
|
||||
}
|
||||
else // 不分包的数据
|
||||
{
|
||||
esp_log_buffer_hex(GATTS_TABLE_TAG, data, len);
|
||||
pad_deal_recived_data(data, len, &data_notify_enable, &txbuf, &txlen);
|
||||
if (txbuf != NULL)
|
||||
{
|
||||
ble_write_data(IDX_DATA_READ_WRITE_VAL, txbuf, txlen);
|
||||
free(txbuf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ble_server_notify(uint8_t *data, int len)
|
||||
{
|
||||
if (data_notify_enable && is_connected)
|
||||
{
|
||||
ble_write_data(IDX_DATA_NOTIFY_VAL, data, len);
|
||||
}
|
||||
}
|
168
main/communication_pad/communication_pad.c
Normal file
168
main/communication_pad/communication_pad.c
Normal file
@ -0,0 +1,168 @@
|
||||
// 与pad之间进行数据通信,具有蓝牙、wifi两种方式
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "inc/communication_pad.h"
|
||||
#include "inc/wifi_softap.h"
|
||||
#include "inc/tcp_server.h"
|
||||
#include "inc/ble_gatts_server.h"
|
||||
#include "../stm32/ModbusS.h"
|
||||
#include "../stm32/config.h"
|
||||
|
||||
static const char *TAG = "communication_pad";
|
||||
|
||||
|
||||
padDataCtrl_t pad_notify_data;
|
||||
struct measureData measure_data;
|
||||
|
||||
|
||||
int register_pad_data(padDataCtrl_t *data_ctrl, uint8_t tag, uint8_t *data, int len, padUpdataFun updata_fun)
|
||||
{
|
||||
if (data_ctrl->cnt >= MAX_NOTIFY_DATA)
|
||||
{
|
||||
ESP_LOGI(TAG, "register_pad_data failed, data_ctrl is full");
|
||||
return -1;
|
||||
}
|
||||
|
||||
data_ctrl->datas[data_ctrl->cnt].tag = tag;
|
||||
data_ctrl->datas[data_ctrl->cnt].data = data;
|
||||
data_ctrl->datas[data_ctrl->cnt].len = len;
|
||||
data_ctrl->datas[data_ctrl->cnt].updata_fun = updata_fun;
|
||||
data_ctrl->cnt++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern flow_t *pflow;
|
||||
extern depth_t *depth_data;
|
||||
static void measure_data_updata(void)
|
||||
{
|
||||
measure_data.speed = depth_data->speed;
|
||||
measure_data.time = depth_data->one_pile_work_time;
|
||||
measure_data.depth = depth_data->depth;
|
||||
measure_data.pile_id = gWordVar[LAST_PILE_ID_ADDR];
|
||||
measure_data.flow1 = pflow[0].flow;
|
||||
measure_data.flow_10cm1 = depth_data->depth_flow[0];
|
||||
measure_data.flow_total1 = pflow[0].total_flow;
|
||||
measure_data.flow2 = pflow[1].flow;
|
||||
measure_data.flow_10cm2 = depth_data->depth_flow[1];
|
||||
measure_data.flow_total2 = pflow[1].total_flow;
|
||||
measure_data.current[0] = gWordVar[AC_CURRENT_REG_ADDR + 0];
|
||||
measure_data.current[1] = gWordVar[AC_CURRENT_REG_ADDR + 1];
|
||||
measure_data.current[2] = gWordVar[AC_CURRENT_REG_ADDR + 2];
|
||||
}
|
||||
|
||||
|
||||
// 上报data_ctrl中的数据
|
||||
static void pad_notify_data_fun(const padDataCtrl_t *data_ctrl)
|
||||
{
|
||||
uint8_t data[1024]; // 这里的大小并没有控制
|
||||
int data_len =0;
|
||||
|
||||
// data[0] = NOTIFY_TAG;
|
||||
// data_len = 1;
|
||||
|
||||
for (int i = 0; i < data_ctrl->cnt; i++)
|
||||
{
|
||||
if (data_ctrl->datas[i].updata_fun) data_ctrl->datas[i].updata_fun(); // 更新数据
|
||||
data[data_len++] = data_ctrl->datas[i].tag;
|
||||
data[data_len++] = data_ctrl->datas[i].len;
|
||||
memcpy(data + data_len, data_ctrl->datas[i].data, data_ctrl->datas[i].len);
|
||||
data_len += data_ctrl->datas[i].len;
|
||||
}
|
||||
|
||||
// 调用蓝牙和wifi的notify函数
|
||||
tcp_server_notify(data, data_len);
|
||||
ble_server_notify(data, data_len);
|
||||
}
|
||||
|
||||
|
||||
static void pad_notify_task(void *pvParameters)
|
||||
{
|
||||
ESP_LOGI(TAG, "pad_notify_task");
|
||||
while (1)
|
||||
{
|
||||
pad_notify_data_fun(&pad_notify_data);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 通信初始化
|
||||
void pad_communication_init(void)
|
||||
{
|
||||
wifi_init_softap();
|
||||
tcp_server_init();
|
||||
|
||||
ble_gatts_server_init();
|
||||
|
||||
register_pad_data(&pad_notify_data, 0xA1, &measure_data, sizeof(measure_data), measure_data_updata);
|
||||
register_pad_data(&pad_notify_data, 0xA2, &gWordVar[REG_GPS_MESSAGE], sizeof(gps_message_t), NULL); // 透传不用更新数据
|
||||
xTaskCreate(pad_notify_task, "pad_notify_task", 4096, NULL, 8, NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* 协议说明:
|
||||
* 功能码1byte + ...
|
||||
* 0XF0:写寄存器 2byte寄存器地址 + 1byte数据长度(字节为单位) + 数据 小端
|
||||
* 0XF1:读寄存器 2byte寄存器地址 + 1byte数据长度(字节为单位)
|
||||
* 0XF2:开notify
|
||||
* 0XF3:关notify
|
||||
* 如果是读寄存器功能,则从参数返回读取结果数组,否则返回NULL,注意,返回的指针需要手动free
|
||||
*/
|
||||
void pad_deal_recived_data(uint8_t *data, int len, int *enable_notify, uint8_t **retbuf, int *retlen)
|
||||
{
|
||||
ESP_LOGI(TAG, "pad_deal_recived_data");
|
||||
*retbuf = NULL;
|
||||
*retlen = 0;
|
||||
|
||||
if (len >= 4 && data[0] == 0xF0) /* 写gWordVar寄存器 */
|
||||
{
|
||||
uint16_t addr = data[1] + (data[2] << 8);
|
||||
uint16_t addr_ = addr;
|
||||
uint8_t cnt = data[3];
|
||||
if (cnt > len - 4)
|
||||
{
|
||||
ESP_LOGI(TAG, "write format is fault, cnt > len - 4");
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "write addr:%d cnt:%d", addr, cnt);
|
||||
uint8_t i;
|
||||
for (i = 0 + 4; i < cnt + 4; i+=2) /* 发送的数据要求两字节对齐 */
|
||||
{
|
||||
gWordVar[addr++] = data[i] + (data[i + 1] << 8);
|
||||
ESP_LOGI(TAG, "gwordvar[%d]:0x%x", addr-1, gWordVar[addr-1]);
|
||||
}
|
||||
|
||||
ModBusWordWriteHook(addr_, cnt);
|
||||
}
|
||||
else if (len >= 4 && data[0] == 0xF1) /* 读gWordVar,返回*/
|
||||
{
|
||||
uint16_t addr = data[1] + (data[2] << 8);
|
||||
uint8_t cnt = data[3];
|
||||
ESP_LOGI(TAG, "read addr:%d cnt:%d", addr, cnt);
|
||||
*retbuf = (uint8_t *)malloc(cnt + 4);
|
||||
(*retbuf)[0] = data[0];
|
||||
(*retbuf)[1] = data[1];
|
||||
(*retbuf)[2] = data[2];
|
||||
(*retbuf)[3] = data[3];
|
||||
memcpy((*retbuf) + 4, (uint8_t *)(gWordVar + addr), cnt);
|
||||
*retlen = cnt + 4;
|
||||
}
|
||||
else if (len == 1 && data[0] == 0xF2) /* 开启notify */
|
||||
{
|
||||
ESP_LOGI(TAG, "enable notify");
|
||||
*enable_notify = 1;
|
||||
}
|
||||
else if (len == 1 && data[0] == 0xF2) /* 关闭notify */
|
||||
{
|
||||
ESP_LOGI(TAG, "disable notify");
|
||||
*enable_notify = 0;
|
||||
}
|
||||
else ESP_LOGI(TAG, "format err!");
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
#define __BLE_GATTS_SERVER_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@ -24,6 +25,8 @@ enum
|
||||
};
|
||||
|
||||
void ble_gatts_server_init(void);
|
||||
void notify_data_fun(int id, uint8_t *data, uint32_t len);
|
||||
void ble_write_data(int id, uint8_t *data, int len);
|
||||
void ble_read_data(uint8_t *data, int len);
|
||||
void ble_server_notify(uint8_t *data, int len);
|
||||
|
||||
#endif
|
63
main/communication_pad/inc/communication_pad.h
Normal file
63
main/communication_pad/inc/communication_pad.h
Normal file
@ -0,0 +1,63 @@
|
||||
#ifndef __COMMUNICATION_PAD_H
|
||||
#define __COMMUNICATION_PAD_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define MAX_NOTIFY_DATA 20
|
||||
#define NOTIFY_TAG 0xF2
|
||||
|
||||
struct measureData // 测量数据
|
||||
{
|
||||
int16_t speed;
|
||||
uint16_t time;
|
||||
int16_t depth;
|
||||
uint16_t pile_id;
|
||||
|
||||
int16_t flow1;
|
||||
uint16_t flow_10cm1;
|
||||
int32_t flow_total1;
|
||||
|
||||
int16_t flow2;
|
||||
uint16_t flow_10cm2;
|
||||
int32_t flow_total2;
|
||||
|
||||
uint16_t current[3];
|
||||
};
|
||||
|
||||
typedef struct gps_message
|
||||
{
|
||||
uint8_t gps_status; // GPS定位状态
|
||||
uint8_t gps_view; // GPS可视状态
|
||||
uint8_t gps_use; // GPS使用状态
|
||||
uint8_t reg[1]; // 保留
|
||||
uint32_t utc; // utc时间
|
||||
double x; // 大地平面坐标
|
||||
double y;
|
||||
double dir; // 方向
|
||||
double pitch; // 俯仰角
|
||||
} gps_message_t;
|
||||
|
||||
|
||||
typedef void (*padUpdataFun)(void);
|
||||
|
||||
typedef struct padDataStruct
|
||||
{
|
||||
uint8_t tag;
|
||||
int len;
|
||||
uint8_t *data;
|
||||
padUpdataFun updata_fun;
|
||||
} padData_t;
|
||||
|
||||
|
||||
typedef struct padDataCtrlStruct
|
||||
{
|
||||
int cnt;
|
||||
padData_t datas[MAX_NOTIFY_DATA]; // 输出数据,即上报数据
|
||||
} padDataCtrl_t;
|
||||
|
||||
|
||||
void pad_communication_init(void);
|
||||
int register_pad_data(padDataCtrl_t *data_ctrl, uint8_t tag, uint8_t *data, int len, padUpdataFun updata_fun);
|
||||
void pad_deal_recived_data(uint8_t *data, int len, int *enable_notify, uint8_t **retbuf, int *ret_len);
|
||||
|
||||
#endif
|
23
main/communication_pad/inc/tcp_server.h
Normal file
23
main/communication_pad/inc/tcp_server.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef __TCP_SERVER_H
|
||||
#define __TCP_SERVER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define TCP_SERVER_PORT 6000
|
||||
|
||||
|
||||
|
||||
// 链表,记录连接的客户端
|
||||
struct clientNode
|
||||
{
|
||||
struct tcp_pcb *pcb;
|
||||
int enable_notify; // 是否使能数据上报
|
||||
struct clientNode *next;
|
||||
};
|
||||
|
||||
void tcp_server_init(void);
|
||||
void ble_write_data(int id, uint8_t *data, int len);
|
||||
void ble_read_data(uint8_t *data, int len);
|
||||
void tcp_server_notify(uint8_t *data, int len);
|
||||
|
||||
#endif
|
6
main/communication_pad/inc/wifi_softap.h
Normal file
6
main/communication_pad/inc/wifi_softap.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef __WIFI_SOFTAP_H
|
||||
#define __WIFI_SOFTAP_H
|
||||
|
||||
void wifi_init_softap(void);
|
||||
|
||||
#endif
|
163
main/communication_pad/tcp_server.c
Normal file
163
main/communication_pad/tcp_server.c
Normal file
@ -0,0 +1,163 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "tcp_server.h"
|
||||
#include "inc/communication_pad.h"
|
||||
|
||||
#define LOG_TAG "[communication_tcp_sr]: "
|
||||
|
||||
|
||||
struct clientNode client_list; // 客户端链表头结点
|
||||
struct clientNode *client_r_p = &client_list; // 客户端链表尾指针
|
||||
|
||||
void client_list_add(struct tcp_pcb *pcb)
|
||||
{
|
||||
struct clientNode *client = (struct clientNode *)malloc(sizeof(struct clientNode));
|
||||
client->pcb = pcb;
|
||||
client->enable_notify = 0;
|
||||
client->next = NULL;
|
||||
client_r_p->next = client;
|
||||
client_r_p = client;
|
||||
}
|
||||
|
||||
void client_list_delete(struct tcp_pcb *pcb)
|
||||
{
|
||||
struct clientNode *p = &client_list;
|
||||
while (p->next)
|
||||
{
|
||||
if (p->next->pcb == pcb)
|
||||
{
|
||||
struct clientNode *q = p->next;
|
||||
p->next = q->next;
|
||||
free(q);
|
||||
return;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
struct clientNode* find_client_by_pcb(struct tcp_pcb *pcb)
|
||||
{
|
||||
struct clientNode *p = client_list.next;
|
||||
struct clientNode *ret = NULL;
|
||||
while (p)
|
||||
{
|
||||
if (p->pcb == pcb)
|
||||
{
|
||||
ret = p;
|
||||
break;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static err_t ModBusSlave_recv(void *arg, struct tcp_pcb *newpcb, struct pbuf *p, err_t err) {
|
||||
if (p != NULL) {
|
||||
uint8_t *rxbuf;
|
||||
uint8_t *txbuf = NULL;
|
||||
int rxlen, txlen = 0;
|
||||
tcp_recved(newpcb, p->tot_len);
|
||||
rxbuf = (uint8_t *)p->payload;
|
||||
rxlen = p->len;
|
||||
ESP_LOGI(LOG_TAG, "recive data %d:", rxlen);
|
||||
esp_log_buffer_hex(LOG_TAG, rxbuf, rxlen);
|
||||
|
||||
// 接受到数据,调用pad_deal_recived_data处理,如果需要回复(读操作),则再将数据发送出去
|
||||
struct clientNode* client = find_client_by_pcb(newpcb);
|
||||
if (!client)
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "err:not find this client");
|
||||
return ERR_OK;
|
||||
}
|
||||
pad_deal_recived_data(rxbuf, rxlen, &client->enable_notify, &txbuf, &txlen);
|
||||
if (txbuf)
|
||||
{
|
||||
tcp_write(newpcb, txbuf, txlen, 1);
|
||||
free(txbuf);
|
||||
}
|
||||
pbuf_free(p);
|
||||
}
|
||||
else if (err == ERR_OK)
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "gps1 remote end is closing the connection\n");
|
||||
tcp_err(newpcb, NULL);
|
||||
tcp_recv(newpcb, NULL);
|
||||
// tcp_poll(newpcb, NULL, 120);
|
||||
err_t err_p = tcp_close(newpcb);
|
||||
client_list_delete(newpcb);
|
||||
if (ERR_OK != err_p)
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "gps1 close error. err:%d\n", err_p);
|
||||
}
|
||||
return err_p;
|
||||
}
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
static void ModBusSlave_conn_err(void *arg, err_t err) {
|
||||
ESP_LOGI(LOG_TAG, "connect err!");
|
||||
// log_printf(LERROR, LOG_TAG "connection error, err:%d\n", err);
|
||||
// cilent_list_delete_all(); // 不知道为什么这个回调函数没有提供newpcb
|
||||
}
|
||||
static err_t ModBusSlave_poll(void *arg, struct tcp_pcb *newpcb) {
|
||||
// if (newpcb) {
|
||||
// // log_printf(LERROR, LOG_TAG "close pcb\n");
|
||||
// tcp_close(newpcb);
|
||||
// newpcb = NULL;
|
||||
// }
|
||||
ESP_LOGI(LOG_TAG, "poll!");
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
static err_t ModBusSlave_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
|
||||
tcp_err(pcb, ModBusSlave_conn_err);
|
||||
tcp_recv(pcb, ModBusSlave_recv);
|
||||
// tcp_poll(pcb, ModBusSlave_poll, 120); // 60 second timeout
|
||||
client_list_add(pcb);
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
void tcp_server_init(void) {
|
||||
struct tcp_pcb *pcb;
|
||||
pcb = tcp_new();
|
||||
tcp_bind(pcb, IP_ADDR_ANY, TCP_SERVER_PORT);
|
||||
pcb = tcp_listen(pcb);
|
||||
tcp_accept(pcb, ModBusSlave_accept);
|
||||
}
|
||||
|
||||
// 遍历所有客户端,如果允许上报,则将数据上报
|
||||
void tcp_server_notify(uint8_t *data, int len)
|
||||
{
|
||||
struct clientNode *p = client_list.next;
|
||||
while (p)
|
||||
{
|
||||
if (p->enable_notify == 1) // 开启上报
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "tcp_server_notify");
|
||||
esp_log_buffer_hex(LOG_TAG, data, len);
|
||||
tcp_write(p->pcb, data, len, 1);
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
}
|
99
main/communication_pad/wifi_softap.c
Normal file
99
main/communication_pad/wifi_softap.c
Normal file
@ -0,0 +1,99 @@
|
||||
#include <string.h>
|
||||
#include "wifi_softap.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sys.h"
|
||||
|
||||
// #include "esp_system.h"
|
||||
// #include "../../../components/esp_wifi/include/esp_wifi.h"
|
||||
// #include "../../../components/esp_hw_support/include/esp_mac.h"
|
||||
|
||||
#define ESP_WIFI_SSID "T2N_056455"
|
||||
#define ESP_WIFI_PASS "01010101"
|
||||
#define ESP_WIFI_CHANNEL 1
|
||||
#define MAX_STA_CONN 8
|
||||
|
||||
static const char *TAG = "wifi softAP";
|
||||
|
||||
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
|
||||
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
|
||||
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
|
||||
MAC2STR(event->mac), event->aid);
|
||||
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
|
||||
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
|
||||
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
|
||||
MAC2STR(event->mac), event->aid);
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_init_softap(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_ap();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&wifi_event_handler,
|
||||
NULL,
|
||||
NULL));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.ap = {
|
||||
.ssid = ESP_WIFI_SSID,
|
||||
.ssid_len = strlen(ESP_WIFI_SSID),
|
||||
.channel = ESP_WIFI_CHANNEL,
|
||||
.password = ESP_WIFI_PASS,
|
||||
.max_connection = MAX_STA_CONN,
|
||||
.authmode = WIFI_AUTH_WPA_WPA2_PSK},
|
||||
};
|
||||
|
||||
nvs_handle_t my_handle;
|
||||
esp_err_t err = nvs_open("wifi", NVS_READWRITE, &my_handle);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
size_t len;
|
||||
char temp_str[64];
|
||||
uint8_t channel;
|
||||
if (nvs_get_str(my_handle, "ap_ssid", temp_str, &len) == ESP_OK)
|
||||
{
|
||||
strncpy((char*)wifi_config.ap.ssid, temp_str, len);
|
||||
wifi_config.ap.ssid_len = len;
|
||||
}
|
||||
if (nvs_get_str(my_handle, "ap_psk", temp_str, &len) == ESP_OK)
|
||||
{
|
||||
strncpy((char*)wifi_config.ap.password, temp_str, len);
|
||||
}
|
||||
if (nvs_get_u8(my_handle, "ap_ch", &channel) == ESP_OK)
|
||||
{
|
||||
wifi_config.ap.channel = channel;
|
||||
}
|
||||
|
||||
nvs_close(my_handle);
|
||||
}
|
||||
if (strlen(ESP_WIFI_PASS) == 0)
|
||||
{
|
||||
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
|
||||
ESP_WIFI_SSID, ESP_WIFI_PASS, ESP_WIFI_CHANNEL);
|
||||
}
|
||||
|
@ -1,192 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "stm32/ModbusS.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#define LOG_TAG "[communication_tcp_sr]: "
|
||||
|
||||
bool tcp_is_connected = false;
|
||||
extern bool data_notify_enable; /* 是否允许数据上报 */
|
||||
extern void gatts_write_data_cb(uint8_t *data, uint32_t len);
|
||||
|
||||
static err_t ModBusSlave_recv(void *arg, struct tcp_pcb *newpcb, struct pbuf *p, err_t err) {
|
||||
if (p != NULL) {
|
||||
uint8_t *rxbuf;
|
||||
int rxlen;
|
||||
tcp_recved(newpcb, p->tot_len);
|
||||
rxbuf = (uint8_t *)p->payload;
|
||||
rxlen = p->len;
|
||||
ESP_LOGI(LOG_TAG, "recive data %d:", rxlen);
|
||||
esp_log_buffer_hex(LOG_TAG, rxbuf, rxlen);
|
||||
gatts_write_data_cb(rxbuf, rxlen);
|
||||
pbuf_free(p);
|
||||
}
|
||||
else if (err == ERR_OK)
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "gps1 remote end is closing the connection\n");
|
||||
for (uint8_t i = 0; i < 1; i++)
|
||||
{ // 清空所有客户端
|
||||
gps1_acce_newpacb[i] = 0;
|
||||
}
|
||||
tcp_err(newpcb, NULL);
|
||||
tcp_recv(newpcb, NULL);
|
||||
tcp_poll(newpcb, NULL, 120);
|
||||
err_t err_p = tcp_close(newpcb);
|
||||
tcp_is_connected = false;
|
||||
if (ERR_OK != err_p)
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "gps1 close error. err:%d\n", err_p);
|
||||
}
|
||||
return err_p;
|
||||
}
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
static void ModBusSlave_conn_err(void *arg, err_t err) {
|
||||
// log_printf(LERROR, LOG_TAG "connection error, err:%d\n", err);
|
||||
tcp_is_connected = false;
|
||||
}
|
||||
static err_t ModBusSlave_poll(void *arg, struct tcp_pcb *newpcb) {
|
||||
// if (newpcb) {
|
||||
// // log_printf(LERROR, LOG_TAG "close pcb\n");
|
||||
// tcp_close(newpcb);
|
||||
// newpcb = NULL;
|
||||
// }
|
||||
ESP_LOGI(LOG_TAG, "poll!");
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
static err_t ModBusSlave_accept(void *arg, struct tcp_pcb *pcb, err_t err) {
|
||||
tcp_err(pcb, ModBusSlave_conn_err);
|
||||
tcp_recv(pcb, ModBusSlave_recv);
|
||||
tcp_poll(pcb, ModBusSlave_poll, 120); // 60 second timeout
|
||||
/* Send out the first message */
|
||||
// tcp_write(pcb, GREETING, strlen(GREETING), 1);
|
||||
tcp_is_connected = true;
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
static void notify_all_data(void)
|
||||
{
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "notify_all_data");
|
||||
uint8_t buf[1024];
|
||||
|
||||
buf[0] = 1; /* tag:深度:速度 时间 深度 桩号 */
|
||||
buf[1] = 8;
|
||||
buf[2] = depth_data->speed & 0xff;
|
||||
buf[3] = (depth_data->speed >> 8) & 0xff;
|
||||
buf[4] = depth_data->one_pile_work_time & 0xff;
|
||||
buf[5] = (depth_data->one_pile_work_time >> 8) & 0xff;
|
||||
buf[6] = depth_data->depth & 0xff;
|
||||
buf[7] = (depth_data->depth >> 8) & 0xff;
|
||||
buf[8] = gWordVar[LAST_PILE_ID_ADDR] & 0xff;
|
||||
buf[9] = (gWordVar[LAST_PILE_ID_ADDR] >> 8) & 0xff;
|
||||
|
||||
|
||||
buf[10] = 2; /* tag:1号通道流量:瞬时,每10cm,总 */
|
||||
buf[11] = 6;
|
||||
buf[12] = pflow[0].flow & 0xff;
|
||||
buf[13] = (pflow[0].flow >> 8) & 0xff;
|
||||
buf[14] = depth_data->depth_flow[0] & 0xff;
|
||||
buf[15] = (depth_data->depth_flow[0] >> 8) & 0xff;
|
||||
buf[16] = pflow[0].total_flow & 0xff;
|
||||
buf[17] = (pflow[0].total_flow >> 8) & 0xff;
|
||||
|
||||
buf[18] = 3; /* tag:2号通道流量:瞬时,每10cm,总 */
|
||||
buf[19] = 6;
|
||||
buf[20] = pflow[1].flow & 0xff;
|
||||
buf[21] = (pflow[1].flow >> 8) & 0xff;
|
||||
buf[22] = depth_data->depth_flow[1] & 0xff;
|
||||
buf[23] = (depth_data->depth_flow[1] >> 8) & 0xff;
|
||||
buf[24] = pflow[1].total_flow & 0xff;
|
||||
buf[25] = (pflow[1].total_flow >> 8) & 0xff;
|
||||
|
||||
buf[26] = 4; /* tag:电流:三个*/
|
||||
buf[27] = 6;
|
||||
buf[28] = gWordVar[AC_CURRENT_REG_ADDR + 0] & 0xff;
|
||||
buf[29] = (gWordVar[AC_CURRENT_REG_ADDR + 0] >> 8) & 0xff;
|
||||
buf[30] = gWordVar[AC_CURRENT_REG_ADDR + 1] & 0xff;
|
||||
buf[31] = (gWordVar[AC_CURRENT_REG_ADDR + 1] >> 8) & 0xff;
|
||||
buf[32] = gWordVar[AC_CURRENT_REG_ADDR + 2] & 0xff;
|
||||
buf[33] = (gWordVar[AC_CURRENT_REG_ADDR + 2] >> 8) & 0xff;
|
||||
|
||||
|
||||
int64_t x = (int64_t)(gpsMessageP->x * 1000);
|
||||
int64_t y = (int64_t)(gpsMessageP->y * 1000);
|
||||
uint16_t dir = (uint16_t)(gpsMessageP->dir * 100);
|
||||
uint16_t pitch = (uint16_t)(gpsMessageP->pitch * 100);
|
||||
uint8_t gps_status = gpsMessageP->gps_status;
|
||||
uint32_t utc_time = gpsMessageP->utc;
|
||||
// ESP_LOGI(GATTS_TABLE_TAG, "x:%f, %f, %lld", gpsMessageP->x, gpsMessageP->x * 1000, (long long int)(gpsMessageP->x * 1000));
|
||||
// ESP_LOGI(GATTS_TABLE_TAG, "gps message x:%lld, y:%lld, dir:%ud, pitch:%ud, gps_status:%d, utc_time:%ud", (long long)x, (long long)y, (unsigned int)dir, (unsigned int)pitch, (int)gps_status, (unsigned int)utc_time);
|
||||
buf[34] = 5; /* tag:CPS信息1:x, y(int64)*/
|
||||
buf[35] = 16;
|
||||
buf[36] = x & 0xff;
|
||||
buf[37] = (x >> 8) & 0xff;
|
||||
buf[38] = (x >> 16) & 0xff;
|
||||
buf[39] = (x >> 24) & 0xff;
|
||||
buf[40] = (x >> 32) & 0xff;
|
||||
buf[41] = (x >> 40) & 0xff;
|
||||
buf[42] = (x >> 48) & 0xff;
|
||||
buf[43] = (x >> 56) & 0xff;
|
||||
buf[44] = y & 0xff;
|
||||
buf[45] = (y >> 8) & 0xff;
|
||||
buf[56] = (y >> 16) & 0xff;
|
||||
buf[47] = (y >> 24) & 0xff;
|
||||
buf[48] = (y >> 32) & 0xff;
|
||||
buf[49] = (y >> 40) & 0xff;
|
||||
buf[50] = (y >> 48) & 0xff;
|
||||
buf[51] = (y >> 56) & 0xff;
|
||||
|
||||
buf[52] = 6; /* tag:CPS信息2:方向,俯仰(uint16), gps定位状态(uint8), UTC时间(uint32)*/
|
||||
buf[53] = 9;
|
||||
buf[54] = dir & 0xff;
|
||||
buf[55] = (dir >> 8) & 0xff;
|
||||
buf[56] = pitch & 0xff;
|
||||
buf[57] = (pitch >> 8) & 0xff;
|
||||
buf[58] = utc_time & 0xff;
|
||||
buf[59] = (utc_time >> 8) & 0xff;
|
||||
buf[60] = (utc_time >> 16) & 0xff;
|
||||
buf[61] = (utc_time >> 24) & 0xff;
|
||||
buf[62] = gps_status & 0xff;
|
||||
/* 需要上报什么数据在此处添加 */
|
||||
|
||||
// tcp_write(pcb, GREETING, strlen(GREETING), 1);
|
||||
}
|
||||
|
||||
|
||||
void notify_tcp_task(void *pvParameters)
|
||||
{
|
||||
ESP_LOGI(GATTS_TABLE_TAG, "notify_tcp_task");
|
||||
while (1)
|
||||
{
|
||||
if (data_notify_enable && tcp_is_connected)
|
||||
{
|
||||
notify_all_data();
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
|
||||
void communication_tcp_init(void) {
|
||||
struct tcp_pcb *pcb;
|
||||
pcb = tcp_new();
|
||||
tcp_bind(pcb, IP_ADDR_ANY, 6061);
|
||||
pcb = tcp_listen(pcb);
|
||||
tcp_accept(pcb, ModBusSlave_accept);
|
||||
|
||||
xTaskCreate(notify_tcp_task, "notify_tcp_Task", 4096, NULL, 8, NULL);
|
||||
}
|
||||
|
18
main/main.c
18
main/main.c
@ -34,7 +34,7 @@
|
||||
// #include "esp_bt_main.h"
|
||||
//#include "ble_server.h"
|
||||
#include "stm32/config.h"
|
||||
#include "ble_gatts_server.h"
|
||||
#include "communication_pad.h"
|
||||
|
||||
|
||||
/* The examples use WiFi configuration that you can set via project configuration menu
|
||||
@ -80,7 +80,7 @@ static const char *TAG = "main";
|
||||
|
||||
//static int s_retry_num = 0;
|
||||
|
||||
extern void wifi_init_softap(void);
|
||||
// extern void wifi_init_softap(void);
|
||||
|
||||
void PWR_4G_Init(void);
|
||||
extern void can_init(void);
|
||||
@ -93,6 +93,7 @@ extern esp_err_t i2c_master_init(void);
|
||||
extern void config_load(void);
|
||||
extern void uart0_modbus_slave_init(void);
|
||||
extern void bt_client_init(void);
|
||||
// extern void communication_tcp_init(void);
|
||||
//extern void ESP32_Uart_Receive_Data(void);
|
||||
uint32_t rtc_clk_apb_freq;
|
||||
|
||||
@ -114,24 +115,25 @@ void app_main(void)
|
||||
ESP_ERROR_CHECK(i2c_master_init());
|
||||
//restore_default();
|
||||
config_load();//读取保存在FRAM里的数据
|
||||
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
||||
// 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_softap();//ok
|
||||
// wifi_init_sta();
|
||||
// can_init();
|
||||
// PWR_4G_Init();
|
||||
ModBusTCPSlave_init();
|
||||
// ModBusTCPSlave_init();
|
||||
// communication_tcp_init();
|
||||
|
||||
ads1220_task_start(); /* 两路电流AD采样获取瞬时流量,计算累计流量 */
|
||||
ads1220_task_start(); /* 两路电流AD采样获取瞬时流量,计算累计流量,目前配置的是10HZ,取决于SPS */
|
||||
BL0939_init(); /* 实时采集通道电流,根据depth_config_t中配置的启动与结束电流,以及持续时间进行move_t机器开关状态的判断 */
|
||||
DEPTH_init(); /* 编码器计算深度,同时进行流量的按深度计数,以及record的记录 */
|
||||
FLOW_init(); /* 两一种计算流量的方法,计算累计流量 */
|
||||
|
||||
uart0_modbus_slave_init();
|
||||
// ble_server_init();
|
||||
ble_gatts_server_init();
|
||||
// ble_gatts_server_init();
|
||||
pad_communication_init();
|
||||
|
||||
// bt_client_init();//ok
|
||||
//ESP32_Uart_Receive_Data();
|
||||
|
@ -284,7 +284,7 @@ int ModbusSlaveProcess(uint8_t *txbuf, uint8_t *rxbuf, uint16_t rxLen, int is_cr
|
||||
out_len = 6;
|
||||
break;
|
||||
case 0x06: //Write Single Register
|
||||
ESP_LOGI(TAG, "modbus 0x06");
|
||||
// ESP_LOGI(TAG, "modbus 0x06");
|
||||
if(add >= gWORD_SIZE)
|
||||
{
|
||||
txbuf[1] = 0x86;
|
||||
@ -293,7 +293,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, gWordVar[add]);
|
||||
// ESP_LOGI(TAG, "%d %d", depth_config->input_type, depth_config->port);
|
||||
memcpy(txbuf,rxbuf,6);
|
||||
ModBusWordWriteHook(add,1);
|
||||
|
@ -44,33 +44,3 @@ void ModBusWordWriteHook(uint16_t addr, uint16_t length);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
// #ifndef _MODBUS_H
|
||||
// #define _MODBUS_H
|
||||
// #include <stdint.h>
|
||||
// #define uint8_t unsigned char
|
||||
// #define uint16_t unsigned short
|
||||
// #define uint32 unsigned int
|
||||
|
||||
// #define setBit(Add) gBitVar[(Add) >> 3] |= (1 << ((Add)&0x07))
|
||||
// #define clrBit(Add) gBitVar[(Add) >> 3] &= ~(1 << ((Add)&0x07))
|
||||
// //#define ModBusTxData uart1_tx
|
||||
|
||||
// extern uint16_t crc16(uint8_t *puchMsg, uint16_t usDataLen);
|
||||
// extern int ModbusSlaveProcess(uint8_t *txbuf, uint8_t *rxbuf, uint16_t rxLen, int is_crc);
|
||||
|
||||
|
||||
|
||||
// int isBitHI(uint16_t Add);
|
||||
// void xorBit(uint16_t Add);
|
||||
|
||||
// void WriteBit(uint16_t Add, uint8_t bit_value);
|
||||
// #define gBIT_SIZE 128
|
||||
// #define gWORD_SIZE 12288
|
||||
// extern uint8_t gBitVar[(gBIT_SIZE + 7) / 8];
|
||||
// extern uint16_t gWordVar[gWORD_SIZE];
|
||||
|
||||
// void modbus(void);
|
||||
// void rs485_send(void *buf, uint16_t len);
|
||||
|
||||
// #endif
|
||||
|
@ -127,6 +127,7 @@ static void IRAM_ATTR ads1220_done_handler(mcpwm_unit_t mcpwm, mcpwm_capture_cha
|
||||
// {
|
||||
// pflow[1].update_time = xTaskGetTickCount();
|
||||
// }
|
||||
// ESP_LOGI(TAG, "ads done");
|
||||
ad_update_time[ch] = edata->cap_value;
|
||||
xQueueSendFromISR(gpio_evt_queue, &ad_update_time[ch], NULL);
|
||||
}
|
||||
@ -322,7 +323,7 @@ void ADS1220_Config(void)
|
||||
// ESP_LOGI(TAG, "ADS1220_WriteCommand.\n");
|
||||
ADS1220_WriteReg(ADS1220_REG0, 0xa0); // <20>Ĵ<EFBFBD><C4B4><EFBFBD>0 <20><><EFBFBD><EFBFBD>: <20><><EFBFBD><EFBFBD>: AIN P = AIN0, AIN N = AVSS <20><><EFBFBD><EFBFBD>ģʽ ʹ<><CAB9>PGA1 (100) <20><><EFBFBD><EFBFBD>PGA(0)
|
||||
|
||||
ADS1220_WriteReg(ADS1220_REG1, 0x00); // <20>Ĵ<EFBFBD><C4B4><EFBFBD>1 <20><><EFBFBD><EFBFBD>: Turboģʽ 40.SPS(00000)<29><>ʹ<EFBFBD><CAB9><EFBFBD>¶ȴ<C2B6><C8B4><EFBFBD><EFBFBD><EFBFBD>(0) <20>رյ<D8B1><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(0)
|
||||
ADS1220_WriteReg(ADS1220_REG1, 0x20); // <20>Ĵ<EFBFBD><C4B4><EFBFBD>1 <20><><EFBFBD><EFBFBD>: Turboģʽ 40.SPS(00000)<29><>ʹ<EFBFBD><CAB9><EFBFBD>¶ȴ<C2B6><C8B4><EFBFBD><EFBFBD><EFBFBD>(0) <20>رյ<D8B1><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(0)
|
||||
|
||||
ADS1220_WriteReg(ADS1220_REG2, 0x20); // <20>Ĵ<EFBFBD><C4B4><EFBFBD>2 <20><><EFBFBD><EFBFBD>: <20><>ѹ<EFBFBD><D1B9> <20>ڲ<EFBFBD>2.048-v<>ο<EFBFBD>(00) ʹ<><CAB9>оƬFIR<49>˲<EFBFBD>50Hz(10) <20><><EFBFBD><EFBFBD>Դ0ma // Low-side<64><65>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD> Ĭ<><C4AC>(0) <20><><EFBFBD><EFBFBD>Դ <20>ر<EFBFBD>
|
||||
ADS1220_WriteReg(ADS1220_REG3, 0x00); // <20>Ĵ<EFBFBD><C4B4><EFBFBD>3 <20><><EFBFBD><EFBFBD>: : IDAC1 connect to AIN2 : IDAC2 disabled
|
||||
@ -414,6 +415,7 @@ void ads1220_task(void)
|
||||
ADS1220_WriteReg(ADS1220_REG0, ch_cmd[ch]);
|
||||
ADS1220_WriteCommand(ADS1220_CMD_START);
|
||||
ad_watchdog_cnt = 0;
|
||||
if (ad_ch == 1)
|
||||
LED2_Toggle();
|
||||
if ((ad_ch == 1 || ad_ch == 3) && flow_config->input_type == 1)
|
||||
{
|
||||
@ -452,6 +454,7 @@ void ads1220_task(void)
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "wait queue failed");
|
||||
ADS1220_Init();
|
||||
}
|
||||
if (zero_totalflow_req)
|
||||
|
@ -114,7 +114,7 @@ static int bl0939_write_reg(uint8_t reg, uint32_t val, int check)
|
||||
|
||||
void bl0939_spi_reset(void)
|
||||
{
|
||||
uint8_t tx_buf[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
||||
uint8_t tx_buf[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; // 软复位SPI接口
|
||||
uint8_t rx_buf[6] = {0};
|
||||
BL0939_SPI_TransmitReceive(tx_buf, rx_buf, 6);
|
||||
}
|
||||
@ -127,13 +127,13 @@ uint32_t comp_threshold(float mA)
|
||||
void bl0939_reset(void)
|
||||
{
|
||||
bl0939_spi_reset();
|
||||
bl0939_write_reg(0x19, 0x005a5a5a, 0); // <EFBFBD><EFBFBD>λ<EFBFBD>û<EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
|
||||
bl0939_write_reg(0x1a, 0x00000055, 1); // <EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
bl0939_write_reg(0x10, 0xffff, 0); // Threshold A
|
||||
bl0939_write_reg(0x19, 0x005a5a5a, 0); // 0x5a5a5a软件复位用户寄存器区
|
||||
bl0939_write_reg(0x1a, 0x00000055, 1); // 寄存器写保护,0x55可以写,其他值不能写
|
||||
bl0939_write_reg(0x10, 0xffff, 0); // Threshold A 电流通道快速有效值控制寄存器
|
||||
bl0939_write_reg(0x1E, 0xffff, 1); // Threshold B
|
||||
bl0939_write_reg(0x18, 0x00002000, 1); // cf
|
||||
bl0939_write_reg(0x1B, 0x000047ff, 0); // cf
|
||||
bl0939_write_reg(0x1a, 0x00000000, 1); // д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
bl0939_write_reg(0x18, 0x00002000, 1); // 用户模式选择寄存器
|
||||
bl0939_write_reg(0x1B, 0x000047ff, 0); // 温度模式控制寄存器
|
||||
bl0939_write_reg(0x1a, 0x00000000, 1); // 开启寄存器写保护
|
||||
}
|
||||
|
||||
// T = 40ms
|
||||
@ -165,11 +165,11 @@ int bl0939_get_voltage()
|
||||
uint8_t bl0939_cmd[36] =
|
||||
{
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0x55, 0x00, 0, 0, 0, 0,
|
||||
0x55, 0x00, 0, 0, 0, 0, // A通道快速有效值,无符号 24bit
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0x55, 0x07, 0, 0, 0, 0,
|
||||
0x55, 0x07, 0, 0, 0, 0, // B通道快速有效值,无符号 24bit
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0x55, 0x06, 0, 0, 0, 0};
|
||||
0x55, 0x06, 0, 0, 0, 0}; // 电压有效值寄存器,无符号 24bit
|
||||
|
||||
int check_sum_ch(uint8_t *buf, int offset)
|
||||
{
|
||||
@ -213,7 +213,7 @@ void BL0939_task()
|
||||
int index = 0;
|
||||
while (1)
|
||||
{
|
||||
vTaskDelay(10);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
// ESP_LOGI(TAG, "BL0939_SPI_TransmitReceive before\n ");
|
||||
bl0939_Transfer_done = 1;
|
||||
|
||||
|
@ -33,19 +33,19 @@
|
||||
#define REG_GPS_WRITE_DATA (REG_GPS_MESSAGE + sizeof(gps_message_t))
|
||||
|
||||
// GPS数据
|
||||
typedef struct gps_message
|
||||
{
|
||||
uint8_t gps_status; // GPS定位状态
|
||||
uint8_t gps_view; // GPS可视状态
|
||||
uint8_t gps_use; // GPS使用状态
|
||||
uint8_t reg[1]; // 保留
|
||||
uint32_t utc; // utc时间
|
||||
double x; // 大地平面坐标
|
||||
double y;
|
||||
double dir; // 方向
|
||||
double pitch; // 俯仰角
|
||||
} gps_message_t;
|
||||
extern gps_message_t *gpsMessageP;
|
||||
// typedef struct gps_message
|
||||
// {
|
||||
// uint8_t gps_status; // GPS定位状态
|
||||
// uint8_t gps_view; // GPS可视状态
|
||||
// uint8_t gps_use; // GPS使用状态
|
||||
// uint8_t reg[1]; // 保留
|
||||
// uint32_t utc; // utc时间
|
||||
// double x; // 大地平面坐标
|
||||
// double y;
|
||||
// double dir; // 方向
|
||||
// double pitch; // 俯仰角
|
||||
// } gps_message_t;
|
||||
// extern gps_message_t *gpsMessageP;
|
||||
|
||||
|
||||
#define WRITE_DOWN_MAGIC 0x55BB // 控制gps采用参数进行运算
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_timer.h"
|
||||
#include "stdlib.h"
|
||||
#include "config.h"
|
||||
#include "utils.h"
|
||||
@ -20,7 +21,7 @@
|
||||
#include "config.h"
|
||||
#include "../../../../components/bt/host/bluedroid/api/include/api/esp_gatts_api.h"
|
||||
// #include "bt_server.h"
|
||||
#include "../ble_gatts_server.h"
|
||||
// #include "../ble_gatts_server.h"
|
||||
|
||||
#include "esp_system.h"
|
||||
#include "driver/uart.h"
|
||||
@ -200,7 +201,7 @@ static bool depth_isr_handler(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id_t cap
|
||||
void *arg)
|
||||
{
|
||||
|
||||
// 双边沿触发中断,只有一个边缘会更新值,所以只有在值发生变化是才更新时间
|
||||
// 双边沿触发中断,只有一个边缘会更新值,所以只有在值发生变化时才更新时间
|
||||
int value = encoder->get_counter_value(encoder);
|
||||
if (value != enc1_value)
|
||||
{
|
||||
@ -333,7 +334,7 @@ int abs_sub(uint32_t enc_update_time, uint32_t _enc_update_time)
|
||||
send_to_bt_t1 send_to_bt1;
|
||||
send_to_bt_t2 send_to_bt2;
|
||||
extern bool is_connected;
|
||||
extern uint16_t spp_handle_table[IDX_NB];
|
||||
// extern uint16_t spp_handle_table[IDX_NB];
|
||||
extern uint16_t spp_conn_id;
|
||||
extern esp_gatt_if_t spp_gatts_if;
|
||||
extern flow_t *pflow;
|
||||
@ -415,6 +416,28 @@ void send_data_to_bt(void)
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
void calculate_depth_speed(int speed_enc_value)
|
||||
{
|
||||
static int speed_last_enc_value = -1;
|
||||
static int64_t speed_last_time = -1;
|
||||
int64_t speed_time = esp_timer_get_time();
|
||||
|
||||
if (speed_last_enc_value == -1) // 第一次,记录值,并不计算
|
||||
{
|
||||
goto exit;
|
||||
}
|
||||
|
||||
depth_data->speed = (speed_enc_value - speed_last_enc_value) * depth_config->N / depth_config->M * 600000ll / (speed_time - speed_last_time);
|
||||
// ESP_LOGI(TAG, "speed:%d", depth_data->speed);
|
||||
exit:
|
||||
speed_last_enc_value = speed_enc_value;
|
||||
speed_last_time = speed_time;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* _enc1_update_time 编码器1上一次更新时间
|
||||
* enc1_update_time 编码器1当前更新时间
|
||||
@ -428,13 +451,8 @@ void depth_task(void *arg)
|
||||
ESP_LOGI(TAG, "depth_task start\n");
|
||||
|
||||
int time_diff = 0;
|
||||
int speed_timeout = 0;
|
||||
int last_speed_enc_value = 0; // 上次速度计算的编码器值
|
||||
int speed_enc_update_time = 0;
|
||||
int speed_calc_count = 0;
|
||||
int enc_value = 0;
|
||||
// int count = 0;
|
||||
int bt_time_count = 0;
|
||||
int64_t work_start_time = 0;
|
||||
uint16_t last_work_state = 0;
|
||||
|
||||
@ -446,22 +464,8 @@ void depth_task(void *arg)
|
||||
|
||||
gWordVar[LAST_PILE_ID_ADDR] = last_pile_id;
|
||||
|
||||
int add_record_time = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
// if (add_record_time++ > 10)
|
||||
// {
|
||||
// add_record_time = 0;
|
||||
// depth_data->sample_count++;
|
||||
// if (depth_data->sample_count > 20)
|
||||
// {
|
||||
// record->pile_id++;
|
||||
// depth_data->sample_count = 0;
|
||||
// }
|
||||
// add_recod_item();
|
||||
// }
|
||||
|
||||
if (_enc1_update_time != enc1_update_time) /* 深度数据更新 */
|
||||
{
|
||||
// ESP_LOGI(TAG, "_enc1_update_time != enc1_update_time\n");
|
||||
@ -502,47 +506,20 @@ void depth_task(void *arg)
|
||||
|
||||
// ESP_LOGI(TAG, "depth_data->depth:0x%x", (unsigned int)depth_data->depth);
|
||||
|
||||
// 500ms计算一次速度
|
||||
if (speed_calc_count++ > 50)
|
||||
{
|
||||
// ESP_LOGI(TAG, "speed_calc_count++ > 50\n");
|
||||
speed_calc_count = 0;
|
||||
int speed_time_diff = abs_sub(enc_update_time, speed_enc_update_time);
|
||||
int time_diff_us = speed_time_diff / (APB_CLK_FREQ / 1000000);
|
||||
if (time_diff_us > 0 && time_diff_us < 5000000)
|
||||
{
|
||||
// ESP_LOGI(TAG, "time_diff_us:%d,dist_diff=%d", time_diff_us, enc_value - last_speed_enc_value);
|
||||
// speed = dist_diff / speed_time_diff speed 单位mm/min dist_diff 单位mm speed_time_diff 单位us
|
||||
depth_data->speed = (enc_value - last_speed_enc_value) * depth_config->N / depth_config->M * 600000ll / time_diff_us;
|
||||
speed_timeout = 0;
|
||||
speed_enc_update_time = enc_update_time;
|
||||
last_speed_enc_value = enc_value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (++speed_timeout > 10)
|
||||
{
|
||||
depth_data->speed = 0;
|
||||
speed_timeout = 0;
|
||||
speed_enc_update_time = enc_update_time;
|
||||
last_speed_enc_value = enc_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t pile_work_state = (depth_data->pile_work_state_and_direction & 0xff00);
|
||||
if (pile_work_state == PILE_STATE_WORK)
|
||||
{
|
||||
// ESP_LOGI(TAG, "pile_work_state == PILE_STATE_WORK\n");
|
||||
/*如果机器从停止状态->工作状态,则重新记录工作开始时间*/
|
||||
if(last_work_state == PILE_STATE_STOP){
|
||||
// work_start_time = esp_timer_get_time();
|
||||
work_start_time = esp_timer_get_time();
|
||||
depth_data->one_pile_work_time = 0;
|
||||
}
|
||||
// int64_t current_work_time = esp_timer_get_time();
|
||||
// if(work_start_time != 0){
|
||||
// depth_data->one_pile_work_time = (uint16_t)((current_work_time - work_start_time)/1000000);
|
||||
// }
|
||||
int64_t current_work_time = esp_timer_get_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);
|
||||
}
|
||||
|
||||
/*下钻,深度增加 计算采样深度流量*/
|
||||
if (enc_value > last_enc_value)
|
||||
@ -629,7 +606,6 @@ void depth_task(void *arg)
|
||||
depth_data->sample_count++;
|
||||
add_recod_item();
|
||||
target_sample_depth -= depth_config->sample_depth;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -667,18 +643,15 @@ void depth_task(void *arg)
|
||||
prev_update_time = enc_update_time;
|
||||
last_enc_value = enc_value;
|
||||
}
|
||||
else{
|
||||
// ESP_LOGI(TAG, "time diff = 0\n");
|
||||
vTaskDelay(100);
|
||||
}
|
||||
|
||||
//每隔500ms向蓝牙发送一次数据
|
||||
// if(bt_time_count++ > 50){
|
||||
// ESP_LOGI(TAG, "bt_time_count++ > 50\n");
|
||||
// bt_time_count = 0;
|
||||
// send_data_to_bt();
|
||||
// }
|
||||
vTaskDelay(10);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
// 500ms计算一次速度
|
||||
if (++speed_calc_count >= 5)
|
||||
{
|
||||
calculate_depth_speed(enc1_value);
|
||||
speed_calc_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
static const char *TAG = "UART0";
|
||||
|
||||
#define BAUD_RATE (115200)
|
||||
#define BAUD_RATE (460800)
|
||||
#define UART_PORT_NUM (0)
|
||||
#define BUF_SIZE (256)
|
||||
#define UART_READ_TOUT (50 / portTICK_PERIOD_MS)
|
||||
|
@ -1,150 +0,0 @@
|
||||
/* WiFi softAP Example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "../../../components/esp_wifi/include/esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "driver/ledc.h"
|
||||
#include "esp_err.h"
|
||||
#include "../../../components/esp_hw_support/include/esp_mac.h"
|
||||
/* The examples use WiFi configuration that you can set via project configuration menu.
|
||||
|
||||
If you'd rather not, just change the below entries to strings with
|
||||
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
||||
*/
|
||||
#define ESP_WIFI_SSID "T2N_056455"
|
||||
#define ESP_WIFI_PASS "01010101"
|
||||
#define ESP_WIFI_CHANNEL 1
|
||||
#define MAX_STA_CONN 8
|
||||
|
||||
static const char *TAG = "wifi softAP";
|
||||
|
||||
#define LEDC_TIMER LEDC_TIMER_0
|
||||
#define LEDC_MODE LEDC_LOW_SPEED_MODE
|
||||
#define LEDC_OUTPUT_IO (21) // Define the output GPIO
|
||||
#define LEDC_CHANNEL LEDC_CHANNEL_0
|
||||
#define LEDC_DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits
|
||||
#define LEDC_DUTY (4095) // Set duty to 50%. ((2 ** 13) - 1) * 50% = 4095
|
||||
#define LEDC_FREQUENCY (3840) // Frequency in Hertz. Set frequency at 5 kHz
|
||||
|
||||
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
// if (event_id == WIFI_EVENT_AP_STACONNECTED) {
|
||||
// wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
|
||||
// ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
|
||||
// MAC2STR(event->mac), event->aid);
|
||||
// } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
|
||||
// wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
|
||||
// ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
|
||||
// MAC2STR(event->mac), event->aid);
|
||||
// }
|
||||
}
|
||||
|
||||
void wifi_init_softap(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_ap();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&wifi_event_handler,
|
||||
NULL,
|
||||
NULL));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.ap = {
|
||||
.ssid = ESP_WIFI_SSID,
|
||||
.ssid_len = strlen(ESP_WIFI_SSID),
|
||||
.channel = ESP_WIFI_CHANNEL,
|
||||
.password = ESP_WIFI_PASS,
|
||||
.max_connection = MAX_STA_CONN,
|
||||
.authmode = WIFI_AUTH_WPA_WPA2_PSK},
|
||||
};
|
||||
|
||||
nvs_handle_t my_handle;
|
||||
esp_err_t err = nvs_open("wifi", NVS_READWRITE, &my_handle);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
size_t len;
|
||||
char temp_str[64];
|
||||
uint8_t channel;
|
||||
if (nvs_get_str(my_handle, "ap_ssid", temp_str, &len) == ESP_OK)
|
||||
{
|
||||
strncpy((char*)wifi_config.ap.ssid, temp_str, len);
|
||||
wifi_config.ap.ssid_len = len;
|
||||
}
|
||||
if (nvs_get_str(my_handle, "ap_psk", temp_str, &len) == ESP_OK)
|
||||
{
|
||||
strncpy((char*)wifi_config.ap.password, temp_str, len);
|
||||
}
|
||||
if (nvs_get_u8(my_handle, "ap_ch", &channel) == ESP_OK)
|
||||
{
|
||||
wifi_config.ap.channel = channel;
|
||||
}
|
||||
|
||||
nvs_close(my_handle);
|
||||
}
|
||||
if (strlen(ESP_WIFI_PASS) == 0)
|
||||
{
|
||||
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
|
||||
ESP_WIFI_SSID, ESP_WIFI_PASS, ESP_WIFI_CHANNEL);
|
||||
}
|
||||
|
||||
// void app_main2(void)
|
||||
// {
|
||||
// // Initialize NVS
|
||||
// esp_err_t ret = nvs_flash_init();
|
||||
// if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
// {
|
||||
// ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
// ret = nvs_flash_init();
|
||||
// }
|
||||
// ESP_ERROR_CHECK(ret);
|
||||
|
||||
// ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
|
||||
// wifi_init_softap();
|
||||
// // ledc_init();
|
||||
// // ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY));
|
||||
// // // Update duty to apply the new value
|
||||
// // ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
|
||||
// // gpio_config_t io_conf = {};
|
||||
// // // disable interrupt
|
||||
// // io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
// // // set as output mode
|
||||
// // io_conf.mode = GPIO_MODE_OUTPUT;
|
||||
// // // bit mask of the pins that you want to set,e.g.GPIO18/19
|
||||
// // io_conf.pin_bit_mask = 1 << 9;
|
||||
// // // disable pull-down mode
|
||||
// // io_conf.pull_down_en = 0;
|
||||
// // // disable pull-up mode
|
||||
// // io_conf.pull_up_en = 0;
|
||||
// // // configure GPIO with the given settings
|
||||
// // gpio_config(&io_conf);
|
||||
// // gpio_set_level(9, 1);
|
||||
// }
|
18
sdkconfig
18
sdkconfig
@ -469,7 +469,6 @@ CONFIG_COMPILER_STACK_CHECK_MODE_NONE=y
|
||||
#
|
||||
# CONFIG_APPTRACE_DEST_JTAG is not set
|
||||
CONFIG_APPTRACE_DEST_NONE=y
|
||||
# CONFIG_APPTRACE_DEST_UART0 is not set
|
||||
# CONFIG_APPTRACE_DEST_UART1 is not set
|
||||
# CONFIG_APPTRACE_DEST_UART2 is not set
|
||||
# CONFIG_APPTRACE_DEST_USB_CDC is not set
|
||||
@ -1178,14 +1177,17 @@ CONFIG_ESP_MAIN_TASK_AFFINITY_CPU0=y
|
||||
# CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set
|
||||
CONFIG_ESP_MAIN_TASK_AFFINITY=0x0
|
||||
CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE=2048
|
||||
# CONFIG_ESP_CONSOLE_UART_DEFAULT is not set
|
||||
CONFIG_ESP_CONSOLE_UART_DEFAULT=y
|
||||
# CONFIG_ESP_CONSOLE_USB_CDC is not set
|
||||
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y
|
||||
# CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG is not set
|
||||
# CONFIG_ESP_CONSOLE_UART_CUSTOM is not set
|
||||
# CONFIG_ESP_CONSOLE_NONE is not set
|
||||
CONFIG_ESP_CONSOLE_SECONDARY_NONE=y
|
||||
# CONFIG_ESP_CONSOLE_SECONDARY_NONE is not set
|
||||
CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG=y
|
||||
CONFIG_ESP_CONSOLE_UART=y
|
||||
CONFIG_ESP_CONSOLE_MULTIPLE_UART=y
|
||||
CONFIG_ESP_CONSOLE_UART_NUM=-1
|
||||
CONFIG_ESP_CONSOLE_UART_NUM=0
|
||||
CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200
|
||||
CONFIG_ESP_INT_WDT=y
|
||||
CONFIG_ESP_INT_WDT_TIMEOUT_MS=300
|
||||
CONFIG_ESP_INT_WDT_CHECK_CPU1=y
|
||||
@ -2201,11 +2203,13 @@ CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ=160
|
||||
CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32
|
||||
CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2304
|
||||
CONFIG_MAIN_TASK_STACK_SIZE=3584
|
||||
# CONFIG_CONSOLE_UART_DEFAULT is not set
|
||||
CONFIG_CONSOLE_UART_DEFAULT=y
|
||||
# CONFIG_CONSOLE_UART_CUSTOM is not set
|
||||
# CONFIG_CONSOLE_UART_NONE is not set
|
||||
# CONFIG_ESP_CONSOLE_UART_NONE is not set
|
||||
CONFIG_CONSOLE_UART_NUM=-1
|
||||
CONFIG_CONSOLE_UART=y
|
||||
CONFIG_CONSOLE_UART_NUM=0
|
||||
CONFIG_CONSOLE_UART_BAUDRATE=115200
|
||||
CONFIG_INT_WDT=y
|
||||
CONFIG_INT_WDT_TIMEOUT_MS=300
|
||||
CONFIG_INT_WDT_CHECK_CPU1=y
|
||||
|
@ -410,12 +410,12 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200
|
||||
#
|
||||
# Partition Table
|
||||
#
|
||||
# CONFIG_PARTITION_TABLE_SINGLE_APP is not set
|
||||
CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y
|
||||
CONFIG_PARTITION_TABLE_SINGLE_APP=y
|
||||
# CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE is not set
|
||||
# CONFIG_PARTITION_TABLE_TWO_OTA is not set
|
||||
# CONFIG_PARTITION_TABLE_CUSTOM is not set
|
||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
||||
CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp_large.csv"
|
||||
CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp.csv"
|
||||
CONFIG_PARTITION_TABLE_OFFSET=0x8000
|
||||
CONFIG_PARTITION_TABLE_MD5=y
|
||||
# end of Partition Table
|
||||
|
Loading…
Reference in New Issue
Block a user