feat: tracked movement on x axis

This commit is contained in:
joshuashoemaker 2021-01-22 20:33:45 -06:00
parent b7e4c83d39
commit 6572fcc7ab
5 changed files with 108 additions and 2 deletions

View File

@ -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

View File

@ -0,0 +1,8 @@
interface IMotor {
pinOne: number,
pinTwo: number,
pinThree: number,
pinFour: number
}
export default IMotor

62
src/Server/MotorMover.ts Normal file
View File

@ -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

View File

@ -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

View File

@ -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}`)
})
}