feat: tracked movement on x axis
This commit is contained in:
parent
be6efeddcf
commit
c7a256269f
17
src/Server/Entities/Motor.ts
Normal file
17
src/Server/Entities/Motor.ts
Normal 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
|
||||||
8
src/Server/Interfaces/IMotor.ts
Normal file
8
src/Server/Interfaces/IMotor.ts
Normal 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
62
src/Server/MotorMover.ts
Normal 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
|
||||||
8
src/Server/UseCases/Factories/makeMotor.ts
Normal file
8
src/Server/UseCases/Factories/makeMotor.ts
Normal 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
|
||||||
@ -2,6 +2,7 @@ import Server from './Server'
|
|||||||
|
|
||||||
import EventManager from './EventManager'
|
import EventManager from './EventManager'
|
||||||
import IEventManager from './Interfaces/IEventManager';
|
import IEventManager from './Interfaces/IEventManager';
|
||||||
|
import MotorMover from './MotorMover';
|
||||||
|
|
||||||
|
|
||||||
function sleep (ms: number) {
|
function sleep (ms: number) {
|
||||||
@ -14,8 +15,18 @@ const main = () => {
|
|||||||
const server = new Server(port)
|
const server = new Server(port)
|
||||||
const eventManager: IEventManager = new EventManager()
|
const eventManager: IEventManager = new EventManager()
|
||||||
|
|
||||||
eventManager.listen('onReceiveOffsets', (offsets: unknown[]) => {
|
const motorMover = new MotorMover({ pinOne: 3, pinTwo: 5, pinThree: 7, pinFour: 11 })
|
||||||
console.log(offsets)
|
// 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}`)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user