forked from wpilibsuite/allwpilib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] Add angular subsystem to SysIdRoutine example (wpilibsuite…
…#6297) Co-authored-by: Tim Winters <[email protected]>
- Loading branch information
1 parent
62cba9a
commit da3abad
Showing
9 changed files
with
304 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
wpilibcExamples/src/main/cpp/examples/SysId/cpp/subsystems/Shooter.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 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. | ||
|
||
#include "subsystems/Shooter.h" | ||
|
||
#include <frc2/command/Commands.h> | ||
#include <units/angle.h> | ||
#include <units/voltage.h> | ||
|
||
Shooter::Shooter() { | ||
m_shooterEncoder.SetDistancePerPulse( | ||
constants::shooter::kEncoderDistancePerPulse.value()); | ||
} | ||
|
||
frc2::CommandPtr Shooter::RunShooterCommand( | ||
std::function<double()> shooterSpeed) { | ||
return frc2::cmd::Run( | ||
[this, shooterSpeed] { | ||
m_shooterMotor.SetVoltage( | ||
units::volt_t{m_shooterFeedback.Calculate( | ||
m_shooterEncoder.GetRate(), shooterSpeed())} + | ||
m_shooterFeedforward.Calculate( | ||
units::turns_per_second_t{shooterSpeed()})); | ||
m_feederMotor.Set(constants::shooter::kFeederSpeed); | ||
}, | ||
{this}) | ||
.WithName("Set Shooter Speed"); | ||
} | ||
|
||
frc2::CommandPtr Shooter::SysIdQuasistatic(frc2::sysid::Direction direction) { | ||
return m_sysIdRoutine.Quasistatic(direction); | ||
} | ||
|
||
frc2::CommandPtr Shooter::SysIdDynamic(frc2::sysid::Direction direction) { | ||
return m_sysIdRoutine.Dynamic(direction); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
wpilibcExamples/src/main/cpp/examples/SysId/include/subsystems/Shooter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
|
||
#include <frc/Encoder.h> | ||
#include <frc/RobotController.h> | ||
#include <frc/controller/PIDController.h> | ||
#include <frc/controller/SimpleMotorFeedforward.h> | ||
#include <frc/motorcontrol/PWMSparkMax.h> | ||
#include <frc2/command/SubsystemBase.h> | ||
#include <frc2/command/sysid/SysIdRoutine.h> | ||
|
||
#include "Constants.h" | ||
|
||
class Shooter : public frc2::SubsystemBase { | ||
public: | ||
Shooter(); | ||
|
||
frc2::CommandPtr RunShooterCommand(std::function<double()> shooterSpeed); | ||
frc2::CommandPtr SysIdQuasistatic(frc2::sysid::Direction direction); | ||
frc2::CommandPtr SysIdDynamic(frc2::sysid::Direction direction); | ||
|
||
private: | ||
frc::PWMSparkMax m_shooterMotor{constants::shooter::kShooterMotorPort}; | ||
frc::PWMSparkMax m_feederMotor{constants::shooter::kFeederMotorPort}; | ||
|
||
frc::Encoder m_shooterEncoder{constants::shooter::kEncoderPorts[0], | ||
constants::shooter::kEncoderPorts[1], | ||
constants::shooter::kEncoderReversed}; | ||
|
||
frc2::sysid::SysIdRoutine m_sysIdRoutine{ | ||
frc2::sysid::Config{std::nullopt, std::nullopt, std::nullopt, | ||
std::nullopt}, | ||
frc2::sysid::Mechanism{ | ||
[this](units::volt_t driveVoltage) { | ||
m_shooterMotor.SetVoltage(driveVoltage); | ||
}, | ||
[this](frc::sysid::SysIdRoutineLog* log) { | ||
log->Motor("shooter-wheel") | ||
.voltage(m_shooterMotor.Get() * | ||
frc::RobotController::GetBatteryVoltage()) | ||
.position(units::turn_t{m_shooterEncoder.GetDistance()}) | ||
.velocity( | ||
units::turns_per_second_t{m_shooterEncoder.GetRate()}); | ||
}, | ||
this}}; | ||
frc::PIDController m_shooterFeedback{constants::shooter::kP, 0, 0}; | ||
frc::SimpleMotorFeedforward<units::turns> m_shooterFeedforward{ | ||
constants::shooter::kS, constants::shooter::kV, constants::shooter::kA}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.