Is our ROBOTC Coding good?

This is our ROBOTC coding for a Flywheel. 4 Drive wheel motors and two cap flippers.

task main()
{ //Program begins, insert code within curly braces
while (true)
{
motor[FrontRight] = vexRT[Ch2];
motor[BackRight] = vexRT[Ch2];
motor[BackRight] = vexRT[Ch4];
motor[FrontRight] = vexRT[Ch4];

	motor[FrontLeft] = vexRT[Ch1];
	motor[BackLeft] = vexRT[Ch1];
	motor[FrontLeft] = vexRT[Ch3];
	motor[BackLeft] = vexRT[Ch3];



	if(vexRT[Btn5D] ==1)
	{
		startMotor(SpinWheelRight, -127);
	}
	if(vexRT[Btn5D] ==1)
	{
		startMotor(SpinWheelLeft, 127);
	}
	if(vexRT[Btn5U] ==1)
	{
		startMotor(Arm,120);
	}
	if(vexRT[Btn6U] ==1)
	{
		startMotor(Arm, -120);
	}
	if(vexRT[Btn6U] ==0)
	{
		stopMotor(Arm);
	}
	if(vexRT[Btn5U] ==0)
	{
		stopMotor(Arm);
	}
	if(vexRT[Btn8R] ==1)
	{
		startMotor(Flipper, 100);
	}
	if(vexRT[Btn5D] ==0)
	{
		stopMotor(SpinWheelLeft);
		stopMotor(SpinWheelRight);
	}
	if(vexRT[Btn8R] ==0)
	{
		stopMotor(Flipper);
	}
}

}

Looks functional, but you may want to add a threshold (dead zone) for your joysticks. It’ll save a little bit of battery, and will also allow for less touchy controls.

And you may want to create separate tasks for each task. It’ll help improve responsiveness.

it should work, but you can super condense your code a lot, i’ll use your flywheel as an example:
motor[SpinWheelRight] = motor[SpinWheelLeft] = (vexRTBtn5U-vexRTBtn5D)*127;

Is there a way to add a deadzone in V5?

Of course. The syntax is a bit different, but the function is relatively the same. I haven’t been messing around in VCS as much as I should be, so I don’t want to give you a wrong bit of code.

ill probably ask the upperclassmen coder for help and look at our old robotc

Probably a good idea. Maybe someone a bit more efficient in VCS can help here.

I’m a bit confused about this portion:

motor[FrontRight] = vexRT[Ch2];
motor[BackRight] = vexRT[Ch2];
motor[BackRight] = vexRT[Ch4];
motor[FrontRight] = vexRT[Ch4];

motor[FrontLeft] = vexRT[Ch1];
motor[BackLeft] = vexRT[Ch1];
motor[FrontLeft] = vexRT[Ch3];
motor[BackLeft] = vexRT[Ch3];

It looks like you are setting each motor twice, to two different channels. This will either make all but the last assignment useless, or make the motors jitter (I believe jittering is what happens in RobotC, but I haven’t used it in a while so I could be mistaken). What sort of drive control are you trying to do?

Judging by what I see you have a 4 motor drive, in which you control it by tank drive. It seems like the drivetrain lines will also collide with each other, but they can simply be in one statement as well as simplified. You can simplify the drive code to down below:

motor[FrontRight] = + vexRT[Ch2] + vexRT[Ch4];
motor[BackRight] = + vexRT[Ch2] + vexRT[Ch4];
motor[FrontLeft] = + vexRT[Ch1] + vexRT[Ch3];
motor[BackLeft] = + vexRT[Ch1] + vexRT[Ch3];

Just change the “+” to a “-” if you want to reverse the motor on the specific joystick axis.
I see what you’re trying to do with the buttons, and they should be simplified to the code down below:


if(vexRT[Btn5D] ==1)
{
motor[SpinWheelRight] = -127;
motor[SpinWheelLeft] = 127;
}
else{
motor[SpinWheelRight] = 0;
motor[SpinWheelLeft] = 0;
}

if(vexRT[Btn5U] ==1)
{
motor[Arm] = 120;
}
else if(vexRT[Btn6U] ==1)
{
motor[Arm] = -120;
}
else{
motor[Arm] = 0;
}

if(vexRT[Btn8R]==1){
motor[Flipper] = 100;
}
else{
motor[Flipper] = 0;
}

So here’s what I got:

task main()
{ //Program begins, insert code within curly braces
while (true)
{
//Drivetrain Code
motor[FrontRight] = + vexRT[Ch2] + vexRT[Ch4];
motor[BackRight] = + vexRT[Ch2] + vexRT[Ch4];
motor[FrontLeft] = + vexRT[Ch1] + vexRT[Ch3];
motor[BackLeft] = + vexRT[Ch1] + vexRT[Ch3];

//Spinwheel
if(vexRT[Btn5D] ==1)
{
motor[SpinWheelRight] = -127;
motor[SpinWheelLeft] = 127;
}
else{
motor[SpinWheelRight] = 0;
motor[SpinWheelLeft] = 0;
}

//Arm Code
if(vexRT[Btn5U] ==1)
{
motor[Arm] = 120;
}
else if(vexRT[Btn6U] ==1)
{
motor[Arm] = -120;
}
else{
motor[Arm] = 0;
}

//Flipper Code
if(vexRT[Btn8R]==1){
motor[Flipper] = 100;
}
else{
motor[Flipper] = 0;
}

}
}

If I made any errors, someone let me know.
Hopefully this helps a bit :slight_smile:

