# Threshold Problems

My robotics team has had some troubles with threshold codes. We have a threshold code for our X-Drive, but it does something funny to the X-Drive. It only runs one side at once, and cannot run both sides at the same time. I have included a code below, so any feedback you can give would be great.

``````
int threshold = 45;

{
//Drives Base
if(abs(vexRT[Ch3]) > threshold)
{
motor[leftfront] = vexRT[Ch3]/2 + vexRT[Ch1]/2 + vexRT[Ch4]/2;
}
else
{
motor[leftfront] = 0;
}

if(abs(vexRT[Ch2]) > threshold)
{
motor[rightfront] = vexRT[Ch3]/2 - vexRT[Ch1]/2 - vexRT[Ch4]/2;
}
else
{
motor[rightfront] = 0;
}

if(abs(vexRT[Ch3]) > threshold)
{
motor[leftback] = vexRT[Ch3]/2 + vexRT[Ch1]/2 - vexRT[Ch4]/2;
}
else
{
motor[leftback] = 0;
}

if(abs(vexRT[Ch2]) > threshold)
{
motor[rightback] = vexRT[Ch3]/2 - vexRT[Ch1]/2 + vexRT[Ch4]/2;
}
else
{
motor[rightback] = 0;
}

``````

X Drives can be a bit finicky, the best way to do thresholds with them is to clamp their values with a function. I can’t remember if ROBOTC implements a

``````
clamp

``````

function or not, but it should be pretty easy to implement regardless.

For example, this is my clamp function from last year

``````
int clamp(int d, int max) {
return abs(d) > max ? d : 0
}

``````

What you’ll want to do is remove all your threshold conditionals, and at the top of your main loop, create three variables for each joystick, for example:

``````
int threshold = 45;

while(true) {
int x1 = clamp(vexRT[Ch2], threshold);
int x2 = clamp(vexRT[Ch3], threshold);
int y1 = clamp(vexRT[Ch4], threshold);

// ...
}

``````

I am not a super advanced coder, and don’t really understand this “clamp” thing. Is there any way you could dumb it down for me?

What that “clamp” function does is it checks whether the distance between zero and the number “d” is greater than the maximum value (“max”). If it is, it will return the number “d.” Otherwise, it will return zero.

Here’s what it looks like without using a ternary statement (it’s just a compressed if-statement):

``````
int clamp(int d, int max) {
if (abs(d) > max) {
return d;
} else {
return 0;
}
}

``````

This implements what is known as a “deadband,” which means that anything less than some value “max” will be ignored.

Thanks! That makes a bit more sense now. I will try to implement this on our new H-Base. Do you know if the code for it will be any different from that of the X-Drive?