Go Down

### Topic: Map function for robot's speed (Read 233 times)previous topic - next topic

#### project_science

##### Aug 15, 2017, 01:19 am
Hi,

I've built a 4 wheel robot.  It's very basic, each motor has it's own 6v hobbyist motors.

Anyhow, the motors have the speed range of 0-255, and I'd like to convert this to a %, 0-100.

The following code is (not calibrated) to drive the robot straight:

Code: [Select]
`// Set Speed of all motorint default_speed = 50;  // % of max speed, range: 0-100.  Change this only.int set_speed = default_speed * (255 / 100);  //use 'set_speed' in code.  Do NOT change// Calibrated speeds of each motorunsigned int M1 = set_speed;unsigned int M2 = set_speed;unsigned int M3 = set_speed;unsigned int M4 = set_speed;`

Does with make sense to use?

Also, how could I calibrate the robot to actually drive straight, as it doesn't right now - it will curve.

#### Delta_G

#1
##### Aug 15, 2017, 04:56 am
You'll be off by a little at least because 255/100 is 2.
If at first you don't succeed, up - home - sudo - enter.

#### project_science

#2
##### Aug 16, 2017, 08:25 pm
You're right.  Here's a change I made:

Code: [Select]
`set_speed = map(default_speed,0,225,0,100);// Set Speed of all motorint default_speed = 50;  // % of max speed, range: 0-100.  Change this only.     // do NOT change line below.  It uses 'set_speed' in codefloat set_speed = default_speed * ((float(255))/(100)); // Calibrated speeds of each motorunsigned int M1 = set_speed;unsigned int M2 = set_speed;unsigned int M3 = set_speed;unsigned int M4 = set_speed;`

I can't help but think there's an easier way.  I was thinking about using the map function ((https://www.arduino.cc/en/Reference/Map), but I couldn't figure out how to use it since there's no sensor input into the argument.  So this is as close as I got (or maybe it's correct?):

Code: [Select]
`set_speed = map(default_speed,0,225,0,100);`

But after reading on the map URL that "Fractional remainders are truncated, and are not rounded...", perhaps the solution I provide above is correct?

#### Power_Broker

#3
##### Aug 19, 2017, 03:57 am
I'd stay away from floats it at all possible (slows down the 8-bit processor a lot). I mean, do you really need to convert the number to a percentage? Who cares how the throttle value is represented as long as the robot behaves as required?
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
- Nikola Tesla

#### Due_unto

#4
##### Aug 19, 2017, 04:13 am
Quote
Also, how could I calibrate the robot to actually drive straight, as it doesn't right now - it will curve.
Even identical motors will run at slightly different speeds at a given voltage. Most of this is due to minor mechanical and electrical variations between motors and variations in load. A popular method (IMHO) is feedback from each motor that represents the motor speed and is compared to a target number which is then used to vary the PWM to the motor to maintain a speed setting.
Do not look into laser with remaining good eye.

Go Up