Skip to content

Commit d44567b

Browse files
Unstick Button Input [AARD-2018] (#1239)
Co-authored-by: Alexey Dmitriev <157652245+AlexD717@users.noreply.github.com>
2 parents f1fd3a5 + 8e1dd9c commit d44567b

File tree

7 files changed

+89
-7
lines changed

7 files changed

+89
-7
lines changed

fission/src/systems/input/DefaultInputs.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class DefaultInputs {
2727

2828
ButtonInput.onKeyboard("intake", "KeyE"),
2929
ButtonInput.onKeyboard("eject", "KeyQ"),
30+
ButtonInput.onKeyboard("unstick", "Space"),
3031

3132
AxisInput.onKeyboardSingleKey("joint 1", "Digit1", negativeModifierKeys),
3233
AxisInput.onKeyboardSingleKey("joint 2", "Digit2", negativeModifierKeys),
@@ -62,6 +63,7 @@ class DefaultInputs {
6263

6364
ButtonInput.onKeyboard("intake", "KeyE"),
6465
ButtonInput.onKeyboard("eject", "KeyQ"),
66+
ButtonInput.onKeyboard("unstick", "Space"),
6567

6668
AxisInput.onKeyboardSingleKey("joint 1", "Digit1", negativeModifierKeys),
6769
AxisInput.onKeyboardSingleKey("joint 2", "Digit2", negativeModifierKeys),
@@ -97,6 +99,7 @@ class DefaultInputs {
9799

98100
ButtonInput.onKeyboard("intake", "Semicolon"),
99101
ButtonInput.onKeyboard("eject", "KeyL"),
102+
ButtonInput.onKeyboard("unstick", "KeyK"),
100103

101104
AxisInput.onKeyboardSingleKey("joint 1", "Slash", negativeModifierKeys),
102105
AxisInput.onKeyboardSingleKey("joint 2", "Period", negativeModifierKeys),
@@ -128,6 +131,7 @@ class DefaultInputs {
128131

129132
ButtonInput.onGamepad("intake", 4),
130133
ButtonInput.onGamepad("eject", 5),
134+
ButtonInput.onGamepad("unstick", 6),
131135

132136
AxisInput.onGamepadButtons("joint 1", 3, 0),
133137
AxisInput.onGamepadButtons("joint 2", 1, 2),
@@ -158,6 +162,7 @@ class DefaultInputs {
158162

159163
ButtonInput.onGamepad("intake", 4),
160164
ButtonInput.onGamepad("eject", 5),
165+
ButtonInput.onGamepad("unstick", 6),
161166

162167
AxisInput.onGamepadButtons("joint 1", 12, 13),
163168
AxisInput.onGamepadButtons("joint 2", 15, 14),
@@ -187,6 +192,7 @@ class DefaultInputs {
187192

188193
ButtonInput.onGamepad("intake", 4),
189194
ButtonInput.onGamepad("eject", 5),
195+
ButtonInput.onGamepad("unstick", 6),
190196

191197
AxisInput.onGamepadButtons("joint 1", 3, 0),
192198
AxisInput.onGamepadButtons("joint 2", 1, 2),
@@ -255,6 +261,7 @@ class DefaultInputs {
255261

256262
ButtonInput.unbound("intake"),
257263
ButtonInput.unbound("eject"),
264+
ButtonInput.unbound("unstick"),
258265

259266
AxisInput.unbound("joint 1"),
260267
AxisInput.unbound("joint 2"),

fission/src/systems/input/InputTypes.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import type { DriveType } from "../simulation/behavior/Behavior"
22
import type Input from "./inputs/Input"
33

4-
export type InputName = "arcadeDrive" | "arcadeTurn" | "tankLeft" | "tankRight" | "intake" | "eject" | `joint ${number}`
4+
export type InputName =
5+
| "arcadeDrive"
6+
| "arcadeTurn"
7+
| "tankLeft"
8+
| "tankRight"
9+
| "intake"
10+
| "eject"
11+
| "unstick"
12+
| `joint ${number}`
513

614
export type ModifierState = Readonly<{
715
alt: boolean

fission/src/systems/preferences/PreferenceTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export type RobotPreferences = {
132132
ejector: EjectorPreferences
133133
driveVelocity: number
134134
driveAcceleration: number
135+
unstickForce: number
135136
sequentialConfig?: SequentialBehaviorPreferences[]
136137
simConfig?: SimConfigData
137138
}
@@ -195,6 +196,7 @@ export function defaultRobotPreferences(): RobotPreferences {
195196
},
196197
driveVelocity: 0,
197198
driveAcceleration: 0,
199+
unstickForce: 8000,
198200
}
199201
}
200202

fission/src/systems/preferences/PreferencesSystem.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ class PreferencesSystem {
102102
return defaultPrefs
103103
}
104104

105-
return allRoboPrefs[miraName]
105+
const defaultPrefs = defaultRobotPreferences()
106+
const mergedPrefs = { ...defaultPrefs, ...allRoboPrefs[miraName] }
107+
allRoboPrefs[miraName] = mergedPrefs
108+
109+
return mergedPrefs
106110
}
107111

108112
/** Sets the RobotPreferences object for the robot of a specific mira name */
@@ -148,7 +152,11 @@ class PreferencesSystem {
148152
return defaultPrefs
149153
}
150154

151-
return allFieldPrefs[miraName]
155+
const defaultPrefs = defaultFieldPreferences()
156+
const mergedPrefs = { ...defaultPrefs, ...allFieldPrefs[miraName] }
157+
allFieldPrefs[miraName] = mergedPrefs
158+
159+
return mergedPrefs
152160
}
153161

154162
/** @returns Preferences for every field that was found in local storage. */
@@ -170,7 +178,12 @@ class PreferencesSystem {
170178
public static getMotorPreferences(miraName: string): MotorPreferences {
171179
const allMotorPrefs = this.getAllMotorPreferences()
172180

173-
allMotorPrefs[miraName] ??= defaultMotorPreferences(miraName)
181+
if (allMotorPrefs[miraName] == undefined) {
182+
allMotorPrefs[miraName] = defaultMotorPreferences(miraName)
183+
} else {
184+
const defaultPrefs = defaultMotorPreferences(miraName)
185+
allMotorPrefs[miraName] = { ...defaultPrefs, ...allMotorPrefs[miraName] }
186+
}
174187

175188
return allMotorPrefs[miraName]
176189
}
@@ -194,6 +207,10 @@ class PreferencesSystem {
194207
if (graphicsPrefs == undefined) {
195208
graphicsPrefs = defaultGraphicsPreferences()
196209
this._preferences[GRAPHICS_PREFERENCE_KEY] = graphicsPrefs
210+
} else {
211+
const defaultPrefs = defaultGraphicsPreferences()
212+
graphicsPrefs = { ...defaultPrefs, ...graphicsPrefs }
213+
this._preferences[GRAPHICS_PREFERENCE_KEY] = graphicsPrefs
197214
}
198215

199216
return graphicsPrefs

fission/src/systems/simulation/synthesis_brain/SynthesisBrain.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class SynthesisBrain extends Brain {
3636
// Tracks how many joins have been made with unique controls
3737
private _currentJointIndex = 1
3838

39+
// Track previous unstick button state to detect button press (not hold)
40+
private _prevUnstickPressed = false
41+
3942
public get assemblyName(): string {
4043
return this._assemblyName
4144
}
@@ -113,6 +116,34 @@ class SynthesisBrain extends Brain {
113116

114117
this._assembly.ejectorActive = InputSystem.getInput("eject", this._brainIndex) > 0.5
115118
this._assembly.intakeActive = InputSystem.getInput("intake", this._brainIndex) > 0.5
119+
120+
// Handle unstick
121+
const unstickPressed = InputSystem.getInput("unstick", this._brainIndex) === 1
122+
if (unstickPressed && !this._prevUnstickPressed) {
123+
this.applyUnstickForce()
124+
}
125+
126+
this._prevUnstickPressed = unstickPressed
127+
}
128+
129+
/**
130+
* Applies a small upward force to the robot's main body to help unstick it
131+
*/
132+
private applyUnstickForce(): void {
133+
const rootBodyId = this._mechanism.getBodyByNodeId(this._mechanism.rootBody)
134+
if (!rootBodyId) {
135+
console.warn("Could not find root body for unstick")
136+
return
137+
}
138+
139+
const body = World.physicsSystem.getBody(rootBodyId)
140+
if (!body) {
141+
console.warn("Could not get body for unstick")
142+
return
143+
}
144+
145+
const unstickForce = new JOLT.Vec3(0, PreferencesSystem.getRobotPreferences(this._assemblyName).unstickForce, 0)
146+
body.AddForce(unstickForce)
116147
}
117148

118149
public disable(): void {

fission/src/test/PreferencesSystem.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ describe("Preference System Robot/Field", () => {
135135
},
136136
driveVelocity: 3,
137137
driveAcceleration: 6,
138+
unstickForce: 8000,
138139
}
139140
const robotPreferences2: RobotPreferences = {
140141
inputsSchemes: [],
@@ -155,6 +156,7 @@ describe("Preference System Robot/Field", () => {
155156
},
156157
driveVelocity: 1.5,
157158
driveAcceleration: 8,
159+
unstickForce: 10000,
158160
}
159161

160162
PreferencesSystem.setRobotPreferences("RobotPreferences1", robotPreferences1)

fission/src/ui/panels/configuring/assembly-config/interfaces/SubsystemRowInterface.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ const SubsystemRowInterface: React.FC<SubsystemRowProps> = ({ robot, driver, seq
3939
const [force, setForce] = useState<number>(
4040
((driver as SliderDriver) || (driver as HingeDriver) || (driver as WheelDriver)).maxForce
4141
)
42+
const [unstickForce, setUnstickForce] = useState<number>(
43+
PreferencesSystem.getRobotPreferences(robot.assemblyName).unstickForce
44+
)
4245

4346
const onChange = useCallback(
44-
(vel: number, force: number) => {
47+
(vel: number, force: number, unstick: number) => {
4548
if (driver instanceof WheelDriver) {
4649
const wheelDrivers = robot?.mechanism
4750
? World.simulationSystem
@@ -78,6 +81,7 @@ const SubsystemRowInterface: React.FC<SubsystemRowProps> = ({ robot, driver, seq
7881
;((driver as SliderDriver) || (driver as HingeDriver)).maxForce = force
7982
}
8083

84+
PreferencesSystem.getRobotPreferences(robot.assemblyName).unstickForce = unstick
8185
PreferencesSystem.savePreferences()
8286
},
8387
[driver, robot.mechanism, robot.assemblyName]
@@ -99,7 +103,7 @@ const SubsystemRowInterface: React.FC<SubsystemRowProps> = ({ robot, driver, seq
99103
// format={{ minimumFractionDigits: 2, maximumFractionDigits: 2 }}
100104
onChange={velocity => {
101105
setVelocity(velocity as number)
102-
onChange(velocity as number, force)
106+
onChange(velocity as number, force, unstickForce)
103107
}}
104108
step={0.01}
105109
/>
@@ -114,7 +118,7 @@ const SubsystemRowInterface: React.FC<SubsystemRowProps> = ({ robot, driver, seq
114118
// format={{ minimumFractionDigits: 2, maximumFractionDigits: 2 }}
115119
onChange={force => {
116120
setForce(force as number)
117-
onChange(velocity, force as number)
121+
onChange(velocity, force as number, unstickForce)
118122
}}
119123
step={0.01}
120124
/>
@@ -129,6 +133,17 @@ const SubsystemRowInterface: React.FC<SubsystemRowProps> = ({ robot, driver, seq
129133
}}
130134
/>
131135
)}
136+
<StatefulSlider
137+
min={0}
138+
max={15000}
139+
defaultValue={unstickForce}
140+
label="Unstick Force"
141+
onChange={(value: number | number[]) => {
142+
setUnstickForce(value as number)
143+
onChange(velocity, force, value as number)
144+
}}
145+
step={100}
146+
/>
132147
</Stack>
133148
</Stack>
134149
<Divider />

0 commit comments

Comments
 (0)