This commit is contained in:
小马_666 2025-05-22 17:46:17 +08:00
parent 7b7bb6b24c
commit 9455f1ee50
8 changed files with 252 additions and 141 deletions

View File

@ -43,9 +43,13 @@ TsSysControl SysControl;
struct rt_timer work_cnt_timer;
struct rt_timer preheat_timer; // 传统模式需要进行预热,
uint8_t preheat_flag = 0; // 预热时间是否结束标志
uint8_t Calibration_flag = 0;//查看是否在标定检测状态
void Send_Laser_Alarm_Event(AlarmEvent event_type);
TeCalibrationStatus g_Calibration_status = kNotCalibrated;
static void Set_Event(AlarmEvent event_type)
{
if (event_type < kMaxEventcnt)
@ -143,63 +147,113 @@ void Work_Cnt_Timer_Callback(void *parameter)
void Preheat_Timer_Callback(void *parameter)
{
BEEP_PREAT_OK;
LED_STOP(r);
LED_STOP(g);
LED_STOP(y);
LOG_D("预热完成");
preheat_flag = 1;
if (g_Calibration_status == kSysGasCalibStatus)
{
Send_Laser_Alarm_Event(kNormalDetectionEvents);
}
else
{
Send_Laser_Alarm_Event(kCalibrationEvent);
}
}
int APP_Calibration_Handle(void)
{
// 定义超时时间,单位为毫秒
#define TIMEOUT_MS (3 * 60 * 1000)
uint8_t i = 0;
uint16_t gas_buf[10] = {0};
uint32_t gas_calibration_voltage = 0;
LED_STOP(g);
LED_STOP(y);
LED_ON(r);
LED_START(r);
uint32_t ticks = 0, gas_calibration_voltage;
// if (Get_Gas_VoltageAdcInt1000x() > 400 && Get_Gas_VoltageAdcInt1000x() < 900)
// {
// LED_OFF(r);
// LED_OFF(g);
// LED_ON(y);
// LOG_D("传感器故障");
// return 0;
// }
if (Get_Gas_VoltageAdcInt1000x() > (MQ_VOLTAGE_ALARM_DEFAULT - 1500) && Get_Gas_VoltageAdcInt1000x() < (MQ_VOLTAGE_ALARM_DEFAULT + 1000))
{
LOG_D("标定开始");
LED_OFF(g);
LED_OFF(y);
LED_OFF(r);
LED_CTRL(y, "200,200", -1);
LED_START(y);
}
else
{
g_Calibration_status = kNotCalibrated;
LOG_D("没有气体,请重新上电标定");
Send_Laser_Alarm_Event(kNotCalibratedEvent);
return 0;
}
Calibration_flag = 1;
while (1)
{
gas_calibration_voltage = Get_Gas_VoltageAdcInt1000x();
LOG_D("ticks[%d] gas_calibration_voltage = %d", ticks++, gas_calibration_voltage);
if (ticks > (TIMEOUT_MS / 1000))
for (i = 0; i < 10; i++)
{
ticks = 0; // 加这个的原因是,如果刚开始没标定,但是时间到了,此时刚通气开始标定,可能直接就读到值了,所以加这个判断
if (gas_calibration_voltage > (MQ_VOLTAGE_ALARM_DEFAULT - 1500) && gas_calibration_voltage < (MQ_VOLTAGE_ALARM_DEFAULT + 1000))
gas_buf[i] = Get_Gas_VoltageAdcInt1000x();
if (gas_buf[i] == 0)
{
return 0;
}
gas_calibration_voltage |= gas_buf[i];
rt_thread_mdelay(1000);
}
if (i == 10)
{
gas_calibration_voltage = (uint16_t)gas_calibration_voltage / 10;
uint8_t calibration_buf[2] = {0};
calibration_buf[0] = gas_calibration_voltage & 0xFF; // 低字节
calibration_buf[1] = (gas_calibration_voltage >> 8) & 0xFF; // 高字节
LOG_D("calibration_buf[0] = %X calibration_buf[1] = %X", calibration_buf[0], calibration_buf[1]);
Flash_Sys_Cfg(kAlarmLValueId, calibration_buf, 2);
LOG_D("标定完成");
// 标定完成打开绿灯常亮,蜂鸣器叫一下
BEEP_CALIBRATION_OK;
LED_STOP(g);
LED_ON(g);
Calibration_flag = 0;
Flash_Set_Calibration_State(kSysGasCalibStatus);
g_Calibration_status = kSysGasCalibStatus;
break;
Send_Laser_Alarm_Event(kNormalDetectionEvents);
return 0;
}
}
if (g_Calibration_status == kSysGasCalibStatus)
else
{
return RT_EOK;
LOG_E("标定错误");
return -1;
}
rt_thread_mdelay(1000);
}
}
// 已经标定了
void APP_Handle(void)
int main(void)
{
// 定义超时时间,单位为毫秒
#define TIMEOUT_MS (3 * 60 * 1000)
#define WORK_TIMER_CNT (1000 * 60 * 60)
if (Get_VIN_VoltageInt1000x() > 10000)
{
SYS_EventInit();
BSP_SYS_Init();
work_duration = Flash_Get_WorkDuration();
LOG_D("工作时长:%d", work_duration);
// 读取历史记录总数
g_Calibration_status = Flash_Get_Calibration_State();
rt_thread_mdelay(10);
}
else
{
LOG_D("欠压复位\r\n");
RCC_ClearFlag();
NVIC_SystemReset(); // 直接重启系统
}
BSP_VIN_Detection_Init();
rt_err_t result = RT_EINVAL;
rt_uint32_t received_event;
@ -237,6 +291,7 @@ void APP_Handle(void)
Send_Laser_Alarm_Event(kPreheatingEvent); // 这一句的作用是设备不是掉电重启的情况,直接进入预热模式
}
RCC_ClearFlag();
while (1)
{
result = rt_event_recv(&alarm_event,
@ -250,7 +305,9 @@ void APP_Handle(void)
Get_Sys_Event_Flag(kSelfCheckEvent) | // 自检
Get_Sys_Event_Flag(kFaultEvent) | // 故障模式
Get_Sys_Event_Flag(kFaultRcyEvent) | // 故障恢复
Get_Sys_Event_Flag(KMuteEvent), // 消音
Get_Sys_Event_Flag(KMuteEvent) | // 消音
Get_Sys_Event_Flag(kCalibrationEvent) | // 标定
Get_Sys_Event_Flag(kNotCalibratedEvent), // 标定
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, &received_event);
if (result == RT_EOK)
@ -272,9 +329,39 @@ void APP_Handle(void)
SysControl.last_status = SysControl.status;
SysControl.status = kPreheatingEvent;
if (SysControl.last_status != kSelfCheckEvent)
{
LED_G_PREAT;
rt_timer_start(&preheat_timer);
}
else
{
LED_STOP(r);
LED_STOP(g);
LED_STOP(y);
LED_CTRL(g, "100,100", -1);
LED_START(g);
}
}
else if (received_event & Get_Sys_Event_Flag(kCalibrationEvent)) // 标定状态
{
LOG_D("标定检测");
SysControl.last_status = SysControl.status;
SysControl.status = kCalibrationEvent;
APP_Calibration_Handle();
}
else if (received_event & Get_Sys_Event_Flag(kNotCalibratedEvent)) // 未标定状态
{
LOG_D("未标定模式");
SysControl.last_status = SysControl.status;
SysControl.status = kNotCalibratedEvent;
LED_STOP(r);
LED_STOP(g);
LED_STOP(y);
LED_ON(r);
}
else if (received_event & Get_Sys_Event_Flag(kNormalDetectionEvents)) // 正常检测
{
SysControl.last_status = SysControl.status;
@ -472,8 +559,37 @@ void APP_Handle(void)
LED_STOP(r);
LED_STOP(g);
LED_STOP(y);
if (g_Calibration_status == kSysGasCalibStatus)
{
if (SysControl.last_status == kPreheatingEvent)
{
if (preheat_flag)
{
Send_Laser_Alarm_Event(kNormalDetectionEvents);
}
else
{
Send_Laser_Alarm_Event(kPreheatingEvent);
}
}
else
{
Send_Laser_Alarm_Event(kNormalDetectionEvents);
}
}
else
{
if (preheat_flag)
{
Send_Laser_Alarm_Event(kNotCalibratedEvent);
}
else
{
Send_Laser_Alarm_Event(kPreheatingEvent);
}
}
}
else if (received_event & Get_Sys_Event_Flag(kSensorFailureEvent)) // 失效
{
LOG_D("传感器失效模式");
@ -494,46 +610,6 @@ void APP_Handle(void)
}
}
int main(void)
{
if (Get_VIN_VoltageInt1000x() > 10000)
{
SYS_EventInit();
BSP_SYS_Init();
work_duration = Flash_Get_WorkDuration();
LOG_D("工作时长:%d", work_duration);
// 读取历史记录总数
g_Calibration_status = Flash_Get_Calibration_State();
rt_thread_mdelay(10);
}
else
{
LOG_D("欠压复位\r\n");
RCC_ClearFlag();
NVIC_SystemReset(); // 直接重启系统
}
BSP_VIN_Detection_Init();
while (1)
{
switch (g_Calibration_status)
{
case kNotCalibrated: // 没标定
{
APP_Calibration_Handle();
break;
}
case kSysGasCalibStatus:
{
APP_Handle();
break;
}
default:
break;
}
rt_thread_mdelay(10);
}
}
#ifdef TEST_ENABLE
static void SYS_Set_RtcProductTime(int argc, char **argv)
{
@ -560,6 +636,7 @@ static void SYS_Set_RtcProductTime(int argc, char **argv)
LOG_E("SYS_Set_RtcProductTime --use _cmd_ [2000 + y] [m] [d] [h] [m] [s]");
}
}
MSH_CMD_EXPORT(SYS_Set_RtcProductTime, "SYS_Set_RtcProductTime");
// 测试系统标定值修改
@ -575,12 +652,14 @@ static void TEST_SYS_Calibartion(int argc, char **argv)
Flash_Set_Calibration_State(kSysGasCalibStatus);
LOG_D("标定值修改为 = %d", Flash_Get_SysCfg(kAlarmLValueId));
g_Calibration_status = kSysGasCalibStatus;
Send_Laser_Alarm_Event(kNormalDetectionEvents); /// 再返回正常模式继续检测
}
else
{
LOG_E("TEST_SYS_Calibartion 0~4095");
}
}
MSH_CMD_EXPORT(TEST_SYS_Calibartion, "修改系统标定值");
static void SYS_SW_Version(void)

