From 6572fcc7ab83d3aba0d0f9615e11fd929d4bfe3c Mon Sep 17 00:00:00 2001 From: joshuashoemaker Date: Fri, 22 Jan 2021 20:33:45 -0600 Subject: [PATCH] feat: tracked movement on x axis --- src/Server/Entities/Motor.ts | 17 ++++++ src/Server/Interfaces/IMotor.ts | 8 +++ src/Server/MotorMover.ts | 62 ++++++++++++++++++++++ src/Server/UseCases/Factories/makeMotor.ts | 8 +++ src/Server/main.ts | 15 +++++- 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 src/Server/Entities/Motor.ts create mode 100644 src/Server/Interfaces/IMotor.ts create mode 100644 src/Server/MotorMover.ts create mode 100644 src/Server/UseCases/Factories/makeMotor.ts diff --git a/src/Server/Entities/Motor.ts b/src/Server/Entities/Motor.ts new file mode 100644 index 0000000..d308f78 --- /dev/null +++ b/src/Server/Entities/Motor.ts @@ -0,0 +1,17 @@ +import IMotor from "../Interfaces/IMotor" + +class Motor implements IMotor { + public pinOne: number + public pinTwo: number + public pinThree: number + public pinFour: number + + constructor (props: IMotor) { + this.pinOne = props.pinOne + this.pinTwo = props.pinTwo + this.pinThree = props.pinThree + this.pinFour = props.pinFour + } +} + +export default Motor diff --git a/src/Server/Interfaces/IMotor.ts b/src/Server/Interfaces/IMotor.ts new file mode 100644 index 0000000..4b93896 --- /dev/null +++ b/src/Server/Interfaces/IMotor.ts @@ -0,0 +1,8 @@ +interface IMotor { + pinOne: number, + pinTwo: number, + pinThree: number, + pinFour: number +} + +export default IMotor diff --git a/src/Server/MotorMover.ts b/src/Server/MotorMover.ts new file mode 100644 index 0000000..ecdcd7b --- /dev/null +++ b/src/Server/MotorMover.ts @@ -0,0 +1,62 @@ +import IMotor from "./Interfaces/IMotor" +import makeMotor from "./UseCases/Factories/makeMotor" +import * as childProcesses from 'child_process' + +class MotorMover { + motor: IMotor + moveProcess: childProcesses.ChildProcessWithoutNullStreams | null = null + pauseIntervalTime: number = 0.05 + movementState: 'CLOCKWISE' | 'COUNTERCLOCKWISE' | "IDLE" = 'IDLE' + + constructor (motor: IMotor) { + this.motor = makeMotor(motor) + } + + public moveClockwise = () => { + if (this.movementState === 'CLOCKWISE') return + + this.moveProcess?.kill() + this.moveProcess = null + + const motorProcessArguments = [ + 'src/Server/moveStepper.py', + this.motor.pinOne.toString(), + this.motor.pinTwo.toString(), + this.motor.pinThree.toString(), + this.motor.pinFour.toString(), + 'clockwise', + this.pauseIntervalTime.toString() + ] + + console.log('start clockwise') + this.moveProcess = childProcesses.spawn('python', motorProcessArguments) + this.movementState = 'CLOCKWISE' + } + + public moveCounterClockwise = () => { + if (this.movementState === 'COUNTERCLOCKWISE') return + + this.moveProcess?.kill() + this.moveProcess = null + + const motorProcessArguments = [ + 'src/Server/moveStepper.py', + this.motor.pinOne.toString(), + this.motor.pinTwo.toString(), + this.motor.pinThree.toString(), + this.motor.pinFour.toString(), + 'counterClockwise', + this.pauseIntervalTime.toString() + ] + + this.moveProcess = childProcesses.spawn('python', motorProcessArguments) + this.movementState = 'COUNTERCLOCKWISE' + } + + public stopMovement = () => { + this.moveProcess?.kill() + this.movementState = 'IDLE' + } +} + +export default MotorMover \ No newline at end of file diff --git a/src/Server/UseCases/Factories/makeMotor.ts b/src/Server/UseCases/Factories/makeMotor.ts new file mode 100644 index 0000000..66ab4fc --- /dev/null +++ b/src/Server/UseCases/Factories/makeMotor.ts @@ -0,0 +1,8 @@ +import Motor from "../../Entities/Motor" +import IMotor from "../../Interfaces/IMotor" + +function makeMotor (props: IMotor) { + return new Motor(props) +} + +export default makeMotor diff --git a/src/Server/main.ts b/src/Server/main.ts index 557ca05..d33ce74 100644 --- a/src/Server/main.ts +++ b/src/Server/main.ts @@ -2,6 +2,7 @@ import Server from './Server' import EventManager from './EventManager' import IEventManager from './Interfaces/IEventManager'; +import MotorMover from './MotorMover'; function sleep (ms: number) { @@ -14,8 +15,18 @@ const main = () => { const server = new Server(port) const eventManager: IEventManager = new EventManager() - eventManager.listen('onReceiveOffsets', (offsets: unknown[]) => { - console.log(offsets) + const motorMover = new MotorMover({ pinOne: 3, pinTwo: 5, pinThree: 7, pinFour: 11 }) + // motorMover.moveClockwise() + + eventManager.listen('onReceiveOffsets', (offsets: any[]) => { + if (offsets[0]?.x > 50) { + motorMover.moveCounterClockwise() + } else if (offsets[0]?.x < - 50) { + motorMover.moveClockwise() + } else { + motorMover.stopMovement() + } + console.log(`moving ${motorMover.movementState}`) }) }