http://www.chiefdelphi.com/forums/sh...ad.php?t=53988
I actually posted about a problem with our code trying to achieve the same purpose....this code works, but is a little sloppy compared to others.
Code:
int Incrementation_Code(int motor_curr, int motor_prev, int padd, int max_step)
{
int change = 0;
int RetVal;
change = motor_curr - motor_prev;
if (change >= padd)
{
//Accelerate
if (change > max_step)
{
RetVal = motor_prev + max_step;
printf("Motor accelerated by MAX = %d. ", max_step);
}
else
{
printf("Motor accelerated by %d. ", change);
RetVal = motor_curr;
}
if ( RetVal > 255 )
{
RetVal = 255;
printf("Detected excess of 255! ");
}
}
else if (change <= -padd)
{
//Decelerate
if (change < -max_step)
{
RetVal = motor_prev - max_step;
printf("Motor decelerated by MAX = %d. ", max_step);
}
else
{
printf("Motor decelerated by %d. ", change);
RetVal = motor_curr;
}
if (RetVal < 0)
{
RetVal = 0;
printf("Detected below-zero condition! ");
}
}
else
{
//no change
RetVal = motor_prev;
printf("No change in motor output value. ");
}
printf("\r\n");
return RetVal;
}
int last_p1x_val=128;
int last_p1y_val=128;
void Default_Routine(void)
{
int maximum_step=10; /*change this to change how much the motor increments the speed by*/
int padding=3;
/*---------- Analog Inputs (Joysticks) to PWM Outputs-----------------------
*--------------------------------------------------------------------------
* This maps the joystick axes to specific PWM outputs. */
p1_y = Incrementation_Code(p1_y, last_p1y_val, padding, maximum_step);
last_p1y_val = p1_y;
pwm01 = p1_y;
pwm02 = p2_y;
pwm03 = p3_y;
pwm04 = p4_y;
p1_x = Incrementation_Code(p1_x, last_p1x_val, padding, maximum_step);
last_p1x_val = p1_x;
pwm05 = p1_x;
pwm06 = p2_x;
pwm07 = p3_x;
pwm08 = p4_x;
pwm09 = p1_wheel;
pwm10 = p2_wheel;
pwm11 = p3_wheel;
pwm12 = p4_wheel;
EDIT: woops, forgot the semi-colons on some of the statements, fixed now