View File

@ -64,6 +64,8 @@ typedef enum
kSelfCheckEvent,
KMuteEvent,
kAlarmExceptionEvent, // 浓度异常报警
kCalibrationEvent, //标定阶段
kNotCalibratedEvent, //未标定状态
kMaxEventcnt,
} AlarmEvent;

View File

@ -38,15 +38,19 @@ extern agile_led_t *led_y;
LED_STOP(r);\
LED_STOP(g);\
LED_STOP(y);\
LED_CTRL(g, "500,2500", -1);\
LED_CTRL(g, "500,2500", 1);\
LED_START(g);\
rt_thread_mdelay(1000);\
LED_CTRL(y, "500,2500", -1);\
LED_CTRL(y, "500,2500", 1);\
LED_START(y);\
rt_thread_mdelay(1000);\
LED_CTRL(r, "500,2500", -1);\
LED_CTRL(r, "500,2500", 1);\
LED_START(r);\
rt_thread_mdelay(1000);\
BEEP_CTRL("500,500", 1); \
BEEP_START; \
LED_CTRL(g, "100,100", -1);\
LED_START(g);\
} while (0U)
#define LED_G_NORMAL \

View File

@ -954,6 +954,7 @@ static void ml307_init_thread_entry(void *parameter)
result = -RT_ERROR;
goto __exit;
}
if(*(uint32_t *)FLASH_IOT_IMEI_ADDR == 0xE339E339)
{
#define ML307_NETDEV_HWADDR_LEN 8
#define ML307_IMEI_LEN 15
@ -1019,6 +1020,7 @@ static void ml307_init_thread_entry(void *parameter)
rt_thread_mdelay(1000);
}
/* set network interface device hardware iccid */
if(*(uint32_t *)FLASH_IOT_ICCID_ADDR == 0xE339E339)
{
#define ML307_ICCID_LEN 20
@ -1045,7 +1047,7 @@ static void ml307_init_thread_entry(void *parameter)
}
/* set network interface device hardware imsi */
{
if(*(uint32_t *)FLASH_IOT_IMSI_ADDR == 0xE339E339){
#define ML307_IMSI_LEN 15
@ -1094,43 +1096,43 @@ static void ml307_init_thread_entry(void *parameter)
result = -RT_ERROR;
goto __exit;
}
// if (at_obj_exec_cmd(device->client, resp, "AT+MIPCALL=1,1") != RT_EOK)
if (at_obj_exec_cmd(device->client, resp, "AT+MIPCALL=0") != RT_EOK)
if (at_obj_exec_cmd(device->client, resp, "AT+MIPCALL=1,1") != RT_EOK)
// if (at_obj_exec_cmd(device->client, resp, "AT+MIPCALL=0") != RT_EOK)
{
result = -RT_ERROR;
goto __exit;
}
// #if defined(AT_DEBUG)
// /* check the GPRS network IP address */
// for (i = 0; i < IPADDR_RETRY; i++)
// {
// if (at_obj_exec_cmd(client, resp, "AT+MIPCALL?") == RT_EOK)
// {
// #define IP_ADDR_SIZE_MAX 16
// char ipaddr_str[8 * IP_ADDR_SIZE_MAX] = {0};
// char ipaddr_v4[IP_ADDR_SIZE_MAX] = {0};
// char ipaddr_v6[4 * IP_ADDR_SIZE_MAX] = {0};
#if defined(AT_DEBUG)
/* check the GPRS network IP address */
for (i = 0; i < IPADDR_RETRY; i++)
{
if (at_obj_exec_cmd(client, resp, "AT+MIPCALL?") == RT_EOK)
{
#define IP_ADDR_SIZE_MAX 16
char ipaddr_str[8 * IP_ADDR_SIZE_MAX] = {0};
char ipaddr_v4[IP_ADDR_SIZE_MAX] = {0};
char ipaddr_v6[4 * IP_ADDR_SIZE_MAX] = {0};
// /* parse response data "+CGPADDR: 1,<IP_address>" */
// if (at_resp_parse_line_args_by_kw(resp, "+MIPCALL:", "+MIPCALL: %*d,%*d,%s", ipaddr_str) > 0)
// {
// const char *ipaddr_v4_str = strstr(ipaddr_str, "\"");
// sscanf(ipaddr_v4_str, "\"%[^\"]", ipaddr_v4);
// const char *ipaddr_v6_str = strstr(ipaddr_str, "\",\"");
// sscanf(ipaddr_v6_str, "\",\"%[^\"]", ipaddr_v6);
/* parse response data "+CGPADDR: 1,<IP_address>" */
if (at_resp_parse_line_args_by_kw(resp, "+MIPCALL:", "+MIPCALL: %*d,%*d,%s", ipaddr_str) > 0)
{
const char *ipaddr_v4_str = strstr(ipaddr_str, "\"");
sscanf(ipaddr_v4_str, "\"%[^\"]", ipaddr_v4);
const char *ipaddr_v6_str = strstr(ipaddr_str, "\",\"");
sscanf(ipaddr_v6_str, "\",\"%[^\"]", ipaddr_v6);
// LOG_D("%s device IP address: %s - %s", device->name, ipaddr_v4, ipaddr_v6);
// break;
// }
// }
// rt_thread_mdelay(1000);
// }
// if (i == IPADDR_RETRY)
// {
// LOG_E("%s device GPRS is get IP address failed", device->name);
// result = -RT_ERROR;
// goto __exit;
// }
LOG_D("%s device IP address: %s - %s", device->name, ipaddr_v4, ipaddr_v6);
break;
}
}
rt_thread_mdelay(1000);
}
if (i == IPADDR_RETRY)
{
LOG_E("%s device GPRS is get IP address failed", device->name);
result = -RT_ERROR;
goto __exit;
}
#endif
Time_Calibration(device);
/* initialize successfully */

View File

@ -55,7 +55,7 @@ static void _CommonBtnEvtCb (void *arg)
if (flex_button_event_read (&user_button[USER_BUTTON_1]) == FLEX_BTN_PRESS_DOUBLE_CLICK)
{
if (SysControl.status > kPreheatingEvent)
if(SysControl.status != kCalibrationEvent)
{
Send_Laser_Alarm_Event (kSelfCheckEvent);
}

View File

@ -895,7 +895,7 @@ int BSP_Flash_Init (void)
Flash_SetProductTimeLimit (2025, 2, 10, 13, 50, 20, kFactoryTimeId);
Set_ExpirationTime (MAX_EXPIRATION_DAYS);
Flash_Set_Calibration_State(kSysGasCalibStatus); // 标定状态
Flash_Set_Calibration_State(kNotCalibrated); // 标定状态
Flash_Set_Valve_Num(0);
sci.hw_ver = SYS_HW_VERSION;
sci.sw_ver = SYS_SW_VERSION;

View File

@ -814,6 +814,7 @@ int Ml307_Send_Device_Failure(struct at_device *device, void *param)
}
return RT_EOK;
}
/*这个【4、优先级最低当同时触发后相同服务直接合并】暂时还没想好怎么做
线
线
@ -943,13 +944,8 @@ int Ml307_Process_Events(Ml307Event ml307_recv_event, struct at_device *device,
if (ret != RT_EOK)
{
ml307_connect_sever_flag = 0;
ml307_conncet_tcp_flag = 0;
if (!ml307_disconnect_retry_flag)
{
LOG_D("断网,启动重连\n");
rt_sem_release(ml307_disconnect_sem);
}
LOG_D("断网,等待连接中......\n");
LOG_D("与服务区断开连接,等待连接中......\n");
}
}
@ -1967,19 +1963,43 @@ static void Ml307_Life_Thread_Entry(void *parameter)
result = -RT_ERROR;
goto __exit;
}
// if (at_obj_exec_cmd(device->client, resp, "AT+MIPCALL=1,1") != RT_EOK)
// {
// result = -RT_ERROR;
// goto __exit;
// }
// int link_stat = 0;
// if (at_resp_parse_line_args_by_kw(resp, "+MIPCALL:", "+MIPCALL: %d,%*d,%*s", &link_stat) > 0)
// {
// if (link_stat == 1)
// {
// result = RT_EOK;
// }
// }
if (at_obj_exec_cmd(device->client, resp, "AT+MIPCALL=1,1") != RT_EOK)
{
result = -RT_ERROR;
goto __exit;
}
#if defined(AT_DEBUG)
/* check the GPRS network IP address */
for (i = 0; i < 10; i++)
{
if (at_obj_exec_cmd(device->client, resp, "AT+MIPCALL?") == RT_EOK)
{
#define IP_ADDR_SIZE_MAX 16
char ipaddr_str[8 * IP_ADDR_SIZE_MAX] = {0};
char ipaddr_v4[IP_ADDR_SIZE_MAX] = {0};
char ipaddr_v6[4 * IP_ADDR_SIZE_MAX] = {0};
/* parse response data "+CGPADDR: 1,<IP_address>" */
if (at_resp_parse_line_args_by_kw(resp, "+MIPCALL:", "+MIPCALL: %*d,%*d,%s", ipaddr_str) > 0)
{
const char *ipaddr_v4_str = rt_strstr(ipaddr_str, "\"");
sscanf(ipaddr_v4_str, "\"%[^\"]", ipaddr_v4);
const char *ipaddr_v6_str = rt_strstr(ipaddr_str, "\",\"");
sscanf(ipaddr_v6_str, "\",\"%[^\"]", ipaddr_v6);
LOG_D("%s device IP address: %s - %s", device->name, ipaddr_v4, ipaddr_v6);
break;
}
}
rt_thread_mdelay(2000);
}
if (i == 10)
{
LOG_E("%s device GPRS is get IP address failed", device->name);
result = -RT_ERROR;
goto __exit;
}
#endif
if (device->class->device_ops->control(device, AT_DEVICE_CTRL_NET_CONN, RT_NULL) == RT_EOK)
{
LOG_D("重连网络成功\n");
@ -2012,6 +2032,7 @@ static void Ml307_Life_Thread_Entry(void *parameter)
else
{
ml307_disconnect_retry_flag = 0;
Ml307_Send_Event(kMl307HeartbeatEvent);
}
}
else
@ -2146,6 +2167,7 @@ int BSP_Ml307_Thread_Init(void)
return ret;
}
// INIT_APP_EXPORT(BSP_Ml307_Thread_Init);
int ml307_device_register(void)
@ -2158,6 +2180,7 @@ int ml307_device_register(void)
AT_DEVICE_CLASS_ML307,
(void *)ml307);
}
// INIT_DEVICE_EXPORT(ml307_device_register);
#endif // IOT_MODULE_SWITCH

View File

@ -47,7 +47,7 @@ uint16_t Get_Gas_VoltageInt1000x(void)
uint16_t Get_Gas_VoltageAdcInt1000x(void)
{
rt_uint16_t voltage_adc = (Get_ADC_Average(kGasAdc) * 3.3 / 4096) * 1000;
// LOG_D("Get_Gas_VoltageAdcInt1000x = %04d", voltage_adc);
LOG_D("Get_Gas_VoltageAdcInt1000x = %04d", voltage_adc);
return voltage_adc;
}
@ -180,6 +180,7 @@ static void Sensor_detection_thread_entry(void *param)
if(calibration_flag == 1)
break;
rt_thread_mdelay(1000);
Get_Gas_VoltageAdcInt1000x();
}
rt_uint16_t alarm_value = Flash_Get_SysCfg(kAlarmLValueId); // 获取系统报警阈值;