-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpan-tilt-hat.js
342 lines (276 loc) · 11.2 KB
/
pan-tilt-hat.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
// Node Wrapper for Pimoroni Pan-Tilt HAT and Waveshare Pan-Tilt HAT
// Copyright (c) 2017, 2018, 2020 Roger Hardiman
//
// The two Pan-Tilt HAT boards have some similarities and some differences
// Both use standard servos for the Pan and Tilt positions and have 3 pin servo headers on the board
//
// Pimoroni use a PIC16F1503 microcontroller chip on the i2c bus to generate PWM signals for the servos
// Pimoroni has a 3rd PWM output that can be connected to PWM controlled LEDs and Lights
// Pimoroni also use the HAT ID EEPROM so this board can be auto-detected
//
// Waveshare use a standard PCA9685 LED/PWM chip on the i2c bus with PWM channel 0 and PWM channel 1 used for the seros
// Waveshare also has a Light Sensor on the i2c bus
//
//
//
// INITIALISAION
// The library autodetects Pimoroni by checking i2c address 0x15 and Waveshare/PCA9685 by checking i2c address 0x40
// ABSOLUTE POSITIONING
// Pan=0, Tilt=0 has the camera looking forword.
// Pan of +90 makes the Pi Camera look to the left
// Pan of -90 makes the Pi Camera look to the right
// Tilt of -80 makes the Pi Cammera look up
// Tilt of +80 makes the Pi Cammera look down
// (The positive and negative directions are the same as the Pimoroni Python driver)
// pan(angle), // angle between -90 and 90
// servo_one(angle) // angle between -90 and 90
// tilt(angle) // angle between -80 and 80
// servo_two(angle) // angle between -80 and 80
//
//
// CONTINUOUS MOVE API (START AND STOP COMMANDS)
// pan_left(speed) // start moving with a speed from 0 to 15. 0 means stop
// pan_right(speed) // start moving with a speed from 0 to 15. 0 means stop
// tilt_up(speed) // start moving with a speed from 0 to 15. 0 means stop
// tilt_down(speed) // start moving with a speed from 0 to 15. 0 means stop
// stop() // stop the pan and the tilt
//
// SHUTDOWN
// close() // closes the class and frees resources
//
// SERVO SIGNAL
// After 2 seconds the servo PWM drive signal is turned off.
var i2c_bus = require("i2c-bus");
const PIMORONI_I2C_ADDRESS = 0x15;
const PIMORONI_CONFIG_REGISTER = 0x00;
const PIMORONI_PAN_SERVO_REGISTER = 0x01;
const PIMORONI_TILT_SERVO_REGISTER = 0x03;
const SERVO_MIN_PWM = 575;
const SERVO_MAX_PWM = 2325;
const WAVESHARE_I2C_ADDRESS = 0x40; // can be changed with resisters on the PCB
const __LED0_ON_L = 0x06;
const __LED0_ON_H = 0x07;
const __LED0_OFF_L = 0x08;
const __LED0_OFF_H = 0x09;
class PanTiltHAT {
constructor() {
this.i2c = i2c_bus.openSync(1);
// Detect make of pan-tilt HAT by reading i2c registers
this.manufacturer = "unknown";
try {
// Try and read from the Pimoroni i2c addresses. If it fails we get an exception from the i2c library.
var pan_pwm = this.i2c.readWordSync(PIMORONI_I2C_ADDRESS, PIMORONI_PAN_SERVO_REGISTER);
var tilt_pwm = this.i2c.readWordSync(PIMORONI_I2C_ADDRESS, PIMORONI_TILT_SERVO_REGISTER);
this.manufacturer = "pimoroni"
} catch (error) {
this.manufacturer = "unknown";
}
if (this.manufacturer === "unknown") {
try {
const REG_MODE1 = 0x00;
const REG_MODE2 = 0x01;
const REG_PRESCALE = 0xFE;
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, REG_MODE1, 0x00);
let prescaleval = 25000000; // 25 MHz
prescaleval /= 4096; // 12 bit
prescaleval /= 50; // 50 Hz
prescaleval -= 1.0;
prescaleval = Math.floor(prescaleval + 0.5);
let oldmode = this.i2c.readByteSync(WAVESHARE_I2C_ADDRESS, REG_MODE1);
let newmode = (oldmode & 0x7F) | 0x10; // Activate sleep
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, REG_MODE1, newmode);
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, REG_PRESCALE, prescaleval);
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, REG_MODE1, oldmode);
//DEV_Delay_ms(200);
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, REG_MODE1, oldmode | 0x80);
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, REG_MODE2, 0x04);
this.manufacturer = "waveshare";
} catch (error) {
console.log("Error initialising waveshare " + error);
this.manufacturer = "unknown";
}
}
// current position in degrees and current speed (used for continuous move API)
this.pan_position = 0;
this.pan_speed = 0;
this.pan_servo_enabled = false;
this.tilt_position = 0;
this.tilt_speed = 0;
this.tilt_servo_enabled = false;
// Timers, used for Continuous Move API and to disable the servos
this.timer = setInterval(this.continuous_move_callback.bind(this), 100); // 100ms
this.disablePanTimer = null;
this.disableTiltTimer = null;
if (this.manufacturer === "pimoroni") this.pimoroni_update_servo_config();
if (this.manufacturer === "waveshare") this.waveshare_update_servo_config();
console.log("Detected " + this.manufacturer + " pan-tilt HAT");
}
// Move Servo One to 'angle'
servo_one(angle) {
if (this.angle > 90) this.angle = 90;
if (this.angle < -90) this.angle = -90;
// Convert angle to a Servo PWM value
let pan_pwm = this.AngleToPWM(angle);
// Clear servo timeout, enable the servo and set a timer to disable it after 2 seconds
clearTimeout(this.disablePanTimer);
this.pan_servo_enabled = true;
// update position
this.pan_position = angle;
if (this.manufacturer === "pimoroni") {
this.pimoroni_update_servo_config();
this.pimoroni_send_pan_pwm(pan_pwm);
}
if (this.manufacturer === "waveshare") {
this.waveshare_update_servo_config();
this.waveshare_send_pan_pwm(pan_pwm);
}
this.disablePanTimer = setTimeout(function () {
// turn off the PWM output
this.pan_servo_enabled = false;
if (this.manufacturer === "pimoroni") this.pimoroni_update_servo_config();
if (this.manufacturer === "waveshare") this.waveshare_update_servo_config();
}.bind(this), 2000);
}
// Move Servo Two to 'angle'
servo_two(angle) {
if (this.angle > 80) this.angle = 80;
if (this.angle < -80) this.angle = -80;
// Convert angle to a Servo PWM value
let tilt_pwm = this.AngleToPWM(angle);
// Clear servo timeout
clearTimeout(this.disableTiltTimer);
this.tilt_servo_enabled = true;
// update position
this.tilt_position = angle;
if (this.manufacturer === "pimoroni") {
this.pimoroni_update_servo_config();
this.pimoroni_send_tilt_pwm(tilt_pwm);
}
if (this.manufacturer === "waveshare") {
this.waveshare_update_servo_config();
this.waveshare_send_tilt_pwm(tilt_pwm);
}
this.disableTiltTimer = setTimeout(function () {
// turn off the PWM output
this.tilt_servo_enabled = false;
if (this.manufacturer === "pimoroni") this.pimoroni_update_servo_config();
if (this.manufacturer === "waveshare") this.waveshare_update_servo_config();
}.bind(this), 2000);
}
// Alias for Servo One and Servo Two
pan(angle) { this.servo_one(angle); };
tilt(angle) { this.servo_two(angle); };
goto_home() { this.pan(0); this.tilt(0); };
// Convert the angle into a PWM value
AngleToPWM(angle) {
return Math.round(SERVO_MIN_PWM + ((SERVO_MAX_PWM - SERVO_MIN_PWM) / 180) * (angle + 90));
}
// Every time the Continouous Move API callback is fired, we calculate the new angle for the servos
// using the current angle and current speed
continuous_move_callback() {
if (this.pan_speed === 0 && this.tilt_speed === 0) return;
let new_pan_position = this.pan_position + (this.pan_speed / 10);
let new_tilt_position = this.tilt_position + (this.tilt_speed / 10);
// range check
if (new_pan_position > 90) {
this.pan_speed = 0;
new_pan_position = 90;
}
if (new_pan_position < -90) {
new_pan_position = -90;
this.pan_speed = 0;
}
if (new_tilt_position > 80) {
new_tilt_position = 80;
this.tilt_speed = 0;
}
if (new_tilt_position < -80) {
new_tilt_position = -80;
this.tilt_speed = 0;
}
if (new_pan_position != this.pan_position) {
this.pan(new_pan_position);
}
if (new_tilt_position != this.tilt_position) {
this.tilt(new_tilt_position);
}
}
pan_left(speed) {
if (speed > 15) speed = 15;
if (speed < 0) speed = 0;
this.pan_speed = speed;
}
pan_right(speed) {
if (speed > 15) speed = 15;
if (speed < 0) speed = 0;
this.pan_speed = -speed;
}
tilt_up(speed) {
if (speed > 15) speed = 15;
if (speed < 0) speed = 0;
this.tilt_speed = -speed;
}
tilt_down(speed) {
if (speed > 15) speed = 15;
if (speed < 0) speed = 0;
this.tilt_speed = speed;
}
stop() {
this.pan_speed = 0;
this.tilt_speed = 0;
}
close() {
this.stop();
clearTimeout(this.timer);
clearTimeout(this.disablePanTimer);
clearTimeout(this.disableTiltTimer);
this.timer = null;
this.disablePanTimer = null;
this.disableTiltTimer = null;
}
//////////////////////////////////////////////////
//
// Hardware Specific Functions
//
//////////////////////////////////////////////////
pimoroni_update_servo_config() {
//console.log("pimoroni update servo config pan=" + this.pan_servo_enabled + " tilt=" + this.tilt_servo_enabled);
let config_byte = 0;
if (this.pan_servo_enabled) config_byte = config_byte | 0x01;
if (this.tilt_servo_enabled) config_byte = config_byte | 0x02;
this.i2c.writeByteSync(PIMORONI_I2C_ADDRESS, PIMORONI_CONFIG_REGISTER, config_byte);
}
pimoroni_send_pan_pwm(pwm) {
//console.log("pimoroni set pan pwm to " + pwm);
this.i2c.writeWordSync(PIMORONI_I2C_ADDRESS, PIMORONI_PAN_SERVO_REGISTER, pwm);
}
pimoroni_send_tilt_pwm(pwm) {
//console.log("pimoroni set tilt pwm to " + pwm);
this.i2c.writeWordSync(PIMORONI_I2C_ADDRESS, PIMORONI_TILT_SERVO_REGISTER, pwm);
}
waveshare_update_servo_config() {
//console.log("waveshare update servo config pan=" + this.pan_servo_enabled + " tilt=" + this.tilt_servo_enabled);
}
waveshare_send_pan_pwm(pwm) {
//console.log("waveshare set pan pwm to " + pwm);
let on = 0;
let off = pwm * 4096 / 20000; // PWM frequency is 50HZ,the period is 20000us, the resolution 12 bit
let channel = 1;
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, __LED0_ON_L + 4 * channel, on & 0xFF);
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, __LED0_ON_H + 4 * channel, on >> 8);
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, __LED0_OFF_L + 4 * channel, off & 0xFF)
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, __LED0_OFF_H + 4 * channel, off >> 8)
}
waveshare_send_tilt_pwm(pwm) {
//console.log("waveshare set tilt pwm to " + pwm);
let on = 0;
let off = pwm * 4096 / 20000; // PWM frequency is 50HZ,the period is 20000us, the resolution 12 bit
let channel = 0;
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, __LED0_ON_L + 4 * channel, on & 0xFF);
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, __LED0_ON_H + 4 * channel, on >> 8);
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, __LED0_OFF_L + 4 * channel, off & 0xFF)
this.i2c.writeByteSync(WAVESHARE_I2C_ADDRESS, __LED0_OFF_H + 4 * channel, off >> 8)
}
}
module.exports = PanTiltHAT;