Go Down

### Topic: PWM code help, Mapping (Read 1 time)previous topic - next topic

#### husein06

##### Dec 30, 2012, 02:30 am
How do I do this?

So the range of my variable "Turn" is from 0-250.
I have 128 is the (forward, zero point), when it's from 0-127(right), I want it to PWM 127 being slow and increasing to 0(fastest).
When its from 129-250(left), I want it to PWM 129 being slow and increasing to 250(fastest).

drive_forward();
drive_Left();
drive_right();

#### patduino

#1
##### Dec 30, 2012, 03:27 amLast Edit: Dec 30, 2012, 03:30 am by patduino Reason: 1
Hi Husein06,

Are you sure your range isn't 0-255?  That's more typical.  Just checking...

Also, when you say fast and slow, i assume you mean forward speed.  I didn't see any discussion about going in reverse.

Let me take a crack at restating what you want the program to do:

• If Turn is between 0 and 127, you want the platform/robot/vehicle to drive to the right, and values between 129 and 250 to drive to the left.

• You also want Turn to encode the forward speed: 0=FASTEST, gradually changing to 127=SLOWEST, and again 129=SLOWEST, gradually increasing to 250=FASTEST.

• If Turn is 128, stop.

If you are driving the motors with PWM signals, they'll want values from 0=off to 255=max on.  You'll need to make sure you know what values to pass them and fix the code below, if necessary.

Here's some pseudo code for you.  Does this look right?

Code: [Select]
`Turn = constrain(Turn, 0, 250)  // ensure range is validIf Turn<=127 then  Direction = RIGHT  Speed = map(Turn, 0, 127, 255, 0)  // PWM 0=off, 255=max onElse if Turn >=129 then  Direction = LEFT  Speed = map(Turn, 129, 250, 0, 255)  // PWM 0=off, 255=max onEnd if`

Looks like a fun project. Good luck!
Pat.
There are 10 types of people in the world, those who understand binary, and those that don't.

#### husein06

#2
##### Dec 30, 2012, 04:10 am
Thanks for the info, so I incorporated what you have into mine.

Is this correct?

The whole goal of this is I'm trying to make the robot drive slow when it's close then become fast when it's far away(aka 0 and 250) prpotional to the distance and PWM.

Code: [Select]
`  if ( turn == 128 ){  drive_forward();    analogWrite(PWMA, 250);// speed control  analogWrite(PWMB, 250);// speed control  }if (turn <= 127){speed=map(turn,0,127,255,0);drive_right();analogWrite(PWMA, speed);analogWrite(PWMB, speed);}if (turn >= 129){speed=map(turn, 129, 250, 0, 255);drive_Left();analogWrite(PWMA, speed);analogWrite(PWMB, speed);}`

#### patduino

#3
##### Dec 30, 2012, 04:30 am
Sure,

I don't understand the ==128 case.  What are you trying to do if turn is 128?  I thought that was the stop case.

Also, what are PWMA and B?  Do these pins drive motors?  What motors?
There are 10 types of people in the world, those who understand binary, and those that don't.

#### husein06

#4
##### Dec 30, 2012, 04:37 am

Sure,

I don't understand the ==128 case.  What are you trying to do if turn is 128?  I thought that was the stop case.

Also, what are PWMA and B?  Do these pins drive motors?  What motors?

EXAMPLE:
(Thinking of an X-Y plane)The 128 is proportional to angle 90 degrees and signals to drive forward, and anything from 129 to 250 is 90 degress to 0 degrees and signals drive right, and 0 to 127 is 90 degrees to 180 degrees and signals drive left.

PWMA and PWMB is PWM to motor driver so I can make the motors drive proportional to the "turn" value:
http://www.pololu.com/catalog/product/1213
and motors:
http://www.parallax.com/Store/Accessories/MotorServoControllers/tabid/160/List/0/SortField/4/ProductID/587/Default.aspx?txtSearch=ball+caster

or do you recommend just driving the motors without PWM?

#### patduino

#5
##### Dec 30, 2012, 05:02 am
You are controlling both motors with the same value. To turn, don't you want one motor to go faster than the other, or are you steering it somehow?

I'm still trying to understand the speed control logic. 80* = SLOW, 90* = 250 (fast), 91* = SLOW.  Is that what you intend? Do we have the logic reversed?

I don't have any problem with PWM motor control.  It's really the best way to control a DC motor like the parallax one you are using (looks like a good one).  Parallax recommends the following controller http://www.parallax.com/tabid/768/List/0/ProductID/64/Default.aspx.

I'm not familiar with it (looks expensive), or the Polulu driver you selected. Note that their website says:

Quote
Since this board is a carrier for the Freescale Semiconductor MC33926 H-bridge, we recommend careful reading of the MC33926 datasheet
There are 10 types of people in the world, those who understand binary, and those that don't.

Go Up