Skip to content

Commit eaf87fc

Browse files
Fixed sim and gen
1 parent 1b27cc7 commit eaf87fc

3 files changed

Lines changed: 69 additions & 98 deletions

File tree

lib/config/hardware-config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,15 +303,15 @@ export function getWPILibMotorType(motorName: string): string {
303303
return "DCMotor.getKrakenX60(1)"
304304
case "Minion":
305305
// From https://store.ctr-electronics.com/products/minion-brushless-motor
306-
return customDCMotor(3.1, 200.46, 1.43, 7200) + " // Minion Motor"
306+
return customDCMotor(3.1, 200.46, 1.43, 7200) + "; // Minion Motor"
307307
case "Krakenx44":
308308
// From https://wcproducts.com/blogs/wcp-blog/kraken-x44
309-
return customDCMotor(4.05, 275, 1.4, 7530) + " // Kraken X44"
309+
return customDCMotor(4.05, 275, 1.4, 7530) + "; // Kraken X44"
310310
case "Vortex":
311311
return "DCMotor.getNEOVortex(1)"
312312
case "Cu60":
313313
// Placeholder - will need actual DCMotor specs for Cu60
314-
return customDCMotor(4.05, 275, 1.4, 7530) + " // Redux Cu60"
314+
return customDCMotor(4.05, 275, 1.4, 7530) + "; // Redux Cu60"
315315
default:
316316
return "DCMotor.getNEO(1)"
317317
}
Lines changed: 64 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,64 @@
1-
import type { MotorConfig } from "@/lib/types"
2-
3-
export const generateImports = (config: MotorConfig): string[] => {
4-
const imports = [
5-
"com.reduxrobotics.nitrate.hardware.ReduxNitrate",
6-
"com.reduxrobotics.nitrate.hardware.ReduxNitrate.IdleMode",
7-
]
8-
9-
if (config.enableCurrentLimit) {
10-
imports.push("com.reduxrobotics.nitrate.hardware.ReduxNitrate.CurrentLimitConfig")
11-
}
12-
13-
return imports
14-
}
15-
16-
export const generateDeclaration = (config: MotorConfig): string => {
17-
return `private final ReduxNitrate m_${config.name}Motor;`
18-
}
19-
20-
export const generateInitialization = (config: MotorConfig): string => {
21-
let code = ` m_${config.name}Motor = new ReduxNitrate(${config.canId});\n`
22-
23-
// Configure motor settings
24-
code += ` m_${config.name}Motor.restoreFactoryDefaults();\n`
25-
26-
// Set idle mode
27-
if (config.brakeMode) {
28-
code += ` m_${config.name}Motor.setIdleMode(IdleMode.kBrake);\n`
29-
} else {
30-
code += ` m_${config.name}Motor.setIdleMode(IdleMode.kCoast);\n`
31-
}
32-
33-
// Set current limit
34-
if (config.enableCurrentLimit && config.currentLimit) {
35-
code += ` m_${config.name}Motor.setSmartCurrentLimit(${config.currentLimit});\n`
36-
}
37-
38-
// Set supply current limit if supported and enabled
39-
if (config.enableSupplyCurrentLimit && config.supplyCurrentLimit) {
40-
code += ` m_${config.name}Motor.setSupplyCurrentLimit(${config.supplyCurrentLimit});\n`
41-
}
42-
43-
// Set ramp rate
44-
if (config.rampRate && config.rampRate > 0) {
45-
code += ` m_${config.name}Motor.setOpenLoopRampRate(${config.rampRate});\n`
46-
code += ` m_${config.name}Motor.setClosedLoopRampRate(${config.rampRate});\n`
47-
}
48-
49-
// Set soft limits
50-
if (config.enableSoftLimits) {
51-
if (config.forwardSoftLimit !== undefined) {
52-
code += ` m_${config.name}Motor.enableSoftLimit(ReduxNitrate.SoftLimitDirection.kForward, true);\n`
53-
code += ` m_${config.name}Motor.setSoftLimit(ReduxNitrate.SoftLimitDirection.kForward, ${config.forwardSoftLimit});\n`
54-
}
55-
if (config.reverseSoftLimit !== undefined) {
56-
code += ` m_${config.name}Motor.enableSoftLimit(ReduxNitrate.SoftLimitDirection.kReverse, true);\n`
57-
code += ` m_${config.name}Motor.setSoftLimit(ReduxNitrate.SoftLimitDirection.kReverse, ${config.reverseSoftLimit});\n`
58-
}
59-
}
60-
61-
// Invert motor if needed
62-
if (config.inverted) {
63-
code += ` m_${config.name}Motor.setInverted(true);\n`
64-
}
65-
66-
code += ` m_${config.name}Motor.burnFlash();\n`
67-
68-
return code
69-
}
70-
71-
export const generateSetVoltage = (config: MotorConfig): string => {
72-
return `m_${config.name}Motor.setVoltage(voltage)`
73-
}
74-
75-
export const generateGetPosition = (config: MotorConfig): string => {
76-
return `m_${config.name}Motor.getPosition().getValueAsDouble()`
77-
}
78-
79-
export const generateGetVelocity = (config: MotorConfig): string => {
80-
return `m_${config.name}Motor.getVelocity().getValueAsDouble()`
81-
}
82-
83-
export const generateResetEncoder = (config: MotorConfig): string => {
84-
return `m_${config.name}Motor.setPosition(0)`
85-
}
86-
87-
export const generatePIDController = (config: MotorConfig): string => {
88-
return `m_${config.name}Motor.getPIDController()`
89-
}
90-
91-
export const generateSetReference = (config: MotorConfig, reference: string, controlType: string): string => {
92-
return `m_${config.name}Motor.getPIDController().setReference(${reference}, ${controlType})`
93-
}
1+
export const getImports = () => `import com.reduxrobotics.nitrate.hardware.ReduxNitrate;
2+
import com.reduxrobotics.nitrate.hardware.ReduxNitrate.IdleMode;
3+
import com.reduxrobotics.nitrate.hardware.ReduxNitrate.CurrentLimitConfig;`
4+
5+
export const getDeclaration = () => `private final ReduxNitrate motor;`
6+
7+
export const getInitialization = () => `motor = new ReduxNitrate(canID);
8+
9+
// Configure motor
10+
motor.setNeutralMode(brakeMode ? NeutralMode.BRAKE : NeutralMode.COAST);
11+
12+
// Configure encoder
13+
encoder = motor.getEncoder();
14+
encoder.setPosition(0);
15+
16+
// Configure PID controller
17+
pidController = motor.getPIDController();
18+
pidController.setP(kP);
19+
pidController.setI(kI);
20+
pidController.setD(kD);
21+
22+
// Set ramp rates
23+
{{#if enableOpenLoopRamp}}
24+
motor.setOpenLoopRampRate(openLoopRampRate);
25+
{{/if}}
26+
{{#if enableClosedLoopRamp}}
27+
motor.setClosedLoopRampRate(closedLoopRampRate);
28+
{{/if}}
29+
30+
// Set current limits
31+
{{#if enableStatorLimit}}
32+
motor.setCurrentLimit(statorCurrentLimit);
33+
{{/if}}
34+
{{#if enableSoftLimits}}
35+
motor.configForwardSoftLimit(forwardSoftLimit);
36+
motor.enableForwardSoftLimit(true);
37+
motor.configReverseSoftLimit(reverseSoftLimit);
38+
motor.enableReverseSoftLimit(true);
39+
{{/if}}`
40+
41+
export const getPeriodic = () => ``
42+
export const getSimulationPeriodic = () => ``
43+
44+
export const getMethods = () => ({
45+
getPositionMethod: `return encoder.getPosition() / gearRatio;`,
46+
47+
getVelocityMethod: `return encoder.getVelocity() / gearRatio;`,
48+
49+
setPositionMethod: `double adjustedPosition = position * gearRatio;
50+
double ffVolts = feedforward.calculate(getVelocity(), acceleration);
51+
pidController.setReference(adjustedPosition, ControlMode.POSITION, ffVolts);`,
52+
53+
setVelocityMethod: `double adjustedVelocity = velocity * gearRatio;
54+
double ffVolts = feedforward.calculate(velocity, acceleration);
55+
pidController.setReference(adjustedVelocity, ControlMode.VELOCITY, ffVolts);`,
56+
57+
setVoltageMethod: `motor.setVoltage(voltage);`,
58+
59+
getVoltageMethod: `return motor.getAppliedOutput() * motor.getBusVoltage();`,
60+
61+
getCurrentMethod: `return motor.getOutputCurrent();`,
62+
63+
getTemperatureMethod: `return motor.getTemperature();`,
64+
})

lib/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export interface FormValues {
22
subsystemName: string
33
mechanismType: "Elevator" | "Arm" | "Pivot"
4-
motorControllerType: "ThriftyNova" | "SparkMAX" | "SparkFlex" | "TalonFX" | "TalonFXS" | "Redux Nitrate"
5-
motorType: "NEO" | "NEO550" | "Minion" | "Krakenx44" | "Krakenx60" | "Vortex" | "Cu60"
4+
motorControllerType: "ThriftyNova" | "SparkMAX" | "SparkFlex" | "TalonFX" | "TalonFXS" | "ReduxNitrate"
5+
motorType: "NEO" | "NEO550" | "Minion" | "Krakenx44" | "Krakenx60" | "Vortex" | "Cu60"
66
canId: number
77
pidValues: {
88
kP: number

0 commit comments

Comments
 (0)