Need help programming in sensors for autonomous

Hello everyone! Team 7368W here still trying to figure out the basics of programming. :smiley:

I wrote an autonomous program to push a cube onto the skyrise base and place two skyrise pieces. It worked but only in the loosest sense of the word. Because we had no sensors on the robot until now, it was wildly inconsistent. We have two encoders and an ultrasonic sensor which I think could come in handy if I figured out how to program them to work. Currently the ultrasonic sensor is mounted on the claw and one of the encoders is on the clawbot style lift. I was thinking about using the other encoder, but we have an x-drive and I don’t think that one encoder would be too effective there.

So my question mostly has to do with this: First off, where do I plug these things in? Secondly, how would I be able to get the robot to spin until it sees a skyrise piece and then stop, close the claw on it, pick up the skyrise piece, and then continue running the autonomous program?

Also if you think that using another type of sensor would be more helpful that could be an option as well. We have bump switches, limit switches, a line tracker, the color sensor, and maybe some others. I just didn’t see a use for them with the current setup.

Thanks!

Here is our autonomous code if that helps:

 motor[port6] = 100;
motor[port7] = -100;
motor[port8] = 100;    //forwards
motor[port9] = -100;
wait1Msec(270);///////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);///////////////////////////////////////////////////////////////////////
motor[port6] = -100;       //backwards
motor[port7] = 100;
motor[port8] = -100;
motor[port9] = 100;
wait1Msec(270);///////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);///////////////////////////////////////////////////////////////////////
motor[port6] = -40;
motor[port7] = -40;
motor[port8] = -40;          // move straight sideways (right)
motor[port9] = -40;
wait1Msec(370);////////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);///////////////////////////////////////////////////////////////////////
motor[port6] = 60;
motor[port7] = -60;       // spin left
motor[port8] = -60;
motor[port9] = 60;
wait1Msec(220);//////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);
motor[port6] = -40;
motor[port7] = -40;
motor[port8] = -40;          // move straight sideways (right)
motor[port9] = -40;
wait1Msec(220);//////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);///////////////////////////////////////////////////////////////////////
motor[leftLift] = -95;       // raise main lift arm
motor[rightLift] = 95;
wait1Msec(680);///////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[port4] = 127;
wait1Msec(460);///////////////////////////////////////////////////////////////////////
motor[port6] = 60;
motor[port7] = -60;       // spin left
motor[port8] = -60;
motor[port9] = 60;
wait1Msec(760);/////////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[port1] = -127;         //lower claw
wait1Msec(190);///////////////////////////////////////////////////////////////////////
motor[port1] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[port6] = 100;
motor[port7] = -100;
motor[port8] = 100;    //forwards
motor[port9] = -100;
wait1Msec(270);///////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port4] = -127;    //close claw
wait1Msec(280);///////////////////////////////////////////////////////////////////////
motor[port4] = -20;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port1] = 127;         
wait1Msec(460);////////////////////////////////////////////////////////////////////////
motor[port1] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[leftLift] = -95;
motor[rightLift] = 95;
wait1Msec(280);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = -100;       //backwards
motor[port7] = 100;
motor[port8] = -100;
motor[port9] = 100;
wait1Msec(170);////////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = -60;
motor[port7] = 60;         // spin right
motor[port8] = 60;
motor[port9] = -60;
wait1Msec(650);//////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = 127;            // extend arm
wait1Msec(180);///////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[port1] = -127;                //lower claw
wait1Msec(40);//////////////////////////////////////////////////////////////////////
motor[port1] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[leftLift] = 20;
motor[rightLift] = -20;        //lower arm
wait1Msec(520);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port4] = 127;           //open claw
wait1Msec(470);///////////////////////////////////////////////////////////////////////
motor[port4] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -90;
motor[rightLift] = 90;        //lift arm
wait1Msec(330);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = -127;           //retract arm
wait1Msec(180);///////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = 60;
motor[port7] = -60;       // spin left
motor[port8] = -60;
motor[port9] = 60;
wait1Msec(760);//////////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = 100;
motor[port7] = -100;
motor[port8] = 100;    //forwards
motor[port9] = -100;
wait1Msec(250);///////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port1] = -127;
wait1Msec(180);
motor[port1] = 0;
wait1Msec(20);///////////////////////////////////////////////////////////////////////
motor[port4] = -127;    //close claw
wait1Msec(280);///////////////////////////////////////////////////////////////////////
motor[port4] = -20;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port1] = 127;       //lift claw
wait1Msec(480);////////////////////////////////////////////////////////////////////////
motor[port1] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[leftLift] = -95;
motor[rightLift] = 95;        //lift arm
wait1Msec(280);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = -100;       //backwards
motor[port7] = 100;
motor[port8] = -100;
motor[port9] = 100;
wait1Msec(120);////////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = -60;
motor[port7] = 60;         // spin right
motor[port8] = 60;
motor[port9] = -60;
wait1Msec(630);//////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = 127;            // extend arm
wait1Msec(180);///////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[port1] = -127;                //lower claw
wait1Msec(60);//////////////////////////////////////////////////////////////////////
motor[port1] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[leftLift] = 10;
motor[rightLift] = -10;        //lower arm
wait1Msec(900);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port4] = 127;           //open claw
wait1Msec(500);///////////////////////////////////////////////////////////////////////
motor[port4] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -90;
motor[rightLift] = 90;        //lift arm
wait1Msec(500);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////

