Announcement

Collapse
No announcement yet.

Adafruit IMU consistently off by 15 degrees

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Originally posted by mathew View Post
    Was there an issue with this in SDK version 2.1? because I had tested the two IMU drivers against each other previously in that SDK version, and noticed very little heading difference, if any. The readings seemed fairly accurate as well.
    I do not know, I just jumped straight to v2.2. Would you mind testing v2.2 with the same code to see if it works?
    Programmer for Team 4997 Masquerade -- 2012 World Champions, 2014 - 2016 Division Finalists
    Founding Member of Team 6433 Neutrinos -- 2015 World Champions

    Check out my intro video to the new tech platform
    Check out my team's Robot Reveal for Res-Q

    Comment


    • #17
      Here is the code I am using. I have kp at 0.1, so the power should range from 0.0 to 0.9 for a 90 degree turn. imu.getAngles() returns a [yaw, pitch, roll] array, so imu.getAngles()[0] returns the current heading.

      Code:
      public enum Direction {
              FORWARD (+1.0),
              BACKWARD (-1.0),
              CLOCKWISE (+1.0),
              COUNTERCLOCKWISE (-1.0);
              public final double value;
              Direction (double value) {this.value = value;}
          }
      
      
          public double getHeading() {return imu.getAngles()[0];}
      
          public double adjustAngle(double angle) {
              while (angle > 180) angle -= 360;
              while (angle <= -180) angle += 360;
              return angle;
          }
      
      public void turnP(double degrees, Direction direction, double timeout, double speed, double kp) {
              MasqTimer timeoutTimer = new MasqTimer();
              double targetAngle = adjustAngle(getHeading() + direction.value * degrees);
              double error;
              double power;
      
              do {
                  error = adjustAngle(targetAngle - getHeading());
                  power = kp * error;
                  power = Range.clip(power, -speed, +speed);
                  driveTrain.setPower(+power, -power);
                  idle();
              } while (opModeIsActive() && error > 0.5  && !timeoutTimer.elapsedTime(timeout, MasqTimer.Resolution.SECONDS));
              driveTrain.stopMotors();
          }
      Programmer for Team 4997 Masquerade -- 2012 World Champions, 2014 - 2016 Division Finalists
      Founding Member of Team 6433 Neutrinos -- 2015 World Champions

      Check out my intro video to the new tech platform
      Check out my team's Robot Reveal for Res-Q

      Comment


      • #18
        Originally posted by 4634 Programmer View Post
        Well yes that was with the telemetry[...] the gyro being very laggy to return headings.
        I'm still confused. When you say it's laggy, I understand that to mean that the interval between two particular events is longer than is expected or is reasonable. As regards the gyro being laggy, which two events are you referring to?

        The way to get to the bottom of this is likely to write log entries as events occur (which get timestamped) and then look a the log post-mortem to gauge the intervals between events of significance.

        Comment


        • #19
          Originally posted by FTC0417 View Post
          I'm still confused. When you say it's laggy, I understand that to mean that the interval between two particular events is longer than is expected or is reasonable. As regards the gyro being laggy, which two events are you referring to?

          The way to get to the bottom of this is likely to write log entries as events occur (which get timestamped) and then look a the log post-mortem to gauge the intervals between events of significance.
          I don't really know how to explain it other than to say the heading being read from the gyro is a bit behind the actual rotation of the robot.

          Comment


          • #20
            Originally posted by Varun Singh View Post
            Here is the code I am using. I have kp at 0.1, so the power should range from 0.0 to 0.9 for a 90 degree turn.
            You mean 0.01 right ?

            Comment


            • #21
              Originally posted by Philbot View Post
              You mean 0.01 right ?
              Thank you very much! I had 0.1 in my code, totally didn't realize my math was off. Fixing it led me to see that there was a sign error in my turning, and so I corrected that and now it works great. Thanks!
              Programmer for Team 4997 Masquerade -- 2012 World Champions, 2014 - 2016 Division Finalists
              Founding Member of Team 6433 Neutrinos -- 2015 World Champions

              Check out my intro video to the new tech platform
              Check out my team's Robot Reveal for Res-Q

              Comment


              • #22
                Originally posted by Varun Singh View Post
                Thank you very much! I had 0.1 in my code, totally didn't realize my math was off. Fixing it led me to see that there was a sign error in my turning, and so I corrected that and now it works great. Thanks!
                We are still struggling to make it work correctly. Would you mind sharing your code?

                Comment


                • #23
                  Originally posted by 4634 Programmer View Post
                  We are still struggling to make it work correctly. Would you mind sharing your code?
                  kp is 0.01
                  you can get rid of the Direction enum and replace it with simpler code
                  replace the code inside of getHeading() to get the data from your gyro

                  Code:
                  public enum Direction {
                          FORWARD (+1.0),
                          BACKWARD (-1.0),
                          CLOCKWISE (+1.0),
                          COUNTERCLOCKWISE (-1.0);
                          public final double value;
                          Direction (double value) {this.value = value;}
                  }
                  
                  
                  public double getHeading() {return imu.getAngles()[0];}
                  
                  public double adjustAngle(double angle) {
                          while (angle > 180) angle -= 360;
                          while (angle <= -180) angle += 360;
                          return angle;
                  }
                  
                  public void turnP(double degrees, Direction direction, double timeout, double speed, double kp) {
                          double targetAngle = adjustAngle(getHeading() + direction.value * degrees);
                          double error;
                          double power;
                  
                          do {
                              error = adjustAngle(targetAngle - getHeading());
                              power = kp * error;
                              power = Range.clip(power, -speed, +speed);
                              driveTrain.setPower(-power, +power);
                              idle();
                          } while (opModeIsActive() && error > 0.5);
                          driveTrain.stopMotors();
                  }
                  Programmer for Team 4997 Masquerade -- 2012 World Champions, 2014 - 2016 Division Finalists
                  Founding Member of Team 6433 Neutrinos -- 2015 World Champions

                  Check out my intro video to the new tech platform
                  Check out my team's Robot Reveal for Res-Q

                  Comment


                  • #24
                    Originally posted by Varun Singh View Post
                    kp is 0.01
                    you can get rid of the Direction enum and replace it with simpler code
                    replace the code inside of getHeading() to get the data from your gyro

                    Code:
                    public enum Direction {
                            FORWARD (+1.0),
                            BACKWARD (-1.0),
                            CLOCKWISE (+1.0),
                            COUNTERCLOCKWISE (-1.0);
                            public final double value;
                            Direction (double value) {this.value = value;}
                    }
                    
                    
                    public double getHeading() {return imu.getAngles()[0];}
                    
                    public double adjustAngle(double angle) {
                            while (angle > 180) angle -= 360;
                            while (angle <= -180) angle += 360;
                            return angle;
                    }
                    
                    public void turnP(double degrees, Direction direction, double timeout, double speed, double kp) {
                            double targetAngle = adjustAngle(getHeading() + direction.value * degrees);
                            double error;
                            double power;
                    
                            do {
                                error = adjustAngle(targetAngle - getHeading());
                                power = kp * error;
                                power = Range.clip(power, -speed, +speed);
                                driveTrain.setPower(-power, +power);
                                idle();
                            } while (opModeIsActive() && error > 0.5);
                            driveTrain.stopMotors();
                    }
                    So I modified that code to run in Eclipse (getting the IMU angle from user input), and for clockwise turns, it works perfectly.

                    However, for counterclockwise turns, the loop terminates after only one iteration for some reason.

                    Here's the sample output:

                    Code:
                    //This was the call I did: turnP(90, Direction.COUNTERCLOCKWISE, 1, .01);
                    
                    Enter an IMU angle: 0
                    Enter an IMU angle: 0
                    
                    Error: -90.0
                    Power 1: 0.9
                    Power 2: -0.9
                    
                    Stopping motors

                    Comment


                    • #25
                      Try:
                      Code:
                      do {
                      ...
                      ...
                      } while (opModeIsActive() && Math.abs(error) > 0.5);

                      Comment


                      • #26
                        That was it, thanks.

                        I knew it had something to do with negatives, but I couldn't figure out what.

                        Comment

                        Working...
                        X