can anyone help me code a quad encoder in RobotC?
what do you need to do with it? The command to get its value is just sensorvaluewhateveryounamedit]
task autonomous()
{
while(SensorValue[Quad] < 100)
{
startMotor(BLDrive, 127);
startMotor(BRDrive, 127);
startMotor(FLDrive, 127);
startMotor(FRDrive, 127);
startMotor(MDrive, 127);
}
}
and it just keeps driving forward
well, the way you would fix this is to set your motors equal to 0. so the code would look like
task autonomous()
{
while(SensorValue[Quad] < 100)
{
startMotor(BLDrive, 127);
startMotor(BRDrive, 127);
startMotor(FLDrive, 127);
startMotor(FRDrive, 127);
startMotor(MDrive, 127);
}
else if (sensorvalue[quad]>100)
{
motor[BLDrive]=0
motor[BRDrive]=0
motor[FLDrive]=0
motor[FRDrive]=0
motor[Mdrive]=0
}
}
You don’t need the else; when execution leaves the inner while loop it will encounter the motor[port]=0; lines.
Don’t forget that statements need a final semicolon in C/C++.
yeah, but else makes me feel better, plus its easier to understand as well (even though comments do the same thing) also coding on the forums is weird.
I’ve gone through every format, guys. do’s, if’s, while’s and none of them have worked.
task autonomous()
{
while(SensorValue[Quad] < 100)
{
startMotor(BLDrive, 127);
startMotor(BRDrive, 127);
startMotor(FLDrive, 127);
startMotor(FRDrive, 127);
startMotor(MDrive, 127);
}
motor[BLDrive]=0;
motor[BRDrive]=0;
motor[FLDrive]=0;
motor[FRDrive]=0;
motor[MDrive]=0;
}
you essentially have to set the motors to 0, without that they’ll just run infinitely. So, setting them equal to 0 as done in my code should work, however if your encoder isn’t counting correctly then obviously it’ll never go the full 100, however generally that bit of code should work.
ill try it
but I have already tried it many times
Are you sure the encoder is counting up? Could it be counting down?
Do you zero the encoder at the start?
I love you guys.
seriously though, why was an if statement not working when the else stopped the motors?
oh yeah, it worked.
no problem, i remember when i struggled to get a motor to run, just glad to help.
okay how do I continue with more tasks? yeah it reverses fine but how do I make it do things after?
another while statement?
If mean like doing turn during autonomous I do mine like this down below. So far with my experience it works well for me. This is how my mentor showed me so tell me if they have any errors.
leftServo
and the right side is your drive motors.Encoders are
leftEncoder
as the encoder is for the left side and other one is for your right encoder for the other side. Pretty straight forward. Everything before task main would go in your pre autonomous and task main bellow would go into your autonomous. Hope this helps
void turnLeftDeg(int degrees, int power)
{
//Reset encoders
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0;
//Determine tickGoal
int tickGoal = (23 * degrees) / 10;
//Start the motors in a left point turn.
motor[leftServo] = -1 * power;
motor[rightServo] = power;
while(SensorValue[rightEncoder] < tickGoal || SensorValue[leftEncoder] > -1 * tickGoal) {
if(SensorValue[rightEncoder] > tickGoal) {motor[rightServo] = 0;}
if(SensorValue[leftEncoder] < -1 * tickGoal) {motor[leftServo] = 0;}
}
//Make sure both motors stop at the end of the turn.
motor[leftServo] = 0;
motor[rightServo] = 0;
}
void turnRightDeg(int degrees, int power)
{
//Reset encoders
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0;
//Determine tickGoal
int tickGoal = (23 * degrees) / 10;
//Start the motors in a left point turn.
motor[leftServo] = power;
motor[rightServo] = -1 * power;
while(SensorValue[leftEncoder] < tickGoal || SensorValue[rightEncoder] > -1 * tickGoal) {
if(SensorValue[leftEncoder] > tickGoal) {motor[leftServo] = 0;}
if(SensorValue[rightEncoder] < -1 * tickGoal) {motor[rightServo] = 0;}
}
//Make sure both motors stop at the end of the turn.
motor[leftServo] = 0;
motor[rightServo] = 0;
}
task main()
{
//Turn right 90 degrees with power 30
turnRightDeg(90,120);
turnLeftDeg(50,40);
wait1Msec(500);
turnRightDeg(160,20);
wait1Msec(500);
turnLeftDeg(200,30);
}
so all that mumbo jumbo is unavoidable?
hang on I get it-do I define the voids above pre auton?
Sorry just edited @SkinnyPanda Robotics
You have to either declare them or at least name them with a forward reference before they’re encountered in the code. If you use a forward reference, you can define them anywhere. Here’s a quick example:
//forward reference examples
// note they end with a semicolon!
void myFastTurn(int degrees);
int currentHeading();
task pre_auton()
{
// pre autonomous stuff here
}
task autonomous()
{
// autonomous stuff here
int myHeading;
// call the currentHeading function; store result in myHeading
myHeading = currentHeading();
myFastTurn(myHeading + 10);
}
task usercontrol()
{
// driver code here
}
void myFastTurn(int degrees)
{
// implementation of myFastTurn
}
int currentHeading()
{
// implementation of a function returning an int
return SensorValue[gyro]
}
And note they’re “functions” not “voids”. It just happens that none of your helper functions return anything; therefore you are declaring them as being type “void.” In the sample above, one of my example functions returns an int. You can see why calling them “voids” doesn’t really make sense; they’re not always of type void.