Map function for robot's speed

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:

// Set Speed of all motor
int 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 motor
unsigned 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.

You're right. Here's a change I made:

set_speed = map(default_speed,0,225,0,100);

// Set Speed of all motor
int default_speed = 50;  // % of max speed, range: 0-100.  Change this only.
     
// do NOT change line below.  It uses 'set_speed' in code
float set_speed = default_speed * ((float(255))/(100)); 


// Calibrated speeds of each motor
unsigned 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?):

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?

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?

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.