Skip to content

Commit

Permalink
Merge pull request #163 from the-orange-alliance/update-ramp-behavior
Browse files Browse the repository at this point in the history
Update ramp behavior
  • Loading branch information
kyle-flynn authored Sep 25, 2024
2 parents 7d5070b + d6a7fa9 commit 2b776d6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 64 deletions.
11 changes: 0 additions & 11 deletions front-end/src/seasons/fgc-2024/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
goalGreenOnlyColorAtom,
goalLedLengthAtom,
matchEndBlueNexusGoalColorAtom,
matchEndRampColorAtom,
matchEndRedNexusGoalColorAtom,
prepareFieldColorAtom,
rampBalancedColorAtom,
Expand Down Expand Up @@ -49,9 +48,6 @@ export const Settings: FC = () => {
useRecoilState(matchEndRedNexusGoalColorAtom);
const [matchEndBlueNexusGoalColor, setMatchEndBlueNexusGoalColor] =
useRecoilState(matchEndBlueNexusGoalColorAtom);
const [matchEndRampColor, setMatchEndRampColor] = useRecoilState(
matchEndRampColorAtom
);
const [redWledWebSocketAddress, setRedWledWebSocketAddress] = useRecoilState(
redWledWebSocketAddressAtom
);
Expand Down Expand Up @@ -154,13 +150,6 @@ export const Settings: FC = () => {
format='string'
inline
/>
<ColorSetting
name='Match End Ramp Color'
value={matchEndRampColor}
onChange={setMatchEndRampColor}
format='string'
inline
/>
<TextSetting
name='Red WLED WebSocket Address'
value={redWledWebSocketAddress}
Expand Down
7 changes: 0 additions & 7 deletions front-end/src/seasons/fgc-2024/stores/settings-store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ export const matchEndBlueNexusGoalColorAtom = atom<string>({
effects: [localStorageEffect('ftf.matchEndBlueNexusGoalColor')]
});

export const matchEndRampColorAtom = atom<string>({
key: 'ftf.matchEndRampColor',
default: FeedingTheFutureFCS.defaultFieldOptions.matchEndRampColor,
effects: [localStorageEffect('ftf.matchEndRampColor')]
});

export const redWledWebSocketAddressAtom = atom<string>({
key: 'ftf.redWledWebSocketAddress',
default: FeedingTheFutureFCS.defaultFieldOptions.redWledWebSocketAddress,
Expand Down Expand Up @@ -167,7 +161,6 @@ export const fieldOptionsSelector: RecoilValueReadOnly<FeedingTheFutureFCS.Field
fieldFaultColor: get(fieldFaultColorAtom),
matchEndRedNexusGoalColor: get(matchEndRedNexusGoalColorAtom),
matchEndBlueNexusGoalColor: get(matchEndBlueNexusGoalColorAtom),
matchEndRampColor: get(matchEndRampColorAtom),
redWledWebSocketAddress: get(redWledWebSocketAddressAtom),
blueWledWebSocketAddress: get(blueWledWebSocketAddressAtom),
centerWledWebSocketAddress: get(centerWledWebSocketAddressAtom),
Expand Down
82 changes: 36 additions & 46 deletions lib/models/src/fcs/FeedingTheFutureFCS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ export class PacketManager {
private broadcastCallback: (update: FieldControlUpdatePacket) => void;
private matchEmitter: EventEmitter;
private actionQueue = new Map<string, Action>();
private matchInProgress: boolean = false;
private matchState: 'prestart' | 'in progress' | 'ended' | 'aborted' =
'prestart';

private previousBalanced = true;

public constructor(
fieldOptions: FieldOptions,
Expand Down Expand Up @@ -247,7 +250,7 @@ export class PacketManager {
};

public handleAbort = (): void => {
this.matchInProgress = false;
this.matchState = 'aborted';

const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips(
Expand Down Expand Up @@ -287,7 +290,7 @@ export class PacketManager {
};

public handleMatchStart = (): void => {
this.matchInProgress = true;
this.matchState = 'in progress';

const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips(
Expand All @@ -309,7 +312,7 @@ export class PacketManager {
};

public handleMatchEnd = (): void => {
this.matchInProgress = false;
this.matchState = 'ended';

const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips(
Expand All @@ -322,11 +325,10 @@ export class PacketManager {
LedStripA.ALL_RED_GOALS,
result
);
applyPatternToStrips(
this.fieldOptions.matchEndRampColor,
[LedStripA.RAMP],
result
);

// Don't apply pattern to ramp. Keep current state and allow future updates
// directly from digital input

applySetpointToMotors(0, MotorA.ALL_GOALS, result);

this.broadcastCallback(result);
Expand All @@ -348,7 +350,7 @@ export class PacketManager {
currentDetails: FeedingTheFuture.MatchDetails,
broadcast: (update: FieldControlUpdatePacket) => void
) => {
if (!this.matchInProgress) return;
if (this.matchState !== 'in progress') return;

this.handleGoalStateChange(
previousDetails.redNexusState.CW1,
Expand Down Expand Up @@ -567,12 +569,6 @@ export class PacketManager {
'blue',
broadcast
);

this.handleRampStateChange(
previousDetails.fieldBalanced,
currentDetails.fieldBalanced,
broadcast
);
};

private handleGoalStateChange = (
Expand Down Expand Up @@ -622,46 +618,42 @@ export class PacketManager {
broadcast(result);
};

private handleRampStateChange = (
previousBalanced: number,
currentBalanced: number,
broadcast: (update: FieldControlUpdatePacket) => void
) => {
if (currentBalanced === previousBalanced) return;
public handleDigitalInputs = (packet: DigitalInputsResult) => {
const balanced = (packet.hubs[RevHub.CENTER_CONTROL_HUB] & 0x1) !== 1;

if (balanced === this.previousBalanced) return;

// clearTimeout(this.timers.get('ramp'));
this.actionQueue.delete('ramp');

const hysteresisWindowMs = currentBalanced
const hysteresisWindowMs = balanced
? this.fieldOptions.rampBalancedHysteresisWindowMs
: this.fieldOptions.rampUnbalancedHysteresisWindowMs;

this.actionQueue.set('ramp', {
timestamp: Date.now() + hysteresisWindowMs,
callback: () => {
const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips(
currentBalanced
? this.fieldOptions.rampBalancedColor
: this.fieldOptions.rampUnbalancedColor,
[LedStripA.RAMP],
result
);
broadcast(result);
if (this.matchState === 'in progress') {
this.matchEmitter.emit(MatchSocketEvent.MATCH_UPDATE_DETAILS_ITEM, {
key: 'fieldBalanced',
value: balanced
} satisfies ItemUpdate);
}

if (this.matchState !== 'prestart' && this.matchState !== 'aborted') {
const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips(
balanced
? this.fieldOptions.rampBalancedColor
: this.fieldOptions.rampUnbalancedColor,
[LedStripA.RAMP],
result
);
this.broadcastCallback(result);
}
}
});
};

public handleDigitalInputs = (packet: DigitalInputsResult) => {
if (!this.matchInProgress) return;

const balanced = (packet.hubs[RevHub.CENTER_CONTROL_HUB] & 0x1) !== 1;
this.matchEmitter.emit(MatchSocketEvent.MATCH_UPDATE_DETAILS_ITEM, {
key: 'fieldBalanced',
value: balanced
} satisfies ItemUpdate);
};

runFoodProductionSequence = (
motor: MotorA,
strip: LedStripA,
Expand All @@ -671,7 +663,7 @@ export class PacketManager {
const steps = 10;

const recurse = (count: number) => {
if (!this.matchInProgress) return;
if (this.matchState !== 'in progress') return;

// Base state. Food production is over, dispense food and update lights
if (count === 0) {
Expand Down Expand Up @@ -761,7 +753,6 @@ export interface FieldOptions {
fieldFaultColor: string;
matchEndRedNexusGoalColor: string;
matchEndBlueNexusGoalColor: string;
matchEndRampColor: string;
redWledWebSocketAddress: string;
blueWledWebSocketAddress: string;
centerWledWebSocketAddress: string;
Expand Down Expand Up @@ -789,7 +780,6 @@ export const defaultFieldOptions: FieldOptions = {
fieldFaultColor: 'ff0000',
matchEndRedNexusGoalColor: 'ff0000',
matchEndBlueNexusGoalColor: '0000ff',
matchEndRampColor: 'ff00ff',
redWledWebSocketAddress: '',
blueWledWebSocketAddress: '',
centerWledWebSocketAddress: '',
Expand Down

0 comments on commit 2b776d6

Please sign in to comment.