2019-09-08 00:11:49 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) 2019 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <frc/AnalogGyro.h>
|
|
|
|
|
#include <frc/Encoder.h>
|
2019-11-20 00:48:22 -05:00
|
|
|
#include <frc/PWMVictorSPX.h>
|
2019-09-08 00:11:49 -04:00
|
|
|
#include <frc/SpeedControllerGroup.h>
|
|
|
|
|
#include <frc/controller/PIDController.h>
|
|
|
|
|
#include <frc/kinematics/DifferentialDriveKinematics.h>
|
|
|
|
|
#include <frc/kinematics/DifferentialDriveOdometry.h>
|
2019-11-15 17:33:18 -08:00
|
|
|
#include <units/units.h>
|
2019-09-08 00:11:49 -04:00
|
|
|
#include <wpi/math>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a differential drive style drivetrain.
|
|
|
|
|
*/
|
|
|
|
|
class Drivetrain {
|
|
|
|
|
public:
|
|
|
|
|
Drivetrain() {
|
|
|
|
|
m_gyro.Reset();
|
|
|
|
|
// Set the distance per pulse for the drive encoders. We can simply use the
|
|
|
|
|
// distance traveled for one rotation of the wheel divided by the encoder
|
|
|
|
|
// resolution.
|
|
|
|
|
m_leftEncoder.SetDistancePerPulse(2 * wpi::math::pi * kWheelRadius /
|
|
|
|
|
kEncoderResolution);
|
|
|
|
|
m_rightEncoder.SetDistancePerPulse(2 * wpi::math::pi * kWheelRadius /
|
|
|
|
|
kEncoderResolution);
|
2019-12-01 02:10:29 -05:00
|
|
|
|
|
|
|
|
m_leftEncoder.Reset();
|
|
|
|
|
m_rightEncoder.Reset();
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the robot angle as a Rotation2d.
|
|
|
|
|
*/
|
|
|
|
|
frc::Rotation2d GetAngle() const {
|
|
|
|
|
// Negating the angle because WPILib Gyros are CW positive.
|
|
|
|
|
return frc::Rotation2d(units::degree_t(-m_gyro.GetAngle()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr units::meters_per_second_t kMaxSpeed =
|
|
|
|
|
3.0_mps; // 3 meters per second
|
|
|
|
|
static constexpr units::radians_per_second_t kMaxAngularSpeed{
|
|
|
|
|
wpi::math::pi}; // 1/2 rotation per second
|
|
|
|
|
|
|
|
|
|
void SetSpeeds(const frc::DifferentialDriveWheelSpeeds& speeds);
|
|
|
|
|
void Drive(units::meters_per_second_t xSpeed,
|
|
|
|
|
units::radians_per_second_t rot);
|
|
|
|
|
void UpdateOdometry();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static constexpr units::meter_t kTrackWidth = 0.381_m * 2;
|
|
|
|
|
static constexpr double kWheelRadius = 0.0508; // meters
|
|
|
|
|
static constexpr int kEncoderResolution = 4096;
|
|
|
|
|
|
2019-11-20 00:48:22 -05:00
|
|
|
frc::PWMVictorSPX m_leftMaster{1};
|
|
|
|
|
frc::PWMVictorSPX m_leftFollower{2};
|
|
|
|
|
frc::PWMVictorSPX m_rightMaster{3};
|
|
|
|
|
frc::PWMVictorSPX m_rightFollower{4};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
frc::SpeedControllerGroup m_leftGroup{m_leftMaster, m_leftFollower};
|
|
|
|
|
frc::SpeedControllerGroup m_rightGroup{m_rightMaster, m_rightFollower};
|
|
|
|
|
|
|
|
|
|
frc::Encoder m_leftEncoder{0, 1};
|
2019-11-03 12:08:05 -05:00
|
|
|
frc::Encoder m_rightEncoder{2, 3};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
frc2::PIDController m_leftPIDController{1.0, 0.0, 0.0};
|
|
|
|
|
frc2::PIDController m_rightPIDController{1.0, 0.0, 0.0};
|
|
|
|
|
|
|
|
|
|
frc::AnalogGyro m_gyro{0};
|
|
|
|
|
|
|
|
|
|
frc::DifferentialDriveKinematics m_kinematics{kTrackWidth};
|
2019-12-01 02:10:29 -05:00
|
|
|
frc::DifferentialDriveOdometry m_odometry{GetAngle()};
|
2019-09-08 00:11:49 -04:00
|
|
|
};
|