pwm mapping 0-100% not working????

Hi,

I've got a 4 wheel robot, and I built a toggle button for the robot between 2 different speeds (manual = max 100% power, autonomous = 40% max power) by the push of 1 button. Pretty basic.

I can make it move how I want, but I'm not sure I'm using the pwm function properly. I know that the range of values for power you can deliver to a motor is '0 - 255', and I'm trying to use a mapping function to put this on a scale of '0 - 100' percent.

The issue begins in the the motor class function Setup_Motors() where I map the pwm.

// scale % into pwm range
   int pwm = map(speed, 0, 100, 0, 255);  
             // takes range of 0-255 and maps it to 0-100 for variable 'speed'

In the private part of the class, I can't deliver the manual part as a % of 'speed'. I have to run it as '255', and not 100. Similarly, I cannot for the autonomous part run as a % of 'speed'. I have to do a percent of '255'. For example, the program does not interpret speed * 0.4 to mean 40% of max speed.

private:
  
static const int speed = 100; // % of maximum speed, used for mapping
static const int manual_speed = 255; // maximum speed
static const int autonomous_speed = 255 * 0.4 ; // % of manual speed

I feel that I'm either not using the pwm properly, or that I'm being redundant (although it DOES work how it is programmed here). When the code is toggled from manual to autonomous, the class function will take its respective speed into the function (which are all called from the main loop).

Here is the code snip-it:

class Motors
{
  public:
  
    void Setup_Motors()
    {
      
       // scale % into pwm range
       int pwm = map(speed, 0, 100, 0, 255);  
             // takes range of 0-255 and maps it to 0-100 for variable 'speed'
                                                 
     // front wheels
     Motor_Left_Front.setSpeed(pwm);
     Motor_Right_Front.setSpeed(pwm);

     // rear wheels
     Motor_Left_Rear.setSpeed(pwm);
     Motor_Right_Rear.setSpeed(pwm);


// Manual Mode Speed
  void Manual_Speed()
   {
     // front wheels
     Motor_Left_Front.setSpeed(manual_speed);
     Motor_Right_Front.setSpeed(manual_speed);
                            
     // rear wheels
     Motor_Left_Rear.setSpeed(manual_speed);
     Motor_Right_Rear.setSpeed(manual_speed);
   }
  
  // Autonomous Mode Speed
  void Autonomous_Speed()
  {
    // front wheels
    Motor_Left_Front.setSpeed(autonomous_speed);
    Motor_Right_Front.setSpeed(autonomous_speed);
                            
    // rear wheels
    Motor_Left_Rear.setSpeed(autonomous_speed);
    Motor_Right_Rear.setSpeed(autonomous_speed);
  }
  
  private:
  
    static const int speed = 100; // % of maximum speed, used for mapping
    static const int manual_speed = 255; // % of maximum speed
    static const int autonomous_speed = 255 * 0.4 ; // % of manual speed
};
     
    }

Where is the setSpeed function? Wouldn't it make more sense for all the mapping to be done there? If you want to use percentages. Down at the bottom where you are setting up constants it looks like you are using values from 0 to 255.

I don't really understand where (or why for that matter) you want to include anything in the range 0 to 100.

Here is the code snip-it:

http://snippets-r-us.com/

Post all your code. I don't see how manual_speed will ever be anything other than 255, as it is const.