how do joystick thresholds work on holonomic drive?
I know how to do it on a standard tank drive but The code for holonomic confuses me a little as to what values it’s actually getting.
instead of using the #define function I would just says something like
Chn4= VexRt[Ch4];
if(Abs(Chn4)< 15){ Chn4 = 0;}
and do all of that stuff for every channel.
#define C1LX vexRT[Ch4]
#define C1LY vexRT[Ch3]
#define C1RX vexRT[Ch1]
#define JOYSTICK_THRESHOLD 40
// Returns the absolute value of foo
int abs(int foo) {
return (foo < 0) ? -foo : foo;
}
task main() {
while(true) {
// Grab editable copies of the joystick values
int x = C1LX;
int y = C1LY;
int r = C1RX;
if(abs(x) <= JOYSTICK_THRESHOLD)
x = 0;
if(abs(y) <= JOYSTICK_THRESHOLD)
y = 0;
if(abs(r) <= JOYSTICK_THRESHOLD)
r = 0;
motor[FL] = -y - x - r;
motor[FR] = y - x - r;
motor[BR] = y + x - r;
motor[BL] = -y + x - r;
wait10Msec(2);
}
}
Let’s see if we can do better…
#define C1LX vexRT[Ch4]
#define C1LY vexRT[Ch3]
#define C1RX vexRT[Ch1]
#define JOYSTICK_THRESHOLD 40
// Returns the absolute value of foo
int abs(int foo) {
return (foo < 0) ? -foo : foo;
}
// Returns the squashed value of foo
int threshold(int foo, int threshold) {
return (abs(foo) <= threshold) ? 0 : foo;
}
task main() {
while(true) {
// Grab editable copies of the joystick values
int x = threshold(C1LX, JOYSTICK_THRESHOLD);
int y = threshold(C1LY, JOYSTICK_THRESHOLD);
int r = threshold(C1RX, JOYSTICK_THRESHOLD);
motor[FL] = -y - x - r;
motor[FR] = y - x - r;
motor[BR] = y + x - r;
motor[BL] = -y + x - r;
wait10Msec(2);
}
}
ROBOTC may wine about how I declared x, y, r. If so just declare them outside the loop but still set them inside. What I wrote is valid C, just not sure if RC will complain or not.
For those wondering why I didn’t just hardcode JOYSTICK_THRESHOLD into the threshold function, the reason is to keep clamp as generic and useful as possible. Maybe somewhere else clamp will be useful to have with a different value?
I DID define JOYSTICK_THRESHOLD so that there’s only one place it’s defined. This is so it can easily be changed and prevents possible unintended errors if you were to try to change it. If it’s only defined once, you can’t miss one of the instances.
That being said, if you need different clamp values for some reason, you can definitely do that with this code.
-Cody
EDIT: Did some name changing, just for more clarity.