Problem with competition template and encoder use

My introductory class has learned how to use shaft encoders with V4 motors/electronics and RobotC, with success. We are now using remote controls and are piecing together both autonomous code and user control code into the supplied Competition template.

One student can get his autonomous code to operate perfectly when it runs as a stand alone program. When he inserts it into the competition template, the left and right motors will run, but it will not stop using any values from the shaft encoders. We have checked to make sure the encoders are working, so I don’t know why they will not stop the drive motors. Code is shown below:

#pragma config(Sensor, dgtl3, LeftEncoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl7, RightEncoder, sensorQuadEncoder)
#pragma config(Motor, port1, RightMotor, tmotorVex393_HBridge, openLoop, encoderPort, dgtl7)
#pragma config(Motor, port6, ClawMotor, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port7, LiftMotor, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port10, LeftMotor, tmotorVex393_HBridge, openLoop, reversed, encoderPort, dgtl3)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

// This code is for the VEX cortex platform
#pragma platform(VEX2)

// Select Download method as “competition”
#pragma competitionControl(Competition)

//Main competition background code…do not modify!
#include “Vex_Competition_Includes.c”

/---------------------------------------------------------------------------/
/* Pre-Autonomous Functions /
/
/
/
You may want to perform some actions before the competition starts. /
/
Do them in the following function. You must return from this function /
/
or the autonomous and usercontrol tasks will not be started. This /
/
function is only called once after the cortex has been powered on and /
/
not every time that the robot is disabled. /
/
---------------------------------------------------------------------------*/

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

// Set bDisplayCompetitionStatusOnLcd to false if you don't want the LCD
// used by the competition include file, for example, you might want
// to display your team name on the LCD in this function.
// bDisplayCompetitionStatusOnLcd = false;

// 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()
{
//motor[ClawMotor] = -10; //let go of ball
//backup
SensorValue[RightEncoder]=0;
SensorValue[LeftEncoder]=0;
while(SensorValue[RightEncoder]>-300)
{
if(SensorValue[RightEncoder]==SensorValue[LeftEncoder])
{
motor[RightMotor]=-80;
motor[LeftMotor]=-80;
}
else if(SensorValue[RightEncoder]>SensorValue[LeftEncoder])
{
motor[RightMotor]=-80;
motor[LeftMotor]=-60;
}
else
{
motor[RightMotor]=-60;
motor[LeftMotor]=-80;
}
}
AutonomousCodePlaceholderForTesting();

}
/---------------------------------------------------------------------------/
/* /
/
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

while (true)
{


	motor[LeftMotor] = vexRT[Ch3];
	motor[RightMotor] = vexRT[Ch2];

	//Arm Control
	if(vexRT[Btn6U] == 1)
	{
		motor[LiftMotor] = 40;
	}
	else if(vexRT[Btn6D] == 1)
	{
		motor[LiftMotor] = -40;
	}
	else
	{
		motor[LiftMotor] = 0;
	}

	//Claw Control
	if(vexRT[Btn5U] == 1)
	{
		motor[ClawMotor] = 40;
	}
	else if(vexRT[Btn5D] == 1)
	{
		motor[ClawMotor] = -40;
	}
	else
	{
		motor[ClawMotor] = 0;
	}

}
UserControlCodePlaceholderForTesting();

}

In the future, please add triple backtics (```) before and after a block of code when posting it on the forum to improve readability.

Here’s the code you posted formatted that way:

#pragma config(Sensor, dgtl3,  LeftEncoder,    sensorQuadEncoder)
#pragma config(Sensor, dgtl7,  RightEncoder,   sensorQuadEncoder)
#pragma config(Motor,  port1,           RightMotor,    tmotorVex393_HBridge, openLoop, encoderPort, dgtl7)
#pragma config(Motor,  port6,           ClawMotor,     tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port7,           LiftMotor,     tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port10,          LeftMotor,     tmotorVex393_HBridge, openLoop, reversed, encoderPort, dgtl3)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// This code is for the VEX cortex platform
#pragma platform(VEX2)

// Select Download method as "competition"
#pragma competitionControl(Competition)

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

/*---------------------------------------------------------------------------*/
/*                          Pre-Autonomous Functions                         */
/*                                                                           */
/*  You may want to perform some actions before the competition starts.      */
/*  Do them in the following function.  You must return from this function   */
/*  or the autonomous and usercontrol tasks will not be started.  This       */
/*  function is only called once after the cortex has been powered on and    */
/*  not every time that the robot is disabled.                               */
/*---------------------------------------------------------------------------*/

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

	// Set bDisplayCompetitionStatusOnLcd to false if you don't want the LCD
	// used by the competition include file, for example, you might want
	// to display your team name on the LCD in this function.
	// bDisplayCompetitionStatusOnLcd = false;

	// 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()
{
	//motor[ClawMotor] = -10; //let go of ball
	//backup
	SensorValue[RightEncoder]=0;
	SensorValue[LeftEncoder]=0;
	while(SensorValue[RightEncoder]>-300)
	{
		if(SensorValue[RightEncoder]==SensorValue[LeftEncoder])
		{
			motor[RightMotor]=-80;
			motor[LeftMotor]=-80;
		}
		else if(SensorValue[RightEncoder]>SensorValue[LeftEncoder])
		{
			motor[RightMotor]=-80;
			motor[LeftMotor]=-60;
		}
		else
		{
			motor[RightMotor]=-60;
			motor[LeftMotor]=-80;
		}
	}
	AutonomousCodePlaceholderForTesting();

}
/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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

	while (true)
	{


		motor[LeftMotor] = vexRT[Ch3];
		motor[RightMotor] = vexRT[Ch2];

		//Arm Control
		if(vexRT[Btn6U] == 1)
		{
			motor[LiftMotor] = 40;
		}
		else if(vexRT[Btn6D] == 1)
		{
			motor[LiftMotor] = -40;
		}
		else
		{
			motor[LiftMotor] = 0;
		}

		//Claw Control
		if(vexRT[Btn5U] == 1)
		{
			motor[ClawMotor] = 40;
		}
		else if(vexRT[Btn5D] == 1)
		{
			motor[ClawMotor] = -40;
		}
		else
		{
			motor[ClawMotor] = 0;
		}

	}
	UserControlCodePlaceholderForTesting();
}

I suspect the problem with this code is that nothing in the autonomous task tells the motors to stop:

task autonomous(){
	//motor[ClawMotor] = -10; //let go of ball
	//backup
	SensorValue[RightEncoder]=0;
	SensorValue[LeftEncoder]=0;
	while(SensorValue[RightEncoder]>-300){
		if(SensorValue[RightEncoder]==SensorValue[LeftEncoder]){
			motor[RightMotor]=-80;
			motor[LeftMotor]=-80;
		}
		else if(SensorValue[RightEncoder]>SensorValue[LeftEncoder]){
			motor[RightMotor]=-80;
			motor[LeftMotor]=-60;
		}
		else{
			motor[RightMotor]=-60;
			motor[LeftMotor]=-80;
		}
	}
	AutonomousCodePlaceholderForTesting();
}

If the content of autonomous were the content of main instead (as it probably was before), then this program will do what you want, since the program will end after the while loop exits. But when it’s in the autonomous task, the program doesn’t end, so the motors will happily keep spinning at whatever power they were last told to use. If you want the motors to stop after the while loop exits, you’ll have to tell them to do that.

4 Likes