[P013](sr04 ultrasonic sensor) plugin hang on HI in state mode when NO_ECHO

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
F.J.S.
Normal user
Posts: 3
Joined: 10 Sep 2021, 20:35

[P013](sr04 ultrasonic sensor) plugin hang on HI in state mode when NO_ECHO

#1 Post by F.J.S. » 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

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;
      }
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.

Code: Select all

          if (value != NO_ECHO) {
				if (value < threshold) state = 1;	
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"

Code: Select all

if (value != NO_ECHO) {...
	...
	if (state != switchstate[event->TaskIndex]) { 
		.....
        	.....
        	switchstate[event->TaskIndex] = state;	//---<---here
              	UserVar[event->BaseVarIndex] = state;	//---<--------
I made a dirty patch to the code that only ensures that if there is no object in range, the state goes low.

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;
      }
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.

TD-er
Core team member
Posts: 5513
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: [P013](sr04 ultrasonic sensor) plugin hang on HI in state mode when NO_ECHO

#2 Post by TD-er » 11 Sep 2021, 00:43

Interesting...
too bad I cannot mark a topic as unread to read your code changes with a clear head in the morning.

F.J.S.
Normal user
Posts: 3
Joined: 10 Sep 2021, 20:35

Re: [P013](sr04 ultrasonic sensor) plugin hang on HI in state mode when NO_ECHO

#3 Post by F.J.S. » 13 Sep 2021, 21:25

Hello again.
I think this would be a cleaner change in the code to avoid the "frozen-hi-state" issue.

(line 255 of mega/src/_P013_HCSR04.ino 13-sep-2021) new proposed code.

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) && (value < threshold) )
				state = 1;
			if (state != switchstate[event->TaskIndex]){
				if (loglevelActiveFor(LOG_LEVEL_INFO)) {
					String log = F("ULTRASONIC : TaskNr: ");
					log += event->TaskIndex +1;
					if (value != NO_ECHO){
						log += F(" state: ");
						log += state;
					}else {
						log += F(" Error: ");
						log += Plugin_013_getErrorStatusString(event->TaskIndex);
					}
					addLog(LOG_LEVEL_INFO,log);
				}
				switchstate[event->TaskIndex] = state;
				UserVar[event->BaseVarIndex] = state;
				event->sensorType = Sensor_VType::SENSOR_TYPE_SWITCH;
				sendData(event);
			}
        }
        success = true;
        break;
    }
this is the comparison with the original code (line 255 of mega/src/_P013_HCSR04.ino 13-sep-2021)
ORIGINAL(left)___________________________________________w/changes(right)
Image

I'm still fighting to get the compiler up and running, but this time with Platformio. I am trying to compile a "normal_ESP8266_4M1M_VCC" version to test the changes in the plugin [P013] sr-04

what do you think TD-er? it could work?

F.J.S.
Normal user
Posts: 3
Joined: 10 Sep 2021, 20:35

Re: [P013](sr04 ultrasonic sensor) plugin hang on HI in state mode when NO_ECHO

#4 Post by F.J.S. » 14 Sep 2021, 00:42

Finally compiled :D !!! (with platformIO)

Code: Select all

RAM:   [=====     ]  52.8% (used 43224 bytes from 81920 bytes)
Flash: [========  ]  81.5% (used 850852 bytes from 1044464 bytes)
Environment              Status    Duration
-----------------------  --------  ------------
normal_ESP8266_4M1M_VCC  SUCCESS   00:01:02.324
I tested the changes.
Everything is working fine!
Now when removing the object in front of the SR-04 sensor, the state immediately drops to low. No more problems with maintaining a "false hi state" indefinitely. (this is only valid for "state mode" on the plugin)

I do not know how to propose this change to be considered at GIT.
I hope someone else will find this useful.
Bye.


Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests