-
Notifications
You must be signed in to change notification settings - Fork 61
Automatic code generation for choreo #1298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Daniel1464
wants to merge
4
commits into
SleipnirGroup:main
Choose a base branch
from
Daniel1464:java-filegen
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
import { Project } from "../document/2025/DocumentTypes"; | ||
import { writeConst } from "./internals"; | ||
|
||
export function genConstsFile(project: Project, packageName: string): string { | ||
const out: string[] = []; | ||
out.push(`package ${packageName};`); | ||
out.push(` | ||
import edu.wpi.first.math.geometry.Translation2d; | ||
import edu.wpi.first.units.measure.*; | ||
import static edu.wpi.first.units.Units.*; | ||
|
||
/** | ||
* Generated file containing document settings for your Choreo project. | ||
* This allows for modifying constants in choreo while keeping your robot code up-to-date. | ||
* DO NOT MODIFY this file yourself, as it is auto-generated. | ||
*/ | ||
public final class ChoreoConsts {`); | ||
|
||
const config = project.config; | ||
const maxLinearVel = | ||
(config.vmax.val / config.gearing.val) * config.radius.val; | ||
const maxWheelForce = | ||
(config.tmax.val * config.gearing.val * 4) / config.radius.val; | ||
const maxLinearAccel = maxWheelForce / config.mass.val; | ||
|
||
out.push(writeConst(config.gearing, "gearing", "Number")); | ||
out.push(writeConst(config.cof, "frictionCoefficient", "Number")); | ||
out.push(writeConst(config.radius, "wheelRadius", "Length")); | ||
out.push(writeConst(config.inertia, "moi", "MoI")); | ||
out.push(writeConst(config.mass, "mass", "Mass")); | ||
out.push(writeConst(config.tmax, "driveMotorMaxTorque", "Torque")); | ||
|
||
const bumperWB = config.bumper.front.val + config.bumper.back.val; | ||
const bumperTW = config.bumper.side.val * 2; | ||
out.push(writeConst(bumperWB, "wheelBaseWithBumpers", "Length")); | ||
out.push(writeConst(bumperTW, "trackWidthWithBumpers", "Length")); | ||
|
||
if (project.type === "Swerve") { | ||
out.push(` | ||
public static final Translation2d[] moduleTranslations = { | ||
new Translation2d(${config.frontLeft.x.val}, ${config.frontLeft.y.val}), | ||
new Translation2d(${config.frontLeft.x.val}, ${-config.frontLeft.y.val}), | ||
new Translation2d(${config.backLeft.x.val}, ${config.backLeft.y.val}), | ||
new Translation2d(${config.backLeft.x.val}, ${-config.backLeft.y.val}), | ||
};`); | ||
const drivebaseRadius = Math.hypot( | ||
config.frontLeft.x.val, | ||
config.frontLeft.y.val | ||
); | ||
const frictionFloorForce = config.mass.val * 9.81 * config.cof.val; | ||
const minLinearForce = Math.min(frictionFloorForce, maxWheelForce); | ||
const maxAngularVel = maxLinearVel / drivebaseRadius; | ||
const maxAngularAccel = | ||
(minLinearForce * drivebaseRadius) / config.inertia.val; | ||
out.push(writeConst(maxAngularVel, "maxAngularVel", "AngVel")); | ||
out.push(writeConst(maxAngularAccel, "maxAngularAccel", "AngAcc")); | ||
} else { | ||
out.push(writeConst(config.differentialTrackWidth, "trackWidth", "Length")); | ||
out.push(""); | ||
} | ||
out.push(writeConst(maxLinearVel, "maxLinearVel", "LinVel")); | ||
out.push(writeConst(maxLinearAccel, "maxLinearAccel", "LinAcc")); | ||
out.push(""); | ||
out.push(" private ChoreoConsts() {}"); | ||
out.push("}"); | ||
return out.join("\n"); | ||
} |
This file contains hidden or 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,46 @@ | ||
import { Project } from "../document/2025/DocumentTypes"; | ||
import { round, writeConst } from "./internals"; | ||
|
||
export function genVarsFile(project: Project, packageName: string): string { | ||
const expressions = project.variables.expressions; | ||
const poses = project.variables.poses; | ||
const out: string[] = []; | ||
out.push(`package ${packageName};\n`); | ||
out.push(` | ||
import edu.wpi.first.math.geometry.Pose2d; | ||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.units.measure.*; | ||
import static edu.wpi.first.units.Units.*; | ||
|
||
/** | ||
* Generated file containing variables defined in Choreo. | ||
* DO NOT MODIFY THIS YOURSELF; instead, change these values | ||
* in the choreo GUI. | ||
*/ | ||
public final class ChoreoVars {`); | ||
for (const varName in project.variables.expressions) { | ||
const data = expressions[varName]; | ||
out.push(writeConst(data.var, varName, data.dimension)); | ||
} | ||
out.push(""); | ||
out.push(" public static final class Poses {"); | ||
for (const poseName in poses) { | ||
const pose = poses[poseName]; | ||
const heading = | ||
Math.abs(pose.heading.val) < 1e-5 | ||
? "Rotation2d.kZero" | ||
: `Rotation2d.fromRadians(${round(pose.heading.val, 3)})`; | ||
const x = round(pose.x.val, 3); | ||
const y = round(pose.y.val, 3); | ||
out.push( | ||
` public static final Pose2d ${poseName} = new Pose2d(${x}, ${y}, ${heading});` | ||
); | ||
} | ||
out.push(""); | ||
out.push(" private Poses() {}"); | ||
out.push(" }"); | ||
out.push(""); | ||
out.push(" private ChoreoVars() {}"); | ||
out.push("}"); | ||
return out.join("\n"); | ||
} |
This file contains hidden or 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,65 @@ | ||
import { DimensionName } from "../document/ExpressionStore"; | ||
import { Expr, Variable } from "../document/2025/DocumentTypes"; | ||
|
||
export interface UnitData { | ||
type: string; | ||
baseUnit: string; | ||
} | ||
|
||
export function unitDataFrom( | ||
choreoDimensionName: DimensionName | ||
): UnitData | null { | ||
switch (choreoDimensionName) { | ||
case "LinAcc": | ||
return { | ||
type: "LinearAcceleration", | ||
baseUnit: "MetersPerSecondPerSecond" | ||
}; | ||
case "LinVel": | ||
return { type: "LinearVelocity", baseUnit: "MetersPerSecond" }; | ||
case "Length": | ||
return { type: "Distance", baseUnit: "Meters" }; | ||
case "Angle": | ||
return { type: "Angle", baseUnit: "Radians" }; | ||
case "AngVel": | ||
return { type: "AngularVelocity", baseUnit: "RadiansPerSecond" }; | ||
case "AngAcc": | ||
return { | ||
type: "AngularAcceleration", | ||
baseUnit: "RadiansPerSecondPerSecond" | ||
}; | ||
case "Time": | ||
return { type: "Time", baseUnit: "Seconds" }; | ||
case "Mass": | ||
return { type: "Mass", baseUnit: "Kilograms" }; | ||
case "Torque": | ||
return { type: "Torque", baseUnit: "NewtonMeters" }; | ||
case "MoI": | ||
return { type: "MomentOfInertia", baseUnit: "KilogramSquareMeters" }; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
export function writeConst( | ||
expr: Expr | number, | ||
variableName: string, | ||
dimension: DimensionName | ||
): string { | ||
const unitData = unitDataFrom(dimension); | ||
let val = typeof expr === "number" ? expr : expr.val; | ||
val = round(val, dimension === "MoI" ? 5 : 3); | ||
if (!dimension || !unitData) { | ||
return ` public static final double ${variableName} = ${val};`; | ||
} | ||
return ` public static final ${unitData.type} ${variableName} = ${unitData.baseUnit}.of(${val});`; | ||
} | ||
|
||
export function writeVar(variable: Variable, name: string): string { | ||
return writeConst(variable.var, name, variable.dimension); | ||
} | ||
|
||
export function round(val: number, digits: number) { | ||
const roundingFactor = Math.pow(10, digits); | ||
return Math.round(val * roundingFactor) / roundingFactor; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest ChoreoRobotConfig.