Announcement

Collapse
No announcement yet.

Gyro in Rev Expansion Hub

Collapse
This topic is closed.
X
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Gyro in Rev Expansion Hub

    Hi all,
    I am part of a first year team. I have recently heard that there is a gyro sensor built into the expansion hub. I need help to get it to work as no one on my team has worked with one in the past. Our robot is not using encoders so we have no idea how to tackle this problem. We need to be used to drive straight and correct the power when the wheels slip as long as do the same when turning and strafing. We are using mecanum wheel btw. Can anyone help us get started on how to approach the gyro sensor for the mentioned uses? Any help would be greatly appreciated.
    Thank you,
    Team Bots & Bytes

  • #2
    First question is, what FTC SDK are you using? (blocks, onbot java, or android studio?) Our team uses blocks are they are a young team with little java experience.

    Whichever you are using, there are some basic building blocks within the SDK to help get you going. It is more proper to refer to the gyro feature as the Rev imu (inertial measurement unit). The Rev hubs come with the imu generally assigned to the zero location of i2C, but you need to check to ensure it exists in your config file and if not, you need to enable it.

    Another question, how have you mounted the hub? If it is flat, then the Z axis within the hub gives you the angle of the bot. If mounted vertically, then you need to either use the X or Y axis for your bot angle.

    Please note that using the imu is a tough task for beginners, because although getting the angle of the bot from the imu is very easy, controlling the bot based on the angle is really hard for beginners. (part of the reason for this is because java is not ideal for controlling anything that needs real-time control) It takes time for the imu to return the angle, and the control is depending on variable loop times, so it is easy to overshoot.

    Here is a high level description of the approach we are using:

    1. Initialize the imu - This is done in the initialization of your program. Simply follow the sample in the SDK.
    2. Establish baseline bot angle- When the imu is initialized, it sets all of the angles at that point to zero. We never reset the bot to zero again after this point, because resetting the imu to zero takes a lot more time than simply asking it for the current angle.
    3. Set an angle variable- We use one variable for bot angle, and often call the imu to refresh this angle when you need to do something related to that angle. The sample code as means to grab just the one angle variable you need (we use Z)

    Once you have the above setup, now comes the hard part.

    In general, there are two things our robot does with the imu:

    1. Turn to a specific angle
    2. Follow specific vector

    Sounds like you just want to do # 2 above, but I will describe the principles for both.

    To turn to a specific angle:

    1.One thing you need to remember is that when you reset your heading (we only do this on initialize), then straight ahead is zero, angles to the left are positive, and to the right are negative. In other words, to turn left 90 degrees you will go to the imu 90 heading. To turn right 90 degrees, you will go to - 90 degrees.
    2. The basic principle of turning to a heading is to setup a loop, and apply power to your wheels appropriately in the right direction until your bot angle variable is equal to the target angle.
    3. However, you cannot simply apply a constant power to the wheels, because you will overshoot your desired heading. So here is the hard part, you need to apply PID principles to the motor turn power. How this is done is by measuring the difference between your current angle and the target angle, and applying power that is proportional to the difference. The concept is that as your difference approaches zero, your applied power to the motors will approach zero. There is a fundamental problem in that as you approach zero, it will never get there. So you have to add a small amount of extra power to the calculation to allow it to get to the zero difference point, where your bot angle is equal to the target angle. (actually, remember that it is equal to or greater/lesser to because it might not hit zero exactly) Your math for the difference needs to take into account the direction; the equation for one direction looks like power = ((base power * (bot angle - target angle)) + extra power) and in the other direction looks like power = ((base power *(target angle - bot angle)) - extra power) Then set one side to power, the other side to (power * -1.0)

    To follow a specified vector:

    1. Same principles as above, use PID approach to apply power as described above, but this time instead of going to zero power at the difference, you go to a constant power between the wheels..
    2. However, there is an additional set of complexity. If you try using the above principles to follow a line, the robot will oscillate back and forth out of control. Remember where I said that Java is unsuitable for control? To solve the problem, you need to program in another step, to add some hysteresis to the control loop. This takes trial and error, but the idea is to only turn the robot within a certain guard band around zero, in other words don't try to approach zero, stop before zero and normalize the power.

    Hope this basic info helps point you in the right direction. If you have additional questions feel free to ask.

    Comment


    • #3
      Is there a possible way to do this with OnBotJava and no encoders?

      Comment


      • #4
        Encoders aren't necessary if you're using the gyro. Here's our routine for left turns. We use a separate method for left vs right. You'll see a variable "degreesLeft" which is a crazy looking formula due to the way the gyros measure degrees from +/- 180. There's very probably a cleaner way to do this, but it works for us. The sleep command is useful to start with since if your robot is shaking when the gyro is read, the value may be erratic.

        public void turnLeft(doubleturnAngle, doubletimeoutS) {
        sleep(500);
        angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
        doublespeed=.5;
        doubleoldDegreesLeft=turnAngle;
        doublescaledSpeed=speed;
        doubletargetHeading=angles.firstAngle+turnAngle;
        doubleoldAngle=angles.firstAngle;
        if(targetHeading<-180) {targetHeading+=360;}
        if(targetHeading>180){targetHeading-=360;}
        doubledegreesLeft = ((int)(Math.signum(angles.firstAngle-targetHeading)+1)/2)*(360-Math.abs(angles.firstAngle-targetHeading))+(int)(Math.signum(targetHeading-angles.firstAngle)+1)/2*Math.abs(angles.firstAngle-targetHeading);
        runtime.reset();
        while(opModeIsActive() &&
        runtime.seconds() < timeoutS &&
        degreesLeft>1&&
        oldDegreesLeft-degreesLeft>=0) { //check to see if we overshot target
        scaledSpeed=degreesLeft/(100+degreesLeft)*speed;
        if(scaledSpeed>1){scaledSpeed=.1;}
        robot.leftBack.setPower(scaledSpeed*1.3); //extra power to back wheels
        robot.rightBack.setPower(-1*scaledSpeed*1.3); //due to extra weight
        robot.leftFront.setPower(scaledSpeed);
        robot.rightFront.setPower(-1*scaledSpeed);
        angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
        oldDegreesLeft=degreesLeft;
        degreesLeft = ((int)(Math.signum(angles.firstAngle-targetHeading)+1)/2)*(360-Math.abs(angles.firstAngle-targetHeading))+(int)(Math.signum(targetHeading-angles.firstAngle)+1)/2*Math.abs(angles.firstAngle-targetHeading);
        if(Math.abs(angles.firstAngle-oldAngle)<1){speed*=1.1;}. //bump up speed to wheels in case robot stalls before reaching target
        oldAngle=angles.firstAngle;
        }
        stopWheels(); //our helper method to set all wheel motors to zero
        sleep(250); //small pause at end of turn
        }

        Comment


        • #5
          We were able to make the robot turn and drive straight. Thank you so much!
          Last edited by amangalampalli2018; 01-23-2019, 02:05 AM.

          Comment

          Working...
          X