If you are looking to have a deadzone for your drivetrain, you can do something like this:
Place this above the while loop but below the “task main() {” condition:


//Creates integers that would be declared as a joystick value in the future
int Chan1;
int Chan2;
int Chan3;
int Chan4;

//Deadzone Value
int deadzone = 15; //Within this value is the deadzone. Smaller number = smaller deadzone

Place this inside the while loop and replace your driver code with this below:


//Sets the created integers to be equal to the joystick value
Chan1 = vexRT[Ch1];
Chan2 = vexRT[Ch2];
Chan3 = vexRT[Ch3];
Chan4 = vexRT[Ch4];

//Checks to see if a joystick value is within the specified deadzone, and replaces the integer to zero if it is

/*
Since the code is read from top to bottom, we can use this as an advantage
and declare the integers first to the joystick value (above), then set them to zero
if they are within to the deadzone after (below).

The values that set the integers to zero will all start with "if" statements and will not chain off
of each other with "else if" statements to make sure that the statements run independantly.
*/

//Checks to see if Channel 1 is within a value of 15 of the deadzone
if( abs( vexRT[Ch1] ) < deadzone ){  //abs() means absolute value, or removes the "-" if the value is negative
Chan1 = 0; // Tells the integer to have a value of zero
}

//Checks to see if Channel 2 is within a value of 15 of the deadzone
if( abs( vexRT[Ch2] ) < deadzone ){  //abs() means absolute value, or removes the "-" if the value is negative
Chan2 = 0; // Tells the integer to have a value of zero
}

//Checks to see if Channel 3 is within a value of 15 of the deadzone
if( abs( vexRT[Ch3] ) < deadzone ){  //abs() means absolute value, or removes the "-" if the value is negative
Chan3 = 0; // Tells the integer to have a value of zero
}

//Checks to see if Channel 4 is within a value of 15 of the deadzone
if( abs( vexRT[Ch4] ) < deadzone ){  //abs() means absolute value, or removes the "-" if the value is negative
Chan4 = 0; // Tells the integer to have a value of zero
}

//Replace the "+"s and "-"s of a channel if you want to reverse them
motor[FrontRight] = + Chan2 + Chan4;
motor[BackRight] = + Chan2  + Chan4;
motor[FrontLeft] = + Chan1 + Chan3;
motor[BackLeft] = + Chan1 + Chan3;

Hopefully this helps :slight_smile:

Here’s my combined code with deadzones for you. Hopefully it works :smiley:


task main()
{ //Program begins, insert code within curly braces

//Creates integers that would be declared as a joystick value in the future
int Chan1;
int Chan2;
int Chan3;
int Chan4;

//Deadzone Value
int deadzone = 15; //Within this value is the deadzone. Smaller number = smaller deadzone

while (true)
{
//Drivetrain Code
//Sets the created integers to be equal to the joystick value
Chan1 = vexRT[Ch1];
Chan2 = vexRT[Ch2];
Chan3 = vexRT[Ch3];
Chan4 = vexRT[Ch4];

//Checks to see if a joystick value is within the specified deadzone, and replaces the integer to zero if it is

/*
Since the code is read from top to bottom, we can use this as an advantage
and declare the integers first to the joystick value (above), then set them to zero
if they are within to the deadzone after (below).

The values that set the integers to zero will all start with "if" statements and will not chain off
of each other with "else if" statements to make sure that the statements run independantly.
*/

//Checks to see if Channel 1 is within a value of 15 of the deadzone
if( abs( vexRT[Ch1] ) < deadzone ){  //abs() means absolute value, or removes the "-" if the value is negative
Chan1 = 0; // Tells the integer to have a value of zero
}

//Checks to see if Channel 2 is within a value of 15 of the deadzone
if( abs( vexRT[Ch2] ) < deadzone ){  //abs() means absolute value, or removes the "-" if the value is negative
Chan2 = 0; // Tells the integer to have a value of zero
}

//Checks to see if Channel 3 is within a value of 15 of the deadzone
if( abs( vexRT[Ch3] ) < deadzone ){  //abs() means absolute value, or removes the "-" if the value is negative
Chan3 = 0; // Tells the integer to have a value of zero
}

//Checks to see if Channel 4 is within a value of 15 of the deadzone
if( abs( vexRT[Ch4] ) < deadzone ){  //abs() means absolute value, or removes the "-" if the value is negative
Chan4 = 0; // Tells the integer to have a value of zero
}

//Replace the "+"s and "-"s of a channel if you want to reverse them
motor[FrontRight] = + Chan2 + Chan4;
motor[BackRight] = + Chan2  + Chan4;
motor[FrontLeft] = + Chan1 + Chan3;
motor[BackLeft] = + Chan1 + Chan3;

//Spinwheel
if(vexRT[Btn5D] ==1)
{
motor[SpinWheelRight] = -127;
motor[SpinWheelLeft] = 127;
}
else{
motor[SpinWheelRight] = 0;
motor[SpinWheelLeft] = 0;
}

//Arm Code
if(vexRT[Btn5U] ==1)
{
motor[Arm] = 120;
}
else if(vexRT[Btn6U] ==1)
{
motor[Arm] = -120;
}
else{
motor[Arm] = 0;
}

//Flipper Code
if(vexRT[Btn8R]==1){
motor[Flipper] = 100;
}
else{
motor[Flipper] = 0;
}

}
}

Thanks to everyone who commented. It’s my first year, so learning what deadzone and how to condense the code will make it very helpful. Also Zach thanks for inputting about the jittering, cause our robot is doing just that.

Additionally, with your flywheel, you won’t be able to make it consistent by merely setting motor values; it will shoot all over the place depending on batteries, other motors, etc. Consider some sort of control loop. PID is the classic one, but for flywheels TBH can be very effective

Okay, thanks for the suggestion.