PWM and HPWM motor control

Hi,

Just wondering,

I just took apart an old ps2 controller and got my 28x1 reading the sticks and had one stick pwm a led.

 

How could I use this to control 2 motors?

Im using the standard 28x1 board and have an l293d plugged in an ready to go.

What i plan to achieve is pwm on forward and backwards on two motors using 2 analogue sticks.

 

Any ideas or help would be great.

to get started…

Here is all the code you need… this is from walter’s RC code so ignore the serial stuff if you are using a wire. Basically, you just need 2 variables from your stick(s). If it is two, it will be the forward/back pot on each stick. If it is just one, see the extra code at the bottom of the post -this is what I use in my single stick transmitter.

 

rcmain:
serin 1,N2400,(85,85,“motornumbers”),b7,b6,bit2,rcmotorspeed 'all you need here is the 2 vars from the 2 sticks, b6 and b7

revon:
select case b6
case 130 to 136
let b6=0
low 1
case >136
let b6=b6-133
let b6=b62
let b6=b6 min 0 max revspeed
low 1
case <130
let b6=b6
2
let b6=255-b6
let b6=b6 min 0 max revspeed
high 1
endselect

select case b7
case 130 to 136
let b7=0
low 2
case >136
let b7=b7-133
let b7=b72
let b7=b7 min 0 max revspeed
low 2
case < 130
let b7=b7
2
let b7=255-b7
let b7=b7 min 0 max revspeed
high 2
endselect
gosub drive
goto rcmain


drive:
pwmduty 1,b6
pwmduty 2,b7
return

 

and if you want to use just one stick:

readadc 0,b1 let b1=b1 min 5 max 250 'Y F/R
readadc 1,b2 let b2=b2 min 5 max 250 'X R/L
let b5= b1-b2+130 min 0 max 255 'left
let b6= b1+b2-130 min 0 max 255’right
let b5=b5+6 min 0 max 255
high 3 pause 3 low 3 pause 1 serout 7,N2400, (85,85,“motornumbers”,b5,b6,bit2,b3)

HPWM?

What does Hewlett-Packard Window Manager have to do with controlling motors?

Or, to put it another way: what’s HPWM?

 

HPWM
Picaxe includes an HPWM output system… It is basically a 4 channel output (i.e. to be connected to a motor driver) using a PWM output. This 4-way set-up gets you away from the problem of having to reverse your pwm pulses, when in reverse, to maintain speed. --Remember a while back the issue of 75% on one direction is 75% off in reverse? At any rate, it is a complicated mess of commands and connections and no one really knows how it works. I said, skip the whole thing and just use relays to reverse your motors!!

Just thought of something…

The high 1 low 1 high 2 low 2 thing… I use relays to reverse my motors and thus do not need to use the hpwm thing. You can still use the code above as a sample, but it will not cut and paste. --All the calculations are correct, though.

BTW–I might just be an idiot, but I could never figure out the HPWm thing.

I think I understand it now…
Could I ask what you have connected to each pin (relavant to code obviously)

Complication?

Did anyone read the manual? It’s a commercial product for heaven’s sake. If you handed over money for it, the least they could do is explain it.

The reversal thing isn’t really complicated. You have one pin which is the PWM and the other is “low”. If you demand reverse, teh pic swaps teh functionality of the pins, so the first becomes “low” and the second befomes PWM.

I guess the H is for H-bridge, then.

HPWM

Thanks great help BaseOverApex.

Maybe a stupid question but, Are all pins capable of pwm on 28x1

I thought it was only a select few.

I was lookin at the pinout and saw more hpwms than pwms so thought i may have to use HPWM.

I suppose Chris has aswered that.

Cheers Chris

Thanks for sharing the code from your amazing bot.

Just a few questions though…

in your code in the section select case b6 you have

low 1

other code

low 1

other code

high 1

I dont know why…

and can your bot reverse aswell (with this code)? I cant see it in your code. Sorry!

 

again, a little confusing…

