Robotc Threshold

I am having some problems with my Threshold Function working correctly. The Joystick works and the wheels work without the threshold. It works if I pull it out of a function, but I would like it as a function. Pls Help


#pragma config(Motor,  port2,           BackRightWheel, tmotorVex393HighSpeed_MC29, openLoop, reversed)
#pragma config(Motor,  port3,           FrontRightWheel, tmotorVex393HighSpeed_MC29, openLoop, reversed)
#pragma config(Motor,  port8,           FrontLeftWheel, tmotorVex393HighSpeed_MC29, openLoop)
#pragma config(Motor,  port9,           BackLeftWheel, tmotorVex393HighSpeed_MC29, openLoop, encoderPort, I2C_1)
#pragma platform(VEX)
#pragma competitionControl(Competition)
#include "Vex_Competition_Includes.c"
float RThreshold;
float LThreshold;
void Threshold(int Channel, int Threshold, float Direction)
{
	if(abs(vexRT[Channel]) < Threshold)
	{
		Direction = 0;
	}
	else
	{
		Direction = 1;
	}
}

task usercontrol()
{
	while (true)
	{
		Threshold(Ch2,15,RThreshold);
		Threshold(Ch3,15,LThreshold);
		motor[FrontRightWheel] = vexRT[Ch2]*RThreshold;
		motor[FrontLeftWheel]  = vexRT[Ch3]*LThreshold;
		motor[BackRightWheel]  = vexRT[Ch2]*RThreshold;
		motor[BackLeftWheel]   = vexRT[Ch3]*LThreshold;
         }
}

I’m not quite sure what you’re trying to do here? You’re setting a variable Direction in the function, but I don’t see it used anywhere. Also, your RThreshold and LThreshold don’t seem to be set to a value anywhere.

In case you want to have a deadzone threshold on the controller, so that the motor only move when the joystick is past a value, this is how you can do it:


if(abs(vexRT[Ch1]) > thresh1)
	{
		motor[XXXX] = vexRT[Ch1];
	}
	else
	{
		motor[XXXX] = 0;
	}

Repeat for each joystick.

I’m going to guess that Omar thought his Threshold function would return a value, Direction. But the returnType of “void” makes that impossible.

I think if you want a function to return a value, you can define it differently, as shown in this, under Advanced Functions (Oops, this particular link is for Robotc for Lego Mindstorms but I think it’s the same code):
http://cdn.robotc.net/pdfs/nxt/reference/hp_functions.pdf

I did start off with this initially, but I thought I could get away with having a function for it instead of wasting space.

So would this work? I don’t have access to my robot.


int RThreshold;
int LThreshold;
int Threshold(int Channel, int Deg)
{
	if(abs(vexRT[Channel]) <Deg)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

task usercontrol()
{
	while (true)
	{
		RThreshold = Threshold(Ch2,15);
		LThreshold = Threshold(Ch3,15);
		motor[FrontRightWheel] = vexRT[Ch2]*RThreshold;
		motor[FrontLeftWheel]  = vexRT[Ch3]*LThreshold;
		motor[BackRightWheel]  = vexRT[Ch2]*RThreshold;
		motor[BackLeftWheel]   = vexRT[Ch3]*LThreshold;
         }
}