-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor_reset.js
93 lines (76 loc) · 1.84 KB
/
motor_reset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const fs = require("fs")
const motorList = require('./data/motor.json')
let Gpio
if (process.env.NODE_ENV != "development") {
Gpio = require('pigpio').Gpio;
}
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
// 개발용 모터 시뮬레이션 클래스
class virtualMotor {
constructor(e) {
this.motorId = e
}
servoWrite(angle) {
console.log(`[${this.motorId}] ${angle}hz`)
}
}
// 모터 클래스
class Motor {
constructor(gpio, name) {
this.name = name
this.motors = gpio.map(e => {
console.log(`${e} GPIO added to ${name} Group`)
if (process.env.NODE_ENV == "development") {
return new virtualMotor(e)
} else {
return new Gpio(e, { mode: Gpio.OUTPUT })
}
})
this.isOpen = false
}
async open(force = false) {
if (!this.isOpen || force) {
console.log(`Open ${this.name}`)
this.isOpen = true
this.motors.forEach(async e => {
e.servoWrite(motorList.config['open-angle']);
await sleep(500);
e.servoWrite(0);
});
return true
} else {
return false
}
}
async close(force = false) {
if (this.isOpen || force) {
console.log(`Close ${this.name}`)
this.isOpen = false
this.motors.forEach(async e => {
e.servoWrite(motorList.config['close-angle']);
await sleep(500);
e.servoWrite(0);
});
return true
} else {
return false
}
}
}
; (async () => {
const motorInfo = {}
// 모터 로딩
for (let model in motorList.motors) {
const motor = new Motor(motorList.motors[model].motors, model)
motorInfo[model] = motor
// 모터 테스트
console.log(`${model} TEST - Open`)
await motor.open()
await sleep(2000)
console.log(`${model} TEST - Close`)
await motor.close()
await sleep(2000)
}
})()