Pages: [1]   Go Down
Author Topic: PWM code help, Mapping  (Read 340 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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();
 
Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Husein06,

We'll need just a little more information.

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:
Turn = constrain(Turn, 0, 250)  // ensure range is valid
If Turn<=127 then

  Direction = RIGHT
  Speed = map(Turn, 0, 127, 255, 0)  // PWM 0=off, 255=max on

Else if Turn >=129 then

  Direction = LEFT
  Speed = map(Turn, 129, 250, 0, 255)  // PWM 0=off, 255=max on

End if

Looks like a fun project. Good luck!
Pat.
« Last Edit: December 29, 2012, 09:30:44 pm by patduino » Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 
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);
}
Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

Pages: [1]   Go Up
Jump to: