Parallel Task, Pause timing off

I needed to a code a robot which can collide to its obstacle. I am using the SRF005 Ultrasonic Sensor to measure the distance. However in my code down below, my pauses in parallel task are horribly off, like pause 500 is 4-5 second long. Could anyone please explain this. Also if my range goes above 40 cm, then during debug I can see the range going 0 then 45, 0 then 46 and so on. I think that this is related to the pause statement being too short for long range, but still I want your help.

#SIMTASK ALL
symbol trig = B.1
symbol echo = C.4
symbol range = w1
symbol currentsense = w0
#define BuzzerPin = C.0

start0:

pulsout trig,2
pulsin echo,1,range
pause 20
let range = range * 10 / 58
debug
readadc10 3,currentsense
goto start0

start1:
pwm B.2,255,1
pwm B.4,255,1
pause 250
low B.2
pause 250
goto start1

start2:
do while range <25
suspend 1
high B.2
high B.4
loop while range <25
resume 1
goto start2

start3:
if currentsense > 180 then
pause 10
if currentsense > 180 then
suspend 1
suspend 2
low B.2
low B.4
high C.1
high C.2
pause 100
low C.1
pause 10
low C.2
resume 1
resume 2
else
goto start3

endif
endif
 

Technical

Technical Support
Staff member
Debug is a really long blocking command. Hence when it is processing in one task nothing is happening in the other.

pulsin can also block (until it times out or is finished)
 

hippy

Technical Support
Staff member
There is also the issue that you are using multi-tasking so, between the PULSOUT which initiates an ultrasound reading and the PULSIN which reads the result, the PICAXE will execute other tasks which may cause the ultrasound reading to be missed.

You would either need to suspend the other tasks while taking an ultrasound reading or use the single pin mode so the PICAXE has no opportunity to run any other tasks while initiating and obtaining a reading.
 
I don't know because I am using Parallel Tasking where you can't use setfreq, and I don't know what Speed it uses. Also I am using the PICAXE 14M2, in parallel mode.

Thanks
 
There is also the issue that you are using multi-tasking so, between the PULSOUT which initiates an ultrasound reading and the PULSIN which reads the result, the PICAXE will execute other tasks which may cause the ultrasound reading to be missed.

You would either need to suspend the other tasks while taking an ultrasound reading or use the single pin mode so the PICAXE has no opportunity to run any other tasks while initiating and obtaining a reading.
So do I just put the mode pin on ground and then only use the one pin for trig and echo. So would I just use

start0:

pulsout trig,2
pulsin trig,1,range
pause 20
let range = range * 10 /58
debug range
goto start0


I could get rid of the debug command if needed.

Thanks
 

hippy

Technical Support
Staff member
For single pin mode I believe it is connect Mode to 0V, leave the Echo pin unconnected, and connect Trigger to a bi-direction I/O pin.

You can then use the "Ultra pin, wordVar". No need to do the range calculation as that is done within the Ultra command,
 
But doesn't the ultra command only work in 4mhz. Would this work in parallel mode. What is the frequency of PICAXE in parallel mode.

Also is readadc also a blocking command because I need this, to check when the motor of the robot have been stalled due to it colliding with an wall. The way I am currently doing is that I have a 1ohm resistor connect to the ground of my H-Bridge, and a pin between this connection goes to pin B.3 on my PICAXE. Then I have readadc to check whether the value of the pin is higher than a value if so then it stops the motor from working.

Pretty much this part of the code:

start3:
if currentsense > 180 then
pause 10
if currentsense > 180 then
suspend 1
suspend 2
low B.2
low B.4
high C.1
high C.2
pause 100
low C.1
pause 10
low C.2
resume 1
resume 2
else
goto start3

Thanks
 
Last edited:
Thanks for your help, I went through and look at the resources given. So based on the code I have given above, it means that my PICAXE 14M2 is currently processing all my command a 4mhz since I have 4 start commands.

So what would the frequency drop to if I add another start label. Eg start 4 so I would have a total of 5 start tasks.

Another thing, so a reason for my very much delayed pause timing could be because I have blocking commands like debug. Or is it something else.

One last thing is readadc, I blocking command in the manual it does say that read temp is blocking so does that mean readadc also is a blocking command.

Thanks for your help. Seriously I have learned a couple of things about PICAXE.
 

AllyCat

Senior Member
Hi,

Another thing, so a reason for my very much delayed pause timing could be because I have blocking commands like debug. Or is it something else.

One last thing is readadc, I blocking command in the manual it does say that read temp is blocking so does that mean readadc also is a blocking command.
DEBUG is a very S-L-O-W command to execute, so best avoided in any time-critical application.

READTEMP is a special "One Wire" serial data bus command which can take up to 700 ms to execute and "blocks" for this period of time (but some workarounds do exist). However READADC takes only about 1 ms to execute, almost regardless of the clock frequency, (where the option to SETFREQ is available, but it's not with multitasking) so is NOT blocking. It appears that recent M2 chips can accept up to 8 tasks (with I guess an effective clock frequency of ~2 MHz), except for the 08M2 (only 4 tasks). But personally I've never found the need to use even two tasks (or to be precise, I find its restrictions too limiting).

Cheers, Alan.
 

hippy

Technical Support
Staff member
All PICAXE commands are blocking to some degree in that they will stop other commands from executing until they have completed. Some are those which won't continue for some time until they have completed, such as READTEMP, PULSIN, PULSOUT, SEROUT and IROUT. The ones which are generally deemed "blocking" in the PICAXE world are those which will wait forever if a required external stimulus is not provided, such as SERIN and IRIN.

As to execution speeds when multi-tasking is in operation; the PICAXE itself will be running at 16MHz but executing code which has been written as if the code were running at 4MHz. Hence PAUSE 1000 still lass a second without having to adjust the number as one would for a single task program running at 16MHz.

What the effective execution speed is is hard to define. In the simplest of terms the PICAXE is executing commands at the 16MHz rate, but how often any particular task gets one of its command executed will depend on how many tasks there are and what they are doing.

In this case the ultrasound reading PULSIN may block for up to 163ms if no pulse comes back or is missed, the DEBUG will take a good chunk of time as well. This reduces the effective execution rate of the other tasks.

If one is spending most of one's time in that PULSIN or DEBUG then other tasks will have far less time in which they can run. It can be that in some cases other tasks slow to a crawl while one task hogs the execution time. This can also be observed by the 'time' variable incrementing far slower than every one second even in single-task programs.

PAUSE commands aren't blocking in multi-tasking mode, they effectively suspend the task they are in while being executed, but they are less fine-grained than they would be in a single tasking program.

There are ways to make everything more fluid and responsive, but once one is doing that, there is often not much to be gained over having a single linear single-task program.

Multi-tasking isn't a panacea. It can be very useful for some programming problems but will be less useful for others, and that can be the case where there is prolonged interaction with external hardware as here. In those case a single-task program can be more appropriate.
 
Top