multitasking

hippy

Technical Support
Staff member
The 28X1 does not support multi-tasking but it is possible to create a program which appears to be multi-tasking.

It is not necessarily easy to do and depends greatly on what you wish to achieve over-all. All commands execute until they complete so the result may not be as multi-tasking as one would like.
 
It might be easier to use a separate 08M2 chip (they're not really power hungry, large or expensive, at least by most standards) to play the tone/tune, leaving the 28X1 to "focus" on driving the motors.

if you configure the 08M2 to use the hardware serial port in background receive mode (hsersetup), it can receive serial commands in the background while the main program still plays the tune/tone. If the 28X1 wants to activate/change the tune or tone, it could use the hserout command to send a signal to the 08M2. Alternatively, if your project doesn't need complex commands, a simple TTL signal between the two would suffice.
 

sanok

New Member
I have seen an obstacle avoiding robots on youtube. Some are based on 28x1 chip. The servo keps going left and right while driving.
How this can be achieved with flowchart commands? If not flowchart then can someone show me how to do it in basic please?
 

hippy

Technical Support
Staff member
This written in PICAXE Basic will continually move a servo backwards and forwards. You may need to adjust the servo channel and its limit values -
Code:
#Picaxe 28x1

Symbol varA = w0

Do
  For varA = 50 To 200
    Servo 0, varA
    Gosub DoSomething
  Next
  For varA = 200 To 50 Step -1
    Servo 0, varA
    Gosub DoSomething
  Next
Loop

DoSomething:
  Pause 20
  Return
It should be reasonably easy to convert that to flowchart commands. The PAUSE 20 determines how quickly the servo sweeps from left to right and back again.

It is also possible to put more things inside the 'DoSomething:' routine such as direction and speed control for a robot.

For a PICAXE M2 one can use separate START tasks to do the servo control while doing other things, but the 28X1 only has one START so things have to be done within separate routines as above.
 

sanok

New Member
This written in PICAXE Basic will continually move a servo backwards and forwards. You may need to adjust the servo channel and its limit values -
Code:
#Picaxe 28x1

Symbol varA = w0

Do
  For varA = 50 To 200
    Servo 0, varA
    Gosub DoSomething
  Next
  For varA = 200 To 50 Step -1
    Servo 0, varA
    Gosub DoSomething
  Next
Loop

DoSomething:
  Pause 20
  Return
It should be reasonably easy to convert that to flowchart commands. The PAUSE 20 determines how quickly the servo sweeps from left to right and back again.

It is also possible to put more things inside the 'DoSomething:' routine such as direction and speed control for a robot.

For a PICAXE M2 one can use separate START tasks to do the servo control while doing other things, but the 28X1 only has one START so things have to be done within separate routines as above.
Hi Hippy, here is my code according to your example:
symbol varA = w0
Do
For varA = 76 To 200
Servo b.4, varA
gosub buggymove
Next
For varA = 200 To 76 Step -1
Servo b.4, varA
gosub buggymove
next
Loop
pause 20
return

buggymove:
low B.0,B.1 'motor forward
low B.3: high B.2
low B.4,B.5
low B.7: high B.6
pause 1000
low B.2,B.3 'motor stop
low B.6,B.7
pause 500
low B.2: high B.3 'motor reverse
low B.6: high B.7
pause 1000
low B.2,B.3 'motor stop
low B.6,B.7
pause 500
return

But the servo swipes only fraction of what it should (like 75-90) and motors spin continuously in one direction without stopping and changing direction
 

hippy

Technical Support
Staff member
But the servo swipes only fraction of what it should (like 75-90) and motors spin continuously in one direction without stopping and changing direction
Now we are into having time-sliced programs.

If you test things with just a PAUSE 20 in the 'DoSomething' routine the back and forth of the servo should be quite consistent; about 3 seconds in each direction. A larger PAUSE value slows it down, a smaller value speeds it up.

The key to making it work is that the 'DoSomething' routine only lasts for 20 ms or however long you need things to last.

Thus, when you add things like setting motor direction and then pausing for a few seconds the routine won't return, the servo motion will drop to an imperceptible crawl. With the code you have I calculate over 10 minutes for one sweep of the servo from left to right and back again.

You need to code 'DoSomething' in a manner which it doesn't excessively delay, doesn't include any PAUSE command longer than you need to make the servo move at the right rate.

One way to do that is to track time. For the first 1000ms you want the buggy to move forward, to stop for 500ms, reverse for 1000ms and stop again for 500ms. A sequence length of 3000ms, with the motor being set at 0ms, 1000ms, 1500ms and 2500ms.

This, untested, should do that repeatedly while still sweeping the servos at the desired rate -
Code:
Symbol varB = w1

buggymove:
  Select Case varB
    Case 0
      low B.0,B.1 'motor forward
      low B.3: high B.2
      low B.4,B.5
      low B.7: high B.6
    Case 1000
      low B.2,B.3 'motor stop
      low B.6,B.7
    Case 1500
      low B.2: high B.3 'motor reverse
      low B.6: high B.7
    Case 2500
      low B.2,B.3 'motor stop
      low B.6,B.7
  End Select
  Pause 20
  varB = varB + 20 // 3000
  Return
But, before that. You need to figure out why the code you had doesn't stop and reverse the motors. Write a program which just tests the motors, ignore the servo, ignore trying to make motors and servo work together for now.

There are three possibilities. Either the code isn't right, you are not setting the correct HIGH and LOW states for the motor pins, there is some conflict breaking things, or the motors turning on is causing the PICAXE to reset and start again, which turns the motors on, causes a reset, and you never escape that endless cycle.

It would be worth letting us know what hardware you have, the motor controllers used and how everything is wired.

I do note you are using B.4 as the servo signal but that also appears to be used as one of the motor control signals. This may be the conflict which is causing things not to work.
 

sanok

New Member
Yes the pin B.4 was the problem. I have excluded it from motor sequence and now motors and servo work at the same time, however servo sweeps slow, 4seconds from one end to the other. Changing value of pause to 2 makes sweep 2sec.
Here is simplified code where motors just run forward without any stops.
When simulating it I noticed that program runs to line 5 (gosub buggymove) only and back to beginning.

Code:
symbol varA = w0
Do
  For varA = 76 To 200
    Servo b.4, varA
    gosub buggymove
  Next
  For varA = 200 To 76 Step -1
    Servo b.4, varA
    gosub buggymove
  next
Loop
return
buggymove:
Symbol varB = w1
  Select Case varB
    Case 0
      high B.3: low B.2 'motor forward
      high B.7: low B.6
   
  End Select
  Pause 20
  varB = varB + 20 // 3000
  Return
 
Last edited:

hippy

Technical Support
Staff member
A servo sweep of 4 seconds with PAUSE 20 is not unsurprising - my prediction of 3 seconds was really just ball-park. Reducing to 2 seconds with PAUSE 2 is less of a reduction than I would have expected but that can be worked round.

One option for when things are slow is to use SETFREQ to have the PICAXE run faster, the X1 will run servos correctly at 4MHz and 16MHz so you could try adding a SETFREQ M16. That will however require altering other timings in the program.

An alternative, possibly better, and somewhat easier path, is to step the servo quicker. This can be done by adding a STEP of other than 1 to the two FOR commands in the main loop -
Rich (BB code):
For varA = 76 To 200 Step 2
For varA = 200 To 76 Step -2
The larger you make the step number the faster it will sweep.
 
Top