This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
first:adxrs450_gyro_board_frc:java [24 Oct 2018 18:07] – [Using the ADXRS450 Gyro for FRC in Java] Kristen Chong | first:adxrs450_gyro_board_frc:java [10 Dec 2018 21:10] (current) – [Re-Zeroing the Gyro] Kristen Chong | ||
---|---|---|---|
Line 13: | Line 13: | ||
\\ | \\ | ||
\\ | \\ | ||
- | ===== Sensor Initialization and Calibration ===== | + | ===== Sensor Initialization and Calibration |
- | Once your gyro is defined and instantiated, | + | Once your gyro is defined and instantiated, |
\\ | \\ | ||
\\ | \\ | ||
Line 21: | Line 21: | ||
\\ | \\ | ||
\\ | \\ | ||
- | <note tip>As a general note, the GetAngle() method will count continuously, | + | <note tip>As a general note, the getAngle() method will count continuously, |
\\ | \\ | ||
\\ | \\ | ||
- | ===== Checking Communications ===== | + | ===== Re-Zeroing |
- | 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" |
- | \\ | + | |
- | \\ | + | |
- | ===== Resetting the Gyro ===== | + | |
- | Sometimes it may be necessary to reset the gyro's "zero degrees" | + | |
\\ | \\ | ||
\\ | \\ | ||
Line 36: | Line 32: | ||
\\ | \\ | ||
< | < | ||
- | 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_myRobot.arcadeDrive(m_joystick.getY(), | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | First we will need to tell the program to import the correct | + | First we will need to tell the program to import the correct |
\\ | \\ | ||
< | < | ||
- | 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; | ||
</ | </ | ||
< | < | ||
- | 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; | ||
</ | </ | ||
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. | ||
\\ | \\ | ||
< | < | ||
- | 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); | ||
</ | </ | ||
< | < | ||
- | 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); | ||
</ | </ | ||
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(), | 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(), | ||
\\ | \\ | ||
< | < | ||
- | old | + | @Override |
+ | public void robotInit() { | ||
+ | m_gyro.setSensitivity(kVoltsPerDegreePerSecond); | ||
+ | } | ||
</ | </ | ||
< | < | ||
- | new | + | @Override |
+ | public void robotInit() { | ||
+ | m_gyro.calibrate(); | ||
+ | } | ||
</ | </ | ||
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. | ||
\\ | \\ | ||
< | < | ||
- | 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_myRobot.arcadeDrive(m_joystick.getY(), | ||
+ | } | ||
+ | } | ||
</ | </ |