Skip to content

Commit

Permalink
Add base for machine calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler committed May 15, 2024
1 parent b76c75a commit fba4f42
Show file tree
Hide file tree
Showing 15 changed files with 447 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ControllerServiceContext } from "./context/ControllerServiceContext";
import Navigation from "./panels/navigation/Navigation";
import PageTitle from "./components/pagetitle/PageTitle";
import WiFiSettings from "./pages/wifisettings/WiFiSettings";
import Calibrate from "./pages/calibrate/Calibrate";

const App = () => {
const navigate = useNavigate();
Expand Down Expand Up @@ -110,6 +111,10 @@ const App = () => {
path="wifi"
element={<WiFiSettings />}
/>
<Route
path="calibrate"
element={<Calibrate />}
/>
</Routes>
</Col>
</Row>
Expand Down
5 changes: 3 additions & 2 deletions src/components/alertmessage/AlertMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { Alert, Col, Row } from "react-bootstrap";

type AlertMessageProps = {
children: ReactNode;
variant?: "danger" | "warning" | "info";
};
const AlertMessage = ({ children }: AlertMessageProps) => {
const AlertMessage = ({ children, variant }: AlertMessageProps) => {
return (
<Alert variant="danger">
<Alert variant={variant}>
<Row>
<Col
xs={1}
Expand Down
299 changes: 299 additions & 0 deletions src/pages/calibrate/Calibrate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
import React, { useContext, useEffect } from "react";
import { useState } from "react";
import { Spinner } from "../../components";
import { GetConfigFilenameCommand, ListFilesCommand } from "../../services";
import usePageView from "../../hooks/usePageView";
import { fileDataToConfig, sleep } from "../../utils/utils";
import { ControllerServiceContext } from "../../context/ControllerServiceContext";
import { Axis, Config, PinConfig } from "../../model/Config";
import PageTitle from "../../components/pagetitle/PageTitle";
import CalibratePin from "./CalibratePin";
import {
GetGpioDumpCommand,
GpioStatus
} from "../../services/controllerservice/commands/GetGpioDumpCommand";
import AlertMessage from "../../components/alertmessage/AlertMessage";
import { Link } from "react-router-dom";

type AxisPinsProps = {
axis: string;
gpioStatusList: GpioStatus[];
axisConfig: Axis;
};

const AxisPins = ({ axis, gpioStatusList, axisConfig }: AxisPinsProps) => {
return (
<>
<CalibratePin
label={axis + " - Motor 0 - Limit all pin"}
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
axisConfig.motor0?.limit_all_pin
)}
/>
<CalibratePin
label={axis + " - Motor 0 - Limit negative pin"}
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
axisConfig.motor0?.limit_neg_pin
)}
/>
<CalibratePin
label={axis + " - Motor 0 - Limit positive pin"}
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
axisConfig.motor0?.limit_pos_pin
)}
/>
<CalibratePin
label={axis + " - Motor 1 - Limit all pin"}
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
axisConfig.motor1?.limit_all_pin
)}
/>
<CalibratePin
label={axis + " - Motor 1 - Limit negative pin"}
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
axisConfig.motor1?.limit_neg_pin
)}
/>
<CalibratePin
label={axis + " - Motor 1 - Limit positive pin"}
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
axisConfig.motor1?.limit_pos_pin
)}
/>
</>
);
};

