| | |
| | | ///////////////////////////////////免布线时间同步代码 |
| | | |
| | | |
| | | typedef enum{ Sync_Lost, //同步丢失 同步时间 100秒同步一次,每次同步开启2秒,发起测距请求 |
| | | Sync_Range,//同步状态 测距 同步时间 10秒同步一次,开启10ms,发起测距请求 |
| | | Sync_Config,//同步状态 配置 同步时间 1秒同步一次,不发起测距请求 |
| | | Sync_Sleep, //同步状态 休眠 同步时间 100秒同步一次,不发起测距请求 |
| | | }sync_state_enum; |
| | | |
| | | sync_state_enum sync_state = Sync_Lost; |
| | | |
| | | static uint8_t sync_interval_s,range_state = 0; |
| | |
| | | switch(sync_state) |
| | | { |
| | | case Sync_Lost: |
| | | sync_interval_s = 100; |
| | | sync_listen_ms =2000; |
| | | sync_interval_s = 10; //100 |
| | | sync_listen_ms =1500; |
| | | range_state = 1; |
| | | break; |
| | | case Sync_Range: |
| | | sync_interval_s = 10; |
| | | sync_listen_ms =5; |
| | | sync_interval_s = 10; //10 |
| | | sync_listen_ms =10; |
| | | range_state = 1; |
| | | break; |
| | | case Sync_Config: |
| | | sync_interval_s = 1; |
| | | sync_listen_ms =5; |
| | | sync_listen_ms =10; |
| | | range_state = 0; |
| | | break; |
| | | case Sync_Sleep: |
| | | sync_interval_s = 100; |
| | | sync_listen_ms =5; |
| | | sync_interval_s = 10;//10 |
| | | sync_listen_ms =10; |
| | | range_state = 0; |
| | | break; |
| | | } |
| | |
| | | |
| | | |
| | | static uint16_t current_count,poll_startcount,start_count,end_count,poll_timeout; |
| | | static uint8_t sync_interval_count = 0,flag_sync_success,sync_lost_count; |
| | | uint8_t sync_interval_count = 100,flag_sync_success,sync_lost_count; |
| | | void SetFlagSyncSuccess(uint8_t value) |
| | | { |
| | | flag_sync_success = value; |
| | |
| | | static uint16_t synclock_zero_count,wg_sync_polltime_100us,uwbpoll_time_count; |
| | | uint16_t GetUwbPollCount(void) |
| | | { |
| | | uwbpoll_time_count = synclock_zero_count+g_com_map[UWBPOLLTIME_MS_IDX]*33; |
| | | if(uwbpoll_time_count>=32768) |
| | | uwbpoll_time_count-=32768; |
| | | return uwbpoll_time_count; |
| | | } |
| | | uint16_t GetWGRecSyncCount(void) |
| | |
| | | } |
| | | void SyncClockProcess(void) //同步时钟函数获取下次UWBpoll的定时器count。 |
| | | { |
| | | memcpy(&wg_sync_polltime_100us,&lora_recbuffer[SYSTIME_IDX],2); //WG下发的同步时间,单位0.1ms。 |
| | | synclock_zero_count = rec_sync_count-(uint32_t)wg_sync_polltime_100us*100/LPTIMER_LSB; |
| | | if(synclock_zero_count<0) |
| | | synclock_zero_count+=32768; |
| | | uwbpoll_time_count = synclock_zero_count + (uint32_t)g_com_map[UWBPOLLTIME_MS_IDX]*1000/LPTIMER_LSB; //设定好的UWB发送时间 |
| | | if(uwbpoll_time_count>32768) |
| | | uwbpoll_time_count-=32768; |
| | | |
| | | } |
| | | static uint16_t current_count,target_count; |
| | | #define HEATBEAT_UPDATE_TIME_S 3600 //心跳包一个小时上传一次 |
| | | static uint16_t current_count,target_count,heartbeat_count; |
| | | static uint8_t heatbeat_buffer[100]; |
| | | void HeartbeatPoll(void) |
| | | {//心跳包上传 ID 版本号 小组 UWB功率 LORA功率 UWB发送时间 |
| | | heartbeat_count+=sync_interval_s; |
| | | if(heartbeat_count>HEATBEAT_UPDATE_TIME_S) |
| | | { |
| | | heartbeat_count = 0; |
| | | heatbeat_buffer[MSG_TYPE_IDX] = LORA_MSGTYPE_ANCHEARTBEAT_POLL; |
| | | memcpy(&heatbeat_buffer[ANC_ID_IDX],&g_com_map[DEV_ID],2); |
| | | heatbeat_buffer[ANC_VERSION_IDX] = g_com_map[VERSION]; |
| | | heatbeat_buffer[ANC_GROUPID_IDX] = g_com_map[GROUP_ID]; |
| | | heatbeat_buffer[ANC_UWBPOWER_IDX] = g_com_map[POWER]; |
| | | heatbeat_buffer[ANC_LORAPOWER_IDX] = g_com_map[LORA_POWER]; |
| | | heatbeat_buffer[ANC_UWBPOLLTIME_IDX] = g_com_map[UWBPOLLTIME_MS_IDX]; |
| | | Radio.Send(heatbeat_buffer, 14); |
| | | delay_us(1700); |
| | | } |
| | | } |
| | | void LoraSyncRecPoll(void) |
| | | { |
| | | if(sync_interval_count++>=sync_interval_s) |
| | | if(++sync_interval_count>=sync_interval_s) |
| | | { |
| | | current_count=HAL_LPTIM_ReadCounter(&hlptim1); //如果需要触发同步就会等待触发时间,提前2ms打开接收 |
| | | target_count = GetWGRecSyncCount()-2000/LPTIMER_LSB; //提前2ms进入接收状态 |
| | | if(target_count<0) |
| | | target_count+=32768; |
| | | while(current_count<target_count||current_count>target_count+16384) |
| | | { |
| | | current_count=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | if(current_count<target_count-300) |
| | | { |
| | | break; |
| | | } |
| | | } |
| | | // if(sync_state!=Sync_Lost) |
| | | // { |
| | | // current_count=HAL_LPTIM_ReadCounter(&hlptim1); //如果需要触发同步就会等待触发时间,提前2ms打开接收 |
| | | // target_count = GetWGRecSyncCount()-RX_BEFORE_SYNCTIME_US/LPTIMER_LSB; //提前2ms进入接收状态 |
| | | // if(target_count<0) |
| | | // target_count+=32768; |
| | | // while(current_count<target_count||current_count>target_count+16384) |
| | | // { |
| | | // current_count=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | // if(current_count<target_count-300) |
| | | // { |
| | | // break; |
| | | // } |
| | | // } |
| | | // } |
| | | sync_interval_count = 0; |
| | | flag_sync_success = 0; |
| | | SwitchLoraSettings(SYNC_CHANNEL_FRQ,7); //切换lora接收频点//因为是从lorasleep唤醒的,可能这里会用的时间多,需要考虑 |
| | | |
| | | SwitchLoraSettings(SYNC_CHANNEL_FRQ,7,22); //切换lora接收频点//因为是从lorasleep唤醒的,可能这里会用的时间多,需要考虑 |
| | | |
| | | start_count=HAL_LPTIM_ReadCounter(&hlptim1); //等待接收完成直到超时 |
| | | poll_timeout=sync_listen_ms*10; //单位0.1ms |
| | | end_count=start_count+(poll_timeout<<2); |
| | |
| | | {end_count-=32768;} |
| | | current_count=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | SetFlagLoraWaitSync(1); |
| | | Radio.Rx(100); |
| | | |
| | | Radio.Rx(sync_listen_ms); |
| | | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); |
| | | while(GetFlagLoraWaitSync()) |
| | | { |
| | | current_count=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | if(current_count>=end_count&¤t_count<end_count+15000) |
| | | { |
| | | break; |
| | | } |
| | | // current_count=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | // if(current_count>=end_count&¤t_count<end_count+15000) |
| | | // { |
| | | // break; |
| | | // } |
| | | } |
| | | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); |
| | | if(flag_sync_success) //同步成功 |
| | | { |
| | | HeartbeatPoll(); |
| | | LED_LG_ON; |
| | | sync_lost_count = 0; |
| | | lora_recbuffer_len = GetLoraBufferAndLen(lora_recbuffer,&rec_sync_count);//获取LORA收到的数据和长度 |
| | | if(lora_recbuffer[MSG_TYPE_IDX]==LORA_MSGTYPE_SYNC) |
| | | { |
| | | memcpy(&g_com_map[SYNCWG_ID_IDX],&lora_recbuffer[WG_ID_IDX],2); |
| | | { uint16_t wg_sendtime_100us; |
| | | memcpy(&g_com_map[SYNCWG_ID_IDX],&lora_recbuffer[WG_ID_IDX],2); |
| | | memcpy(&g_com_map2[SYNCWG_ID_IDX],&lora_recbuffer[WG_ID_IDX],2); |
| | | memcpy(&wg_sendtime_100us,&lora_recbuffer[SYSTIME_IDX],2); |
| | | synclock_zero_count = rec_sync_count-(wg_sendtime_100us+158)*3.27;//减去17ms 因为lora发送会延迟17ms收到 |
| | | if(synclock_zero_count<0) |
| | | synclock_zero_count+=32768; |
| | | sync_lost_count = 0; |
| | | if(lora_recbuffer[RANGE_STATE_IDX]) |
| | | { |
| | | sync_state = Sync_Range; |
| | |
| | | } |
| | | } |
| | | }else{ |
| | | // LED_LR_ON; |
| | | if(sync_lost_count++>SYNCLOST_THRES) |
| | | { |
| | | sync_state = Sync_Lost; |
| | | SyncStateSwitch(); |
| | | } |
| | | } |
| | | Radio.Sleep(); |
| | | // LED_LR_OFF; |
| | | LED_LG_OFF; |
| | | } |
| | | // Radio.Sleep( ); //如果超时就sleep |
| | | } |
| | |
| | | tag_num_tosend = 25; |
| | | } |
| | | count1++; |
| | | dwt_setrxaftertxdelay(400);//设置发送完成后开启接收延迟时间 |
| | | dwt_setrxtimeout(0);//接收超时时间 |
| | | memcpy(&uwb_sendbuffer[TAG_ID_IDX],&dev_id,2); |
| | | uwb_sendbuffer[GROUP_ID_IDX] = group_id; |
| | | uwb_sendbuffer[BATTARY_IDX] = bat_percent; |
| | |
| | | dwt_writetxfctrl(13+4*tag_num_tosend, 0);//设置超宽带发送数据长度 |
| | | |
| | | dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);//开启发送,发送完成后等待一段时间开启接收,等待时间在dwt_setrxaftertxdelay中设置 |
| | | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); |
| | | //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); |
| | | |
| | | start_count=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | poll_timeout=tag_num_tosend*SLOT_SCALE+30; //单位0.1ms |
| | | poll_timeout=tag_num_tosend*SLOT_SCALE+10; //单位0.1ms |
| | | end_count=start_count+(poll_timeout<<2); |
| | | if(end_count>=32768) |
| | | {end_count-=32768;} |