-
Notifications
You must be signed in to change notification settings - Fork 20
1027 add minimal bottlecapsorter using ip20 module and one beckoff stepper #1028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
TheBest6337
wants to merge
4
commits into
master
Choose a base branch
from
1027-add-bottlecapsorter-using-ip20-module-and-one-wago-stepper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
101671d
Add Minimal Bottle Sorter machine implementation with control and UI …
TheBest6337 1398c72
Add EK1100 Bus Coupler and adjust roles in Minimal Bottle Sorter
TheBest6337 6be7f9a
Refactor minimal bottle sorter module imports and clean up code forma…
TheBest6337 1605553
Format code for better readability in useMinimalBottleSorter function
TheBest6337 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
electron/src/machines/minimalbottlesorter/MinimalBottleSorterControlPage.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import { Page } from "@/components/Page"; | ||
| import { ControlGrid } from "@/control/ControlGrid"; | ||
| import { ControlCard } from "@/control/ControlCard"; | ||
| import { Label } from "@/control/Label"; | ||
| import { SelectionGroup } from "@/control/SelectionGroup"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { EditValue } from "@/control/EditValue"; | ||
| import { Badge } from "@/components/ui/badge"; | ||
| import { useMinimalBottleSorter } from "./useMinimalBottleSorter"; | ||
| import React from "react"; | ||
|
|
||
| export function MinimalBottleSorterControlPage() { | ||
| const { | ||
| state, | ||
| liveValues, | ||
| setStepperSpeed, | ||
| setStepperDirection, | ||
| setStepperEnabled, | ||
| pulseOutput, | ||
| } = useMinimalBottleSorter(); | ||
|
|
||
| const safeState = state ?? { | ||
| stepper_enabled: false, | ||
| stepper_speed: 0, | ||
| stepper_direction: true, | ||
| outputs: [false, false, false, false, false, false, false, false], | ||
| }; | ||
| const safeLiveValues = liveValues ?? { | ||
| stepper_actual_speed: 0, | ||
| stepper_position: 0, | ||
| }; | ||
|
|
||
| return ( | ||
| <Page> | ||
| <ControlGrid columns={2}> | ||
| {/* Stepper Motor Control */} | ||
| <ControlCard title="Stepper Motor"> | ||
| <div className="space-y-6"> | ||
| {/* Enable/Disable */} | ||
| <Label label="Motor Enable"> | ||
| <SelectionGroup<"Enabled" | "Disabled"> | ||
| value={safeState.stepper_enabled ? "Enabled" : "Disabled"} | ||
| orientation="vertical" | ||
| className="grid h-full grid-cols-2 gap-2" | ||
| options={{ | ||
| Disabled: { | ||
| children: "Disabled", | ||
| icon: "lu:CirclePause", | ||
| isActiveClassName: "bg-red-600", | ||
| className: "h-full", | ||
| }, | ||
| Enabled: { | ||
| children: "Enabled", | ||
| icon: "lu:CirclePlay", | ||
| isActiveClassName: "bg-green-600", | ||
| className: "h-full", | ||
| }, | ||
| }} | ||
| onChange={(value) => setStepperEnabled(value === "Enabled")} | ||
| /> | ||
| </Label> | ||
|
|
||
| {/* Direction */} | ||
| <Label label="Direction"> | ||
| <SelectionGroup<"Forward" | "Backward"> | ||
| value={safeState.stepper_direction ? "Forward" : "Backward"} | ||
| orientation="vertical" | ||
| className="grid h-full grid-cols-2 gap-2" | ||
| options={{ | ||
| Forward: { | ||
| children: "Forward", | ||
| icon: "lu:ArrowRight", | ||
| isActiveClassName: "bg-blue-600", | ||
| className: "h-full", | ||
| }, | ||
| Backward: { | ||
| children: "Backward", | ||
| icon: "lu:ArrowLeft", | ||
| isActiveClassName: "bg-blue-600", | ||
| className: "h-full", | ||
| }, | ||
| }} | ||
| onChange={(value) => setStepperDirection(value === "Forward")} | ||
| /> | ||
| </Label> | ||
|
|
||
| {/* Speed Control */} | ||
| <Label label="Speed (mm/s)"> | ||
| <EditValue | ||
| title="Speed" | ||
| value={safeState.stepper_speed} | ||
| onChange={setStepperSpeed} | ||
| renderValue={(v) => v.toFixed(1)} | ||
| min={0} | ||
| max={100} | ||
| step={0.1} | ||
| /> | ||
| </Label> | ||
|
|
||
| {/* Live Values */} | ||
| <div className="space-y-2"> | ||
| <div className="flex justify-between"> | ||
| <span className="text-sm text-gray-400">Actual Speed:</span> | ||
| <Badge className="bg-blue-600"> | ||
| {safeLiveValues.stepper_actual_speed.toFixed(1)} steps/s | ||
| </Badge> | ||
| </div> | ||
| <div className="flex justify-between"> | ||
| <span className="text-sm text-gray-400">Position:</span> | ||
| <Badge className="bg-purple-600"> | ||
| {safeLiveValues.stepper_position.toLocaleString()} steps | ||
| </Badge> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </ControlCard> | ||
|
|
||
| {/* Digital Output Pulses */} | ||
| <ControlCard title="Digital Output Pulses"> | ||
| <div className="grid grid-cols-2 gap-4"> | ||
| {safeState.outputs.map((output, index) => ( | ||
| <div key={index} className="space-y-2"> | ||
| <Label label={`Output ${index + 1}`}> | ||
| <Button | ||
| onClick={() => pulseOutput(index, 100)} | ||
| className={`w-full ${output ? "bg-green-600" : "bg-gray-600"}`} | ||
| disabled={output} | ||
| > | ||
| {output ? "Active" : "Pulse"} | ||
| </Button> | ||
| </Label> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| <div className="mt-4 text-sm text-gray-400"> | ||
| Click to pulse output for 100ms | ||
| </div> | ||
| </ControlCard> | ||
| </ControlGrid> | ||
| </Page> | ||
| ); | ||
| } |
21 changes: 21 additions & 0 deletions
21
electron/src/machines/minimalbottlesorter/MinimalBottleSorterPage.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { Topbar } from "@/components/Topbar"; | ||
| import { bottleSorterSerialRoute } from "@/routes/routes"; | ||
| import React from "react"; | ||
|
|
||
| export function MinimalBottleSorterPage(): React.JSX.Element { | ||
| const { serial } = bottleSorterSerialRoute.useParams(); | ||
|
|
||
| return ( | ||
| <Topbar | ||
| pathname={`/_sidebar/machines/minimalbottlesorter/${serial}`} | ||
| items={[ | ||
| { | ||
| link: "control", | ||
| activeLink: "control", | ||
| title: "Control", | ||
| icon: "lu:ToggleLeft", | ||
| }, | ||
| ]} | ||
| /> | ||
| ); | ||
| } |
93 changes: 93 additions & 0 deletions
93
electron/src/machines/minimalbottlesorter/minimalBottleSorterNamespace.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { StoreApi } from "zustand"; | ||
| import { create } from "zustand"; | ||
| import { z } from "zod"; | ||
| import { | ||
| EventHandler, | ||
| eventSchema, | ||
| Event, | ||
| handleUnhandledEventError, | ||
| NamespaceId, | ||
| createNamespaceHookImplementation, | ||
| ThrottledStoreUpdater, | ||
| } from "@/client/socketioStore"; | ||
| import { MachineIdentificationUnique } from "@/machines/types"; | ||
|
|
||
| // ========== Event Schema ========== | ||
|
|
||
| export const stateEventDataSchema = z.object({ | ||
| stepper_enabled: z.boolean(), | ||
| stepper_speed: z.number(), | ||
| stepper_direction: z.boolean(), | ||
| outputs: z.array(z.boolean()).length(8), | ||
| }); | ||
|
|
||
| export const liveValuesEventDataSchema = z.object({ | ||
| stepper_actual_speed: z.number(), | ||
| stepper_position: z.number(), | ||
| }); | ||
|
|
||
| export const stateEventSchema = eventSchema(stateEventDataSchema); | ||
| export const liveValuesEventSchema = eventSchema(liveValuesEventDataSchema); | ||
|
|
||
| export type StateEvent = z.infer<typeof stateEventDataSchema>; | ||
| export type LiveValuesEvent = z.infer<typeof liveValuesEventDataSchema>; | ||
|
|
||
| // ========== Store ========== | ||
| export type MinimalBottleSorterNamespaceStore = { | ||
| state: StateEvent | null; | ||
| liveValues: LiveValuesEvent | null; | ||
| }; | ||
|
|
||
| export const createMinimalBottleSorterNamespaceStore = | ||
| (): StoreApi<MinimalBottleSorterNamespaceStore> => | ||
| create<MinimalBottleSorterNamespaceStore>(() => ({ | ||
| state: null, | ||
| liveValues: null, | ||
| })); | ||
|
|
||
| // ========== Message Handler ========== | ||
| export function minimalBottleSorterMessageHandler( | ||
| store: StoreApi<MinimalBottleSorterNamespaceStore>, | ||
| throttledUpdater: ThrottledStoreUpdater<MinimalBottleSorterNamespaceStore>, | ||
| ): EventHandler { | ||
| return (event: Event<any>) => { | ||
| const updateStore = ( | ||
| updater: ( | ||
| state: MinimalBottleSorterNamespaceStore, | ||
| ) => MinimalBottleSorterNamespaceStore, | ||
| ) => throttledUpdater.updateWith(updater); | ||
|
|
||
| try { | ||
| if (event.name === "StateEvent") { | ||
| const parsed = stateEventSchema.parse(event); | ||
| updateStore((current) => ({ ...current, state: parsed.data })); | ||
| } else if (event.name === "LiveValuesEvent") { | ||
| const parsed = liveValuesEventSchema.parse(event); | ||
| updateStore((current) => ({ ...current, liveValues: parsed.data })); | ||
| } else { | ||
| handleUnhandledEventError(event.name); | ||
| } | ||
| } catch (error) { | ||
| console.error(`Error processing ${event.name}:`, error); | ||
| throw error; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| // ========== Namespace Hook ========== | ||
| const useMinimalBottleSorterNamespaceImplementation = | ||
| createNamespaceHookImplementation<MinimalBottleSorterNamespaceStore>({ | ||
| createStore: createMinimalBottleSorterNamespaceStore, | ||
| createEventHandler: minimalBottleSorterMessageHandler, | ||
| }); | ||
|
|
||
| export function useMinimalBottleSorterNamespace( | ||
| machine_identification_unique: MachineIdentificationUnique, | ||
| ): MinimalBottleSorterNamespaceStore { | ||
| const namespaceId: NamespaceId = { | ||
| type: "machine", | ||
| machine_identification_unique, | ||
| }; | ||
|
|
||
| return useMinimalBottleSorterNamespaceImplementation(namespaceId); | ||
| } | ||
126 changes: 126 additions & 0 deletions
126
electron/src/machines/minimalbottlesorter/useMinimalBottleSorter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,126 @@ | ||||
| import { toastError } from "@/components/Toast"; | ||||
| import { useStateOptimistic } from "@/lib/useStateOptimistic"; | ||||
| import { bottleSorterSerialRoute } from "@/routes/routes"; | ||||
| import { MachineIdentificationUnique } from "@/machines/types"; | ||||
| import { | ||||
| useMinimalBottleSorterNamespace, | ||||
| StateEvent, | ||||
| LiveValuesEvent, | ||||
|
||||
| LiveValuesEvent, |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The backend uses i128 for stepper_position, but the frontend schema uses z.number() which represents a JavaScript number (max safe integer is 2^53-1). This type mismatch may cause precision loss or overflow issues if the stepper position exceeds JavaScript's safe integer range. Consider using z.bigint() or documenting the expected range of the stepper position.