Skip to content

Commit

Permalink
WPILibBrain Mapping (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterBarclay authored Jul 26, 2024
2 parents f467f95 + a78113e commit 3799d07
Show file tree
Hide file tree
Showing 22 changed files with 327 additions and 64 deletions.
6 changes: 4 additions & 2 deletions fission/src/Synthesis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import PracticeSettingsModal from "@/modals/configuring/PracticeSettingsModal"
import RoboRIOModal from "@/modals/configuring/RoboRIOModal"
import SettingsModal from "@/modals/configuring/SettingsModal"
import RCConfigEncoderModal from "@/modals/configuring/rio-config/RCConfigEncoderModal"
import RCConfigPwmGroupModal from "@/modals/configuring/rio-config/RCConfigPwmGroupModal"
import RCCreateDeviceModal from "@/modals/configuring/rio-config/RCCreateDeviceModal"
import DeleteAllThemesModal from "@/modals/configuring/theme-editor/DeleteAllThemesModal"
import DeleteThemeModal from "@/modals/configuring/theme-editor/DeleteThemeModal"
Expand Down Expand Up @@ -61,6 +60,8 @@ import SceneOverlay from "./ui/components/SceneOverlay.tsx"
import WPILibWSWorker from "@/systems/simulation/wpilib_brain/WPILibWSWorker.ts?worker"
import WSViewPanel from "./ui/panels/WSViewPanel.tsx"
import Lazy from "./util/Lazy.ts"
import RCConfigPWMGroupModal from "@/modals/configuring/rio-config/RCConfigPWMGroupModal.tsx"
import RCConfigCANGroupModal from "@/modals/configuring/rio-config/RCConfigCANGroupModal.tsx"
import DebugPanel from "./ui/panels/DebugPanel.tsx"
import NewInputSchemeModal from "./ui/modals/configuring/theme-editor/NewInputSchemeModal.tsx"
import AssignNewSchemeModal from "./ui/modals/configuring/theme-editor/AssignNewSchemeModal.tsx"
Expand Down Expand Up @@ -200,7 +201,8 @@ const initialModals = [
<DeleteAllThemesModal key="delete-all-themes" modalId="delete-all-themes" />,
<NewThemeModal key="new-theme" modalId="new-theme" />,
<RCCreateDeviceModal key="create-device" modalId="create-device" />,
<RCConfigPwmGroupModal key="config-pwm" modalId="config-pwm" />,
<RCConfigPWMGroupModal key="config-pwm" modalId="config-pwm" />,
<RCConfigCANGroupModal key="config-can" modalId="config-can" />,
<RCConfigEncoderModal key="config-encoder" modalId="config-encoder" />,
<MatchModeModal key="match-mode" modalId="match-mode" />,
<ConfigMotorModal key="config-motor" modalId="config-motor" />,
Expand Down
5 changes: 4 additions & 1 deletion fission/src/mirabuf/IntakeSensorSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "@/util/TypeConversions"
import { OnContactPersistedEvent } from "@/systems/physics/ContactEvents"
import InputSystem from "@/systems/input/InputSystem"
import SynthesisBrain from "@/systems/simulation/synthesis_brain/SynthesisBrain"

class IntakeSensorSceneObject extends SceneObject {
private _parentAssembly: MirabufSceneObject
Expand Down Expand Up @@ -53,7 +54,9 @@ class IntakeSensorSceneObject extends SceneObject {
World.SceneRenderer.scene.add(this._mesh)

this._collision = (event: OnContactPersistedEvent) => {
if (InputSystem.getInput("intake", this._parentAssembly.brain?.brainIndex ?? -1)) {
const brain = this._parentAssembly.brain
const brainIndex = brain instanceof SynthesisBrain ? brain.brainIndex ?? -1 : -1
if (InputSystem.getInput("intake", brainIndex)) {
if (this._joltBodyId && !World.PhysicsSystem.isPaused) {
const body1 = event.message.body1
const body2 = event.message.body2
Expand Down
19 changes: 12 additions & 7 deletions fission/src/mirabuf/MirabufSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import * as THREE from "three"
import JOLT from "@/util/loading/JoltSyncLoader"
import { BodyAssociate, LayerReserve } from "@/systems/physics/PhysicsSystem"
import Mechanism from "@/systems/physics/Mechanism"
import SynthesisBrain from "@/systems/simulation/synthesis_brain/SynthesisBrain"
import InputSystem from "@/systems/input/InputSystem"
import TransformGizmos from "@/ui/components/TransformGizmos"
import { EjectorPreferences, FieldPreferences, IntakePreferences } from "@/systems/preferences/PreferenceTypes"
import PreferencesSystem from "@/systems/preferences/PreferencesSystem"
import { MiraType } from "./MirabufLoader"
import IntakeSensorSceneObject from "./IntakeSensorSceneObject"
import EjectableSceneObject from "./EjectableSceneObject"
import Brain from "@/systems/simulation/Brain"
import WPILibBrain from "@/systems/simulation/wpilib_brain/WPILibBrain"
import ScoringZoneSceneObject from "./ScoringZoneSceneObject"
import { SceneOverlayTag } from "@/ui/components/SceneOverlayEvents"
import { ProgressHandle } from "@/ui/components/ProgressNotificationData"
import SynthesisBrain from "@/systems/simulation/synthesis_brain/SynthesisBrain"

const DEBUG_BODIES = false

Expand All @@ -32,7 +34,7 @@ class MirabufSceneObject extends SceneObject {
private _assemblyName: string
private _mirabufInstance: MirabufInstance
private _mechanism: Mechanism
private _brain: SynthesisBrain | undefined
private _brain: Brain | undefined

private _debugBodies: Map<string, RnDebugMeshes> | null
private _physicsLayerReserve: LayerReserve | undefined
Expand Down Expand Up @@ -111,7 +113,9 @@ class MirabufSceneObject extends SceneObject {

// creating nametag for robots
if (this.miraType === MiraType.ROBOT) {
this._nameTag = new SceneOverlayTag(() => (this._brain ? this._brain.inputSchemeName : "Not Configured"))
this._nameTag = new SceneOverlayTag(() =>
this._brain instanceof SynthesisBrain ? this._brain.inputSchemeName : "Not Configured"
)
}
}

Expand Down Expand Up @@ -149,7 +153,7 @@ class MirabufSceneObject extends SceneObject {
// Simulation
World.SimulationSystem.RegisterMechanism(this._mechanism)
const simLayer = World.SimulationSystem.GetSimulationLayer(this._mechanism)!
this._brain = new SynthesisBrain(this._mechanism, this._assemblyName)
this._brain = new WPILibBrain(this._mechanism)
simLayer.SetBrain(this._brain)

// Intake
Expand All @@ -159,7 +163,8 @@ class MirabufSceneObject extends SceneObject {
}

public Update(): void {
if (InputSystem.getInput("eject", this._brain?.brainIndex ?? -1)) {
const brainIndex = this._brain instanceof SynthesisBrain ? this._brain.brainIndex ?? -1 : -1
if (InputSystem.getInput("eject", brainIndex)) {
this.Eject()
}

Expand Down Expand Up @@ -253,7 +258,7 @@ class MirabufSceneObject extends SceneObject {
this._scoringZones = []

this._mechanism.nodeToBody.forEach(bodyId => {
World.PhysicsSystem.RemoveBodyAssocation(bodyId)
World.PhysicsSystem.RemoveBodyAssociation(bodyId)
})

this._nameTag?.Dispose()
Expand All @@ -271,7 +276,7 @@ class MirabufSceneObject extends SceneObject {
this._debugBodies?.clear()
this._physicsLayerReserve?.Release()

this._brain?.clearControls()
if (this._brain && this._brain instanceof SynthesisBrain) this._brain?.clearControls()
}

public Eject() {
Expand Down
2 changes: 2 additions & 0 deletions fission/src/systems/physics/Mechanism.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Jolt from "@barclah/jolt-physics"
import { LayerReserve } from "./PhysicsSystem"
import { RigidNodeId } from "@/mirabuf/MirabufParser"
import { mirabuf } from "@/proto/mirabuf"

export interface MechanismConstraint {
parentBody: Jolt.BodyID
childBody: Jolt.BodyID
constraint: Jolt.Constraint
info?: mirabuf.IInfo
}

class Mechanism {
Expand Down
3 changes: 2 additions & 1 deletion fission/src/systems/physics/PhysicsSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class PhysicsSystem extends WorldSystem {
this._bodyAssociations.set(assocation.associatedBody, assocation)
}

public RemoveBodyAssocation(bodyId: Jolt.BodyID) {
public RemoveBodyAssociation(bodyId: Jolt.BodyID) {
this._bodyAssociations.delete(bodyId.GetIndexAndSequenceNumber())
}

Expand Down Expand Up @@ -399,6 +399,7 @@ class PhysicsSystem extends WorldSystem {
parentBody: bodyIdA,
childBody: bodyIdB,
constraint: x,
info: jInst.info ?? undefined, // remove possibility for null
})
)
}
Expand Down
6 changes: 3 additions & 3 deletions fission/src/systems/simulation/SimulationSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ class SimulationLayer {
this._mechanism.constraints.forEach(x => {
if (x.constraint.GetSubType() == JOLT.EConstraintSubType_Hinge) {
const hinge = JOLT.castObject(x.constraint, JOLT.HingeConstraint)
const driver = new HingeDriver(hinge)
const driver = new HingeDriver(hinge, x.info)
this._drivers.push(driver)
const stim = new HingeStimulus(hinge)
this._stimuli.push(stim)
} else if (x.constraint.GetSubType() == JOLT.EConstraintSubType_Vehicle) {
const vehicle = JOLT.castObject(x.constraint, JOLT.VehicleConstraint)
const driver = new WheelDriver(vehicle)
const driver = new WheelDriver(vehicle, x.info)
this._drivers.push(driver)
const stim = new WheelRotationStimulus(vehicle.GetWheel(0))
this._stimuli.push(stim)
} else if (x.constraint.GetSubType() == JOLT.EConstraintSubType_Slider) {
const slider = JOLT.castObject(x.constraint, JOLT.SliderConstraint)
const driver = new SliderDriver(slider)
const driver = new SliderDriver(slider, x.info)
this._drivers.push(driver)
const stim = new SliderStimulus(slider)
this._stimuli.push(stim)
Expand Down
4 changes: 2 additions & 2 deletions fission/src/systems/simulation/behavior/Behavior.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Driver from "../driver/Driver"
import Stimulus from "../stimulus/Stimulus"
import Driver from "@/systems/simulation/driver/Driver"
import Stimulus from "@/systems/simulation/stimulus/Stimulus"

abstract class Behavior {
private _drivers: Driver[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import WheelDriver from "../driver/WheelDriver"
import WheelRotationStimulus from "../stimulus/WheelStimulus"
import Behavior from "./Behavior"
import WheelDriver from "@/systems/simulation/driver/WheelDriver"
import WheelRotationStimulus from "@/systems/simulation/stimulus/WheelStimulus"
import Behavior from "@/systems/simulation/behavior/Behavior"
import InputSystem from "@/systems/input/InputSystem"

class ArcadeDriveBehavior extends Behavior {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import HingeDriver from "../driver/HingeDriver"
import HingeStimulus from "../stimulus/HingeStimulus"
import Behavior from "./Behavior"
import HingeDriver from "@/systems/simulation/driver/HingeDriver"
import HingeStimulus from "@/systems/simulation/stimulus/HingeStimulus"
import Behavior from "@/systems/simulation/behavior/Behavior"
import InputSystem from "@/systems/input/InputSystem"

class GenericArmBehavior extends Behavior {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SliderDriver from "../driver/SliderDriver"
import SliderStimulus from "../stimulus/SliderStimulus"
import Behavior from "./Behavior"
import SliderDriver from "@/systems/simulation/driver/SliderDriver"
import SliderStimulus from "@/systems/simulation/stimulus/SliderStimulus"
import Behavior from "@/systems/simulation/behavior/Behavior"
import InputSystem from "@/systems/input/InputSystem"

class GenericElevatorBehavior extends Behavior {
Expand Down
12 changes: 12 additions & 0 deletions fission/src/systems/simulation/driver/Driver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { mirabuf } from "@/proto/mirabuf"

abstract class Driver {
private _info?: mirabuf.IInfo

constructor(info?: mirabuf.IInfo) {
this._info = info
}

public abstract Update(deltaT: number): void

public get info() {
return this._info
}
}

export enum DriverControlMode {
Expand Down
5 changes: 3 additions & 2 deletions fission/src/systems/simulation/driver/HingeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Jolt from "@barclah/jolt-physics"
import Driver, { DriverControlMode } from "./Driver"
import { GetLastDeltaT } from "@/systems/physics/PhysicsSystem"
import JOLT from "@/util/loading/JoltSyncLoader"
import { mirabuf } from "@/proto/mirabuf"

class HingeDriver extends Driver {
private _constraint: Jolt.HingeConstraint
Expand Down Expand Up @@ -52,8 +53,8 @@ class HingeDriver extends Driver {
}
}

public constructor(constraint: Jolt.HingeConstraint) {
super()
public constructor(constraint: Jolt.HingeConstraint, info?: mirabuf.IInfo) {
super(info)

this._constraint = constraint

Expand Down
5 changes: 3 additions & 2 deletions fission/src/systems/simulation/driver/SliderDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Jolt from "@barclah/jolt-physics"
import Driver, { DriverControlMode } from "./Driver"
import { GetLastDeltaT } from "@/systems/physics/PhysicsSystem"
import JOLT from "@/util/loading/JoltSyncLoader"
import { mirabuf } from "@/proto/mirabuf"

class SliderDriver extends Driver {
private _constraint: Jolt.SliderConstraint
Expand Down Expand Up @@ -55,8 +56,8 @@ class SliderDriver extends Driver {
}
}

public constructor(constraint: Jolt.SliderConstraint) {
super()
public constructor(constraint: Jolt.SliderConstraint, info?: mirabuf.IInfo) {
super(info)

this._constraint = constraint

Expand Down
24 changes: 21 additions & 3 deletions fission/src/systems/simulation/driver/WheelDriver.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import Jolt from "@barclah/jolt-physics"
import Driver from "./Driver"
import JOLT from "@/util/loading/JoltSyncLoader"
import { SimType } from "../wpilib_brain/WPILibBrain"
import { mirabuf } from "@/proto/mirabuf"

const LATERIAL_FRICTION = 0.6
const LONGITUDINAL_FRICTION = 0.8

class WheelDriver extends Driver {
private _constraint: Jolt.VehicleConstraint
private _wheel: Jolt.WheelWV
public deviceType?: SimType
public device?: string
private _reversed: boolean

private _targetWheelSpeed: number = 0.0

Expand All @@ -22,17 +27,30 @@ class WheelDriver extends Driver {
return this._constraint
}

public constructor(constraint: Jolt.VehicleConstraint) {
super()
public constructor(
constraint: Jolt.VehicleConstraint,
info?: mirabuf.IInfo,
deviceType?: SimType,
device?: string,
reversed: boolean = false
) {
super(info)

this._constraint = constraint
this._reversed = reversed
this.deviceType = deviceType
this.device = device
this._wheel = JOLT.castObject(this._constraint.GetWheel(0), JOLT.WheelWV)
this._wheel.set_mCombinedLateralFriction(LATERIAL_FRICTION)
this._wheel.set_mCombinedLongitudinalFriction(LONGITUDINAL_FRICTION)
}

public Update(_: number): void {
this._wheel.SetAngularVelocity(this._targetWheelSpeed)
this._wheel.SetAngularVelocity(this._targetWheelSpeed * (this._reversed ? -1 : 1))
}

public set reversed(val: boolean) {
this._reversed = val
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import Behavior from "../behavior/Behavior"
import World from "@/systems/World"
import WheelDriver from "../driver/WheelDriver"
import WheelRotationStimulus from "../stimulus/WheelStimulus"
import ArcadeDriveBehavior from "../behavior/ArcadeDriveBehavior"
import ArcadeDriveBehavior from "../behavior/synthesis/ArcadeDriveBehavior"
import { SimulationLayer } from "../SimulationSystem"
import Jolt from "@barclah/jolt-physics"
import JOLT from "@/util/loading/JoltSyncLoader"
import HingeDriver from "../driver/HingeDriver"
import HingeStimulus from "../stimulus/HingeStimulus"
import GenericArmBehavior from "../behavior/GenericArmBehavior"
import GenericArmBehavior from "../behavior/synthesis/GenericArmBehavior"
import SliderDriver from "../driver/SliderDriver"
import SliderStimulus from "../stimulus/SliderStimulus"
import GenericElevatorBehavior from "../behavior/GenericElevatorBehavior"
import GenericElevatorBehavior from "../behavior/synthesis/GenericElevatorBehavior"
import PreferencesSystem from "@/systems/preferences/PreferencesSystem"
import InputSystem from "@/systems/input/InputSystem"

Expand Down
Loading

0 comments on commit 3799d07

Please sign in to comment.