Let's Make Robots!

Picaxe PWM code help

Hello LMR,

I have a throughly noob question or two.

I have yet to use my 20M2 board that has been sitting around waiting for a project. A friend is trying to fix an "Aquabot" for his wife's boss. So I figured that it would come in handy on this project, the only problem being I have never programmed anything for picaxe (or any uC) before. It's a pool cleaning robot that runs for about 7 hours at a clip, it has two motors, one is the impeller which is always on, and the other is a stepper, which I need to turn on and run forward for a random amount of time between lets say 2 and 6 minutes and then reverse for another random 2 to 6 minutes.

I have a 555 pulse generator connected to an 'EasyDriver' http://www.schmalzhaus.com/EasyDriver/ to control the stepper. At first I was trying to avoid using the picaxe altogether but after getting the stepper running from the easydriver which was recieving pulses from the 555, I realized that you can't just take and dump high frequency pulses from the start, you need to ramp it up, or else the stepper locks up. So I connected a LDR accross my 555 timer's frequency adjust pot. and viola! Next I added an LED to pin b.2 on my 20M2 board for the pwm signal. It is connected from pin b.2 to anode, cathode to resistor to GND. My plan is to pwm the led that I have pointing at the LDR and use it as a optoisolator/ digital pot/ gas pedal for the picaxe to control the frequency of pulses for the easydriver.

So my idea of the code is like something this:

 

main

toggle(?) direction pin (to easydriver input)

pwm pin b.2 LED from off to on over a 3 second period and hold LED on

pause for a random time between 2 and 6 minutes

turn off LED

goto main

 

I also plan on adding an output pin to put the easydriver into sleep mode in the future.

I know the 20m2 is a bit of overkill here and if I had an 08m I's use that... but it is what it is I guess.

Any pointers with the pwm code or any of this would be greatly appreciated, meanwhile I'm back to the picaxe manuals.

Thanks in advance!

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Symbol stepperDirection = b.0
Symbol LED = b.2
Symbol t = w3
Symbol delaytime = w4
Symbol countdown = 75 'number of mS removed from pause in rampup
Symbol maxdelay = 1000 'maximum pause in rampup routine 

main:
  toggle stepperDirection 

  gosub rampup 

  'w0 is a word variable 2 bytes. It will hold up to 65535.
  b0 = time
  random b0
  b1 = b0 // 6 

  do while b1 < 2 OR b1 > 6 'make sure we don't have less than a
    random b0       '2 minute pause.
    b1 = b0 // 6
  loop 

  'this will pause between 2 minutes and 6 minutes
  for b2 = 0 to b1
    pause 60000 
  next b2 

  LOW LED

goto main 

rampup:
  t = 0 

  do while t < maxdelay
    HIGH LED
    pause 10
    LOW LED
    delaytime = maxdelay - t
    pause delaytime
    t = t + countdown
  loop
return

I got it going... my timing abilities are lax as well.

EDIT: oops I meant to post this down below...

 

Yep. That's the part I meant. Thanks for clairifying.

I added your loop into the code I posted (which was pieced together from picaxe docs & forum) and came up with this:

 

main:

for b1=0 to 255

pwmout b.1,255,b1

pause 20

next

 

let w0 = time       'w0 is a word variable 2 bytes. It will hold up to 65535.

random w0

let w1 = w0

 

do while w0 < 20000 'make sure we don't have less than a

 let w0 = time  '2 minute pause.

 random w0

 let w1 = w0

loop

 

let w2 = w1 * 6     'this will pause between 2 minutes and 6 1/2 minutes

 

pause w2

 

for b1=255 to 0 step -1

pause 20

pwmout b.1,255,b1

next

 

toggle c.1

 

goto main

 

I timed this a few times, with the motor and all attached,and it never seems to reverse. I figure I goofed it up somehow though.

Thanks again for your help. 

 

 

Thanks Chuck, I'm using a 555 to make the pulses though and just using the picaxe to toggle directions and throttle the 555 with the LED & LDR

I figured it out though...

Now I just need to crunch the numbers and find a way to limit the random timing to between 2 and 6 minutes.

 

main:

for b1=0 to 255

pwmout b.1,255,b1

pause 20

next

let w0 = time ; seed w0 with time value

random w0 ; put random value into w0

let w1 = w0*5000

Pause w1

for b1=255 to 0 step -1

pause 20

pwmout b.1,255,b1

next

toggle c.1

goto main

Symbol direction b.0
Symbol LED b.2
Symbol t b4
Symbol countdown = 25 'number of mS removed from pause in rampup
Symbol maxdelay = 1000 'maximum pause in rampup routine

main:
  toggle direction

  gosub rampup

  'w0 is a word variable 2 bytes. It will hold up to 65535.
  w0 = time
  w1 = random w0

  do while w1 < 20000 'make sure we don't have less than a
    w1 = random w0    '2 minute pause.
  loop

  pause w1 * 6 'this will pause between 2 minutes and 6 1/2 minutes

  LED = 0

goto main

rampup:
  t = 0

  do while t < maxdelay
    LED = 1
    pause maxdelay - t
    t = t + countdown
  loop
return

I just typed this up. I did not run it through the simulator. One thing I am unsure about is your whole setup. I am guessing the impeller is on a pod that has its direction controlled by the stepper. I suppose the program would cause a rather aimless wandering, if that is what you are after.

In the rampup routine you need to turn the LED off so you can turn it on again. After LED = 1 there should be a pause 100 and then LED = 0.

 

rampup:
  t = 0

  do while t < maxdelay
    LED = 1
    pause 100
    LED = 0
    pause maxdelay - t
    t = t + countdown
  loop
return

 

Just saw this after I posted my last post... Looks complex, but I will see what I can gleam from it. Thanks for the time you put into this!

The impeller is always on and one direction, no control necessary. The stepper controllers the tracks/scrubbers/flippers and basically just move the thing forward or backwards. I figure there are so many random things influencing this thing underwater (pool jets, water depth, etc.) that a random means of control is the best. At first I was considering mounting a tilt switch to reverse after climbing the wall long enough but I think that it might be overkill.


I still lack a bit in commenting my code. :)

Oh no, you code commenting was great, it's my understanding that is lacking. The code I just posted was my first stab ever @ programming in like 20 years (and that was qbasic on dos) so bear with me here.

I kind of understand your code... What is the reason for using software PWM rather than the PWMOUT? If I am even understanding it correctly. I think the random code bit might be just what I needed... A little loop / checking action.

Thanks!

do you mean the rampup routine?

The rampup routine will, as I envision it, flash the LED at quicker intervals over a period of time. It is just a concept. I don't even know if it would work properly. I am sure there are better ways of doing it. I just don't currently see them. :)