Waitforchange Command Verses Goto Loop

 
#1

I remember DJ commenting a few weeks ago that using the waitforchange command takes just a little bit more resources then a loop using a GoTo command. It seems that most proficient script writers here are under the impression that it's the other way around. I thought so too until I read DJ's comment. Did I misunderstand what DJ was saying and the waitforchange command is really most efficient way to go?

Also; if the waitforchange is truly a loop that runs in the background and constantly checks status of the $variable do we need to insert a Sleep command like a GoTo loop to help with freeing up resources?

Any thoughts or answers? Thanks!

#2

It depends on the usage. Wait for change is indeed a loop that runs in the background.

#3

This is only based on my understanding and logic...

There is a limited bandwidth to the EZ-B over wifi and the EZ-B has a processor that is many time smaller than the one in your computer. It is easy for your computer to overpower this processor. That being said, the way I use the WaitForChange is that I first think about where the resources are going to be consumed. I dont use a WaitForChange when querying an ADC port for example. I would rather control how quickly the commands are being sent so as to prevent lag with other components. I use a loop with a sleep command to limit this. If the processing is taking place on the computer, for example between roborealm and EZ-Builder, I use a waitforchange without much concern for how many resources are being consumed. The PC has multiple processor cores and can easily handle something running in the background without much concern.

I hope first that I understand things correctly and then I hope that I offered an explanation of "depends on the usage".

#4

You got it - it's using wait for change with commands querying the ezb. It's the bandwidth that is the constraint. The ezb can respond and operate very quickly - the limitation is the wifi connection speed and latency.

Using wait for change in just a variable is great and very effecient.

#5

Thanks for taking the time to explain. That makes sense.

Could you give me an example of using wait for change so I can try to understand that concept better?

#6

post 29 on this thread has one

http://www.ez-robot.com/Community/Forum/Thread?threadId=7667&page=3

This is two applications on the PC communicating with each other over TCP port 6666. There is really no latency here and no real network traffic. This is querying a variable in EZ-Builder for a change and not jumping over to the EZ-B for anything.

Something like

Code:


:beginofloop
waitforchange(adc0)
if(adc0>100)
do something
endif
goto(beginofloop)



This would be bad. This is where you would want to use another loop instead of the waitforchange because the wait for change has no built in delays. It should look more like this

Code:


:beginofloop
$thevalue = GetValue(ADC0)
:waitforchangeloop
if(adc0 <> $thevalue)
do something
endif
sleep(1000)
goto(waitforchangeloop)
goto(beginofloop)



The second would query the port on the EZ-B every second instead of multiple times a second.

**This code is untested and is simply for demonstration purposes.

#7

OK, thanks. I get it.

So the code would be having the Wait For Change command just watch a stated Variable in Ez Builder for change. This command would have to be in a loop with an If statement that would trigger an action when the variable changes. Winky

#8

Dave,

The real difference is where is the processing taking place. If I am just looking for a variable change in EZ-Builder, the WaitForChange is great. If I am querying a port on the EZ-B, the waitforchange command isn't so great. The reason is that there is no delay in the command. it will run say 1000 times a second (just an estimate). If I query a port on the EZ-B that many times, what do I gain? What do I loose? The gain would be nothing and the lose would be performance from anything else on the V4. It would saturate or flood the Wifi connection with useless traffic.

If I determined that I want to have this port queried every second as that is fast enough, I would have to insert a delay in the loop. you cant do that with the WaitForChange command, but with a loop and sleep command it can be done. The sleep prevents the wifi connection from becoming saturated to the point that your other ports can't respond to the queries being asked of them.

This isn't an issue with looking at a variable for it to change in EZ-Builder. It is within its own application on its own machine with no network latency. It is just hitting memory on your computer which is very fast. Network and especially wifi is the slowest part of the chain. It really depends on where the processing is happening as to what the best use is. As a rule, I would say that if you are querying something on the EZ-B (digital, analog, I2C, Serial, Temp, Voltage...) I would use the loop method. If your doing something within EZ-Builder that isn't specific to the hardware on the EZ-B, you should use the WaitForChange.

#9

@David... when you first came on here I think I helped you once sort out a problem you were having with a certain script.... since then you have pretty much leap frogged over everyone on here.... Anyway, thanks (and DJ too) for that awesome tidbit of info. You just helped me streamline my code....

Man this is a great place to learn how to become a robot genius...
Richard Smile

#10

Richard, Thanks for the kind words. I dont feel like I have leapfrogged but just have a programer background to leverage off of. I would love to have Dave's robot manufacturing skills and a lot of the skills you possess. The thing that I love about the community is that we can all leverage off of each others strengths to all have some incredible robots (along to a huge thanks to DJ and the EZ-Robot team for allowing this to happen).

I also think you helped me wrap my head around what was happening in EZ-Builder (along with others here). Understanding is key with scripting and with pretty much everything that we do here. Fear to learn is the thing that keeps most people back. This community doesn't have that fear which makes it a very powerful community.