/* * MIT License * * Copyright (c) PhotonVision * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #pragma once #include #include #include #include #include #include static constexpr int numModules{4}; class SwerveDriveSim { public: SwerveDriveSim( const wpi::math::SimpleMotorFeedforward& driveFF, const wpi::math::DCMotor& driveMotor, double driveGearing, wpi::units::meter_t driveWheelRadius, const wpi::math::SimpleMotorFeedforward& steerFF, const wpi::math::DCMotor& steerMotor, double steerGearing, const wpi::math::SwerveDriveKinematics& kinematics); SwerveDriveSim( const wpi::math::LinearSystem<2, 1, 2>& drivePlant, wpi::units::volt_t driveKs, const wpi::math::DCMotor& driveMotor, double driveGearing, wpi::units::meter_t driveWheelRadius, const wpi::math::LinearSystem<2, 1, 2>& steerPlant, wpi::units::volt_t steerKs, const wpi::math::DCMotor& steerMotor, double steerGearing, const wpi::math::SwerveDriveKinematics& kinematics); void SetDriveInputs(const std::array& inputs); void SetSteerInputs(const std::array& inputs); static Eigen::Matrix CalculateX( const Eigen::Matrix& discA, const Eigen::Matrix& discB, const Eigen::Matrix& x, wpi::units::volt_t input, wpi::units::volt_t kS); void Update(wpi::units::second_t dt); void Reset(const wpi::math::Pose2d& pose, bool preserveMotion); void Reset(const wpi::math::Pose2d& pose, const std::array, numModules>& moduleDriveStates, const std::array, numModules>& moduleSteerStates); wpi::math::Pose2d GetPose() const; std::array GetModulePositions() const; std::array GetNoisyModulePositions(wpi::units::meter_t driveStdDev, wpi::units::radian_t steerStdDev); std::array GetModuleStates(); std::array, numModules> GetDriveStates() const; std::array, numModules> GetSteerStates() const; wpi::units::radians_per_second_t GetOmega() const; wpi::units::ampere_t GetCurrentDraw(const wpi::math::DCMotor& motor, wpi::units::radians_per_second_t velocity, wpi::units::volt_t inputVolts, wpi::units::volt_t batteryVolts) const; std::array GetDriveCurrentDraw() const; std::array GetSteerCurrentDraw() const; wpi::units::ampere_t GetTotalCurrentDraw() const; private: std::random_device rd{}; std::mt19937 generator{rd()}; std::normal_distribution randDist{0.0, 1.0}; const wpi::math::LinearSystem<2, 1, 2> drivePlant; const wpi::units::volt_t driveKs; const wpi::math::DCMotor driveMotor; const double driveGearing; const wpi::units::meter_t driveWheelRadius; const wpi::math::LinearSystem<2, 1, 2> steerPlant; const wpi::units::volt_t steerKs; const wpi::math::DCMotor steerMotor; const double steerGearing; const wpi::math::SwerveDriveKinematics kinematics; std::array driveInputs{}; std::array, numModules> driveStates{}; std::array steerInputs{}; std::array, numModules> steerStates{}; wpi::math::Pose2d pose{wpi::math::Pose2d{}}; wpi::units::radians_per_second_t omega{0}; };