Response to "RobotC Sensor Program"

@Ashton wrote

It looks like you are wanting to drive forward at maximum speed forever with the sensors keeping the drive motion straight. If that is your goal, you are not far off.

The main thing that will keep your code from working is the high likelihood (based on the way they are mounted on the bot) that one of the encoders will be generating negative numbers, and the other positive numbers. That being the case, you will promptly be stuck in the if test with the negative encoder less than the positive and drift that direction. You can avoid that error by testing for the absolute value rather than the raw value. For example:


if(abs(SensorValue[LeftEncoder]) > abs(SensorValue[RightEncoder]))
{
motor(leftwheels) = 100;
motor(rightwheels) = 127;
}

It is also important to know that motor power of 100 and 127 are not that different in most cases… The vex motors can reach their maximum speed at something like 90 in a low load situation. You would see a greater affect using 80 and 90 as your power values.

Lastly, if you want to drive straight for a set distance, you would change your while to check the value of one of the sensors versus a target, like this:


while (abs(SensorValue[LeftEncoder]) < 2500)
{
// your if statements
wait1msec(25) // don't hog cpu
}
motor(leftwheels) = 0;
motor(rightwheels) = 0;

I’ve fixed my issue using this program:

void autoDriveGate(int speed, int speed2, int ticks) //800 ticks, 127 speed, 100 speed2

{while(SensorValue[LeftEncoder] < ticks || SensorValue[RightEncoder] < ticks)
{
//…Move Forward
motor[leftwheels] = speed;
motor[rightwheels] = speed;
}
if (SensorValue[LeftEncoder] > SensorValue[RightEncoder])
{
motor[leftwheels] = speed2;
motor[rightwheels] = speed;
}
if (SensorValue[LeftEncoder] < SensorValue[RightEncoder])
{
motor[leftwheels] = speed;
motor[rightwheels] = speed2;
}
if (SensorValue[LeftEncoder] > ticks || SensorValue[RightEncoder] > ticks)
{
motor(leftwheels) = 0;
motor(rightwheels) = 0;
}
}

task autonomous()
{
wait1Msec(500);

//Clear Encoders
resetSensor(LeftEncoder);
resetSensor(RightEncoder);

nMotorEncoder(leftwheels) = 0;
nMotorEncoder(rightwheels) = 0;

autoDriveGate(127, 100, 800);
wait1Msec(100);
resetSensor(LeftEncoder);
resetSensor(RightEncoder);
wait1Msec(600);
motor(port2) = 127;
motor(port3) = 127;
motor(port8) = 127;
motor(port4) = 127;
motor(port5) = 127;
wait1Msec(250);
stopMotor(port4);
stopMotor(port5);
wait1Msec(950);
stopMotor(port2);
stopMotor(port3);
stopMotor(port8);
wait1Msec(300);
motor(port6) = 127;
motor(port7) = 127;
wait1Msec(700);
motor(port6) = -63;
motor(port7) = -63;
wait1Msec(300);
stopMotor(port6);
stopMotor(port7);

Just ran this program in a competition and it scored 3 points every single time it ran. It goes straight so I seem to have fixed my issue.

I have a new issue now.

I have a potentiometer attached to our arm, and I have code that raises it to a limit then it stops, and that works perfectly. Here is that code:

void autoArm(int speed)

{while(SensorValue[Pot1] > 115 && SensorValue[Pot1] < 1718) //MAY HAVE TO CHANGE to = 115 instead of > 115, same with 1718
{
motor(port2) = speed;
motor(port3) = speed;
motor(port8) = speed;
}
if (SensorValue[Pot1] < 114)
{
motor(port2) = speed;
motor(port3) = speed;
motor(port8) = speed;
}
if (SensorValue[Pot1] > 1718 || SensorValue[Pot1] <4095)
{
motor(port2) = 0;
motor(port3) = 0;
motor(port8) = 0;
}
}

I also have a potentiometer attached to our claw, so I can autonomously limit how far it opens and shuts, but I cannot get it to work. Here is the problematic code:

void autoClaw(int speed)
{while(SensorValue[Pot2] < 47)
{
motor(port4) = speed;
motor(port5) = speed;
}
if (SensorValue[Pot2] > 48 || SensorValue[Pot2] < 4095)
{
motor(port4) = 0;
motor(port5) = 0;
}
}

Ports 4 and 5 run our claw, and they are defined in Motor and Sensor Setup. Our 2nd Potentiometer is plugged and reading values.

What is the start value of your potentiometer and what is the desired end value? The void as written would drive the claw from 0 to 47 and stop, which is only about 3 degrees of rotation. Seems unlikely that you would want that.
I think the following would be a more broadly usable void function.


void autoClaw(int speed, int target) // negative speed to close, positive to open, target is desired pot value
{
  if (SensorValue[Pot2] < 100 || SensorValue[Pot2] > 4000) // don't break the sensor
  {
      while(abs(SensorValue[Pot2] - target) > 25) // while off by more than 25
      {
            motor(port4) = speed;  // run motors at speed to target
            motor(port5) = speed;
      }
   }
   motor(port4) = 0; // stop the claw if target is reached or range limit is exceeded
   motor(port5) = 0;
}

I would recommend using a slow motor speed, like 60, to avoid going past your target. This is simple code, and if your target is passed, it will not stop running until it reaches a the limit. If you poke around the forum, you will find proportional drive code or PD code or PID code that will drive your claw to exactly the pot value you want.