Skip to content

Commit

Permalink
fix: Accelerometers metrics are relative, matching real devices.
Browse files Browse the repository at this point in the history
Also included some minor tweaks.
  • Loading branch information
HunterBarclay committed Sep 30, 2024
1 parent 394cdea commit 5347554
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
25 changes: 14 additions & 11 deletions fission/src/systems/simulation/wpilib_brain/SimInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { SimCANEncoder, SimGyro, SimAccel, SimDIO, SimAI } from "./WPILibBrain"
import Mechanism from "@/systems/physics/Mechanism"
import Jolt from "@barclah/jolt-physics"
import JOLT from "@/util/loading/JoltSyncLoader"
import { JoltQuat_ThreeQuaternion, JoltVec3_ThreeVector3 } from "@/util/TypeConversions"
import * as THREE from 'three'

export abstract class SimInput {
constructor(protected _device: string) {}
Expand Down Expand Up @@ -93,31 +95,32 @@ export class SimGyroInput extends SimInput {
export class SimAccelInput extends SimInput {
private _robot: Mechanism
private _joltID?: Jolt.BodyID
private _joltBody?: Jolt.Body
private _prevVel: Jolt.Vec3
private _prevVel: THREE.Vector3

constructor(device: string, robot: Mechanism) {
super(device)
this._robot = robot
this._joltID = this._robot.nodeToBody.get(this._robot.rootBody)
this._prevVel = new JOLT.Vec3(0, 0, 0)

if (this._joltID) this._joltBody = World.PhysicsSystem.GetBody(this._joltID)
this._prevVel = new THREE.Vector3(0,0,0)
}

public Update(deltaT: number) {
const newVel = this._joltBody?.GetLinearVelocity()
if (!newVel) return
if (!this._joltID) return
const body = World.PhysicsSystem.GetBody(this._joltID)

const rot = JoltQuat_ThreeQuaternion(body.GetRotation())
const mat = new THREE.Matrix4().makeRotationFromQuaternion(rot).transpose()
const newVel = JoltVec3_ThreeVector3(body.GetLinearVelocity()).applyMatrix4(mat)

const x = (newVel.GetX() - this._prevVel.GetX()) / deltaT
const y = (newVel.GetY() - this._prevVel.GetY()) / deltaT
const z = (newVel.GetZ() - this._prevVel.GetZ()) / deltaT
const x = (newVel.x - this._prevVel.x) / deltaT
const y = (newVel.y - this._prevVel.y) / deltaT
const z = (newVel.y - this._prevVel.y) / deltaT

SimAccel.SetX(this._device, x)
SimAccel.SetY(this._device, y)
SimAccel.SetZ(this._device, z)

this._prevVel = new JOLT.Vec3(newVel.GetX(), newVel.GetY(), newVel.GetZ())
this._prevVel = newVel
}
}

Expand Down
2 changes: 1 addition & 1 deletion fission/src/systems/simulation/wpilib_brain/SimOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class CANOutputGroup extends SimOutputGroup {
const average =
this.ports.reduce((sum, port) => {
const device = SimCAN.GetDeviceWithID(port, SimType.CANMotor)
return sum + +(device?.get("<percentOutput") ?? 0)
return sum + (device?.get("<percentOutput") as number | undefined ?? 0)
}, 0) / this.ports.length

this.drivers.forEach(d => {
Expand Down
16 changes: 8 additions & 8 deletions fission/src/systems/simulation/wpilib_brain/WPILibBrain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import WPILibWSWorker from "./WPILibWSWorker?worker"
import { SimulationLayer } from "../SimulationSystem"
import World from "@/systems/World"

import { SimOutput } from "./SimOutput"
import { SimInput } from "./SimInput"
import { SimAnalogOutput, SimDigitalOutput, SimOutput } from "./SimOutput"
import { SimAccelInput, SimAnalogInput, SimDigitalInput, SimGyroInput, SimInput } from "./SimInput"

Check warning on line 10 in fission/src/systems/simulation/wpilib_brain/WPILibBrain.ts

View workflow job for this annotation

GitHub Actions / ESLint Format Validation

'SimGyroInput' is defined but never used. Allowed unused vars must match /^_/u

const worker: Lazy<Worker> = new Lazy<Worker>(() => new WPILibWSWorker())

Expand Down Expand Up @@ -145,7 +145,7 @@ export class SimCAN {
private constructor() {}

public static GetDeviceWithID(id: number, type: SimType): DeviceData | undefined {
const id_exp = /.*\[(\d+)\]/g
const id_exp = /SYN.*\[(\d+)\]/g
const entries = [...simMap.entries()].filter(([simType, _data]) => simType == type)
for (const [_simType, data] of entries) {
for (const key of data.keys()) {
Expand Down Expand Up @@ -380,11 +380,11 @@ class WPILibBrain extends Brain {
}

// this.addSimInput(new SimGyroInput("Test Gyro[1]", mechanism))
// this.addSimInput(new SimAccelInput("ADXL362[4]", mechanism))
// this.addSimInput(new SimDigitalInput("SYN DI[0]", () => Math.random() > 0.5))
// this.addSimOutput(new SimDigitalOutput("SYN DO[1]"))
// this.addSimInput(new SimAnalogInput("SYN AI[0]", () => Math.random() * 12))
// this.addSimOutput(new SimAnalogOutput("SYN AO[1]"))
this.addSimInput(new SimAccelInput("ADXL362[4]", mechanism))
this.addSimInput(new SimDigitalInput("SYN DI[0]", () => Math.random() > 0.5))

Check warning on line 384 in fission/src/systems/simulation/wpilib_brain/WPILibBrain.ts

View check run for this annotation

Autodesk Chorus / security/semgrep

app.chorus.semgrep.rules.njsscan.crypto.node_insecure_random_generator

crypto.pseudoRandomBytes()/Math.random() is a cryptographically weak random number generator.

Check warning on line 384 in fission/src/systems/simulation/wpilib_brain/WPILibBrain.ts

View check run for this annotation

Autodesk Chorus / security/semgrep

app.chorus.semgrep.rules.njsscan.semantic_grep.crypto.node_insecure_random_generator

crypto.pseudoRandomBytes()/Math.random() is a cryptographically weak random number generator.
this.addSimOutput(new SimDigitalOutput("SYN DO[1]"))
this.addSimInput(new SimAnalogInput("SYN AI[0]", () => Math.random() * 12))

Check warning on line 386 in fission/src/systems/simulation/wpilib_brain/WPILibBrain.ts

View check run for this annotation

Autodesk Chorus / security/semgrep

app.chorus.semgrep.rules.njsscan.crypto.node_insecure_random_generator

crypto.pseudoRandomBytes()/Math.random() is a cryptographically weak random number generator.

Check warning on line 386 in fission/src/systems/simulation/wpilib_brain/WPILibBrain.ts

View check run for this annotation

Autodesk Chorus / security/semgrep

app.chorus.semgrep.rules.njsscan.semantic_grep.crypto.node_insecure_random_generator

crypto.pseudoRandomBytes()/Math.random() is a cryptographically weak random number generator.
this.addSimOutput(new SimAnalogOutput("SYN AO[1]"))
}

public addSimOutput(device: SimOutput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const RCConfigEncoderModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
>
<Label size={LabelSize.Small}>Name</Label>
<Input placeholder="..." className="w-full" onInput={setName} />
<Dropdown label="Encoders" options={devices.map(n => n[0])} onSelect={s => setSelectedDevice(s)} />
<Dropdown label="CAN Encoders" options={devices.map(n => n[0])} onSelect={s => setSelectedDevice(s)} />
<Dropdown
label="Stimuli"
options={[...stimMap.keys()]}
Expand Down
25 changes: 12 additions & 13 deletions fission/src/ui/panels/WSViewPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Panel, { PanelPropsImpl } from "@/components/Panel"
import { SimMapUpdateEvent, SimGeneric, simMap, SimType } from "@/systems/simulation/wpilib_brain/WPILibBrain"
import { SimGeneric, simMap, SimType } from "@/systems/simulation/wpilib_brain/WPILibBrain"
import {
Box,
Stack,
Expand All @@ -12,12 +12,14 @@ import {
TableRow,
Typography,
} from "@mui/material"
import { useCallback, useEffect, useMemo, useState } from "react"
import { useEffect, useMemo, useReducer, useState } from "react"
import Dropdown from "../components/Dropdown"
import Input from "../components/Input"
import Button from "../components/Button"
import { SynthesisIcons } from "../components/StyledComponents"

const TABLE_UPDATE_INTERVAL = 250

type ValueType = "string" | "number" | "object" | "boolean"

const TypoStyled = styled(Typography)({
Expand Down Expand Up @@ -47,8 +49,6 @@ function generateTableBody() {
SimType.AO,
]

console.log(simMap)

return (
<TableBody>
{names.map(name =>
Expand Down Expand Up @@ -95,7 +95,9 @@ function setGeneric(simType: SimType, device: string, field: string, value: stri
}

const WSViewPanel: React.FC<PanelPropsImpl> = ({ panelId }) => {
const [tb, setTb] = useState(generateTableBody())
// const [tb, setTb] = useState(generateTableBody())

const [table, updateTable] = useReducer((_) => generateTableBody(), generateTableBody())

const [selectedType, setSelectedType] = useState<SimType | undefined>()
const [selectedDevice, setSelectedDevice] = useState<string | undefined>()
Expand All @@ -115,17 +117,14 @@ const WSViewPanel: React.FC<PanelPropsImpl> = ({ panelId }) => {
setSelectedDevice(undefined)
}, [selectedType])

const onSimMapUpdate = useCallback((_: Event) => {
setTb(generateTableBody())
}, [])

useEffect(() => {
window.addEventListener(SimMapUpdateEvent.TYPE, onSimMapUpdate)
const func = () => { updateTable() }
const id: NodeJS.Timeout = setInterval(func, TABLE_UPDATE_INTERVAL)

return () => {
window.removeEventListener(SimMapUpdateEvent.TYPE, onSimMapUpdate)
clearTimeout(id)
}
}, [onSimMapUpdate])
}, [updateTable])

return (
<Panel
Expand Down Expand Up @@ -155,7 +154,7 @@ const WSViewPanel: React.FC<PanelPropsImpl> = ({ panelId }) => {
</TableCell>
</TableRow>
</TableHead>
{tb}
{table}
</Table>
</TableContainer>
<Stack>
Expand Down

0 comments on commit 5347554

Please sign in to comment.