Wiki

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
first:adxrs450_gyro_board_frc:java [24 Oct 2018 18:07]
Kristen Chong [Using the ADXRS450 Gyro for FRC in Java]
first:adxrs450_gyro_board_frc:java [10 Dec 2018 21:10] (current)
Kristen Chong [Re-Zeroing the Gyro]
Line 13: Line 13:
 \\ \\
 \\ \\
-===== Sensor Initialization and Calibration ===== +===== Sensor Initialization and Calibration ​with calibrate() ​===== 
-Once your gyro is defined and instantiated,​ you will need to initialize it with a calibration where it will execute as soon as the robot is powered on, typically robotInit(). To do this, you will need to call the calibrate() method. Calibration is necessary for proper operation and **MUST** be performed before the gyro can be used. For more information,​ please see the general FRC Gyro page.+Once your gyro is defined and instantiated,​ you will need to initialize it with a calibration where it will execute as soon as the robot is powered on, typically robotInit(). To do this, you will need to call the calibrate() method. Calibration is necessary for proper operation and **MUST** be performed before the gyro can be used. For more information,​ please see the [[first:​adxrs450_gyro_board_frc|general FRC Gyro page]].
 \\ \\
 \\ \\
Line 21: Line 21:
 \\ \\
 \\ \\
-<note tip>As a general note, the GetAngle() method will count continuously,​ meaning when it reaches 360 degrees, it will continue to 361, not zero. This is to make any functionality in your code using the gyro angle easier to implement without having to keep track of where in the 0-360 range your robot is or how many rotations have happened. </​note>​+<note tip>As a general note, the getAngle() method will count continuously,​ meaning when it reaches 360 degrees, it will continue to 361, not zero. This is to make any functionality in your code using the gyro angle easier to implement without having to keep track of where in the 0-360 range your robot is or how many rotations have happened. </​note>​
 \\ \\
 \\ \\
-===== Checking Communications ===== +===== Re-Zeroing ​the Gyro with reset() ===== 
-If you ever need to check that the gyro is responding to the RoboRIO, you can do this by using isConnected(). It will return a Boolean indicating whether it is connected or not. You usually will not need to use this, but it's available if you need to. +Sometimes it may be necessary to reset the gyro's "zero degrees"​ position. All gyros will have some amount of drift over time and it's physically impossible to calibrate out all sources of drift. In this case, you can use reset() to reset the current gyro heading to 0 degrees. When doing this, your robot should be facing the direction you want zero degrees to be, particularly if you are driving with field-oriented drive using an omnidirectional drive base. Otherwise, your robot may begin to behave incorrectly. For this reason, you should never automate this routine. Note that this **does not** recalibrate the gyro, so you don't have to be sitting still to perform this method properly.
-\\ +
-\\ +
-===== Resetting the Gyro ===== +
-Sometimes it may be necessary to reset the gyro's "zero degrees"​ position. All gyros will have some amount of drift over time and it's physically impossible to calibrate out all sources of drift. In this case, you can use reset() to reset the current gyro heading to 0 degrees. When doing this, your robot should be facing the direction you want zero degrees to be, particularly if you are driving with field-oriented drive using an omnidirectional drive base. Otherwise, your robot may begin to behave incorrectly. For this reason, you should never automate this routine. Note that this *does not* recalibrate the gyro, so you don't have to be sitting still to perform this method properly.+
 \\ \\
 \\ \\
Line 36: Line 32:
 \\ \\
 <​code>​ <​code>​
