mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
SCRIPT namespace replacements
This commit is contained in:
committed by
Peter Johnson
parent
ae6c043632
commit
9aca8e0fd6
@@ -28,11 +28,11 @@ Drivetrain::Drivetrain() {
|
||||
m_leftEncoder.Reset();
|
||||
m_rightEncoder.Reset();
|
||||
|
||||
frc::SmartDashboard::PutData("FieldSim", &m_fieldSim);
|
||||
frc::SmartDashboard::PutData("Approximation", &m_fieldApproximation);
|
||||
wpi::SmartDashboard::PutData("FieldSim", &m_fieldSim);
|
||||
wpi::SmartDashboard::PutData("Approximation", &m_fieldApproximation);
|
||||
}
|
||||
|
||||
void Drivetrain::SetSpeeds(const frc::DifferentialDriveWheelSpeeds& speeds) {
|
||||
void Drivetrain::SetSpeeds(const wpi::math::DifferentialDriveWheelSpeeds& speeds) {
|
||||
const auto leftFeedforward = m_feedforward.Calculate(speeds.left);
|
||||
const auto rightFeedforward = m_feedforward.Calculate(speeds.right);
|
||||
const double leftOutput = m_leftPIDController.Calculate(
|
||||
@@ -40,22 +40,22 @@ void Drivetrain::SetSpeeds(const frc::DifferentialDriveWheelSpeeds& speeds) {
|
||||
const double rightOutput = m_rightPIDController.Calculate(
|
||||
m_rightEncoder.GetRate(), speeds.right.value());
|
||||
|
||||
m_leftLeader.SetVoltage(units::volt_t{leftOutput} + leftFeedforward);
|
||||
m_rightLeader.SetVoltage(units::volt_t{rightOutput} + rightFeedforward);
|
||||
m_leftLeader.SetVoltage(wpi::units::volt_t{leftOutput} + leftFeedforward);
|
||||
m_rightLeader.SetVoltage(wpi::units::volt_t{rightOutput} + rightFeedforward);
|
||||
}
|
||||
|
||||
void Drivetrain::Drive(units::meters_per_second_t xSpeed,
|
||||
units::radians_per_second_t rot) {
|
||||
void Drivetrain::Drive(wpi::units::meters_per_second_t xSpeed,
|
||||
wpi::units::radians_per_second_t rot) {
|
||||
SetSpeeds(m_kinematics.ToWheelSpeeds({xSpeed, 0_mps, rot}));
|
||||
}
|
||||
|
||||
void Drivetrain::PublishCameraToObject(
|
||||
frc::Pose3d objectInField, frc::Transform3d robotToCamera,
|
||||
nt::DoubleArrayEntry& cameraToObjectEntry,
|
||||
frc::sim::DifferentialDrivetrainSim drivetrainSimulator) {
|
||||
frc::Pose3d robotInField{drivetrainSimulator.GetPose()};
|
||||
frc::Pose3d cameraInField = robotInField + robotToCamera;
|
||||
frc::Transform3d cameraToObject{cameraInField, objectInField};
|
||||
wpi::math::Pose3d objectInField, wpi::math::Transform3d robotToCamera,
|
||||
wpi::nt::DoubleArrayEntry& cameraToObjectEntry,
|
||||
wpi::sim::DifferentialDrivetrainSim drivetrainSimulator) {
|
||||
wpi::math::Pose3d robotInField{drivetrainSimulator.GetPose()};
|
||||
wpi::math::Pose3d cameraInField = robotInField + robotToCamera;
|
||||
wpi::math::Transform3d cameraToObject{cameraInField, objectInField};
|
||||
|
||||
// Publishes double array with Translation3D elements {x, y, z} and Rotation3D
|
||||
// elements {w, x, y, z} which describe the cameraToObject transformation.
|
||||
@@ -69,24 +69,24 @@ void Drivetrain::PublishCameraToObject(
|
||||
cameraToObjectEntry.Set(val);
|
||||
}
|
||||
|
||||
frc::Pose3d Drivetrain::ObjectToRobotPose(
|
||||
frc::Pose3d objectInField, frc::Transform3d robotToCamera,
|
||||
nt::DoubleArrayEntry& cameraToObjectEntry) {
|
||||
wpi::math::Pose3d Drivetrain::ObjectToRobotPose(
|
||||
wpi::math::Pose3d objectInField, wpi::math::Transform3d robotToCamera,
|
||||
wpi::nt::DoubleArrayEntry& cameraToObjectEntry) {
|
||||
std::vector<double> val{cameraToObjectEntry.Get()};
|
||||
|
||||
// Reconstruct cameraToObject Transform3D from networktables.
|
||||
frc::Translation3d translation{units::meter_t{val[0]}, units::meter_t{val[1]},
|
||||
units::meter_t{val[2]}};
|
||||
frc::Rotation3d rotation{frc::Quaternion{val[3], val[4], val[5], val[6]}};
|
||||
frc::Transform3d cameraToObject{translation, rotation};
|
||||
wpi::math::Translation3d translation{wpi::units::meter_t{val[0]}, wpi::units::meter_t{val[1]},
|
||||
wpi::units::meter_t{val[2]}};
|
||||
wpi::math::Rotation3d rotation{wpi::math::Quaternion{val[3], val[4], val[5], val[6]}};
|
||||
wpi::math::Transform3d cameraToObject{translation, rotation};
|
||||
|
||||
return frc::ObjectToRobotPose(objectInField, cameraToObject, robotToCamera);
|
||||
return wpi::math::ObjectToRobotPose(objectInField, cameraToObject, robotToCamera);
|
||||
}
|
||||
|
||||
void Drivetrain::UpdateOdometry() {
|
||||
m_poseEstimator.Update(m_imu.GetRotation2d(),
|
||||
units::meter_t{m_leftEncoder.GetDistance()},
|
||||
units::meter_t{m_rightEncoder.GetDistance()});
|
||||
wpi::units::meter_t{m_leftEncoder.GetDistance()},
|
||||
wpi::units::meter_t{m_rightEncoder.GetDistance()});
|
||||
|
||||
// Publish cameraToObject transformation to networktables --this would
|
||||
// normally be handled by the computer vision solution.
|
||||
@@ -95,28 +95,28 @@ void Drivetrain::UpdateOdometry() {
|
||||
|
||||
// Compute the robot's field-relative position exclusively from vision
|
||||
// measurements.
|
||||
frc::Pose3d visionMeasurement3d = ObjectToRobotPose(
|
||||
wpi::math::Pose3d visionMeasurement3d = ObjectToRobotPose(
|
||||
m_objectInField, m_robotToCamera, m_cameraToObjectEntryRef);
|
||||
|
||||
// Convert robot's pose from Pose3d to Pose2d needed to apply vision
|
||||
// Convert robot's pose from wpi::math::Pose3d to wpi::math::Pose2d needed to apply vision
|
||||
// measurements.
|
||||
frc::Pose2d visionMeasurement2d = visionMeasurement3d.ToPose2d();
|
||||
wpi::math::Pose2d visionMeasurement2d = visionMeasurement3d.ToPose2d();
|
||||
|
||||
// Apply vision measurements. For simulation purposes only, we don't input a
|
||||
// latency delay -- on a real robot, this must be calculated based either on
|
||||
// known latency or timestamps.
|
||||
m_poseEstimator.AddVisionMeasurement(visionMeasurement2d,
|
||||
frc::Timer::GetTimestamp());
|
||||
wpi::Timer::GetTimestamp());
|
||||
}
|
||||
|
||||
void Drivetrain::SimulationPeriodic() {
|
||||
// To update our simulation, we set motor voltage inputs, update the
|
||||
// simulation, and write the simulated positions and velocities to our
|
||||
// simulated encoder and gyro.
|
||||
m_drivetrainSimulator.SetInputs(units::volt_t{m_leftLeader.Get()} *
|
||||
frc::RobotController::GetInputVoltage(),
|
||||
units::volt_t{m_rightLeader.Get()} *
|
||||
frc::RobotController::GetInputVoltage());
|
||||
m_drivetrainSimulator.SetInputs(wpi::units::volt_t{m_leftLeader.Get()} *
|
||||
wpi::RobotController::GetInputVoltage(),
|
||||
wpi::units::volt_t{m_rightLeader.Get()} *
|
||||
wpi::RobotController::GetInputVoltage());
|
||||
m_drivetrainSimulator.Update(20_ms);
|
||||
|
||||
m_leftEncoderSim.SetDistance(m_drivetrainSimulator.GetLeftPosition().value());
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#include "Drivetrain.hpp"
|
||||
|
||||
class Robot : public frc::TimedRobot {
|
||||
class Robot : public wpi::TimedRobot {
|
||||
public:
|
||||
void AutonomousPeriodic() override {
|
||||
TeleopPeriodic();
|
||||
@@ -36,18 +36,18 @@ class Robot : public frc::TimedRobot {
|
||||
void SimulationPeriodic() override { m_drive.SimulationPeriodic(); }
|
||||
|
||||
private:
|
||||
frc::XboxController m_controller{0};
|
||||
wpi::XboxController m_controller{0};
|
||||
|
||||
// Slew rate limiters to make joystick inputs more gentle; 1/3 sec from 0
|
||||
// to 1.
|
||||
frc::SlewRateLimiter<units::scalar> m_speedLimiter{3 / 1_s};
|
||||
frc::SlewRateLimiter<units::scalar> m_rotLimiter{3 / 1_s};
|
||||
wpi::math::SlewRateLimiter<wpi::units::scalar> m_speedLimiter{3 / 1_s};
|
||||
wpi::math::SlewRateLimiter<wpi::units::scalar> m_rotLimiter{3 / 1_s};
|
||||
|
||||
Drivetrain m_drive;
|
||||
};
|
||||
|
||||
#ifndef RUNNING_FRC_TESTS
|
||||
int main() {
|
||||
return frc::StartRobot<Robot>();
|
||||
return wpi::StartRobot<Robot>();
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -41,9 +41,9 @@ class Drivetrain {
|
||||
public:
|
||||
Drivetrain();
|
||||
|
||||
static constexpr units::meters_per_second_t kMaxSpeed =
|
||||
static constexpr wpi::units::meters_per_second_t kMaxSpeed =
|
||||
3.0_mps; // 3 meters per second
|
||||
static constexpr units::radians_per_second_t kMaxAngularSpeed{
|
||||
static constexpr wpi::units::radians_per_second_t kMaxAngularSpeed{
|
||||
std::numbers::pi}; // 1/2 rotation per second
|
||||
|
||||
/**
|
||||
@@ -51,15 +51,15 @@ class Drivetrain {
|
||||
*
|
||||
* @param speeds The desired wheel speeds.
|
||||
*/
|
||||
void SetSpeeds(const frc::DifferentialDriveWheelSpeeds& speeds);
|
||||
void SetSpeeds(const wpi::math::DifferentialDriveWheelSpeeds& speeds);
|
||||
|
||||
/** Drives the robot with the given linear velocity and angular velocity.
|
||||
*
|
||||
* @param xSpeed Linear velocity.
|
||||
* @param rot Angular Velocity.
|
||||
*/
|
||||
void Drive(units::meters_per_second_t xSpeed,
|
||||
units::radians_per_second_t rot);
|
||||
void Drive(wpi::units::meters_per_second_t xSpeed,
|
||||
wpi::units::radians_per_second_t rot);
|
||||
|
||||
/**
|
||||
* Updates the field-relative position.
|
||||
@@ -90,9 +90,9 @@ class Drivetrain {
|
||||
* robot's drivetrain.
|
||||
*/
|
||||
void PublishCameraToObject(
|
||||
frc::Pose3d objectInField, frc::Transform3d robotToCamera,
|
||||
nt::DoubleArrayEntry& cameraToObjectEntry,
|
||||
frc::sim::DifferentialDrivetrainSim drivetrainSimulator);
|
||||
wpi::math::Pose3d objectInField, wpi::math::Transform3d robotToCamera,
|
||||
wpi::nt::DoubleArrayEntry& cameraToObjectEntry,
|
||||
wpi::sim::DifferentialDrivetrainSim drivetrainSimulator);
|
||||
|
||||
/**
|
||||
* Queries the camera-to-object transformation from networktables to compute
|
||||
@@ -106,71 +106,71 @@ class Drivetrain {
|
||||
* @param cameraToObjectEntry The networktables entry publishing and querying
|
||||
* example computer vision measurements.
|
||||
*/
|
||||
frc::Pose3d ObjectToRobotPose(frc::Pose3d objectInField,
|
||||
frc::Transform3d robotToCamera,
|
||||
nt::DoubleArrayEntry& cameraToObjectEntry);
|
||||
wpi::math::Pose3d ObjectToRobotPose(wpi::math::Pose3d objectInField,
|
||||
wpi::math::Transform3d robotToCamera,
|
||||
wpi::nt::DoubleArrayEntry& cameraToObjectEntry);
|
||||
|
||||
private:
|
||||
static constexpr units::meter_t kTrackwidth = 0.381_m * 2;
|
||||
static constexpr units::meter_t kWheelRadius = 0.0508_m;
|
||||
static constexpr wpi::units::meter_t kTrackwidth = 0.381_m * 2;
|
||||
static constexpr wpi::units::meter_t kWheelRadius = 0.0508_m;
|
||||
static constexpr int kEncoderResolution = 4096;
|
||||
|
||||
static constexpr std::array<double, 7> kDefaultVal{0.0, 0.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.0};
|
||||
|
||||
frc::Transform3d m_robotToCamera{
|
||||
frc::Translation3d{1_m, 1_m, 1_m},
|
||||
frc::Rotation3d{0_rad, 0_rad, units::radian_t{std::numbers::pi / 2}}};
|
||||
wpi::math::Transform3d m_robotToCamera{
|
||||
wpi::math::Translation3d{1_m, 1_m, 1_m},
|
||||
wpi::math::Rotation3d{0_rad, 0_rad, wpi::units::radian_t{std::numbers::pi / 2}}};
|
||||
|
||||
nt::NetworkTableInstance m_inst{nt::NetworkTableInstance::GetDefault()};
|
||||
nt::DoubleArrayTopic m_cameraToObjectTopic{
|
||||
wpi::nt::NetworkTableInstance m_inst{wpi::nt::NetworkTableInstance::GetDefault()};
|
||||
wpi::nt::DoubleArrayTopic m_cameraToObjectTopic{
|
||||
m_inst.GetDoubleArrayTopic("m_cameraToObjectTopic")};
|
||||
nt::DoubleArrayEntry m_cameraToObjectEntry =
|
||||
wpi::nt::DoubleArrayEntry m_cameraToObjectEntry =
|
||||
m_cameraToObjectTopic.GetEntry(kDefaultVal);
|
||||
nt::DoubleArrayEntry& m_cameraToObjectEntryRef = m_cameraToObjectEntry;
|
||||
wpi::nt::DoubleArrayEntry& m_cameraToObjectEntryRef = m_cameraToObjectEntry;
|
||||
|
||||
frc::AprilTagFieldLayout m_aprilTagFieldLayout{
|
||||
frc::AprilTagFieldLayout::LoadField(frc::AprilTagField::k2024Crescendo)};
|
||||
frc::Pose3d m_objectInField{m_aprilTagFieldLayout.GetTagPose(0).value()};
|
||||
wpi::apriltag::AprilTagFieldLayout m_aprilTagFieldLayout{
|
||||
wpi::apriltag::AprilTagFieldLayout::LoadField(wpi::apriltag::AprilTagField::k2024Crescendo)};
|
||||
wpi::math::Pose3d m_objectInField{m_aprilTagFieldLayout.GetTagPose(0).value()};
|
||||
|
||||
frc::PWMSparkMax m_leftLeader{1};
|
||||
frc::PWMSparkMax m_leftFollower{2};
|
||||
frc::PWMSparkMax m_rightLeader{3};
|
||||
frc::PWMSparkMax m_rightFollower{4};
|
||||
wpi::PWMSparkMax m_leftLeader{1};
|
||||
wpi::PWMSparkMax m_leftFollower{2};
|
||||
wpi::PWMSparkMax m_rightLeader{3};
|
||||
wpi::PWMSparkMax m_rightFollower{4};
|
||||
|
||||
frc::Encoder m_leftEncoder{0, 1};
|
||||
frc::Encoder m_rightEncoder{2, 3};
|
||||
wpi::Encoder m_leftEncoder{0, 1};
|
||||
wpi::Encoder m_rightEncoder{2, 3};
|
||||
|
||||
frc::PIDController m_leftPIDController{1.0, 0.0, 0.0};
|
||||
frc::PIDController m_rightPIDController{1.0, 0.0, 0.0};
|
||||
wpi::math::PIDController m_leftPIDController{1.0, 0.0, 0.0};
|
||||
wpi::math::PIDController m_rightPIDController{1.0, 0.0, 0.0};
|
||||
|
||||
frc::OnboardIMU m_imu{frc::OnboardIMU::kFlat};
|
||||
wpi::OnboardIMU m_imu{wpi::OnboardIMU::kFlat};
|
||||
|
||||
frc::DifferentialDriveKinematics m_kinematics{kTrackwidth};
|
||||
wpi::math::DifferentialDriveKinematics m_kinematics{kTrackwidth};
|
||||
|
||||
// Gains are for example purposes only - must be determined for your own
|
||||
// robot!
|
||||
frc::DifferentialDrivePoseEstimator m_poseEstimator{
|
||||
wpi::math::DifferentialDrivePoseEstimator m_poseEstimator{
|
||||
m_kinematics,
|
||||
m_imu.GetRotation2d(),
|
||||
units::meter_t{m_leftEncoder.GetDistance()},
|
||||
units::meter_t{m_rightEncoder.GetDistance()},
|
||||
frc::Pose2d{},
|
||||
wpi::units::meter_t{m_leftEncoder.GetDistance()},
|
||||
wpi::units::meter_t{m_rightEncoder.GetDistance()},
|
||||
wpi::math::Pose2d{},
|
||||
{0.01, 0.01, 0.01},
|
||||
{0.1, 0.1, 0.1}};
|
||||
|
||||
// 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};
|
||||
wpi::math::SimpleMotorFeedforward<wpi::units::meters> m_feedforward{1_V, 3_V / 1_mps};
|
||||
|
||||
// Simulation classes
|
||||
frc::sim::EncoderSim m_leftEncoderSim{m_leftEncoder};
|
||||
frc::sim::EncoderSim m_rightEncoderSim{m_rightEncoder};
|
||||
frc::Field2d m_fieldSim;
|
||||
frc::Field2d m_fieldApproximation;
|
||||
frc::LinearSystem<2, 2, 2> m_drivetrainSystem =
|
||||
frc::LinearSystemId::IdentifyDrivetrainSystem(
|
||||
wpi::sim::EncoderSim m_leftEncoderSim{m_leftEncoder};
|
||||
wpi::sim::EncoderSim m_rightEncoderSim{m_rightEncoder};
|
||||
wpi::Field2d m_fieldSim;
|
||||
wpi::Field2d m_fieldApproximation;
|
||||
wpi::math::LinearSystem<2, 2, 2> m_drivetrainSystem =
|
||||
wpi::math::LinearSystemId::IdentifyDrivetrainSystem(
|
||||
1.98_V / 1_mps, 0.2_V / 1_mps_sq, 1.5_V / 1_mps, 0.3_V / 1_mps_sq);
|
||||
frc::sim::DifferentialDrivetrainSim m_drivetrainSimulator{
|
||||
m_drivetrainSystem, kTrackwidth, frc::DCMotor::CIM(2), 8, 2_in};
|
||||
wpi::sim::DifferentialDrivetrainSim m_drivetrainSimulator{
|
||||
m_drivetrainSystem, kTrackwidth, wpi::math::DCMotor::CIM(2), 8, 2_in};
|
||||
};
|
||||
|
||||
@@ -13,12 +13,12 @@
|
||||
*/
|
||||
class ExampleGlobalMeasurementSensor {
|
||||
public:
|
||||
static frc::Pose2d GetEstimatedGlobalPose(
|
||||
const frc::Pose2d& estimatedRobotPose) {
|
||||
auto randVec = frc::MakeWhiteNoiseVector(0.1, 0.1, 0.1);
|
||||
return frc::Pose2d{estimatedRobotPose.X() + units::meter_t{randVec(0)},
|
||||
estimatedRobotPose.Y() + units::meter_t{randVec(1)},
|
||||
static wpi::math::Pose2d GetEstimatedGlobalPose(
|
||||
const wpi::math::Pose2d& estimatedRobotPose) {
|
||||
auto randVec = wpi::math::MakeWhiteNoiseVector(0.1, 0.1, 0.1);
|
||||
return wpi::math::Pose2d{estimatedRobotPose.X() + wpi::units::meter_t{randVec(0)},
|
||||
estimatedRobotPose.Y() + wpi::units::meter_t{randVec(1)},
|
||||
estimatedRobotPose.Rotation() +
|
||||
frc::Rotation2d{units::radian_t{randVec(2)}}};
|
||||
wpi::math::Rotation2d{wpi::units::radian_t{randVec(2)}}};
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user