Optical Shaft Encoder Code

Hello!
We are planning to use an optical shaft encoder for our autonomous, specifically our base. We needed a bit of help on coding the encoders. We use RobotC and we currently have a time based autonomous. Also, we do not use task main. We only use void statements, task autonomous, and task usercontrol. Thank you!

What specifically do you want help with? Encoder PID or just implementing a basic encoder autonomous?

There are sample encoder programs if you go file > open sample program. A file will open, and the encoders should be easy to find.

PID and other more complex things are different, of course, so what are you trying to do?

we are trying to code the autonomous with it. I tried to use natural language, but then our robot did not move at all. (FYI i don’t rlly ever use natural language) here is our code.

/////////////////////////////////////////////////////////////////////////////////////////
//
// Pre-Autonomous Functions
//
/////////////////////////////////////////////////////////////////////////////////////////

////////////////////////// Basic functions/////////////////////////

void driveOnly()
{
motor[leftbasemotors] = vexRT[Ch3];
motor[rightbasemotors] = vexRT[Ch2];
motor[rightbackbase] = vexRT[Ch2];
motor[leftbackbase] = vexRT[Ch3];
}

void pre_auton()
{
driveOnly();
motor[bottomliftmotorR] = 0;
motor[bottomliftmotorL] = 0;
motor[leftarmflipmotor] = 0;
motor[rightarmflipmotor] = 0;
motor[clawmotor] = 0;
motor[mobileliftmotor] = 0;

}

/////////////////////////////////////////////////////////////////////////////////////////
//
// 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()
{

}

///////////////////////////////////////////////////////////////////////////////////
//
// 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()
{
motor[bottomliftmotorR] = 0;
motor[bottomliftmotorL] = 0;
motor[leftarmflipmotor] = 0;
motor[rightarmflipmotor] = 0;
motor[clawmotor] = 0;
motor[mobileliftmotor] = 0;

	if(vexRT[Btn6UXmtr2] == 1)
	{
		driveOnly();
		motor[bottomliftmotorR] = 127;
		motor[bottomliftmotorL] = 127;
		wait1Msec(50);
	}
	else if(vexRT[Btn6DXmtr2] == 1)
	{
		driveOnly();
		motor[bottomliftmotorR] = -127;
		motor[bottomliftmotorL] = -127;
		wait1Msec(50);
	}


	if(vexRT[Btn5UXmtr2] == 1)
	{
		driveOnly();
		motor[rightarmflipmotor] = 100;
		motor[leftarmflipmotor] = 100;
		wait1Msec(50);
	}

	else if(vexRT[Btn5DXmtr2] == 1)
	{
		driveOnly();
		motor[rightarmflipmotor] = -100;
		motor[leftarmflipmotor] = -100;
		wait1Msec(50);
	}

	if(vexRT[Btn8RXmtr2] == 1)
	{
		driveOnly();
		motor[clawmotor] = 63;
		wait1Msec(50);
	}

	else if(vexRT[Btn8LXmtr2] == 1)
	{
		driveOnly();
		motor[clawmotor] = -63;
		wait1Msec(100);
	}

	if(vexRT[Btn7U] == 1)

	{
		driveOnly();
		motor[mobileliftmotor] = 127;
		wait1Msec(50);
	}

	else if(vexRT[Btn7D] == 1)
	{
		driveOnly();
		motor[mobileliftmotor] = -127;
		wait1Msec(50);
	}
}

I don’t see any code in autonomous. Meanwhile, there is no while(true) or similar encapsulating the joystick commands. So your tasks are just going to end in an instant.

I do not have any auton code right now… Also I purposely placed no while true. Though if you have any suggestions for the sensor code I am fully open to!
Thank you

@Alpha Dream <3 you have to put your autonomous code in the autonomous task. You also don’t have to set your motors to 0 in pre-autonomous since your robot is disabled anyway. The user control task is for the code you want to use during the driver control period, that is why you need a while(true). There are a lot of fundamental errors in this code and I would recommend checking out some YouTube videos or the robotC learning pages online to learn some more of the basics before moving onto encoders. Oh and please never use Natural Language.

You said you were trying to code autonomous, so that’s just confusing.

That’s why nothing is happening. You have made your program end in a tiny fraction of a second. Right now the only way it should do anything is if you’re holding down certain buttons when you turn it on (well, already have them down a moment later, once the code is running, really). For example, if you hold down Btn8RXmtr2, your claw should move for 0.05 s and then stop. Even then, 0.05 s isn’t much time. So your program is still going to end in just a small fraction of a second, even if you’re holding several buttons down.

As I said, any time you’re trying to read the controller, you need to have some sort of a loop, such as while(true).

