Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/web/calculators/flywheel/components/FlywheelCalculator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
calculateRecoveryTime,
calculateShooterWheelSurfaceSpeed,
calculateSpeedAfterShot,
calculateVPerRps,
calculateWindupTime,
} from "web/calculators/flywheel/flywheelMath";
import VelocityControlGainsAnalysis from "web/calculators/shared/components/VelocityControlGainsAnalysis";
Expand Down Expand Up @@ -207,6 +208,14 @@ export default function FlywheelCalculator(): JSX.Element {
);
}, [get.motor.freeSpeed, get.motorRatio, get.shooterRadius]);

const kVAngular = useMemo(() => {
if (get.motorRatio.asNumber() == 0) {
return new Measurement(0, "V*s/rotation");
}

return calculateVPerRps(get.motor, get.motorRatio);
}, [get.motor, get.motorRatio]);

const kA = useMemo(() => {
if (get.flywheelRadius.scalar == 0) {
return new Measurement(0, "V*s^2/m");
Expand Down Expand Up @@ -235,6 +244,39 @@ export default function FlywheelCalculator(): JSX.Element {
get.currentLimit,
]);

const kAAngular = useMemo(() => {
if (get.motorRatio.asNumber() == 0) {
return new Measurement(0, "V*s^2/rotation");
}

const stallTorqueAtShooter = new MotorRules(get.motor, get.currentLimit, {
voltage: nominalVoltage,
rpm: new Measurement(0, "rpm"),
})
.solve()
.torque.mul(get.motor.quantity)
.mul(get.motorRatio.asNumber())
.mul(get.efficiency / 100);

// Convert moment of inertia to kg*m^2 for proper unit calculation
const moiInSI = totalMomentOfInertia.to("kg*m^2");

// kA = V_nominal * I / tau_stall
// Units: V * kg*m^2 / (N*m) = V * s^2
const kAInSec = nominalVoltage.mul(moiInSI).div(stallTorqueAtShooter);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a division by zero guard above this

if stallTorqueAtShooter == 0
  return new Measurement(0, "...")


// Convert to rotations: V*s^2/rotation
// Since 1 rotation = 2*pi rad, and rad is dimensionless in this context,
// V*s^2/rad = V*s^2, so we just need to format it properly
return new Measurement(kAInSec.scalar, "V*s^2/rotation");
}, [
get.motor,
get.currentLimit,
get.motorRatio,
get.efficiency,
totalMomentOfInertia,
]);

useEffect(() => {
if (!get.useCustomShooterMoi) {
set.setShooterMomentOfInertia(
Expand Down Expand Up @@ -515,7 +557,12 @@ export default function FlywheelCalculator(): JSX.Element {
numberRoundTo={0}
/>
</SingleInputLine>
<VelocityControlGainsAnalysis kv={kV} ka={kA} />
<VelocityControlGainsAnalysis
kv={kV}
ka={kA}
kVAlt={kVAngular}
kAAlt={kAAngular}
/>
</Column>
</Columns>
</>
Expand Down
9 changes: 9 additions & 0 deletions src/web/calculators/flywheel/flywheelMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import Motor, { nominalVoltage } from "common/models/Motor";
import Ratio from "common/models/Ratio";
import { MotorRules } from "common/models/Rules";

export function calculateVPerRps(motor: Motor, ratio: Ratio): Measurement {
if (motor.quantity === 0 || ratio.asNumber() === 0) {
return new Measurement(0, "V*s/rotation");
}

const maxShooterRps = motor.freeSpeed.div(ratio.asNumber()).to("rotation/s");
return nominalVoltage.div(maxShooterRps).to("V*s/rotation");
}

export function calculateWindupTime(
momentOfInertia: Measurement,
motor: Motor,
Expand Down
30 changes: 30 additions & 0 deletions src/web/calculators/shared/components/FeedforwardAnalysis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ export interface FeedforwardAnalysisProps {
kA: Measurement;
distanceType: "linear" | "angular";
kG?: Measurement;
kVAlt?: Measurement;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just name these kvAngular and kaAngular rather than 'alt'

kAAlt?: Measurement;
}

const FeedforwardAnalysis: React.FC<FeedforwardAnalysisProps> = ({
kG,
kV,
kA,
distanceType,
kVAlt,
kAAlt,
}) => {
return (
<div>
Expand Down Expand Up @@ -44,6 +48,19 @@ const FeedforwardAnalysis: React.FC<FeedforwardAnalysisProps> = ({
defaultUnit={distanceType === "angular" ? "V*s/rotation" : "V*s/m"}
/>
</SingleInputLine>
{kVAlt !== undefined && (
<SingleInputLine
label="kV (angular)"
id="kVAlt"
tooltip="Velocity feedforward gain in angular units. Use this for RPM-based velocity control."
>
<MeasurementOutput
stateHook={[kVAlt, () => undefined]}
numberRoundTo={4}
defaultUnit="V*s/rotation"
/>
</SingleInputLine>
)}
<SingleInputLine
label="kA"
id="kA"
Expand All @@ -57,6 +74,19 @@ const FeedforwardAnalysis: React.FC<FeedforwardAnalysisProps> = ({
}
/>
</SingleInputLine>
{kAAlt !== undefined && (
<SingleInputLine
label="kA (angular)"
id="kAAlt"
tooltip="Acceleration feedforward gain in angular units. Use this for RPM-based velocity control."
>
<MeasurementOutput
stateHook={[kAAlt, () => undefined]}
numberRoundTo={4}
defaultUnit="V*s^2/rotation"
/>
</SingleInputLine>
)}
<SingleInputLine
label="System Response Time"
id="systemResponseTime"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ interface VelocityControlGainsAnalysisProps {
ka: Measurement;
dt?: Measurement;
velTolerance?: Measurement;
kVAlt?: Measurement;
kAAlt?: Measurement;
}

const VelocityControlGainsAnalysis: React.FC<
VelocityControlGainsAnalysisProps
> = ({ kv, ka, dt, velTolerance }) => {
> = ({ kv, ka, dt, velTolerance, kVAlt, kAAlt }) => {
return (
<div>
<Divider color="primary">Estimated Control Gains</Divider>
Expand All @@ -23,7 +25,13 @@ const VelocityControlGainsAnalysis: React.FC<
particular attention to units. Always be careful the first time you
enable closed-loop control of a mechanism.
</Message>
<FeedforwardAnalysis kV={kv} kA={ka} distanceType={"linear"} />
<FeedforwardAnalysis
kV={kv}
kA={ka}
distanceType={"linear"}
kVAlt={kVAlt}
kAAlt={kAAlt}
/>
<VelocityFeedbackAnalysis
kv={kv}
ka={ka}
Expand Down
Loading