VCS C++ Gyro Help!!


#1

Hello. We are struggling on how to program a gyro. In the file below, we have our code that we are attempting to use. it is reading an error. Any help will be appreciated?
2.0 RFP.vex (10.5 KB)
gyro.JPG


#2

It says servo value so that probably won’t work


#3

But I don’t know that coding language so


#4

The inherent problem with this code is the formatting “servovalue”, like James said.

You need to use a gyro like this:


while(Gyro.value(rotationUnits::deg) < 90)

Edit: This page is very helpful if you need to know how to use different sensors. http://help.vexcodingstudio.com/index.html

Also in this case, it would never stop. You need to add a stop for all the motors, because Motor.spin in VCS will not stop until told to, unlike rotateFor.

A better way to format this would be:

BC_M1.spin(directionType::fwd, 50, velocityUnits::pct);
BC_M2.spin(directionType::fwd, 50, velocityUnits::pct);
NBC_M1.spin(directionType::rev, 50, velocityUnits::pct);
NBC_M2.spin(directionType::rev, 50, velocityUnits::pct);
while(Gyro.value(rotationUnits::deg) < 90){}
BC_M1.stop();
BC_M2.stop();
NBC_M1.stop();
NBC_M2.stop();

Additionally, if you want to make your life a little easier, and your code shorter and more understandable, use functions. I did a little reorganization to make it cleaner. This isn’t strictly necessary, but makes it easier to fix later, and saves on some keystrokes. It also makes it easier to help you if ever needed again.

void wait(int time){
     task::sleep(time);
}

void leftSpin(int speed){
    BC_M1.spin(directionType::fwd, speed, velocityUnits::pct);
    BC_M2.spin(directionType::fwd, speed, velocityUnits::pct);
}

void rightSpin(int speed){
    NBC_M1.spin(directionType::fwd, speed, velocityUnits::pct);
    NBC_M2.spin(directionType::fwd, speed, velocityUnits::pct);
}

void drive(int distance){
    BC_M1.rotateFor(distance, rotationUnits::rev, false);
    BC_M2.rotateFor(distance, rotationUnits::rev, false);
    NBC_M1.rotateFor(distance, rotationUnits::rev, false);
    NBC_M2.rotateFor(distance, rotationUnits::rev); 
}

void setVelocity(int velocity){
    BC_M1.setVelocity(velocity, velocityUnits::pct);
    BC_M2.setVelocity(velocity, velocityUnits::pct);
    NBC_M1.setVelocity(velocity, velocityUnits::pct);
    NBC_M2.setVelocity(velocity, velocityUnits::pct);
}

void intake(int speed){
    INTAKE.spin(directionType::fwd, speed, velocityUnits::rpm);
}

void stop(){
    BC_M1.stop();
    BC_M2.stop();
    NBC_M1.stop();
    NBC_M2.stop();
}

void autonomous( void ) {
        setVelocity(100); //Set chassis motors to 100%
        intake(100); //Start intake moving foward
        drive(4.8); //Drive chassis foward 40" (4.8 rotations)
        wait(250); //Stop chassis motor
        drive(-1.5); //Chassis back up 12" (1.5 rotations)
        wait(250); //Stop chassis motor
        //Make the robot rotate until 90 degrees
        leftSpin(-50); //not sure if left/right is inverted, due to names of motors
        rightSpin(50);
        while(Gyro.value(rotationUnits::deg) < 90){}
        stop();  
}   


#5

Sorry, we are using VCS C++ V5.

Jack202020, does this change anything you gave me in the code above? Thanks for your patience…


#6

No, that code is written for VEX C++ in VCS for V5.


#7

We tried that code and made it work for our robot setup. The robot still did a circle. We tried to reverse the motors and that did nothing. We also replaced the gyro and that did nothing. Thank you for your patience.


#8

I have attached our gyro code as written so far, per your directions. The robot is still just rotating on its central axis (like a top). We have a green light on the Gyro itself. When we go into the brain "devices’ 3-WIRE for the “H” port (which is what the Gyro is plugged into) we see an Analog input of 1847, and a PWM of 255. Not sure if that helps you help us, but thanks for helping. Very frustrating!!
gyro 2.0.JPG


#9

Don’t try to calibrate your gyro in the middle of autonomous. Also, don’t pass arguments to the startCalibration method, as some arguments will change the scaling factor of the gyro.


#10

Here is new code
2.0 RFP gyro.vex (11 KB)