What programming software are you using?

Currently using RobotC.

Here is our autonomous code if that helps:

 motor[port6] = 100;
motor[port7] = -100;
motor[port8] = 100;    //forwards
motor[port9] = -100;
wait1Msec(270);///////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);///////////////////////////////////////////////////////////////////////
motor[port6] = -100;       //backwards
motor[port7] = 100;
motor[port8] = -100;
motor[port9] = 100;
wait1Msec(270);///////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);///////////////////////////////////////////////////////////////////////
motor[port6] = -40;
motor[port7] = -40;
motor[port8] = -40;          // move straight sideways (right)
motor[port9] = -40;
wait1Msec(370);////////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);///////////////////////////////////////////////////////////////////////
motor[port6] = 60;
motor[port7] = -60;       // spin left
motor[port8] = -60;
motor[port9] = 60;
wait1Msec(220);//////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);
motor[port6] = -40;
motor[port7] = -40;
motor[port8] = -40;          // move straight sideways (right)
motor[port9] = -40;
wait1Msec(220);//////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);///////////////////////////////////////////////////////////////////////
motor[leftLift] = -95;       // raise main lift arm
motor[rightLift] = 95;
wait1Msec(680);///////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[port4] = 127;
wait1Msec(460);///////////////////////////////////////////////////////////////////////
motor[port6] = 60;
motor[port7] = -60;       // spin left
motor[port8] = -60;
motor[port9] = 60;
wait1Msec(760);/////////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[port1] = -127;         //lower claw
wait1Msec(190);///////////////////////////////////////////////////////////////////////
motor[port1] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[port6] = 100;
motor[port7] = -100;
motor[port8] = 100;    //forwards
motor[port9] = -100;
wait1Msec(270);///////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port4] = -127;    //close claw
wait1Msec(280);///////////////////////////////////////////////////////////////////////
motor[port4] = -20;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port1] = 127;         
wait1Msec(460);////////////////////////////////////////////////////////////////////////
motor[port1] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[leftLift] = -95;
motor[rightLift] = 95;
wait1Msec(280);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = -100;       //backwards
motor[port7] = 100;
motor[port8] = -100;
motor[port9] = 100;
wait1Msec(170);////////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = -60;
motor[port7] = 60;         // spin right
motor[port8] = 60;
motor[port9] = -60;
wait1Msec(650);//////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = 127;            // extend arm
wait1Msec(180);///////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[port1] = -127;                //lower claw
wait1Msec(40);//////////////////////////////////////////////////////////////////////
motor[port1] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[leftLift] = 20;
motor[rightLift] = -20;        //lower arm
wait1Msec(520);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port4] = 127;           //open claw
wait1Msec(470);///////////////////////////////////////////////////////////////////////
motor[port4] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -90;
motor[rightLift] = 90;        //lift arm
wait1Msec(330);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = -127;           //retract arm
wait1Msec(180);///////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = 60;
motor[port7] = -60;       // spin left
motor[port8] = -60;
motor[port9] = 60;
wait1Msec(760);//////////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = 100;
motor[port7] = -100;
motor[port8] = 100;    //forwards
motor[port9] = -100;
wait1Msec(250);///////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port1] = -127;
wait1Msec(180);
motor[port1] = 0;
wait1Msec(20);///////////////////////////////////////////////////////////////////////
motor[port4] = -127;    //close claw
wait1Msec(280);///////////////////////////////////////////////////////////////////////
motor[port4] = -20;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port1] = 127;       //lift claw
wait1Msec(480);////////////////////////////////////////////////////////////////////////
motor[port1] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[leftLift] = -95;
motor[rightLift] = 95;        //lift arm
wait1Msec(280);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = -100;       //backwards
motor[port7] = 100;
motor[port8] = -100;
motor[port9] = 100;
wait1Msec(120);////////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port6] = -60;
motor[port7] = 60;         // spin right
motor[port8] = 60;
motor[port9] = -60;
wait1Msec(630);//////////////////////////////////////////////////////////////////////
motor[port6] = 0;
motor[port7] = 0;
motor[port8] = 0;
motor[port9] = 0;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = 127;            // extend arm
wait1Msec(180);///////////////////////////////////////////////////////////////////////
motor[leftSpitMotor] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[port1] = -127;                //lower claw
wait1Msec(60);//////////////////////////////////////////////////////////////////////
motor[port1] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[leftLift] = 10;
motor[rightLift] = -10;        //lower arm
wait1Msec(900);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////
motor[port4] = 127;           //open claw
wait1Msec(500);///////////////////////////////////////////////////////////////////////
motor[port4] = 0;
wait1Msec(20);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -90;
motor[rightLift] = 90;        //lift arm
wait1Msec(500);////////////////////////////////////////////////////////////////////////
motor[leftLift] = -25;
motor[rightLift] = -25;
wait1Msec(20);/////////////////////////////////////////////////////////////////////////

