[P013](sr04 ultrasonic sensor) plugin hang on HI in state mode when NO_ECHO
Posted: 10 Sep 2021, 23:41
Hi everyone.
The complement [P013] (ultrasonic sensor sr04) hangs in the state = "HI" working on "state mode" when the sensor ping timeout (NO_ECHO). I think this behavior is not correct since the state remains in "hi" when there is no longer any object in front of the sensor.
I think this will be better understood by looking at the plugin code:
at line 255 of mega/src/_P013_HCSR04.ino
if an object is in front of the sensor at a short distance (value < theshold) then state -> "hi", but if the objets move out of the sensor range, value = "NO_ECHO" (defined as 0) all the code that make changes on state will not run and de state will continue on "hi" when no objet in fron of sensor.
only when the object is again in front of the sensor at a distance such that value> = threshold, we can reach the code that changes state and finally it will go "low"
I made a dirty patch to the code that only ensures that if there is no object in range, the state goes low.
I think this part of the program should be reviewed by someone with more knowledge to clean up the code.
When NO-ECHO, is it necessary to log "Error:" 10 times per second?
I'm trying to compile these changes into a copy of the Master branch with the Arduino IDE, but I'm having a headache with libraries. I still couldn't taste the change. I am interested in your opinion about it, thank you very much.
The complement [P013] (ultrasonic sensor sr04) hangs in the state = "HI" working on "state mode" when the sensor ping timeout (NO_ECHO). I think this behavior is not correct since the state remains in "hi" when there is no longer any object in front of the sensor.
I think this will be better understood by looking at the plugin code:
at line 255 of mega/src/_P013_HCSR04.ino
Code: Select all
case PLUGIN_TEN_PER_SECOND: // If we select state mode, do more frequent checks and send only state changes
{
int16_t operatingMode = PCONFIG(0);
int16_t threshold = PCONFIG(1);
if (operatingMode == OPMODE_STATE) {
uint8_t state = 0;
float value = Plugin_013_read(event->TaskIndex);
if (value != NO_ECHO) {
if (value < threshold) state = 1;
if (state != switchstate[event->TaskIndex])
{
if (loglevelActiveFor(LOG_LEVEL_INFO)) {log...}
switchstate[event->TaskIndex] = state;
UserVar[event->BaseVarIndex] = state;
event->sensorType = Sensor_VType::SENSOR_TYPE_SWITCH;
sendData(event);
}
}
else {
if (loglevelActiveFor(LOG_LEVEL_INFO)) {log...}
}
}
success = true;
break;
}
Code: Select all
if (value != NO_ECHO) {
if (value < threshold) state = 1;
Code: Select all
if (value != NO_ECHO) {...
...
if (state != switchstate[event->TaskIndex]) {
.....
.....
switchstate[event->TaskIndex] = state; //---<---here
UserVar[event->BaseVarIndex] = state; //---<--------
Code: Select all
// src/_P013_HCSR04.ino @line 255
case PLUGIN_TEN_PER_SECOND: // If we select state mode, do more frequent checks and send only state changes
{
int16_t operatingMode = PCONFIG(0);
int16_t threshold = PCONFIG(1);
if (operatingMode == OPMODE_STATE)
{
uint8_t state = 0;
float value = Plugin_013_read(event->TaskIndex);
//if (value != NO_ECHO) //---no more
//{
if ( (value < threshold) && (value != NO_ECHO) ) //---add condition
state = 1;
if (state != switchstate[event->TaskIndex])
{
if (loglevelActiveFor(LOG_LEVEL_INFO) && (value != NO_ECHO)) {//---add condition
String log = F("ULTRASONIC : TaskNr: ");
log += event->TaskIndex +1;
log += F(" state: ");
log += state;
addLog(LOG_LEVEL_INFO,log);
}
switchstate[event->TaskIndex] = state;
UserVar[event->BaseVarIndex] = state;
event->sensorType = Sensor_VType::SENSOR_TYPE_SWITCH;
sendData(event);
}
//}
if (value == NO_ECHO) { //---add condicion
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
String log = F("ULTRASONIC : TaskNr: ");
log += event->TaskIndex +1;
log += F(" Error: ");
log += Plugin_013_getErrorStatusString(event->TaskIndex);
addLog(LOG_LEVEL_INFO,log);
}
}
}
success = true;
break;
}
When NO-ECHO, is it necessary to log "Error:" 10 times per second?
I'm trying to compile these changes into a copy of the Master branch with the Arduino IDE, but I'm having a headache with libraries. I still couldn't taste the change. I am interested in your opinion about it, thank you very much.