Code Trouble. Please Help.

Hi. We are a new team having trouble coding a limit on how much our claw can move. It will not open at all when we run the following code:


#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, dgtl1,  ArmEncoder,     sensorQuadEncoder)
#pragma config(Sensor, I2C_1,  ,               sensorQuadEncoderOnI2CPort,    , AutoAssign )
#pragma config(Sensor, I2C_2,  ,               sensorQuadEncoderOnI2CPort,    , AutoAssign )
#pragma config(Motor,  port1,           backRightDrive, tmotorVex393TurboSpeed_HBridge, openLoop)
#pragma config(Motor,  port2,           rightClaw,     tmotorVex393TurboSpeed_MC29, openLoop, encoderPort, I2C_2)
#pragma config(Motor,  port3,           leftClaw,      tmotorVex393TurboSpeed_MC29, openLoop, reversed, encoderPort, I2C_1)
#pragma config(Motor,  port4,           farLeftDump,   tmotorVex393TurboSpeed_MC29, openLoop, reversed)
#pragma config(Motor,  port5,           InLeftDump,    tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor,  port6,           InRightDump,   tmotorVex393TurboSpeed_MC29, openLoop, reversed)
#pragma config(Motor,  port7,           farRightDump,  tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor,  port8,           frontRightDrive, tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor,  port9,           backLeftDrive, tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor,  port10,          frontLeftDrive, tmotorVex393TurboSpeed_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#pragma platform(VEX2)

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

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


void pre_auton()
{

}


task autonomous()
{

}


task usercontrol()
{

	SensorValue[ArmEncoder]=0;
	nMotorEncoder[rightClaw] = 0;
	nMotorEncoder[leftClaw] = 0;
	while (true)
	{

		motor[backLeftDrive] = motor[frontLeftDrive] = vexRT[Ch1] + vexRT[Ch3]; //drive left
		motor[backRightDrive] = motor[frontRightDrive] =  vexRT[Ch1] - vexRT[Ch3]; //drive right

		if(getJoystickValue(Btn5U) == 1){
			motor[leftClaw] = motor[rightClaw] = 127;
		}
		else if(getJoystickValue(Btn5D) == 1 && nMotorEncoder[leftClaw] << 200){
			motor[leftClaw] = -127;
		}
	
	else if(getJoystickValue(Btn5D) == 1 && nMotorEncoder[rightClaw] << -200){ 
		motor[rightClaw] = 0;
		}
		else if(getJoystickValue(Btn5D) == 1 && nMotorEncoder[leftClaw] >> 200){
			motor[leftClaw] = 0;
		}
		else if(getJoystickValue(Btn5D) == 1 && nMotorEncoder[rightClaw] >> -200) {
			motor[rightClaw] = 127;
		}
		else{
         motor[leftClaw] = 0;
         motor[rightClaw] = 0;
        }

		//	if(getJoystickValue(Btn5D) == 1)
		//	{
		//		motor[rightClaw] = motor[leftClaw] = -100;
		//	}

		//	else if (getJoystickValue(Btn5U) == 1)
		//	{
		//		motor[rightClaw] = motor[leftClaw] = 100;
		//	}
		//else
		//	{
		//	motor[rightClaw] = motor[leftClaw] = 10;
		//		}
		if(getJoystickValue(Btn6U) ==  1 && SensorValue[ArmEncoder] <= 110)
		{
			motor[farLeftDump] = motor[InLeftDump] = motor[farRightDump] = motor[InRightDump] =  100;
		}
		else if(getJoystickValue(Btn6D) == 1)
		{
			motor[farLeftDump] = motor[InLeftDump] = motor[farRightDump] = motor[InRightDump] = -50;
		}
		else if(SensorValue[ArmEncoder] <= 110)
		{
			motor[farLeftDump] = motor[InLeftDump] = motor[farRightDump] = motor[InRightDump] = 15;
		}
		else
		{
			motor[farLeftDump] = motor[InLeftDump] = motor[farRightDump] = motor[InRightDump] = 0;
		}
	}
}

We know it is not a problem with the sensors because they are not sending back values. Help is much appreciated. Thank you!

Are your sensors attached to the correct ports on your robot? also are the sensors functioning correctly if you use them with a different bit of code?

The comparison statements have an issue.


nMotorEncoder[leftClaw] << 200

does not compare nMotorEncoder[leftClaw] with 200, it actually shifts left the value of nMotorEncoder[leftClaw] by 200, what you need is


nMotorEncoder[leftClaw] < 200

same issue with statements that include “>>”

We changed the > signs but it still will not open.

We know the sensors work as they return values to the computer when we plug the robot in.

Also is // if(getJoystickValue(Btn5D) == 1)
// {
// motor[rightClaw] = motor[leftClaw] = -100;
// }

	//	else if (getJoystickValue(Btn5U) == 1)
	//	{
	//		motor[rightClaw] = motor[leftClaw] = 100;
	//	}
	//else
	//	{
	//	motor[rightClaw] = motor[leftClaw] = 10;
	//		}

section meant to be commented out?

That was the basic code from before we added sensors. It’s just there as a backup.

I don’t know which way the encoders are counting, but try changing the logic to something like this.

    if(getJoystickValue(Btn5U) == 1){
      motor[leftClaw] = motor[rightClaw] = 127;
    }
    else
    if(getJoystickValue(Btn5D) == 1) {
      // control left claw
      if(nMotorEncoder[leftClaw] < 200){
        motor[leftClaw] = -127;
      }
      else if(nMotorEncoder[leftClaw] > 200){
        motor[leftClaw] = 0;
      }
    
      // control right claw
      if(nMotorEncoder[rightClaw] < -200){ 
        motor[rightClaw] = 0;
      }
      else if(nMotorEncoder[rightClaw] > -200) {
        motor[rightClaw] = 127;
      }
    }
    else {
       motor[leftClaw]  = 0;
       motor[rightClaw] = 0;
    }

Thank you all so much for helping! It’s finally working.

@MechaBot Something else worth noting is that since you’re using encoders (vs. a potentiometer) to tell the claw position, you will want to make sure that your claw is always in the same position before you turn on your robot. Otherwise, the encoders will have a different “zero” point. So if you start your robot with the claw open, say, when the program was written using encoder values for when the claw has started closed, the claw probably won’t function right.

On the other hand, a potentiometer doesn’t have problems like this since it will read the same value for a given position. The caveat is that a potentiometer has a limited range of motion.