toggle switch with deadzone threshold mecanum drive

#pragma config(Motor,  port2,           frontRight,    tmotorNormal, openLoop)
#pragma config(Motor,  port3,           backRight,     tmotorNormal, openLoop)
#pragma config(Motor,  port4,           frontLeft,     tmotorNormal, openLoop, reversed)
#pragma config(Motor,  port5,           backLeft,      tmotorNormal, openLoop, reversed)
#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)

#include "Vex_Competition_Includes.c"   //Main competition background code...do not modify!

/////////////////////////////////////////////////////////////////////////////////////////
//
//                          Pre-Autonomous Functions
//
// You may want to perform some actions before the competition starts. Do them in the
// following function.
//
/////////////////////////////////////////////////////////////////////////////////////////

void pre_auton()
{
	// Set bStopTasksBetweenModes to false if you want to keep user created tasks running between
	// Autonomous and Tele-Op modes. You will need to manage all user created tasks if set to false.
	bStopTasksBetweenModes = true;

	// All activities that occur before the competition starts
	// Example: clearing encoders, setting servo positions, ...
}

/////////////////////////////////////////////////////////////////////////////////////////
//
//                                 Autonomous Task
//
// This task is used to control your robot during the autonomous phase of a VEX Competition.
// You must modify the code to add your own robot specific commands here.
//
/////////////////////////////////////////////////////////////////////////////////////////

task autonomous()
{
	// .....................................................................................
	// Insert user code here.
	// .....................................................................................

	AutonomousCodePlaceholderForTesting();  // Remove this function call once you have "real" code.
}

/////////////////////////////////////////////////////////////////////////////////////////
//
//                                 User Control Task
//
// This task is used to control your robot during the user control phase of a VEX Competition.
// You must modify the code to add your own robot specific commands here.
//
/////////////////////////////////////////////////////////////////////////////////////////

task usercontrol()
{
	// User control code here, inside the loop
	//Create "deadzone" variables. Adjust threshold value to increase/decrease deadzone
	int X2 = 0, Y1 = 0, X1 = 0, threshold = 15;

	//Loop Forever
	while(1 == 1)
	{
		//if(btn5D == true & speed == full)
	  //  	thenspeed == 1/3
		//elseif(btn5D == true)
		//		speed == full
	
	
	
	//Create "deadzone" for Y1/Ch3
		if(abs(vexRT[Ch3]) > threshold)
			Y1 = vexRT[Ch3];
		else
			Y1 = 0;
		//Create "deadzone" for X1/Ch4
		if(abs(vexRT[Ch4]) > threshold)
			X1 = vexRT[Ch4];
		else
			X1 = 0;
		//Create "deadzone" for X2/Ch1
		if(abs(vexRT[Ch1]) > threshold)
			X2 = vexRT[Ch1];
		else
			X2 = 0;

		//Remote Control Commands
		motor[frontRight] = Y1 - X2 - X1;
		motor[backRight] =  Y1 - X2 + X1;
		motor[frontLeft] = Y1 + X2 + X1;
		motor[backLeft] =  Y1 + X2 - X1;
	}
	// This is the main execution loop for the user control program. Each time through the loop
	// your program should update motor + servo values based on feedback from the joysticks.

	// .....................................................................................
	// Insert user code here. This is where you use the joystick values to update your motors, etc.
	// .....................................................................................

	UserControlCodePlaceholderForTesting(); // Remove this function call once you have "real" code
}

ok this by no means is this my final code but im confused on what to do next… i think i need to implement some functions but can i put all of this deadzone code in a function, or am i completely missing the picture…

my toggle switch isnt implemented yet it is after some double bakslashes right after while 1 == 1

You can put some of the deadzone code in a function to reduce repetition. What I would recommend is something as follows:

int filterJoystick(int joystickValue, int threshold) {
  if (joystickValue > threshold) return joystickValue; //out of deadzone
  else return 0; //inside the dead zone
}

Then you can edit your code to be something like the following:

task usercontrol()
{
  // User control code here, inside the loop
  //Create "deadzone" variables. Adjust threshold value to increase/decrease deadzone
  int X2 = 0, Y1 = 0, X1 = 0, threshold = 15;

  //Loop Forever
  while(1 == 1)
  {
    //if(btn5D == true & speed == full)
    //  	thenspeed == 1/3
    //elseif(btn5D == true)
    //		speed == full
    
    
    Y1 = filterJoystick(vexRT[Ch3], threshold);
    X1 = filterJoystick(vexRT[Ch4], threshold);
    X2 = filterJoystick(vexRT[Ch1], threshold);

    //Remote Control Commands
    motor[frontRight] = Y1 - X2 - X1;
    motor[backRight] =  Y1 - X2 + X1;
    motor[frontLeft] = Y1 + X2 + X1;
    motor[backLeft] =  Y1 + X2 - X1;
  }

  // This is the main execution loop for the user control program. Each time through the loop
  // your program should update motor + servo values based on feedback from the joysticks.
}

A slightly different approach to the filter would be the following (it’s either more convoluted or more straightforward, depending on your point of view):


const int THRESHOLD = 15; //global constant--I'm fuzzy on this syntax

int filterJoystick(TVexJoysticks ch) { //Argument is channel number instead of joystick value
  return vexRT[ch]*(vexRT[ch] > THRESHOLD); //0 if less than, 1 otherwise
}

//Controls drive speed
void drive(int forward, int turn, int strafe) {
    motor[frontRight] = forward - turn - strafe;
    motor[backRight] =  forward - turn + strafe;
    motor[frontLeft] = forward + turn + strafe;
    motor[backLeft] =  forward + turn - strafe;
}

//<other code from template>

task usercontrol() {
  while(true) { //Control loop
    //Figure out joystick input
    int Y1 = filterJoystick(Ch3);
    int X1 = filterJoystick(Ch4);
    int X2 = filterJoystick(Ch1);

    //Send results to motors
    drive(Y1, X2, X1);
  }
}

In this refactored code, the drive function allows an easy way to command the drive system. This function can be used in autonomous, too, not just usercontrol() (see below). Actually, you really don’t even need the X1, X2, and Y1 variables anymore, but I think they help keep the code readable. It’s possible that they could use better names, though.

In autonomous, if you want to tell your robot to go straight for one second then turn left for 2 seconds, you could simply write:

drive(100, 0, 0); //Forward...
wait1msec(1000); //...for one second
drive(0, 0, 0); //All stop

wait1msec(500); //Short pause to let robot settle

drive(0, 100, 0); //turn...
wait1msec(1000); //...for one second
drive(0, 0, 0); //all stop

Should you not use the absolute value of the controller output? The current configuration would only work on the positive side of things, on the negative vexRT[ch] would always be less than THRESHOLD and therefore a value of zero would be returned.
for example

abs()


int filterJoystick(TVexJoysticks ch) { //Argument is channel number instead of joystick value
  return vexRT[ch]*(abs(vexRT[ch]) > THRESHOLD); //0 if less than, 1 otherwise
}

Good catch; yes, you must compare against the absolute value of vexRT[chX].