Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CoryNessCTR committed Dec 8, 2023
1 parent 9f8adee commit 5225e1f
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 86 deletions.
2 changes: 1 addition & 1 deletion java/CANcoder/src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Robot extends TimedRobot {
private final DutyCycleOut fwdOut = new DutyCycleOut(0);
private final XboxController controller = new XboxController(0);

double currentTime = Timer.getFPGATimestamp();
private double currentTime = Timer.getFPGATimestamp();

private final Mechanisms mechanism = new Mechanisms();

Expand Down
24 changes: 8 additions & 16 deletions java/CANcoder/src/main/java/frc/robot/sim/TalonFXSimProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.simulation.DCMotorSim;
import frc.robot.sim.PhysicsSim.SimProfile;

Expand All @@ -21,11 +20,10 @@ class TalonFXSimProfile extends SimProfile {

private final DCMotorSim _motorSim;
private final CANcoderSimState _cancoderSim; // We need a sim state in order to change the values of CANcoder
private final XboxController m_controller = new XboxController(0);

/**
* Creates a new simulation profile for a TalonFX device.
*
*
* @param falcon
* The TalonFX device
* @param accelToFullTime
Expand All @@ -46,7 +44,7 @@ public TalonFXSimProfile(final TalonFX falcon, final CANcoder canCoder, final do

/**
* Runs the simulation profile.
*
*
* This uses very rudimentary physics simulation and exists to allow users to
* test features of our products in simulation using our examples out of the
* box. Users may modify this to utilize more accurate physics simulation.
Expand All @@ -58,24 +56,18 @@ public void run() {
_motorSim.update(getPeriod());

// SET SIM PHYSICS INPUTS
double velocity_rps = Units.radiansToRotations(_motorSim.getAngularVelocityRadPerSec());
double position = _motorSim.getAngularPositionRotations();
double velocity = Units.radiansToRotations(_motorSim.getAngularVelocityRadPerSec());

_falcon.getSimState().setRawRotorPosition(_motorSim.getAngularPositionRotations());
_falcon.getSimState().setRotorVelocity(velocity_rps);
_falcon.getSimState().setRawRotorPosition(position);
_falcon.getSimState().setRotorVelocity(velocity);

_falcon.getSimState().setSupplyVoltage(12 - _falcon.getSimState().getSupplyCurrent() * kMotorResistance);

_canCoder.getSimState().setRawPosition(_motorSim.getAngularPositionRotations()/_gearRatio);
_canCoder.getSimState().setVelocity(velocity_rps/_gearRatio);
_canCoder.getSimState().setRawPosition(_motorSim.getAngularPositionRotations() / _gearRatio);
_canCoder.getSimState().setVelocity(velocity / _gearRatio);

double Yaxis = m_controller.getLeftY();
double motorVoltage = Yaxis * 12; // Scales joystick axcis to motor voltage ( +-12v)
_motorSim.setInputVoltage(motorVoltage);
_motorSim.update(.02);
double position = _motorSim.getAngularPositionRotations();
double velocity = _motorSim.getAngularVelocityRPM()/60;
_cancoderSim.setRawPosition(position);
_cancoderSim.setVelocity(velocity);

}
}
1 change: 0 additions & 1 deletion java/FusedCANcoder/src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public void testPeriodic() {}
@Override
public void simulationInit() {
PhysicsSim.getInstance().addTalonFX(m_fx, m_cc, 25, 0.001);
cc_pos.setUpdateFrequency(10);
}

@Override
Expand Down
22 changes: 11 additions & 11 deletions java/MotionMagic/src/main/java/frc/robot/Mechanisms.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
public class Mechanisms {
double HEIGHT = 1; // Controls the height of the mech2d SmartDashboard
double WIDTH = 1; // Controls the height of the mech2d SmartDashboard

Mechanism2d mech = new Mechanism2d(WIDTH, HEIGHT);
// Velocity
MechanismLigament2d VelocityMech = mech.
getRoot("velocityLineReferencePosition", 0.75, 0.5).
append(new MechanismLigament2d("velocityLine", 1,90, 6, new Color8Bit(Color.kAliceBlue)));

MechanismLigament2d midline = mech.
getRoot("midline", 0.7, 0.5).
append(new MechanismLigament2d("midline", 0.1, 0, 3, new Color8Bit(Color.kCyan)));
//Position

//Position
MechanismLigament2d arm = mech.
getRoot("pivotPoint", 0.25, 0.5).
append(new MechanismLigament2d("arm", .2, 0, 0, new Color8Bit(Color.kAliceBlue)));

MechanismLigament2d side1 = arm.append(new MechanismLigament2d("side1", 0.15307, 112.5, 6, new Color8Bit(Color.kAliceBlue)));
MechanismLigament2d side2 = side1.append(new MechanismLigament2d("side2", 0.15307, 45, 6, new Color8Bit(Color.kAliceBlue)));
MechanismLigament2d side3 = side2.append(new MechanismLigament2d("side3", 0.15307, 45, 6, new Color8Bit(Color.kAliceBlue)));
Expand All @@ -41,14 +41,14 @@ public class Mechanisms {

/**
* Runs the mech2d widget in GUI.
*
* This utilizes GUI to simulate and display a TalonFX and exists to allow users to test and understand
* features of our products in simulation using our examples out of the box. Users may modify to have a
*
* This utilizes GUI to simulate and display a TalonFX and exists to allow users to test and understand
* features of our products in simulation using our examples out of the box. Users may modify to have a
* display interface that they find more intuitive or visually appealing.
*/
public void update(StatusSignal<Double> position, StatusSignal<Double> velocity) {
*/
public void update(StatusSignal<Double> position, StatusSignal<Double> velocity) {
VelocityMech.setLength(velocity.getValue()/120); // Divide by 120 to scale motion to fit in the window
arm.setAngle(position.getValue() * 360);
SmartDashboard.putData("mech2d", mech); // Creates mech2d in SmartDashboard
}
}
}
25 changes: 0 additions & 25 deletions java/Pigeon2/src/main/java/frc/robot/Mechanisms.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package frc.robot;

import com.ctre.phoenix6.StatusSignal;
import com.ctre.phoenix6.hardware.Pigeon2;
import com.ctre.phoenix6.sim.Pigeon2SimState;

import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.simulation.DCMotorSim;
import edu.wpi.first.wpilibj.smartdashboard.Mechanism2d;
import edu.wpi.first.wpilibj.smartdashboard.MechanismLigament2d;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
Expand All @@ -22,9 +17,6 @@ public class Mechanisms {
private final double ROOT_X = WIDTH / 2;
private final double ROOT_Y = HEIGHT / 2;

private final Pigeon2SimState pidgeySim; // We need a sim state in order to change the values of pidgey
private final DCMotorSim motorSim = new DCMotorSim(DCMotor.getFalcon500(1), 100, 100);
private final XboxController controller = new XboxController(0); // Uses an Xbox controller for setting the CANcoder simulation
private Mechanism2d mech = new Mechanism2d(WIDTH, HEIGHT); // Main mechanism object
private MechanismLigament2d wrist = mech.
getRoot("base", ROOT_X, ROOT_Y).
Expand All @@ -35,25 +27,8 @@ public class Mechanisms {
@SuppressWarnings("unused")
private MechanismLigament2d rightArrow = wrist.append(new MechanismLigament2d("RightArrow", 0.1, -150, 6, new Color8Bit(Color.kAliceBlue)));

public Mechanisms(Pigeon2 pigeon) {
pidgeySim = pigeon.getSimState();
}

public void update(StatusSignal<Double> position) {
wrist.setAngle(position.getValue());
SmartDashboard.putData("mech2d", mech);
}

/**
* Didn't want to move in our full physics class, since this is just for pigeon
* Call this periodically in the simulation periodic method
*/
public void simUpdate(){
double Yaxis = controller.getLeftY();
double motorVoltage = Yaxis * 12; // scales joystick axis to motor voltage ( +-12v)
motorSim.setInputVoltage(motorVoltage);
motorSim.update(.02);
double position = motorSim.getAngularPositionRotations()*360;
pidgeySim.setRawYaw(position);
}
}
21 changes: 17 additions & 4 deletions java/Pigeon2/src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
package frc.robot;

import com.ctre.phoenix6.configs.Pigeon2Configuration;
import com.ctre.phoenix6.controls.DutyCycleOut;
import com.ctre.phoenix6.hardware.Pigeon2;
import com.ctre.phoenix6.hardware.TalonFX;

import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.XboxController;
import frc.robot.sim.PhysicsSim;

/**
* The VM is configured to automatically run this class, and to call the functions corresponding to
Expand All @@ -19,10 +23,15 @@
public class Robot extends TimedRobot {
private final double PRINT_PERIOD = 0.5; // Update every 500 ms

/* Keep a reference for a TalonFX around so we can drive the thing the Pigeon is on */
private final TalonFX talonfx = new TalonFX(0, "rio");
private final Pigeon2 pidgey = new Pigeon2(1, "rio");
private double currentTime = Timer.getFPGATimestamp();

private final Mechanisms mechanisms = new Mechanisms(pidgey);
private final XboxController joystick = new XboxController(0);
private final DutyCycleOut control = new DutyCycleOut(0);

private final Mechanisms mechanisms = new Mechanisms();

/**
* This function is run when the robot is first started up and should be used for any
Expand Down Expand Up @@ -101,7 +110,9 @@ public void teleopInit() {
}

@Override
public void teleopPeriodic() {}
public void teleopPeriodic() {
talonfx.setControl(control.withOutput(joystick.getLeftY()));
}

@Override
public void disabledInit() {}
Expand All @@ -116,10 +127,12 @@ public void testInit() {}
public void testPeriodic() {}

@Override
public void simulationInit() {}
public void simulationInit() {
PhysicsSim.getInstance().addTalonFX(talonfx, pidgey, 0.001);
}

@Override
public void simulationPeriodic() {
mechanisms.simUpdate();
PhysicsSim.getInstance().run();
}
}
86 changes: 86 additions & 0 deletions java/Pigeon2/src/main/java/frc/robot/sim/PhysicsSim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package frc.robot.sim;

import java.util.ArrayList;

import com.ctre.phoenix6.Utils;
import com.ctre.phoenix6.hardware.Pigeon2;
import com.ctre.phoenix6.hardware.TalonFX;

/**
* Manages physics simulation for CTRE products.
*/
public class PhysicsSim {
private static final PhysicsSim sim = new PhysicsSim();

/**
* Gets the robot simulator instance.
*/
public static PhysicsSim getInstance() {
return sim;
}

/**
* Adds a TalonFX controller to the simulator.
*
* @param falcon
* The TalonFX device
* @param can
* The CANcoder device
* @param gearRatio
* The gear reduction of the TalonFX
* @param rotorInertia
* Rotational Inertia of the mechanism at the rotor
*/
public void addTalonFX(TalonFX falcon, Pigeon2 pigeon, final double rotorInertia) {
if (falcon != null) {
TalonFXSimProfile simFalcon = new TalonFXSimProfile(falcon, pigeon, rotorInertia);
_simProfiles.add(simFalcon);
}
}

/**
* Runs the simulator:
* - enable the robot
* - simulate sensors
*/
public void run() {
// Simulate devices
for (SimProfile simProfile : _simProfiles) {
simProfile.run();
}
}

private final ArrayList<SimProfile> _simProfiles = new ArrayList<SimProfile>();

/**
* Holds information about a simulated device.
*/
static class SimProfile {
private double _lastTime;
private boolean _running = false;

/**
* Runs the simulation profile.
* Implemented by device-specific profiles.
*/
public void run() {
}

/**
* Returns the time since last call, in seconds.
*/
protected double getPeriod() {
// set the start time if not yet running
if (!_running) {
_lastTime = Utils.getCurrentTimeSeconds();
_running = true;
}

double now = Utils.getCurrentTimeSeconds();
final double period = now - _lastTime;
_lastTime = now;

return period;
}
}
}
65 changes: 65 additions & 0 deletions java/Pigeon2/src/main/java/frc/robot/sim/TalonFXSimProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package frc.robot.sim;

import com.ctre.phoenix6.hardware.Pigeon2;
import com.ctre.phoenix6.hardware.TalonFX;

import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.simulation.DCMotorSim;
import frc.robot.sim.PhysicsSim.SimProfile;

/**
* Holds information about a simulated TalonFX.
*/
class TalonFXSimProfile extends SimProfile {
private final double kMotorResistance = 0.002; // Assume 2mOhm resistance for voltage drop calculation
private final TalonFX _falcon;
private final Pigeon2 _pigeon;

private final DCMotorSim _motorSim;

/**
* Creates a new simulation profile for a TalonFX device.
*
* @param falcon
* The TalonFX device
* @param accelToFullTime
* The time the motor takes to accelerate from 0 to full,
* in seconds
* @param fullVel
* The maximum motor velocity, in rotations per second
* @param sensorPhase
* The phase of the TalonFX sensors
*/
public TalonFXSimProfile(final TalonFX falcon, final Pigeon2 pigeon, final double rotorInertia) {
this._falcon = falcon;
this._pigeon = pigeon;
this._motorSim = new DCMotorSim(DCMotor.getFalcon500(1), 1.0, rotorInertia);
}

/**
* Runs the simulation profile.
*
* This uses very rudimentary physics simulation and exists to allow users to
* test features of our products in simulation using our examples out of the
* box. Users may modify this to utilize more accurate physics simulation.
*/
public void run() {
/// DEVICE SPEED SIMULATION

_motorSim.setInputVoltage(_falcon.getSimState().getMotorVoltage());

_motorSim.update(getPeriod());

/// SET SIM PHYSICS INPUTS
double velocity_rps = Units.radiansToRotations(_motorSim.getAngularVelocityRadPerSec());

_falcon.getSimState().setRawRotorPosition(_motorSim.getAngularPositionRotations());
_falcon.getSimState().setRotorVelocity(velocity_rps);

_falcon.getSimState().setSupplyVoltage(12 - _falcon.getSimState().getSupplyCurrent() * kMotorResistance);

_pigeon.getSimState().setRawYaw(_motorSim.getAngularPositionRotations());

}
}
Loading

0 comments on commit 5225e1f

Please sign in to comment.