2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-07-12 21:03:25 -04:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-08-20 09:02:01 -07:00
|
|
|
#include <wpi/SymbolExports.h>
|
|
|
|
|
|
2020-07-12 21:03:25 -04:00
|
|
|
#include "frc/controller/PIDController.h"
|
|
|
|
|
#include "frc/controller/ProfiledPIDController.h"
|
|
|
|
|
#include "frc/geometry/Pose2d.h"
|
|
|
|
|
#include "frc/geometry/Rotation2d.h"
|
|
|
|
|
#include "frc/kinematics/ChassisSpeeds.h"
|
|
|
|
|
#include "frc/trajectory/Trajectory.h"
|
2021-06-16 17:45:51 +03:00
|
|
|
#include "units/angle.h"
|
|
|
|
|
#include "units/velocity.h"
|
2020-07-12 21:03:25 -04:00
|
|
|
|
|
|
|
|
namespace frc {
|
|
|
|
|
/**
|
|
|
|
|
* This holonomic drive controller can be used to follow trajectories using a
|
2021-09-22 13:27:26 -07:00
|
|
|
* holonomic drivetrain (i.e. swerve or mecanum). Holonomic trajectory following
|
|
|
|
|
* is a much simpler problem to solve compared to skid-steer style drivetrains
|
|
|
|
|
* because it is possible to individually control forward, sideways, and angular
|
|
|
|
|
* velocity.
|
2020-07-12 21:03:25 -04:00
|
|
|
*
|
|
|
|
|
* The holonomic drive controller takes in one PID controller for each
|
|
|
|
|
* direction, forward and sideways, and one profiled PID controller for the
|
|
|
|
|
* angular direction. Because the heading dynamics are decoupled from
|
|
|
|
|
* translations, users can specify a custom heading that the drivetrain should
|
|
|
|
|
* point toward. This heading reference is profiled for smoothness.
|
|
|
|
|
*/
|
2021-08-20 09:02:01 -07:00
|
|
|
class WPILIB_DLLEXPORT HolonomicDriveController {
|
2020-07-12 21:03:25 -04:00
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a holonomic drive controller.
|
|
|
|
|
*
|
|
|
|
|
* @param xController A PID Controller to respond to error in the
|
|
|
|
|
* field-relative x direction.
|
|
|
|
|
* @param yController A PID Controller to respond to error in the
|
|
|
|
|
* field-relative y direction.
|
|
|
|
|
* @param thetaController A profiled PID controller to respond to error in
|
|
|
|
|
* angle.
|
|
|
|
|
*/
|
|
|
|
|
HolonomicDriveController(
|
2023-09-15 19:57:31 -07:00
|
|
|
PIDController xController, PIDController yController,
|
2020-12-28 10:12:52 -08:00
|
|
|
ProfiledPIDController<units::radian> thetaController);
|
2020-07-12 21:03:25 -04:00
|
|
|
|
2022-12-25 21:48:27 -05:00
|
|
|
HolonomicDriveController(const HolonomicDriveController&) = default;
|
|
|
|
|
HolonomicDriveController& operator=(const HolonomicDriveController&) =
|
|
|
|
|
default;
|
|
|
|
|
HolonomicDriveController(HolonomicDriveController&&) = default;
|
|
|
|
|
HolonomicDriveController& operator=(HolonomicDriveController&&) = default;
|
|
|
|
|
|
2020-07-12 21:03:25 -04:00
|
|
|
/**
|
|
|
|
|
* Returns true if the pose error is within tolerance of the reference.
|
|
|
|
|
*/
|
|
|
|
|
bool AtReference() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the pose error which is considered tolerable for use with
|
|
|
|
|
* AtReference().
|
|
|
|
|
*
|
2021-10-14 18:09:38 -07:00
|
|
|
* @param tolerance Pose error which is tolerable.
|
2020-07-12 21:03:25 -04:00
|
|
|
*/
|
|
|
|
|
void SetTolerance(const Pose2d& tolerance);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the next output of the holonomic drive controller.
|
|
|
|
|
*
|
2022-11-24 09:13:50 +02:00
|
|
|
* @param currentPose The current pose, as measured by odometry or pose
|
|
|
|
|
* estimator.
|
|
|
|
|
* @param trajectoryPose The desired trajectory pose, as sampled for the
|
|
|
|
|
* current timestep.
|
|
|
|
|
* @param desiredLinearVelocity The desired linear velocity.
|
|
|
|
|
* @param desiredHeading The desired heading.
|
|
|
|
|
* @return The next output of the holonomic drive controller.
|
2020-07-12 21:03:25 -04:00
|
|
|
*/
|
2022-11-24 09:13:50 +02:00
|
|
|
ChassisSpeeds Calculate(const Pose2d& currentPose,
|
|
|
|
|
const Pose2d& trajectoryPose,
|
|
|
|
|
units::meters_per_second_t desiredLinearVelocity,
|
|
|
|
|
const Rotation2d& desiredHeading);
|
2020-07-12 21:03:25 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the next output of the holonomic drive controller.
|
|
|
|
|
*
|
2022-11-24 09:13:50 +02:00
|
|
|
* @param currentPose The current pose, as measured by odometry or pose
|
|
|
|
|
* estimator.
|
|
|
|
|
* @param desiredState The desired trajectory pose, as sampled for the current
|
|
|
|
|
* timestep.
|
|
|
|
|
* @param desiredHeading The desired heading.
|
|
|
|
|
* @return The next output of the holonomic drive controller.
|
2020-07-12 21:03:25 -04:00
|
|
|
*/
|
|
|
|
|
ChassisSpeeds Calculate(const Pose2d& currentPose,
|
|
|
|
|
const Trajectory::State& desiredState,
|
2022-11-24 09:13:50 +02:00
|
|
|
const Rotation2d& desiredHeading);
|
2020-07-12 21:03:25 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enables and disables the controller for troubleshooting purposes. When
|
|
|
|
|
* Calculate() is called on a disabled controller, only feedforward values
|
|
|
|
|
* are returned.
|
|
|
|
|
*
|
|
|
|
|
* @param enabled If the controller is enabled or not.
|
|
|
|
|
*/
|
|
|
|
|
void SetEnabled(bool enabled);
|
|
|
|
|
|
2023-01-17 03:33:15 +11:00
|
|
|
/**
|
|
|
|
|
* Returns the rotation ProfiledPIDController
|
|
|
|
|
*/
|
|
|
|
|
ProfiledPIDController<units::radian>& getThetaController();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the X PIDController
|
|
|
|
|
*/
|
|
|
|
|
PIDController& getXController();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the Y PIDController
|
|
|
|
|
*/
|
|
|
|
|
PIDController& getYController();
|
|
|
|
|
|
2020-07-12 21:03:25 -04:00
|
|
|
private:
|
|
|
|
|
Pose2d m_poseError;
|
2021-02-13 01:11:57 -05:00
|
|
|
Rotation2d m_rotationError;
|
2020-07-12 21:03:25 -04:00
|
|
|
Pose2d m_poseTolerance;
|
|
|
|
|
bool m_enabled = true;
|
|
|
|
|
|
2023-09-15 19:57:31 -07:00
|
|
|
PIDController m_xController;
|
|
|
|
|
PIDController m_yController;
|
2020-07-12 21:03:25 -04:00
|
|
|
ProfiledPIDController<units::radian> m_thetaController;
|
2021-04-19 00:00:11 -04:00
|
|
|
|
|
|
|
|
bool m_firstRun = true;
|
2020-07-12 21:03:25 -04:00
|
|
|
};
|
|
|
|
|
} // namespace frc
|