Quote:
Originally Posted by esquared
You could also write this as 1 function by passing the joystick & pwm by reference to your acceleration function. Also, there is nothing wrong with modifying the pwm output variable directly, since the actual pwm output value isn't changed until the Putdata() in Process_Data_From_Master_uP().
|
Ask, and ye shall receive. Below is the code we've been using for 2 years now, you pass in the speed you want, your current speed, and the limit for how much your speed can change by, it takes care of the rest from there.
This goes in a .c file:
Code:
/*******************************************************************************
*
* FUNCTION: motor ramping()
*
* PURPOSE: Makes the motor gradually change speed
*
* PARAMETERS: desired_speed, current_speed,
* change_speed //small interval to change the motor speed
*
* RETURNS: mew_motor_speed the small change from the current motor speed
*
* COMMENTS:
*
*******************************************************************************/
unsigned char ramp_motor_speed(unsigned char requested_speed,
unsigned char present_speed,
unsigned char modified_speed)
{
unsigned char new_motor_speed;
// comparing the desired speed to the current speed,
// if they are the same if moves on, if not it goes to the else
if (requested_speed == present_speed)
{
new_motor_speed = requested_speed;
}
else
{
if (requested_speed < present_speed)
{
// if the requested speed is less then the current speed
// it will subtract the desired speed from the current speed
// to get the interval to change the speed
if ((present_speed - requested_speed) < modified_speed)
{
modified_speed = present_speed - requested_speed;
new_motor_speed = present_speed - modified_speed;
}
else
{
new_motor_speed = present_speed - modified_speed;
}
}
else if (requested_speed > present_speed)
{
// if the requested speed is greater then the current speed
// it will subtract the current speed from the desired speed
// to get the interval to change the speed
if ((requested_speed - present_speed) < modified_speed)
{
modified_speed = requested_speed - present_speed;
new_motor_speed = present_speed + modified_speed;
}
else
{
new_motor_speed = present_speed + modified_speed;
}
}
}
return (new_motor_speed);
}
This goes in a .h file:
Code:
//example to call function
// current_speed = pwm
// // change the motor speed by the change_speed value
// pwm = ramp_motor_speed(desired_speed, current_speed, drive_interval)
//
//variables for above function
#define DRIVE_INTERVAL 6 //limits drive motors to changing 6 pwm values per loop
unsigned char ramp_motor_speed(unsigned char requested_speed, unsigned char present_speed, unsigned char modified_speed);
On a side note, if you're using the drive setup 612 is, their code looks easier to setup and use than ours. Ours is designed open ended since we use it for both arm motors and drive motors.