mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add feed-forward and slew rate limiting to advanced drive examples (#2301)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2019-2020 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. */
|
||||
@@ -15,19 +15,32 @@ frc::MecanumDriveWheelSpeeds Drivetrain::GetCurrentState() const {
|
||||
}
|
||||
|
||||
void Drivetrain::SetSpeeds(const frc::MecanumDriveWheelSpeeds& wheelSpeeds) {
|
||||
const auto frontLeftOutput = m_frontLeftPIDController.Calculate(
|
||||
const auto frontLeftFeedforward =
|
||||
m_feedforward.Calculate(wheelSpeeds.frontLeft);
|
||||
const auto frontRightFeedforward =
|
||||
m_feedforward.Calculate(wheelSpeeds.frontRight);
|
||||
const auto backLeftFeedforward =
|
||||
m_feedforward.Calculate(wheelSpeeds.rearLeft);
|
||||
const auto backRightFeedforward =
|
||||
m_feedforward.Calculate(wheelSpeeds.rearRight);
|
||||
|
||||
const double frontLeftOutput = m_frontLeftPIDController.Calculate(
|
||||
m_frontLeftEncoder.GetRate(), wheelSpeeds.frontLeft.to<double>());
|
||||
const auto frontRightOutput = m_frontRightPIDController.Calculate(
|
||||
const double frontRightOutput = m_frontRightPIDController.Calculate(
|
||||
m_frontRightEncoder.GetRate(), wheelSpeeds.frontRight.to<double>());
|
||||
const auto backLeftOutput = m_backLeftPIDController.Calculate(
|
||||
const double backLeftOutput = m_backLeftPIDController.Calculate(
|
||||
m_backLeftEncoder.GetRate(), wheelSpeeds.rearLeft.to<double>());
|
||||
const auto backRightOutput = m_backRightPIDController.Calculate(
|
||||
const double backRightOutput = m_backRightPIDController.Calculate(
|
||||
m_backRightEncoder.GetRate(), wheelSpeeds.rearRight.to<double>());
|
||||
|
||||
m_frontLeftMotor.Set(frontLeftOutput);
|
||||
m_frontRightMotor.Set(frontRightOutput);
|
||||
m_backLeftMotor.Set(backLeftOutput);
|
||||
m_backRightMotor.Set(backRightOutput);
|
||||
m_frontLeftMotor.SetVoltage(units::volt_t{frontLeftOutput} +
|
||||
frontLeftFeedforward);
|
||||
m_frontRightMotor.SetVoltage(units::volt_t{frontRightOutput} +
|
||||
frontRightFeedforward);
|
||||
m_backLeftMotor.SetVoltage(units::volt_t{backLeftOutput} +
|
||||
backLeftFeedforward);
|
||||
m_backRightMotor.SetVoltage(units::volt_t{backRightOutput} +
|
||||
backRightFeedforward);
|
||||
}
|
||||
|
||||
void Drivetrain::Drive(units::meters_per_second_t xSpeed,
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2019-2020 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include <frc/SlewRateLimiter.h>
|
||||
#include <frc/TimedRobot.h>
|
||||
#include <frc/XboxController.h>
|
||||
|
||||
@@ -23,23 +24,32 @@ class Robot : public frc::TimedRobot {
|
||||
frc::XboxController m_controller{0};
|
||||
Drivetrain m_mecanum;
|
||||
|
||||
// Slew rate limiters to make joystick inputs more gentle; 1/3 sec from 0
|
||||
// to 1.
|
||||
frc::SlewRateLimiter<units::scalar> m_xspeedLimiter{3 / 1_s};
|
||||
frc::SlewRateLimiter<units::scalar> m_yspeedLimiter{3 / 1_s};
|
||||
frc::SlewRateLimiter<units::scalar> m_rotLimiter{3 / 1_s};
|
||||
|
||||
void DriveWithJoystick(bool fieldRelative) {
|
||||
// Get the x speed. We are inverting this because Xbox controllers return
|
||||
// negative values when we push forward.
|
||||
const auto xSpeed =
|
||||
-m_controller.GetY(frc::GenericHID::kLeftHand) * Drivetrain::kMaxSpeed;
|
||||
const auto xSpeed = -m_xspeedLimiter.Calculate(
|
||||
m_controller.GetY(frc::GenericHID::kLeftHand)) *
|
||||
Drivetrain::kMaxSpeed;
|
||||
|
||||
// Get the y speed or sideways/strafe speed. We are inverting this because
|
||||
// we want a positive value when we pull to the left. Xbox controllers
|
||||
// return positive values when you pull to the right by default.
|
||||
const auto ySpeed =
|
||||
-m_controller.GetX(frc::GenericHID::kLeftHand) * Drivetrain::kMaxSpeed;
|
||||
const auto ySpeed = -m_yspeedLimiter.Calculate(
|
||||
m_controller.GetX(frc::GenericHID::kLeftHand)) *
|
||||
Drivetrain::kMaxSpeed;
|
||||
|
||||
// Get the rate of angular rotation. We are inverting this because we want a
|
||||
// positive value when we pull to the left (remember, CCW is positive in
|
||||
// mathematics). Xbox controllers return positive values when you pull to
|
||||
// the right by default.
|
||||
const auto rot = -m_controller.GetX(frc::GenericHID::kRightHand) *
|
||||
const auto rot = -m_rotLimiter.Calculate(
|
||||
m_controller.GetX(frc::GenericHID::kRightHand)) *
|
||||
Drivetrain::kMaxAngularSpeed;
|
||||
|
||||
m_mecanum.Drive(xSpeed, ySpeed, rot, fieldRelative);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2019-2020 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. */
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <frc/Encoder.h>
|
||||
#include <frc/PWMVictorSPX.h>
|
||||
#include <frc/controller/PIDController.h>
|
||||
#include <frc/controller/SimpleMotorFeedforward.h>
|
||||
#include <frc/geometry/Translation2d.h>
|
||||
#include <frc/kinematics/MecanumDriveKinematics.h>
|
||||
#include <frc/kinematics/MecanumDriveOdometry.h>
|
||||
@@ -72,4 +73,8 @@ class Drivetrain {
|
||||
m_backRightLocation};
|
||||
|
||||
frc::MecanumDriveOdometry m_odometry{m_kinematics, GetAngle()};
|
||||
|
||||
// Gains are for example purposes only - must be determined for your own
|
||||
// robot!
|
||||
frc::SimpleMotorFeedforward<units::meters> m_feedforward{1_V, 3_V / 1_mps};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user