const Calibrate = () => {
usePageView("Calibration");
const controllerService = useContext(ControllerServiceContext);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [config, setConfig] = useState<Config | undefined>();
const [configFile, setConfigFile] = useState<string>();
const [gpioStatusList, setGpioStatusList] = useState<GpioStatus[]>([]);

useEffect(() => {
if (!controllerService) {
return;
}

let timer;
setIsLoading(true);
controllerService
.connect()
.then(async () => {
await controllerService.serialPort.write(Buffer.from([0x0c])); // CTRL-L Restting echo mode
const configFilenameCommand = await controllerService.send(
new GetConfigFilenameCommand()
);
setConfigFile(configFilenameCommand.getFilename());
const listCommand = await controllerService.send(
new ListFilesCommand()
);

if (
listCommand
.getFiles()
.map((f) => f.name)
.indexOf(configFilenameCommand.getFilename()) > -1
) {
const fileData = await controllerService.downloadFile(
configFilenameCommand.getFilename()
);
setConfig(fileDataToConfig(fileData.toString()));

await sleep(2000);
timer = setInterval(() => {
controllerService
.send(new GetGpioDumpCommand())
.then((command) => {
setGpioStatusList(command.getStatusList());
})
.catch((error) => console.error(error));
}, 500);
}
})
.finally(() => {
setIsLoading(false);
});

return () => {
if (timer) {
clearInterval(timer);
}
};
}, [
controllerService,
setIsLoading,
setConfig,
setGpioStatusList,
setConfigFile
]);

return (
<>
<PageTitle>Calibrate</PageTitle>
{isLoading && (
<>
Loading configuration <Spinner />
</>
)}

{!isLoading && !config && (
<AlertMessage variant="warning">
Could not find the config file <b>{configFile}</b>.<br />
Go to the <Link to="/files">file browser</Link> and check
that you have a valid configuration file.
</AlertMessage>
)}

{!isLoading && config && (
<>
<AlertMessage variant="info">
This page is still a work in progress and will
eventually be used for calibrating and fine tune your
machine.
</AlertMessage>

<h3>Input pins</h3>
<p>
Here you can check that your input pins are triggered
correctly. Trigger them manually to make sure they are
configured properly.
</p>
{config?.axes?.x && (
<AxisPins
axis={"X"}
gpioStatusList={gpioStatusList}
axisConfig={config?.axes?.x}
/>
)}
{config?.axes?.y && (
<AxisPins
axis={"Y"}
gpioStatusList={gpioStatusList}
axisConfig={config?.axes?.y}
/>
)}
{config?.axes?.z && (
<AxisPins
axis={"Z"}
gpioStatusList={gpioStatusList}
axisConfig={config?.axes?.z}
/>
)}
{config?.axes?.a && (
<AxisPins
axis={"A"}
gpioStatusList={gpioStatusList}
axisConfig={config?.axes?.a}
/>
)}
{config?.axes?.b && (
<AxisPins
axis={"B"}
gpioStatusList={gpioStatusList}
axisConfig={config?.axes?.b}
/>
)}
{config?.axes?.c && (
<AxisPins
axis={"C"}
gpioStatusList={gpioStatusList}
axisConfig={config?.axes?.c}
/>
)}
<CalibratePin
label="Probe pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(config.probe?.pin)}
/>
<CalibratePin
label="Probe tool setter pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
config.probe?.toolsetter_pin
)}
/>
<CalibratePin
label="Cycle start pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
config.control?.cycle_start_pin
)}
/>
<CalibratePin
label="E-stop pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
config.control?.estop_pin
)}
/>
<CalibratePin
label="Fault pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
config.control?.fault_pin
)}
/>
<CalibratePin
label="Feed hold pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
config.control?.feed_hold_pin
)}
/>
<CalibratePin
label="Macro0 pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
config.control?.macro0_pin
)}
/>
<CalibratePin
label="Macro1 pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
config.control?.macro1_pin
)}
/>
<CalibratePin
label="Macro2 pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
config.control?.macro2_pin
)}
/>
<CalibratePin
label="Macro3 pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
config.control?.macro3_pin
)}
/>
<CalibratePin
label="Reset pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
config.control?.reset_pin
)}
/>
<CalibratePin
label="Safety door pin"
gpioStatusList={gpioStatusList}
pinConfig={PinConfig.fromString(
config.control?.safety_door_pin
)}
/>
</>
)}
</>
);
};
export default Calibrate;
51 changes: 51 additions & 0 deletions src/pages/calibrate/CalibratePin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from "react";
import { Pin, PinConfig } from "../../model/Config";
import { GpioStatus } from "../../services/controllerservice/commands/GetGpioDumpCommand";
import { Badge, Col, Row } from "react-bootstrap";

type Props = {
label: string;
pinConfig?: PinConfig;
gpioStatusList: GpioStatus[];
};

const CalibratePin = ({ label, pinConfig, gpioStatusList }: Props) => {
const gpioStatus: GpioStatus | undefined = gpioStatusList.find(
(s) => s.pin === pinConfig?.pin
);

if (!pinConfig || pinConfig.pin === Pin.NO_PIN) {
return;
}

return (
<Row
style={{
padding: "5px",
paddingTop: "14px",
margin: 0,
border: "1px solid lightgray"
}}
>
<Col>{label}</Col>
<Col>
<h5>
{!gpioStatus && <Badge bg="secondary">?</Badge>}

{((gpioStatus?.state == 1 && pinConfig?.active == "low") ||
(gpioStatus?.state == 0 &&
pinConfig?.active == "high")) && (
<Badge bg="success">INACTIVE</Badge>
)}
{((gpioStatus?.state == 0 && pinConfig?.active == "low") ||
(gpioStatus?.state == 1 &&
pinConfig?.active == "high")) && (
<Badge bg="danger">TRIGGERED</Badge>
)}
</h5>
</Col>
</Row>
);
};

export default CalibratePin;
Loading

0 comments on commit fba4f42

Please sign in to comment.