Walter draws around 40amps durning drive so obviously a lm293 motor driver (at about 1 amp) is a little lacking… Where a lm293 uses 4 pins for forward and reverse (high/ high, high/low, etc.) my setup is a little different. The motor controller I built (oddbot’s design) uses 2 pwm inputs (one for each motor) but to reverse the motors, 2 relays are used. The high 1/low 1 and high 2/low 2 click the relays on and off thus turning reverse on and off. You will have to rearange the code above a lot to get your hpwm to work.

Your to-do list:

Read the manual (Manual 2, Basic commands) and learn the HPWM inside and out -the code, the pins etc.

Wire it to your motor controller –Read the manual (Manual 2, Basic commands). It explains exactly what outputs do what. -It’s almost on the first page, the diagrams with all the pin-outs.

Run test codes and figure out all the basic moves

Use my code on the one-stick thing, or wire both sticks and run some tests with debug to figure out what it is spitting out.

Use my “select case” examples as a “sample” of something your final code will “sorta” look like.

Put it all together --You should expect to spend 1-3 evenings on each of the above steps

–Don’t try to cut/paste the above code IT WILL NOT WORK FOR YOU.

Awesome chris.

Thanks alot,

You even wrote me a todo list… was not expecting that…

I understand your code wont work for me. haha

I know what my sticks are spitting out

a challenge is a challenge and im gona try.

Sorry…
I just checked the pin-outs and it ain’t gunna happen… The 28x1 (standard) board uses pins 4,5,6 and 7 to goto the l293d motor controller. The HPWM outputs are pins 1,2,4 and c2. The 2 regular PWM outputs are pins c1 and c2. It seems there is not going to be any PWM control available to goto your motor controller as long as it is connected to your 28x1 board. However, you can still use the joysticks, you are just going have (only) the standard motor controls --fwd, rev, spinR, spinL, slideR and slideL. It’s just going to take a little time using debug to get the numbers from the ADC’s coming in from your joysticks and translating those numbers into motor movements.

Actually this is going to be simple

Your joysticks center at about 130 on your adc’s. If you use 2 sticks, you will use fwd/back on each stick -each stick controls one wheel. Here’s your code -I would use select case for this…

rightwheel:

if b5 > 125 and < 135 then do nothing -Stick is centerered

if b5 >135 make the wheel go fwd

if b5 < 125 make the wheel go rev

leftwheel:

if b6 > 125 and < 135 then do nothing -Stick is centerered

if b6 >135 make the wheel go fwd

if b6< 125 make the wheel go rev

if you want to use one stick, still use the above code but use this first to mix up/down R/L

readadc 0,b1 let b1=b1 min 5 max 250 'Y F/R
readadc 1,b2 let b2=b2 min 5 max 250 'X R/L
let b5= b1-b2+130 min 0 max 255 'left
let b6= b1+b2-130 min 0 max 255’right
let b5=b5+6 min 0 max 255

 

I already have code that does the above^^^
Thanks again chris…

The problem is i want to pwm relative to how far the stick is pushed,
i.e. push a little, go slow, push all the way go fast.
and as i said i have already done this with an led.

What I woud realy like to know now is how to use pwm and a motor controller…
Any ideas?

Yup…
It’s breadboard time… --gotta get that motor driver off of the 28 board

Pic vs PicAxe

 

I’m afraid I’m not picaxe expert. My field is the raw pic. If I had to guess, I’d say there are only a limited number of the pins on the picaxe which can be PWM. It seems reaonable that the internal softwarewould only be coded to control some rather than all of the pins in order to reduce overhead. In teh raw pic, you can cause your program to address any pin.

Again, if I had to guess, I’d say my first explaination might have been over simple and it may in fact be the case that the PWM pin is always PWM, but it’s paired with a HPWM pin. In this scenario, the for “25% forward” the PWM would output 25% and the HPWM would be set low. For “25% reverse,” thePWM would output 75% and the HPWM would be set high.

I have to stress, I don’t use these chips, so this is 100% speculation. However, if I were setting up a company to make basic bootloaders for PICs? That’s how I’d do it!!!

 

Ass-umptions
Assuming my second scenario above is correct, you could actually drive your relay coil from the HPWM output and your FET from the PWM. It’s an ASSUMPTION, though…