Asked — Edited

Waitforchange Command Verses Goto Loop

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!


ARC Pro

Upgrade to ARC Pro

Stay at the forefront of robot programming innovation with ARC Pro, ensuring your robot is always equipped with the latest advancements.

PRO
Synthiam
#1  

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

#2  

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 ARC, 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".

PRO
Synthiam
#3  

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.

#4  

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?

#5  

post 29 on this thread has one

https://synthiam.com/Community/Questions/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 ARC for a change and not jumping over to the EZ-B for anything.

Something like


: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


: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.

#6  

OK, thanks. I get it.

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

#7  

Dave,

The real difference is where is the processing taking place. If I am just looking for a variable change in ARC, 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 ARC. 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 ARC that isn't specific to the hardware on the EZ-B, you should use the WaitForChange.

#8  

@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:)