-
Notifications
You must be signed in to change notification settings - Fork 1
/
PTZ_Control.ino
424 lines (361 loc) · 11.8 KB
/
PTZ_Control.ino
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <BMDSDIControl.h>
#include <include/BMDSDIControlShieldRegisters.h>
#include <Streaming.h>
const int shieldAddress = 0x6E;
BMD_SDICameraControl_I2C sdiCameraControl(shieldAddress);
/* Constants */
const byte kBufferSize = kRegICDATA_Width;
const int kHeaderLen = 4;
const int kAlignment = 4;
/* Define pin mappings */
const int pinLeft = 3;
const int pinRight = 4;
const int pinUp = 5;
const int pinDown = 6;
const int pinUDRate = 10; // use this pin for tilt speed or single-speed controller
const int pinLRRate = 11; // use this pin for pan speed if using dual-speed controller, otherwise ignored
const int rateOverride = 12; // use this pin if constant max speed required for single-speed controller
const int joyX(A0); // pins for joystick analogue inputs
const int joyY(A1);
const int joyPress(2); // pin for joystick button press - must be interrupt compatible
/* joystick setup */
const int anaMax = 1023; // max analogue value output by joystick (depending on 8 or 10 bit analogue sampling)
const int anaScalefactor = (anaMax + 1) / 256;
unsigned long modeChangetime;
const int deBouncetime = 500; // debounce interval for interrupt trigger
int udRate;
int lrRate;
int joyXdefault;
int joyYdefault;
int joyPressed;
volatile boolean mode(true); // default mode is joystick control; change to False for default ATEM control
void setup() {
Serial.begin(9600);
Serial << "Starting camera control...\n";
sdiCameraControl.begin();
Wire.setClock(400000);
sdiCameraControl.setOverride(false);
pinMode(pinLeft, OUTPUT);
pinMode(pinRight, OUTPUT);
pinMode(pinUp, OUTPUT);
pinMode(pinDown, OUTPUT);
pinMode(pinUDRate, OUTPUT);
pinMode(pinLRRate, OUTPUT);
pinMode(rateOverride, OUTPUT);
digitalWrite(pinLeft, LOW);
digitalWrite(pinRight, LOW);
digitalWrite(pinUp, LOW);
digitalWrite(pinDown, LOW);
analogWrite(pinUDRate, LOW);
analogWrite(pinLRRate, LOW);
analogWrite(rateOverride, HIGH);
joyXdefault = analogRead(joyX); // finds the default x/y value for joystick without movement - usually not the exact midpoint. Assumes joystick not disturbed when unit powered on. If it is, reset the unit.
joyYdefault = analogRead(joyY);
Serial.print("X def: ");
Serial.println(joyXdefault);
Serial.print("Y def: ");
Serial.println(joyYdefault);
pinMode(joyPress, INPUT_PULLUP); // these lines configure the interrupt for joystick button press to change modes
attachInterrupt(digitalPinToInterrupt(joyPress), changeMode, LOW);
Serial << "Setup Done\n\n";
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
switch (mode) {
case false:
atemControl();
break;
case true:
joyControl();
break;
}
}
void changeMode() {
// interrupt triggered - change mode
if (millis() >= modeChangetime + deBouncetime) {
Serial.print("Changing mode to ");
mode = !mode;
digitalWrite(pinLeft, LOW);
digitalWrite(pinRight, LOW);
digitalWrite(pinUp, LOW);
digitalWrite(pinDown, LOW);
analogWrite(pinUDRate, LOW);
analogWrite(pinLRRate, LOW);
switch (mode) {
case false:
Serial.println("ATEM Mode");
break;
case true:
Serial.println("Joystick Control Mode");
break;
}
modeChangetime = millis();
}
}
void atemControl() {
static unsigned long aliveTimerVal = 0;
static int lineNumber = 0;
byte rxData[kBufferSize];
byte* command = nullptr;
int dataLen;
int ptzID = 11;
int upDown = 0;
int leftRight = 0;
int udRate = 0;
int lrRate = 0;
// Read the data from the device. Add 1 to compensate for 3G-SDI Shield Read issue.
dataLen = sdiCameraControl.read(rxData, kBufferSize) + 1;
//initialise the first command to the start of the rxData payload
command = rxData;
if (dataLen >= 4)
{
/* -------------- For debugging -------------- */
// printDataDec(rxData, dataLen); // Print entire payload in decimal format
// printDataHex(rxData, dataLen); // Print entire payload in hex format
// Serial << "---- Received " << dataLen << " bytes of data. \n";
// loop through each command in the packet received
while (command < rxData + dataLen)
{
static int prevDestination = 0;
int destination = command[0];
int length = command[1];
int cmdID = command[2];
int reserved = command[3];
int category = command[4];
int param = command[5];
int type = command[6];
byte op1 = command[7]; // apparently always 0
byte op2 = command[8]; // these two bytes are x value
byte op3 = command[9];
byte op4 = command[10]; // these two bytes are y value
byte op5 = command[11];
int16_t xInput = op2 + (int8_t) op3 * 256;
int16_t yInput = op4 + (int8_t) op5 * 256;
// print result to Serial
// Serial << "(" << lineNumber++ << ") \t";
// Serial << "ID [" << category << "." << param << "]: \t";
// printType(type);
//
// Serial << "len=" << length - 3 << "\t";
// Serial << "Data = ";
// printCmdData(command, length - kHeaderLen + 1);
// Determine how far the pointer needs to increment
int multiplier = length / kAlignment + 1;
if (length % kAlignment != 0)
length = kAlignment * multiplier; // Is the length already divisible by 4?
// Increment the command pointer to the position of the next command
command += length + kHeaderLen;
// Is the command PTZ?
if (category = ptzID) {
if (param == 0) { // case 11.0
Serial.print("x:");
Serial.print(xInput);
Serial.print(" y:");
Serial.println(yInput);
if (xInput > 0) { // right
lrRate = (abs (xInput) - 1) / 8; // reduce range from -2048+2048 to 0-255
analogWrite(pinLRRate, lrRate);
digitalWrite(pinLeft, LOW);
digitalWrite(pinRight, HIGH);
Serial.print("Left ");
Serial.println(lrRate);
} else if (xInput < 0) { // left
lrRate = (abs (xInput) - 1) / 8 ; // reduce range from -2048+2048 to 0-255
analogWrite(pinLRRate, lrRate);
digitalWrite(pinRight, LOW);
digitalWrite(pinLeft, HIGH);
Serial.print("Right ");
Serial.println(lrRate);
} else {
Serial.println("Centre");
digitalWrite(pinRight, LOW);
digitalWrite(pinLeft, LOW);
}
if (yInput < 0) { // up
udRate = (abs (yInput) - 1) / 8; // reduce range from -2048+2048 to 0-255
analogWrite(pinUDRate, udRate);
digitalWrite(pinDown, LOW);
digitalWrite(pinUp, HIGH);
Serial.print("Up ");
Serial.println(udRate);
} else if (yInput > 0) { // down
udRate = (abs (yInput) - 1) / 8; // reduce range from -2048+2048 to 0-255
analogWrite(pinUDRate, udRate);
digitalWrite(pinUp, LOW);
digitalWrite(pinDown, HIGH);
Serial.print("Down ");
Serial.println(udRate);
} else {
Serial.println("Middle");
analogWrite(pinUDRate, lrRate); // if tilt axis not in use, use speed controller for pan axis in single-speed mode
digitalWrite(pinUp, LOW);
digitalWrite(pinDown, LOW);
}
}
}
}
}
}
void joyControl() {
int xVal = 0;
int yVal = 0;
int xShifted;
int yShifted;
float xScalefactor;
float yScalefactor;
int xOutput = 0;
int yOutput = 0;
xVal = analogRead(joyX);
yVal = analogRead(joyY);
if (xVal < joyXdefault - 10) {
// Move left
xShifted = xVal - joyXdefault;
xScalefactor = float(anaMax) / float(joyXdefault);
xOutput = abs(xScalefactor * xShifted / anaScalefactor);
Serial.print("joyXdefault: ");
Serial.println(joyXdefault);
Serial.print("xVal: ");
Serial.println(xVal);
Serial.print("xShifted: ");
Serial.println(xShifted);
Serial.print("xScalefactor: ");
Serial.println(xScalefactor);
Serial.print("xOutput: ");
Serial.println(xOutput);
analogWrite(pinLRRate, xOutput); // pinLRrate when separate
digitalWrite(pinRight, LOW);
digitalWrite(pinLeft, HIGH);
} else if (xVal > joyXdefault + 10) {
// Move right
xShifted = xVal - joyXdefault;
xScalefactor = float(anaMax) / (float(anaMax) - float(joyXdefault));
xOutput = abs(xScalefactor * xShifted / anaScalefactor);
Serial.print("joyXdefault: ");
Serial.println(joyXdefault);
Serial.print("xVal: ");
Serial.println(xVal);
Serial.print("xShifted: ");
Serial.println(xShifted);
Serial.print("xScalefactor: ");
Serial.println(xScalefactor);
Serial.print("xOutput: ");
Serial.println(xOutput);
analogWrite(pinLRRate, xOutput); // pinLRRate when separate
digitalWrite(pinLeft, LOW);
digitalWrite(pinRight, HIGH);
} else {
analogWrite(pinLRRate, 0);
digitalWrite(pinLeft, LOW);
digitalWrite(pinRight, LOW);
}
if (yVal < joyYdefault - 15) {
// Move down
yShifted = yVal - joyYdefault;
yScalefactor = float(anaMax) / float(joyYdefault);
yOutput = abs(yScalefactor * yShifted / anaScalefactor);
Serial.print("joyYdefault: ");
Serial.println(joyYdefault);
Serial.print("yVal: ");
Serial.println(yVal);
Serial.print("yShifted: ");
Serial.println(yShifted);
Serial.print("yScalefactor: ");
Serial.println(yScalefactor);
Serial.print("yOutput: ");
Serial.println(yOutput);
analogWrite(pinUDRate, yOutput);
digitalWrite(pinUp, LOW);
digitalWrite(pinDown, HIGH);
} else if (yVal > joyYdefault + 15) {
// Move up
yShifted = yVal - joyYdefault;
yScalefactor = float(anaMax) / (float(anaMax) - float(joyYdefault));
yOutput = abs(yScalefactor * yShifted / anaScalefactor);
Serial.print("joyYdefault: ");
Serial.println(joyYdefault);
Serial.print("yVal: ");
Serial.println(yVal);
Serial.print("yShifted: ");
Serial.println(yShifted);
Serial.print("yScalefactor: ");
Serial.println(yScalefactor);
Serial.print("yOutput: ");
Serial.println(yOutput);
analogWrite(pinUDRate, yOutput); // pinLRRate when separate
digitalWrite(pinDown, LOW);
digitalWrite(pinUp, HIGH);
} else {
analogWrite(pinUDRate, xOutput); // if tilt axis not in use, use speed controller for pan axis in single-speed mode
digitalWrite(pinUp, LOW);
digitalWrite(pinDown, LOW);
}
}
/* Debugging code from here onwards */
// print the command data from the packet in hex
void printCmdData(const byte* data, const int len )
{
Serial << "[";
for (int i = 0; i < len; i++)
{
if (data[i + 7] < 0x10u)
Serial.print("0");
Serial.print(data[i + 7], HEX);
if (i != len - 1)
Serial.print(" ");
}
Serial << "]" << endl;
}
// print the type of the operation
void printType(const int type)
{
switch (type)
{
case 0:
Serial << "void\t";
break;
case 1:
Serial << "int8\t";
break;
case 2:
Serial << "int16\t";
break;
case 3:
Serial << "int32\t";
break;
case 4:
Serial << "int64\t";
break;
case 5:
Serial << "string\t";
break;
case 128:
Serial << "fixed16\t";
break;
default:
Serial << "Unknown Type\t";
}
}
// print byte array data as decimal
void printDataDec(const byte* data, const int len)
{
for (int i = 0; i < len; i++)
{
Serial << data[i];
if (i != len - 1)
Serial << " ";
}
Serial.print("\n");
}
// print byte array data as hexidecimal
void printDataHex(const byte* data, const int len)
{
for (int i = 0; i < len; i++)
{
if (data[i] < 0x10u)
Serial.print("0");
Serial.print(data[i], HEX);
if (i != len - 1)
Serial << " ";
}
Serial.print("\n");
}