Great catch Pixel, I’ve reformatted your code while going through it, mostly just unnecessary polish.
int rpmRight = 0;
int rpmLeft = 0;
int targetRPM = 7200;
int driverCheck = 0;
#define NO_RPM 0
#define SLOW_RPM 6000
#define MED_RPM 7000
#define FAST_RPM 10000
int max(int a, int b)
{
return (a > b) ? a : b;
}
int clamp(int value, int min, int max)
{
return (value > max) ? max : (value < min) ? min : value;
}
void getRightRPM(void * ignore)
{
while(true)
{
rpmRight = encoderGet(rightEncoder) * 33;
encoderReset(rightEncoder);
lcdPrint(uart1, 1, "Right: %d RPMs", rpmRight);
delay(20);
}
}
void getLeftRPM(void * ignore)
{
while(true)
{
rpmLeft = encoderGet(leftEncoder) * 33;
encoderReset(leftEncoder);
lcdPrint(uart1, 2, "Left: %d RPMs", rpmLeft);
delay(20);
}
}
void maintainRPM(void * ignore)
{
int speedRight = 0;
int speedLeft = 0;
while(true)
{
float kp = 0.05;
int diffRight = targetRPM - rpmRight;
int diffLeft = targetRPM - rpmLeft;
speedRight += kp * diffRight;
speedLeft += kp * diffLeft;
speedRight = clamp(speedRight, 30, 127);
speedLeft = clamp(speedLeft, 30, 127);
motorSet(2, speedRight);
motorSet(3, speedRight);
motorSet(4, speedLeft);
motorSet(5, speedLeft);
motorSet(1, (driverCheck || analogRead(1) > 1500) ? -127 : 0);
delay(50);
}
}
void operatorControl()
{
taskCreate(maintainRPM, TASK_DEFAULT_STACK_SIZE, NULL, TASK_PRIORITY_DEFAULT);
taskCreate(getRightRPM, TASK_DEFAULT_STACK_SIZE, NULL, TASK_PRIORITY_DEFAULT);
taskCreate(getLeftRPM, TASK_DEFAULT_STACK_SIZE, NULL, TASK_PRIORITY_DEFAULT);
while(true)
{
int joystickX = joystickGetAnalog(1, 4) + joystickGetAnalog(1, 1);
int joystickY = joystickGetAnalog(1, 3) + joystickGetAnalog(1, 2);
motorSet(6, joystickY - joystickX);
motorSet(7, joystickY + joystickX);
motorSet(8, -joystickY - joystickX);
motorSet(10, joystickY - joystickX);
motorSet(9, 127);
// joy, button gr, button
if(joystickGetDigital(1, 8, 1))
targetRPM = SLOW_RPM;
else if(joystickGetDigital(1, 8, 8))
targetRPM = MED_RPM;
else if(joystickGetDigital(1, 8, 4))
targetRPM = FAST_RPM;
else if(joystickGetDigital(1, 7, 1))
targetRPM = NO_RPM;
driverCheck = joystickGetDigital(1, 6, 1);
// Joystick and motor values refreshed every 20 ms
delay(20);
}
}
Out of curiosity why are you doing this?
int joystickX = joystickGetAnalog(1, 4) + joystickGetAnalog(1, 1);
int joystickY = joystickGetAnalog(1, 3) + joystickGetAnalog(1, 2);