diff --git a/Core/Src/yunhorn_sts_distance_rss.c b/Core/Src/yunhorn_sts_distance_rss.c index dc6edd8..85a2a38 100644 --- a/Core/Src/yunhorn_sts_distance_rss.c +++ b/Core/Src/yunhorn_sts_distance_rss.c @@ -55,7 +55,7 @@ //volatile distance_measure_cfg_t distance_cfg={1.5, 2.0, 1, 63, 2, 10, 0.5, 1.3, 0.2}; // SPARK FUN EXAMPLE // GOOD --- volatile distance_measure_cfg_t distance_cfg={0.4, 3.5, 4, 63, 0, 10, 0.5, 1.3, 0.2}; -volatile distance_measure_cfg_t distance_cfg={0.8, 3.5, 4, 63, 2, 10, 0.5, 1.3, 0.2}; +volatile distance_measure_cfg_t distance_cfg={0.8, 3.5, 2, 63, 2, 10, 0.5, 1.3, 0.2}; //volatile distance_measure_cfg_t distance_cfg={1.5, 3.3, 2, 63, 4, 10, 0.8182f, 0.4, 0.2}; extern volatile uint16_t sts_distance_rss_distance, sts_sensor_install_height; diff --git a/Core/Src/yunhorn_sts_presence_rss.c b/Core/Src/yunhorn_sts_presence_rss.c index 075049f..7b32be9 100644 --- a/Core/Src/yunhorn_sts_presence_rss.c +++ b/Core/Src/yunhorn_sts_presence_rss.c @@ -123,8 +123,8 @@ volatile uint8_t detected_hs_zone=0;; volatile uint16_t motion_count=0, motion_feature_count=0; typedef struct { - uint32_t presence_score; // 1000* - uint32_t presence_distance; // 1000*, in mm + uint16_t presence_score; // 1000* + uint16_t presence_distance; // 1000*, in mm } STS_presence_result_t; static STS_presence_result_t sts_motion_dataset[DEFAULT_MOTION_DATASET_LEN]; @@ -309,7 +309,7 @@ static void print_result(acc_detector_presence_result_t result) //uint32_t detected_zone = (uint32_t)((float)(result.presence_distance - DEFAULT_START_M) / (float)DEFAULT_ZONE_LENGTH); // 2024-08-05 uint32_t detected_zone = (uint32_t)((float)(result.presence_distance) / (float)DEFAULT_ZONE_LENGTH); - APP_LOG(TS_OFF, VLEVEL_H,"Motion in zone: %u, distance: %d, score: %d\n", (unsigned int)detected_zone, + APP_LOG(TS_OFF, VLEVEL_H,"\r\nMotion in zone: %u, distance: %d, score: %d\n", (unsigned int)detected_zone, (int)(result.presence_distance * 1000.0f), (int)(result.presence_score * 1000.0f)); } @@ -430,18 +430,13 @@ int sts_presence_rss_fall_rise_detection(void) //APP_LOG(TS_OFF, VLEVEL_L, "\r\nHS_ZONE=%u", (int)detected_hs_zone); detected_hs_zone = 6 - detected_zone; motion_in_hs_zone[detected_hs_zone][(motion_detected_count)]++; - } - sts_motion_dataset[motion_count].presence_distance = 1000*result.presence_distance; - sts_motion_dataset[motion_count].presence_score = 1000*result.presence_score; - if (sts_presence_fall_detection == TRUE) - { - if (motion_count ++ == DEFAULT_MOTION_DATASET_LEN) - { - // STS_YunhornCheckStandardDeviation(); no Deviation in middle 2024 -7-22 - motion_count = 0; - } + sts_motion_dataset[motion_count].presence_distance = 1000*result.presence_distance; + sts_motion_dataset[motion_count].presence_score = 1000*result.presence_score; + + motion_count++; + } } @@ -506,21 +501,13 @@ int sts_presence_rss_fall_rise_detection(void) detected_hs_zone = 6 - detected_zone; motion_in_hs_zone[detected_hs_zone][(motion_detected_count)]++; - } - - if (sts_presence_fall_detection == TRUE) - { sts_motion_dataset[motion_count].presence_distance = 1000*result.presence_distance; sts_motion_dataset[motion_count].presence_score = 1000*result.presence_score; - if (motion_count ++ == DEFAULT_MOTION_DATASET_LEN) - { - STS_YunhornCheckStandardDeviation(); - motion_count = 0; - - } + motion_count++; } + } //acc_integration_sleep_ms(1000 / DEFAULT_UPDATE_RATE_PRESENCE); // 15 ms, DEFAULT_UPDATE_RATE); @@ -528,6 +515,11 @@ int sts_presence_rss_fall_rise_detection(void) } //APP_LOG(TS_OFF, VLEVEL_L,"Second Half, Fall Rise Detection, Motion Count = %u \r\n", (int)motion_count); + motion_count = motion_count%DEFAULT_MOTION_DATASET_LEN; // get all required number of motion data + + if (sts_presence_fall_detection == TRUE) + STS_YunhornCheckStandardDeviation(); + uint8_t thiscnt= motion_detected_count; if (motion_detected_count++ == 10) { @@ -558,21 +550,28 @@ int sts_presence_rss_fall_rise_detection(void) } - +#ifndef MAX +#define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) ) +#endif //#ifdef LOG_RSS - APP_LOG(TS_OFF, VLEVEL_L,"\r\nSensor at Ceiling Height: %u mm\r\n",(uint16_t)sts_sensor_install_height); - APP_LOG(TS_OFF, VLEVEL_L,"\r\n|Motion Distance Zone| ##### |Height cm|\r\n"); - for (uint8_t k=0; k<12; k++) { - if (motion_in_hs_zone[k][thiscnt]>0) { - APP_LOG(TS_OFF, VLEVEL_L,"\r\n| %2u | %4u | %4u |\r\n", k, (uint8_t)motion_in_hs_zone[k][thiscnt], (int)(k+1)*40); + APP_LOG(TS_OFF, VLEVEL_L,"\r\nSensor at Ceiling Height: %u mm\r\n",(uint16_t)sts_sensor_install_height); + APP_LOG(TS_OFF, VLEVEL_L,"\r\n|Motion Distance Zone| ##### |Height cm|\r\n"); + uint8_t kk = (uint8_t)(sts_sensor_install_height/400+1); + APP_LOG(TS_OFF, VLEVEL_L,"\r\n|-----------Ceiling-------Sensor---V-----|\r\n"); + for (uint8_t k=0; k<=kk; k++) + { + if (motion_in_hs_zone[kk-k][thiscnt]>0) { + + APP_LOG(TS_OFF, VLEVEL_L,"\r\n| %2u | %4u | %4u |\r\n", kk-k, (uint8_t)motion_in_hs_zone[kk-k][thiscnt], (int)(kk-k+1)*40); } else { - APP_LOG(TS_OFF, VLEVEL_L,"\r\n| %2u | | |\r\n", k); + APP_LOG(TS_OFF, VLEVEL_L,"\r\n| %2u | | |\r\n", kk-k); } - } + } + APP_LOG(TS_OFF, VLEVEL_L,"\r\n|-----------Floor Ground-----------^-----|\r\n"); //#endif average_distance = (1000.0f*average_distance)/average_result; // in meters average_score = (1000.0f*average_score)/average_result; @@ -643,7 +642,8 @@ void STS_YunhornCheckStandardDeviation(void) //STANDARD VARIANCE sigma standard_variance_presence_distance = (uint32_t)sqrt(variance_presence_distance); standard_variance_presence_score = (uint32_t)sqrt(variance_presence_score); - + APP_LOG(TS_OFF, VLEVEL_M, "\r\n Standard Variance_Presence_Distance=%u \r\n Standard Variance Presence Score=%u\r\n", + standard_variance_presence_distance, standard_variance_presence_score); // ROC distance // SUM @@ -653,11 +653,11 @@ void STS_YunhornCheckStandardDeviation(void) sum_roc_distance += ((uint32_t)roc_distance[i]); } - average_roc_distance = (uint32_t)sum_roc_distance/(float)(DEFAULT_MOTION_DATASET_LEN-1); + average_roc_distance = (uint32_t)sum_roc_distance/(DEFAULT_MOTION_DATASET_LEN-1); for (j = 0; j < (DEFAULT_MOTION_DATASET_LEN-1); j++) { - variance_roc_distance += (uint32_t)(pow((float)roc_distance[j] - (uint32_t)average_roc_distance,2)); + variance_roc_distance += (uint32_t)(pow(roc_distance[j] - average_roc_distance,2)); } // average variance_roc_distance /= (uint32_t)(DEFAULT_MOTION_DATASET_LEN-1); @@ -674,11 +674,11 @@ void STS_YunhornCheckStandardDeviation(void) sum_roc_acc += ((uint32_t)roc_acc[i]); } - average_roc_acc = (uint32_t)sum_roc_acc/(float)(DEFAULT_MOTION_DATASET_LEN-2); + average_roc_acc = (uint32_t)sum_roc_acc/(DEFAULT_MOTION_DATASET_LEN-2); for (j = 0; j < (DEFAULT_MOTION_DATASET_LEN-2); j++) { - variance_roc_acc += (uint32_t)pow((uint32_t)((uint32_t)roc_acc[j] - (uint32_t)average_roc_acc),2.0f); + variance_roc_acc += (uint32_t)pow((uint32_t)((uint32_t)roc_acc[j] - (uint32_t)average_roc_acc),2); } variance_roc_acc /= (uint32_t)(DEFAULT_MOTION_DATASET_LEN-2); @@ -686,7 +686,7 @@ void STS_YunhornCheckStandardDeviation(void) standard_variance_roc_acc = (uint32_t)sqrt((uint32_t)variance_roc_acc); //Normallize to m/s --- * DEFAULT_MOTION_DATASET_LEN for One single second - +#if 0 average_roc_distance *= (uint32_t)DEFAULT_MOTION_DATASET_LEN; variance_roc_distance *= (uint32_t)DEFAULT_MOTION_DATASET_LEN; standard_variance_roc_distance *= (uint32_t)DEFAULT_MOTION_DATASET_LEN; @@ -694,32 +694,34 @@ void STS_YunhornCheckStandardDeviation(void) average_roc_acc *= (uint32_t)DEFAULT_MOTION_DATASET_LEN; variance_roc_acc *= (uint32_t)DEFAULT_MOTION_DATASET_LEN; standard_variance_roc_acc *= (uint32_t)DEFAULT_MOTION_DATASET_LEN; - +#endif // print result //#ifdef LOG_RSS - APP_LOG(TS_OFF, VLEVEL_L, "\r\n---Sensor Install Height=%6u-----Distance Average =%6u; Variance = %6u ; Standard =%6u \r\n", - (int)sts_sensor_install_height, (int)(average_presence_distance), (int)(variance_presence_distance), (int)(standard_variance_presence_distance)); + APP_LOG(TS_OFF, VLEVEL_M, "\r\n---Sensor Install Height=%6u-----\r\n-------------Distance Average =%6u; Standard_Variance =%6u \r\n", + (int)sts_sensor_install_height, (int)(average_presence_distance), (int)(standard_variance_presence_distance)); //#endif //#ifdef LOG_RSS - APP_LOG(TS_OFF, VLEVEL_M, "\r\n\n-------------Motion Average =%6u; Variance = %6u ; Standard =%6u \r\n", - (int)(average_presence_score), (int)(variance_presence_score), (int)(standard_variance_presence_score)); + APP_LOG(TS_OFF, VLEVEL_M, "\r\n\n-------------Motion Average =%6u; Standard_Variance =%6u \r\n", + (int)(average_presence_score), (int)(standard_variance_presence_score)); //#endif //#ifdef LOG_RSS - APP_LOG(TS_OFF, VLEVEL_L, "\r\n-------------ROC Dist Average =%6u; Variance = %6u ; Standard =%6u \r\n", - (int)(average_roc_distance), (int)(variance_roc_distance), (int)(standard_variance_roc_distance)); + APP_LOG(TS_OFF, VLEVEL_M, "\r\n-------------ROC Dist Average =%6u; Standard_Variance =%6u \r\n", + (int)(average_roc_distance), (int)(standard_variance_roc_distance)); - APP_LOG(TS_OFF, VLEVEL_L, "\r\n-------------ROC ACC Average =%6u; Variance = %6u ; Standard =%6u \r\n", - (int)(average_roc_acc), (int)(variance_roc_acc), (int)(standard_variance_roc_acc)); + APP_LOG(TS_OFF, VLEVEL_M, "\r\n-------------ROC ACC Average =%6u; Standard_Variance =%6u \r\n", + (int)(average_roc_acc), (int)(standard_variance_roc_acc)); + + //sts_fall_rising_pattern_factor1 = (int)(standard_variance_roc_distance); + sts_fall_rising_pattern_factor1 = (int)(average_roc_distance/10); + sts_fall_rising_pattern_factor2 = (int)(fabs(average_presence_distance - last_average_presence_distance)); - sts_fall_rising_pattern_factor1 = (int)(standard_variance_roc_distance); - sts_fall_rising_pattern_factor2 = (int)(fabs(average_presence_distance - fmax(0,last_average_presence_distance))*100.0f); // in cm APP_LOG(TS_OFF, VLEVEL_L,"\r\nAvg-Dist =%6u, Last_AVG-Dist =%6u \r\n", (int)(average_presence_distance), (int)(last_average_presence_distance)); - APP_LOG(TS_OFF, VLEVEL_L, "\r\nThreshold 1: \r\nAcc = %6u \r\nMeasure 1 = %6u ---- \r\n", - (int)(sts_fall_detection_acc_threshold), (int)(sts_fall_rising_pattern_factor1)); + APP_LOG(TS_OFF, VLEVEL_M, "\r\nThreshold 1: Acc = %6u ---- Measure 1 = %6u ---- \r\n", + (int)(sts_fall_detection_acc_threshold), (int)(sts_fall_rising_pattern_factor1/10)); - APP_LOG(TS_OFF, VLEVEL_L, "\r\nThreshold 2: \r\nDis = %6u cm \r\nMeasure 2 = %6u cm ---- \r\n", - (int)(sts_fall_detection_depth_threshold), (int)(sts_fall_rising_pattern_factor2)); + APP_LOG(TS_OFF, VLEVEL_M, "\r\nThreshold 2: Dis = %6u cm --- Measure 2 = %6u cm ---- \r\n", + (int)(sts_fall_detection_depth_threshold), (int)(sts_fall_rising_pattern_factor2/10)); //#endif // ******************************************* diff --git a/LoRaWAN/App/lora_app.c b/LoRaWAN/App/lora_app.c index 9d6f2e1..b93f7c7 100644 --- a/LoRaWAN/App/lora_app.c +++ b/LoRaWAN/App/lora_app.c @@ -62,7 +62,7 @@ extern volatile uint8_t sts_lamp_bar_flashing_color; volatile uint8_t last_sts_lamp_bar_color=STS_DARK; extern volatile uint8_t sts_rss_result; extern volatile uint8_t sts_rss_result_changed_flag, sts_hall1_changed_flag, sts_hall2_changed_flag, sts_reed_hall_changed_flag; -volatile uint8_t sts_fall_detection_acc_threshold = 10, +volatile uint8_t sts_fall_detection_acc_threshold = 20, sts_fall_detection_depth_threshold=20, sts_fall_confirm_threshold_in_10sec=1, sts_occupancy_overtime_threshold_in_10min=2; @@ -684,7 +684,7 @@ void LoRaWAN_Init(void) #endif #if defined(STS_O7)||defined(STS_O6) - UTIL_TIMER_Create(&YunhornSTSRSSWakeUpTimer, YUNHORN_STS_RSS_WAKEUP_CHECK_TIME, UTIL_TIMER_PERIODIC, OnYunhornSTSOORSSWakeUpTimerEvent, NULL); + //UTIL_TIMER_Create(&YunhornSTSRSSWakeUpTimer, YUNHORN_STS_RSS_WAKEUP_CHECK_TIME, UTIL_TIMER_ONESHOT, OnYunhornSTSOORSSWakeUpTimerEvent, NULL); //UTIL_TIMER_Start(&YunhornSTSRSSWakeUpTimer); UTIL_TIMER_Create(&YunhornSTSHeartBeatTimer, YUNHORN_STS_HEART_BEAT_CHECK_TIME, UTIL_TIMER_PERIODIC, OnYunhornSTSHeartBeatTimerEvent, NULL); @@ -1429,6 +1429,8 @@ static void OnJoinRequest(LmHandlerJoinParams_t *joinParams) //UTIL_TIMER_Start(&STSDurationCheckTimer); OnYunhornSTSHeartBeatPeriodicityChanged(HeartBeatPeriodicity); + UTIL_TIMER_Create(&YunhornSTSRSSWakeUpTimer, YUNHORN_STS_RSS_WAKEUP_CHECK_TIME, UTIL_TIMER_ONESHOT, OnYunhornSTSOORSSWakeUpTimerEvent, NULL); + STS_SENSOR_Distance_Test_Process(); APP_LOG(TS_OFF, VLEVEL_H, "\r\nRSS Measured Distance=[%u] mm \r\n", (uint16_t)sts_distance_rss_distance); @@ -1772,10 +1774,13 @@ static void OnYunhornSTSSamplingPeriodicityChanged(uint32_t periodicity) /* Update timer periodicity */ #if defined(STS_O6)||defined(STS_O7) - UTIL_TIMER_Stop(&YunhornSTSRSSWakeUpTimer); - UTIL_TIMER_SetPeriod(&YunhornSTSRSSWakeUpTimer, SamplingPeriodicity); - UTIL_TIMER_Start(&YunhornSTSRSSWakeUpTimer); - APP_LOG(TS_OFF, VLEVEL_M,"**************** Sampling Timer Periodicity = %u (sec)\r\n", (SamplingPeriodicity/1000) ); + if (STS_LoRa_WAN_Joined !=0) + { + UTIL_TIMER_Stop(&YunhornSTSRSSWakeUpTimer); + UTIL_TIMER_SetPeriod(&YunhornSTSRSSWakeUpTimer, SamplingPeriodicity); + UTIL_TIMER_Start(&YunhornSTSRSSWakeUpTimer); + APP_LOG(TS_OFF, VLEVEL_M,"**************** Sampling Timer Periodicity = %u (sec)\r\n", (SamplingPeriodicity/1000) ); + } #else UTIL_TIMER_Stop(&YunhornSTSSamplingCheckTimer); @@ -2820,7 +2825,7 @@ void OnRestoreSTSCFGContextProcess(void) #if defined(STS_O7)||defined(STS_O6) - if ((sts_version == sts_cfg_nvm.version)&& (NVM_CFG_PARAMETER_SIZE == sts_cfg_nvm.length)) +// if ((sts_version == sts_cfg_nvm.version)&& (NVM_CFG_PARAMETER_SIZE == sts_cfg_nvm.length)) { STS_PRESENCE_SENSOR_Init(); //STS_PRESENCE_SENSOR_RSS_Init(); diff --git a/STM32CubeIDE/Release/STS_O7.bin b/STM32CubeIDE/Release/STS_O7.bin index 434c1cc..5f53157 100644 Binary files a/STM32CubeIDE/Release/STS_O7.bin and b/STM32CubeIDE/Release/STS_O7.bin differ