Skip to content

Commit 8d17e1b

Browse files
[AARD-1867] Minor fixes for code simulation (#1113)
* Minor fixes for code simulation * cleanup: Format * fix: Disable SSL
1 parent 434468e commit 8d17e1b

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

fission/src/systems/simulation/wpilib_brain/WPILibBrain.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ export function getSimMap(): SimMap | undefined {
137137
export class SimGeneric {
138138
private constructor() {}
139139

140+
public static GetUnsafe<T>(simType: SimType, device: string, field: string): T | undefined
141+
public static GetUnsafe<T>(simType: SimType, device: string, field: string, defaultValue: T): T
142+
public static GetUnsafe<T>(simType: SimType, device: string, field: string, defaultValue?: T): T | undefined {
143+
const map = getSimMap()?.get(simType)
144+
if (!map) {
145+
// console.warn(`No '${simType}' devices found`)
146+
return undefined
147+
}
148+
149+
const data = map.get(device)
150+
if (!data) {
151+
// console.warn(`No '${simType}' device '${device}' found`)
152+
return undefined
153+
}
154+
155+
return (data.get(field) as T | undefined) ?? defaultValue
156+
}
157+
140158
public static Get<T>(simType: SimType, device: string, field: string): T | undefined
141159
public static Get<T>(simType: SimType, device: string, field: string, defaultValue: T): T
142160
public static Get<T>(simType: SimType, device: string, field: string, defaultValue?: T): T | undefined {
@@ -214,6 +232,10 @@ export class SimDriverStation {
214232
SimGeneric.Set<string>(SimType.DriverStation, "", ">match_time", gameData)
215233
}
216234

235+
public static IsEnabled(): boolean {
236+
return SimGeneric.GetUnsafe<boolean>(SimType.DriverStation, "", ">enabled", false)
237+
}
238+
217239
public static SetMode(mode: RobotSimMode) {
218240
SimGeneric.Set<boolean>(SimType.DriverStation, "", ">enabled", mode != RobotSimMode.Disabled)
219241
SimGeneric.Set<boolean>(SimType.DriverStation, "", ">autonomous", mode == RobotSimMode.Auto)
@@ -228,7 +250,7 @@ export class SimPWM {
228250
private constructor() {}
229251

230252
public static GetSpeed(device: string): number | undefined {
231-
return SimGeneric.Get(SimType.PWM, device, PWM_SPEED, 0.0)
253+
return SimDriverStation.IsEnabled() ? SimGeneric.Get(SimType.PWM, device, PWM_SPEED, 0.0) : 0.0
232254
}
233255

234256
public static GetPosition(device: string): number | undefined {
@@ -268,7 +290,9 @@ export class SimCANMotor {
268290
private constructor() {}
269291

270292
public static GetPercentOutput(device: string): number | undefined {
271-
return SimGeneric.Get(SimType.CANMotor, device, CANMOTOR_PERCENT_OUTPUT, 0.0)
293+
return SimDriverStation.IsEnabled()
294+
? SimGeneric.Get(SimType.CANMotor, device, CANMOTOR_PERCENT_OUTPUT, 0.0)
295+
: 0.0
272296
}
273297

274298
public static GetBrakeMode(device: string): number | undefined {

simulation/SyntheSimJava/src/main/java/com/autodesk/synthesis/revrobotics/RelativeEncoder.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public RelativeEncoder(com.revrobotics.RelativeEncoder original, CANEncoder enco
2323

2424
@Override
2525
public double getPosition() {
26-
return (m_encoder.getPosition() - m_zero) * m_positionConversionFactor * m_invertedFactor;
26+
return m_encoder.getPosition() * m_positionConversionFactor * m_invertedFactor - m_zero;
2727
}
2828

2929
@Override
@@ -33,22 +33,19 @@ public double getVelocity() {
3333

3434
@Override
3535
public REVLibError setPosition(double position) {
36-
m_zero = position;
37-
m_original.setPosition(position);
36+
m_zero = m_encoder.getPosition() * m_positionConversionFactor * m_invertedFactor - position;
3837
return REVLibError.kOk;
3938
}
4039

4140
@Override
4241
public REVLibError setPositionConversionFactor(double factor) {
4342
m_positionConversionFactor = factor;
44-
m_original.setPositionConversionFactor(factor);
4543
return REVLibError.kOk;
4644
}
4745

4846
@Override
4947
public REVLibError setVelocityConversionFactor(double factor) {
5048
m_velocityConversionFactor = factor;
51-
m_original.setVelocityConversionFactor(factor);
5249
return REVLibError.kOk;
5350
}
5451

@@ -90,7 +87,6 @@ public int getCountsPerRevolution() {
9087
@Override
9188
public REVLibError setInverted(boolean inverted) {
9289
m_invertedFactor = inverted ? -1.0 : 1.0;
93-
m_original.setInverted(inverted);
9490
return REVLibError.kOk;
9591
}
9692

simulation/samples/JavaAutoSample/src/main/java/frc/robot/Robot.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
import edu.wpi.first.wpilibj.XboxController;
1919

2020
import com.autodesk.synthesis.revrobotics.CANSparkMax;
21+
import com.autodesk.synthesis.revrobotics.RelativeEncoder;
22+
import com.autodesk.synthesis.revrobotics.SparkAbsoluteEncoder;
2123
import com.kauailabs.navx.frc.AHRS;
24+
import com.autodesk.synthesis.CANEncoder;
2225
import com.autodesk.synthesis.ctre.TalonFX;
2326

2427
/**
@@ -42,6 +45,7 @@ public class Robot extends TimedRobot {
4245
private CANSparkMax m_sparkLeft = new CANSparkMax(1, MotorType.kBrushless);
4346
private CANSparkMax m_sparkRight = new CANSparkMax(2, MotorType.kBrushless);
4447
private CANSparkMax m_sparkArm = new CANSparkMax(3, MotorType.kBrushless);
48+
private RelativeEncoder m_encoder;
4549

4650
/**
4751
* This function is run when the robot is first started up and should be used
@@ -53,6 +57,11 @@ public void robotInit() {
5357
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
5458
m_chooser.addOption("My Auto", kCustomAuto);
5559
SmartDashboard.putData("Auto choices", m_chooser);
60+
61+
m_encoder = m_sparkLeft.getEncoderSim();
62+
// 4 inch diameter wheels, default is 1 unit = 1 radian.
63+
// Following conversion factor is 1 unit = 1 inch travelled.
64+
m_encoder.setPositionConversionFactor(2.0);
5665
}
5766

5867
/**
@@ -91,32 +100,34 @@ public void autonomousInit() {
91100
m_autoSelected = m_chooser.getSelected();
92101
// m_autoSelected = SmartDashboard.getString("Auto Selector", kDefaultAuto);
93102
System.out.println("Auto selected: " + m_autoSelected);
94-
m_sparkLeft.getAbsoluteEncoderSim()
103+
m_encoder.setPosition(0.0);
104+
m_autoState = AutoState.Stage1;
95105
}
96106

97107
enum AutoState {
98108
Stage1, Stage2
99109
}
100-
101110
private AutoState m_autoState = AutoState.Stage1;
102111

103112
/** This function is called periodically during autonomous. */
104113
@Override
105114
public void autonomousPeriodic() {
106115
switch (m_autoState) {
107-
case Stage1: {
116+
case Stage1:
108117
m_sparkLeft.set(0.5);
109118
m_sparkRight.set(0.5);
110-
if (m_sparkLeft.getEncoder())
119+
if (m_encoder.getPosition() > 36.0) {
120+
m_autoState = AutoState.Stage2;
121+
System.out.println("--- Transitioning to Stage 2 ---");
122+
}
111123
break;
112-
} case Stage2: {
124+
case Stage2:
125+
m_sparkLeft.set(0.5);
126+
m_sparkRight.set(-0.5);
113127
break;
114-
} default: {
128+
default:
115129
break;
116-
}
117130
}
118-
m_SparkMax1.set(0.2);
119-
m_SparkMax2.set(-0.2);
120131
}
121132

122133
/** This function is called once when teleop is enabled. */

0 commit comments

Comments
 (0)