I’m using robotc.

So I think I’ve gotten a start on integrating a potentiometer to select the different program in accordance with what side of the field we are on.

here’s the code:

 
task autonomous()
{
	while(SensorValue[Pot] >= 0 && SensorValue[Pot] < 2000)
	{

Red side autonomous

SensorValue[Pot] = 2000;
}

while(SensorValue[Pot] > 2000 && SensorValue[Pot] < 4100)
{

Blue Side autonomous

SensorValue[Pot] = 2000;
}

But when I go to run the autonomous, it fails and a yellow arrow appears next to the first ā€œwhileā€ line. What am i doing wrong?

Why are you using while loops for this? I would think a set of if-statements might work better. Something more like this perhaps?


task autonomous()
{
if ( SensorValue[Pot] >= 0 && SensorValue[Pot] <= 2000)
    {
    //Insert code to run Red side autonomous
    }
    else if ( SensorValue[Pot] > 2000 && SensorValue[Pot] < 4100 )
    {
      //Insert code to run Blue side autonomous
    }

}

Also, I don’t see any reason for this line SensorValue[Pot] = 2000;
What is that supposed to do?

Also, be careful that you match all your curly braces, so that each { is paired with its }.

The Cortex has specific ports for analog, digital, and motor connections. Click on your ā€œMotors and Sensors tabā€ in your RobotC editor to set up which motors and sensors plug into which ports. Note that some sensors will use two ports.

I was thinking that perhaps telling the program to set the sensor to something that would void the loop would prevent it from running the program endlessly, but that certainly didn’t work. I’ll give the if statements a shot as that makes much more sense. Thanks!

The next time the cortex goes to read the potentiometer it will just fill in the new value it gets.

The potentiometer is different from a wheel encoder in that it is reading the voltage drop along the variable resistor inside the pot. Setting a value might be there for an instant but it will most likely revert back to what the sensor actually states is the resistance next time the background processes update the sensor values.

An encoder is a different story. It is keeping a running count of the clicks it saw fly by in the wheel. An interrupt is used to manage the click detection and updating the internal counters in the sensor value.

Next, you only want to run the auton routine once so no need for a while loop.

If you really want that while loop, make another variable to say int runAlready = 0. Initialize to false (0) and set to true (1) at the end of the auton. Use that in the while loop to prevent running a second time via adding && runAlready == 0 to the while condition. Messing with the potentiometer value will not give you what you want.

Make sure you always shut off your motors at some point. Don’t rely on the competition field control to shut them off for you.