Yes we are trying to code the autonomous. Though what I meant was that we currently have no working autonomous code, therefore, we have no autonomous. I was just wondering if someone can help with the sensor code, like to show us an example line since we have watched many sensor coding videos, yet none were successful for us.

Actually, all of our code **besides **our sensor code is successful. Our buttons work just fine, the only time it doesn’t work is when (once again) we try a new type of autonomous code.

Thank you

This is really confusing. You’re saying the code you presented works perfectly as-is? So where is the stuff that isn’t working that you wanted us to look at?

Here is what we have tried:

  1. We have placed in the sensors then used natural language code lines.
    A. Forward(500, degrees, 126)
    B. moveStraightForRotations(100, rightEncoder, leftEncoder). *Note that the rightEncoder and leftEncoder are the names of the two sensors in our motor and sensor setup

Can you show us the code that isn’t working?

Here is the non-working code

/////////////////////////////////////////////////////////////////////////////////////////
//
// Pre-Autonomous Functions
//
/////////////////////////////////////////////////////////////////////////////////////////

////////////////////////// Basic functions/////////////////////////

void driveOnly()
{
motor[leftbasemotors] = vexRT[Ch3];
motor[rightbasemotors] = vexRT[Ch2];
motor[rightbackbase] = vexRT[Ch2];
motor[leftbackbase] = vexRT[Ch3];
}

void pre_auton()
{
driveOnly();
motor[bottomliftmotorR] = 0;
motor[bottomliftmotorL] = 0;
motor[leftarmflipmotor] = 0;
motor[rightarmflipmotor] = 0;
motor[clawmotor] = 0;
motor[mobileliftmotor] = 0;

}

/////////////////////////////////////////////////////////////////////////////////////////
//
// 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()
{
Forward(500, degrees, 126)
}

///////////////////////////////////////////////////////////////////////////////////
//
// 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()
{
motor[bottomliftmotorR] = 0;
motor[bottomliftmotorL] = 0;
motor[leftarmflipmotor] = 0;
motor[rightarmflipmotor] = 0;
motor[clawmotor] = 0;
motor[mobileliftmotor] = 0;

if(vexRT[Btn6UXmtr2] == 1)
{
driveOnly();
motor[bottomliftmotorR] = 127;
motor[bottomliftmotorL] = 127;
wait1Msec(50);
}
else if(vexRT[Btn6DXmtr2] == 1)
{
driveOnly();
motor[bottomliftmotorR] = -127;
motor[bottomliftmotorL] = -127;
wait1Msec(50);
}

if(vexRT[Btn5UXmtr2] == 1)
{
driveOnly();
motor[rightarmflipmotor] = 100;
motor[leftarmflipmotor] = 100;
wait1Msec(50);
}

else if(vexRT[Btn5DXmtr2] == 1)
{
driveOnly();
motor[rightarmflipmotor] = -100;
motor[leftarmflipmotor] = -100;
wait1Msec(50);
}

if(vexRT[Btn8RXmtr2] == 1)
{
driveOnly();
motor[clawmotor] = 63;
wait1Msec(50);
}

else if(vexRT[Btn8LXmtr2] == 1)
{
driveOnly();
motor[clawmotor] = -63;
wait1Msec(100);
}

if(vexRT[Btn7U] == 1)

{
driveOnly();
motor[mobileliftmotor] = 127;
wait1Msec(50);
}

else if(vexRT[Btn7D] == 1)
{
driveOnly();
motor[mobileliftmotor] = -127;
wait1Msec(50);
}
}

I think you missed this from the natural language pdf entry for forward():

“Note that for desirable results with the following set of functions, you must use the “robotType();” Setup Function with either recbot or swervebot in the beginning of your “task main()”.”

Also note that main() won’t show up yet.

What do they mean by robotType(); ?

You should really look at the documentation on RobotC Natural Language if you want to use it. robotType() is the very first command in the pdf.

http://cdn.robotc.net/pdfs/natural-language/Natural_Language_Cortex.pdf

Ultimately the issue is that at some point you have to identify for the program which motors (really, motor ports) should rotate, in other words, which motor is on the right side and which is on the left side. If you’ve never done this, how will the program know were to send the signals? With regular commands, which tell it what ports to use for the motors involved. Your motors need to be in the appropriate ports, or redirected by wiring them to those ports through a power expander, to use a command like forward(), which has the ports already set.

I tried it, though it also doesn’t work. For some reason the robot just moves completely different motors…

Do you have natural language turned on? If you go to robot and platform type do you have natural language clicked?

It says Natural Language 2.0 right now…

Do you have the motors plugged into the same ports as are used by the default Recbot (or Swervbot)? That’s where you’re telling the program they’re plugged in.