-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (43 loc) · 963 Bytes
/
index.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
const wpi = require("wiring-pi")
const maxSpeed = 480
const ioInitialized = false
function ioInit() {
if(ioInitialized) return
wpi.wiringPiSetupGpio()
wpi.pinMode(12, wpi.GPIO.PWM_OUTPUT)
wpi.pinMode(13, wpi.GPIO.PWM_OUTPUT)
wpi.pwmSetMode(wpi.GPIO.PWM_MODE_MS)
wpi.pwmSetRange(maxSpeed)
wpi.pwmSetClock(2)
wpi.pinMode(5, wpi.GPIO.OUTPUT)
wpi.pinMode(6, wpi.GPIO.OUTPUT)
ioInitialized = true
}
function Motor(pwmPin, dirPin) {
this.pwmPin = pwmPin
this.dirPin = dirPin
}
Motor.prototype.setSpeed(speed) {
var dirValue
if(speed < 0) {
speed = -speed
dirValue = 1
} else {
dirValue = 0
}
ioInit()
wpi.digitalWrite(this.dirPin, dirValue)
wpi.pwmWrite(this.pwmPin, speed)
}
function Motors() {
this.motor1 = new Motor(12, 5)
this.motor2 = new Motor(13, 6)
}
Motors.prototype.setSpeeds(m1Speed, m2Speed) {
this.motor1.setSpeed(m1Speed)
this.motor2.setSpeed(m2Speed)
}
module.exports = {
motors: Motors(),
maxSpeed: maxSpeed
}