| /*----------------------------------------------------------------------------*/ | |
| /* Copyright (c) FIRST 2008. 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.lunatecs316.frc; | |
| import edu.wpi.first.wpilibj.CounterBase.EncodingType; | |
| import edu.wpi.first.wpilibj.Encoder; | |
| import edu.wpi.first.wpilibj.RobotDrive; | |
| import edu.wpi.first.wpilibj.SimpleRobot; | |
| public class EncoderExample extends SimpleRobot { | |
| // Constants | |
| public static final double kDistancePerRevolution = 18.84; // guestimate from your code | |
| public static final double kPulsesPerRevolution = 1024; // for an AS5145B Magnetic Encoder | |
| public static final double kDistancePerPulse = kDistancePerRevolution / kPulsesPerRevolution; | |
| // | |
| // Encoder Wiring: | |
| // | |
| // Left Encoder A Channel -> DIO 1, Module 1 | |
| // Left Encoder B Channel -> DIO 2, Module 1 | |
| // Right Encoder A Channel -> DIO 3, Module 1 | |
| // Right Encoder B Channel -> DIO 4, Module 1 | |
| // | |
| // Details on WPILib Encoder class: http://wpilib.screenstepslive.com/s/3120/m/7912/l/85770-measuring-rotation-of-a-wheel-or-other-shaft-using-encoders | |
| // | |
| private Encoder leftEncoder = new Encoder(1, 2, false, EncodingType.k4X); | |
| private Encoder rightEncoder = new Encoder(3, 4, true, EncodingType.k4X); // Count direction reversed | |
| // Two motor drive | |
| private RobotDrive drive = new RobotDrive(1, 2); | |
| /** | |
| * Initialize the robot. | |
| */ | |
| public void robotInit() { | |
| leftEncoder.setDistancePerPulse(kDistancePerPulse); | |
| rightEncoder.setDistancePerPulse(kDistancePerPulse); | |
| leftEncoder.start(); | |
| rightEncoder.start(); | |
| resetEncoders(); | |
| } | |
| /** | |
| * This function is called once each time the robot enters autonomous mode. | |
| */ | |
| public void autonomous() { | |
| // Drive forwards for 50 units | |
| resetEncoders(); | |
| do { | |
| drive.arcadeDrive(0.5, 0.0); | |
| } while (getAverageEncoderPosition() < 50.0); | |
| drive.arcadeDrive(0.0, 0.0); | |
| } | |
| private double getAverageEncoderPosition() { | |
| return (leftEncoder.getDistance() + rightEncoder.getDistance()) / 2; | |
| } | |
| private void resetEncoders() { | |
| leftEncoder.reset(); | |
| rightEncoder.reset(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment