-
Notifications
You must be signed in to change notification settings - Fork 0
/
StepperMotors.js
123 lines (103 loc) · 2.83 KB
/
StepperMotors.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const Gpio = require('pigpio')
.Gpio;
/*
This class drives the 2 stepper motors.
Both share the same pulse pin.
Each one have their own direction pin
Doc : https://github.com/fivdi/pigpio/blob/master/doc/gpio.md
*/
class StepperMotors {
constructor(gpioPinNumberPulse, gpioPinNumberDirA, gpioPinNumberDirB, stepsDoneCB, stepsPerRevolution) {
this._gpioPulse = new Gpio(gpioPinNumberPulse, {
mode: Gpio.OUTPUT
});
this._gpioDirA = new Gpio(gpioPinNumberDirA, {
mode: Gpio.OUTPUT
});
this._gpioDirB = new Gpio(gpioPinNumberDirB, {
mode: Gpio.OUTPUT
});
/*
full step : 200 / revolution
1/16 step : 3200 / revolution
*/
this._stepsPerRevolution = stepsPerRevolution;
this._maxPeriod = 30000 / stepsPerRevolution; // 1 rev in 30s
this._minPeriod = 1000 / stepsPerRevolution; // 1 rev in 5s
// dela t between min and max period for 1 revolutions
// constant accel in 2 revolutions
this._deltaT = (this._maxPeriod - this._minPeriod) / stepsPerRevolution;
console.log("stepsPerRevolution : " + this._stepsPerRevolution);
console.log("_maxPeriod : " + this._maxPeriod);
console.log("_minPeriod : " + this._minPeriod);
console.log("_deltaT : " + this._deltaT);
this._stepToGo = 0;
this._moveTimeoutHandler = null;
this._stepPeriod = this._maxPeriod;
this._stepsDoneCB = stepsDoneCB;
this.stop();
}
forward(steps) {
this._gpioDirA = 1;
this._gpioDirB = 0;
this._start(steps);
}
backward(steps) {
this._gpioDirA = 0;
this._gpioDirB = 1;
this._start(steps);
}
left(steps) {
this._gpioDirA = 1;
this._gpioDirB = 1;
this._start(steps);
}
right(steps) {
this._gpioDirA = 0;
this._gpioDirB = 0;
this._start(steps);
}
_start(steps) {
if (steps <= 0) return;
this._stepToGo = steps;
this._moveAStep();
}
_moveAStep() {
if (this._stepToGo <= 0) {
this.stop();
this._stepsDoneCB();
return;
}
//console.log("stepToGo:" + this._stepToGo + " stepPeriod:" + this._stepPeriod);
// pulseLen (micro seconds) should be < pulse period => period > 100us
// trigger level to 1
this._gpioPulse.trigger(100, 1);
this._stepToGo--;
this._moveTimeoutHandler = setTimeout(() => {
this._moveAStep();
}, this._stepPeriod);
// compute period accell
// at 1 revolution from the end
if (this._stepToGo < this._stepsPerRevolution) {
// decrease speed at the end minus 2 revolutions
if (this._stepPeriod < this._maxPeriod) {
this._stepPeriod += this._deltaT;
}
} else {
// increase speed until max
if (this._stepPeriod > this._minPeriod) {
this._stepPeriod -= this._deltaT;
}
}
}
stop() {
if (this._moveTimeoutHandler != null) {
clearTimeout(this._moveTimeoutHandler);
this._moveTimeoutHandler = null;
}
this._gpioPulse.digitalWrite(0);
this._stepToGo = 0;
this._stepPeriod = this._maxPeriod;
}
}
module.exports = StepperMotors;