-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotorpgpio.mjs
More file actions
65 lines (55 loc) · 1.39 KB
/
motorpgpio.mjs
File metadata and controls
65 lines (55 loc) · 1.39 KB
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
import pigpio from 'pigpio'
const Gpio = pigpio.Gpio
let frequency = 8000
const HIGH = 1
const LOW = 0
let PWM
let motorUP
let motorDOWN
const initMotor = (in1 = 24, in2 = 23, en = 25, freqParam = 100) => {
pigpio.configureClock(1, pigpio.CLOCK_PCM)
motorUP = new Gpio(in1, {mode: Gpio.OUTPUT})
motorDOWN = new Gpio(in2, {mode: Gpio.OUTPUT})
PWM = new Gpio(en, {mode: Gpio.OUTPUT})
PWM.pwmRange(150)
frequency = freqParam
motorUP.digitalWrite(LOW)
motorDOWN.digitalWrite(LOW)
PWM.pwmFrequency(frequency)
PWM.pwmWrite(1)
}
const setFrequency = (freqParam) => {
frequency = freqParam
PWM.pwmFrequency(frequency)
}
const setRange = (rangeParam) => {
PWM.pwmRange(rangeParam)
}
const setSpeedSync = speed => {
PWM.pwmWrite(speed)
}
const runSync = direction => {
if (direction === 'back') {
motorUP.digitalWrite(LOW)
motorDOWN.digitalWrite(HIGH)
} else if (direction === 'fwd') {
motorUP.digitalWrite(HIGH)
motorDOWN.digitalWrite(LOW)
} else {
console.warn(`${direction} is not supported by runSync function`)
}
}
const runForward = () => {
runSync('fwd')
}
const runBackward = () => {
runSync('back')
}
const stopSync = () => {
motorUP.digitalWrite(LOW)
motorDOWN.digitalWrite(LOW)
}
const terminateGPIO = () => {
pigpio.terminate()
}
export {initMotor, setFrequency, setRange, setSpeedSync, runSync, runForward, runBackward, stopSync, terminateGPIO}