-old open+/​*----------------------------------------------------------------------------*/​ 
 +/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. ​                       */ 
 +/* Open Source Software - may be modified and shared by FRC teams. The code   */ 
 +/* must be accompanied by the FIRST BSD license file in the root directory of */ 
 +/* the project. ​                                                              */ 
 +/​*----------------------------------------------------------------------------*/​ 
 + 
 +package org.usfirst.frc.team2655.robot;​ 
 + 
 +import edu.wpi.first.wpilibj.AnalogGyro;​ 
 +import edu.wpi.first.wpilibj.IterativeRobot;​ 
 +import edu.wpi.first.wpilibj.Joystick;​ 
 +import edu.wpi.first.wpilibj.Spark;​ 
 +import edu.wpi.first.wpilibj.drive.DifferentialDrive;​ 
 + 
 +/** 
 + * This is a sample program to demonstrate how to use a gyro sensor to make a 
 + * robot drive straight. This program uses a joystick to drive forwards and 
 + * backwards while the gyro is used for direction keeping. 
 + */ 
 +public class Robot extends IterativeRobot { 
 + private static final double kAngleSetpoint = 0.0; 
 + private static final double kP = 0.005; // propotional turning constant 
 + 
 + // gyro calibration constant, may need to be adjusted; 
 + // gyro value of 360 is set to correspond to one full revolution 
 + private static final double kVoltsPerDegreePerSecond = 0.0128; 
 + 
 + private static final int kLeftMotorPort = 0; 
 + private static final int kRightMotorPort = 1; 
 + private static final int kGyroPort = 0; 
 + private static final int kJoystickPort = 0; 
 + 
 + private DifferentialDrive m_myRobot 
 + = new DifferentialDrive(new Spark(kLeftMotorPort),​ 
 + new Spark(kRightMotorPort));​ 
 + private AnalogGyro m_gyro = new AnalogGyro(kGyroPort);​ 
 + private Joystick m_joystick = new Joystick(kJoystickPort);​ 
 + 
 + @Override 
 + public void robotInit() { 
 + m_gyro.setSensitivity(kVoltsPerDegreePerSecond);​ 
 +
 + 
 + /** 
 + * The motor speed is set from the joystick while the RobotDrive turning 
 + * value is assigned from the error between the setpoint and the gyro angle. 
 + */ 
 + @Override 
 + public void teleopPeriodic() { 
 + double turningValue = (kAngleSetpoint - m_gyro.getAngle()) * kP; 
 + // Invert the direction of the turn if we are going backwards 
 + turningValue = Math.copySign(turningValue,​ m_joystick.getY());​ 
 + m_myRobot.arcadeDrive(m_joystick.getY(),​ turningValue);​ 
 +
 +
 </​code>​ </​code>​
-First we will need to tell the program to import the correct ​package ​for using the ADXRS450. Change your import statement from AnalogGyro to ADXRS450_Gyro like below.+First we will need to tell the program to import the correct ​library ​for using the ADXRS450. Change your import statement from AnalogGyro to ADXRS450_Gyro like below. You'll also need to import the SPI library so you can use the SPI port in your code, which is required for the gyro to communicate with the RoboRIO.
 \\ \\
 <​code>​ <​code>​
-old+package org.usfirst.frc.team9999.robot;​ 
 + 
 +import edu.wpi.first.wpilibj.AnalogGyro;​ 
 +import edu.wpi.first.wpilibj.IterativeRobot;​ 
 +import edu.wpi.first.wpilibj.Joystick;​ 
 +import edu.wpi.first.wpilibj.Spark;​ 
 +import edu.wpi.first.wpilibj.drive.DifferentialDrive;​
 </​code>​ </​code>​
 <​code>​ <​code>​
-new+package org.usfirst.frc.team9999.robot;​ 
 + 
 +import edu.wpi.first.wpilibj.ADXRS450_Gyro;​ 
 +import edu.wpi.first.wpilibj.SPI;​ 
 +import edu.wpi.first.wpilibj.IterativeRobot;​ 
 +import edu.wpi.first.wpilibj.Joystick;​ 
 +import edu.wpi.first.wpilibj.Spark;​ 
 +import edu.wpi.first.wpilibj.drive.DifferentialDrive;​
 </​code>​ </​code>​
 Now we need to fix all of the variable definitions. You'll need to fix the gyro port variable as well as the gyro itself, and delete the kVoltsPerDegrePerSecond variable and its related comments. In case you're using a port other than CS0, I've explicitly defined my gyro port below. If you're using CS0, you don't have to include the port. Now we need to fix all of the variable definitions. You'll need to fix the gyro port variable as well as the gyro itself, and delete the kVoltsPerDegrePerSecond variable and its related comments. In case you're using a port other than CS0, I've explicitly defined my gyro port below. If you're using CS0, you don't have to include the port.
 \\ \\
 <​code>​ <​code>​
-old+public class Robot extends IterativeRobot { 
 + private static final double kAngleSetpoint = 0.0; 
 + private static final double kP = 0.005; // propotional turning constant 
 + 
 + // gyro calibration constant, may need to be adjusted; 
 + // gyro value of 360 is set to correspond to one full revolution 
 + private static final double kVoltsPerDegreePerSecond = 0.0128; 
 + 
 + private static final int kLeftMotorPort = 0; 
 + private static final int kRightMotorPort = 1; 
 + private static final int kGyroPort = 0; 
 + private static final int kJoystickPort = 0; 
 + 
 + private DifferentialDrive m_myRobot 
 + = new DifferentialDrive(new Spark(kLeftMotorPort),​ 
 + new Spark(kRightMotorPort));​ 
 + private AnalogGyro m_gyro = new AnalogGyro(kGyroPort);​ 
 + private Joystick m_joystick = new Joystick(kJoystickPort);​
 </​code>​ </​code>​
 <​code>​ <​code>​
-new+public class Robot extends IterativeRobot { 
 + private static final double kAngleSetpoint = 0.0; 
 + private static final double kP = 0.005; // propotional turning constant 
 + 
 + private static final int kLeftMotorPort = 0; 
 + private static final int kRightMotorPort = 1; 
 + private static final SPI.Port kGyroPort = SPI.Port.kOnboardCS0;​ 
 + private static final int kJoystickPort = 0; 
 + 
 + private DifferentialDrive m_myRobot 
 +new DifferentialDrive(new Spark(kLeftMotorPort),​ 
 + new Spark(kRightMotorPort));​ 
 + private ADXRS450_Gyro m_gyro = new ADXRS450_Gyro(kGyroPort);​ 
 + private Joystick m_joystick = new Joystick(kJoystickPort);​
 </​code>​ </​code>​
 Next you'll need to fix the method called in robotInit() to be calibrate() instead of setSensitivity(). You can also delete the argument from setSensitivity(),​ since calibrate does not take any arguments. Next you'll need to fix the method called in robotInit() to be calibrate() instead of setSensitivity(). You can also delete the argument from setSensitivity(),​ since calibrate does not take any arguments.
 \\ \\
 <​code>​ <​code>​
-old+ @Override 
 + public void robotInit() { 
 + m_gyro.setSensitivity(kVoltsPerDegreePerSecond);​ 
 + }
 </​code>​ </​code>​
 <​code>​ <​code>​
-new+ @Override 
 + public void robotInit() { 
 + m_gyro.calibrate();​ 
 + }
 </​code>​ </​code>​
 You can leave teleopPeriodic() alone since nothing is different here between using data from an analog gyro and the ADXRS450. When you're done, your code should now look like this. You can leave teleopPeriodic() alone since nothing is different here between using data from an analog gyro and the ADXRS450. When you're done, your code should now look like this.
 \\ \\
 <​code>​ <​code>​
-new full+/​*----------------------------------------------------------------------------*/​ 
 +/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. ​                       */ 
 +/* Open Source Software - may be modified and shared by FRC teams. The code   */ 
 +/* must be accompanied by the FIRST BSD license file in the root directory of */ 
 +/* the project. ​                                                              */ 
 +/​*----------------------------------------------------------------------------*/​ 
 + 
 +package org.usfirst.frc.team2655.robot;​ 
 + 
 +import edu.wpi.first.wpilibj.ADXRS450_Gyro;​ 
 +import edu.wpi.first.wpilibj.SPI;​ 
 +import edu.wpi.first.wpilibj.IterativeRobot;​ 
 +import edu.wpi.first.wpilibj.Joystick;​ 
 +import edu.wpi.first.wpilibj.Spark;​ 
 +import edu.wpi.first.wpilibj.drive.DifferentialDrive;​ 
 + 
 +/** 
 + * This is a sample program to demonstrate how to use a gyro sensor to make a 
 + * robot drive straight. This program uses a joystick to drive forwards and 
 + * backwards while the gyro is used for direction keeping. 
 + */ 
 +public class Robot extends IterativeRobot { 
 + private static final double kAngleSetpoint = 0.0; 
 + private static final double kP = 0.005; // propotional turning constant 
 + 
 + private static final int kLeftMotorPort = 0; 
 + private static final int kRightMotorPort = 1; 
 + private static final SPI.Port kGyroPort = SPI.Port.kOnboardCS0;​ 
 + private static final int kJoystickPort = 0; 
 + 
 + private DifferentialDrive m_myRobot 
 +new DifferentialDrive(new Spark(kLeftMotorPort),​ 
 + new Spark(kRightMotorPort));​ 
 + private ADXRS450_Gyro m_gyro = new ADXRS450_Gyro(kGyroPort);​ 
 + private Joystick m_joystick = new Joystick(kJoystickPort);​ 
 + 
 + @Override 
 + public void robotInit() { 
 + m_gyro.calibrate();​ 
 +
 + 
 + /** 
 + * The motor speed is set from the joystick while the RobotDrive turning 
 + * value is assigned from the error between the setpoint and the gyro angle. 
 + */ 
 + @Override 
 + public void teleopPeriodic() { 
 + double turningValue = (kAngleSetpoint - m_gyro.getAngle()) * kP; 
 + // Invert the direction of the turn if we are going backwards 
 + turningValue = Math.copySign(turningValue,​ m_joystick.getY());​ 
 + m_myRobot.arcadeDrive(m_joystick.getY(),​ turningValue);​ 
 +
 +}
 </​code>​ </​code>​
first/adxrs450_gyro_board_frc/java.1540397278.txt.gz · Last modified: 24 Oct 2018 18:07 by Kristen Chong