-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathObjectTrackingCamera.ino
More file actions
169 lines (151 loc) · 4.02 KB
/
ObjectTrackingCamera.ino
File metadata and controls
169 lines (151 loc) · 4.02 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
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
#include <ServoTimer2.h>
#include <AltSoftSerial.h>
#define PACKAGE_LENGTH 20
#define DEBUG
// Declare the Servo pin
int servoPinHorizon = 3;
int servoPinVertical = 5;
// Create a servo object
ServoTimer2 servoHorizon ;
ServoTimer2 servoVertical;
int DELTA_ANGLE = 1;
int INIT_ANGLE = 45;
// AltSoftSerial always uses these pins:
//
// Board Transmit Receive PWM Unusable
// ----- -------- ------- ------------
// Arduino Uno 9 8 10
// Arduino Uno Pin 9 <==> Bluetooth 4.0 UART CC2541 HM-10 RX
// Arduino Uno Pin 8 <==> Bluetooth 4.0 UART CC2541 HM-10 TX
// Arduino Uno Pin VCC <==> Bluetooth 4.0 UART CC2541 HM-10 VCC
// Arduino Uno Pin GND <==> Bluetooth 4.0 UART CC2541 HM-10 GND
AltSoftSerial hmSerial;
int en_pin = 10;
char buf[PACKAGE_LENGTH];
bool receive = false;
int ledState = LOW;
bool tracking = false;
int objCenterX;
int camW;
int objCenterY;
int camH;
int camCenterX;
int camCenterY;
void setup() {
// put your setup code here, to run once:
hmSerial.begin(19200);
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
for(int i=0 ; i < 20; i++){
buf[i] = '\0';
}
servoHorizon.attach(servoPinHorizon);
servoVertical.attach(servoPinVertical);
servoHorizon.write(angle2Value(INIT_ANGLE));
servoVertical.write(angle2Value(INIT_ANGLE));
}
void loop() {
if (Serial.available()) {
String data = Serial.readString();
sendCommand(data);
}
readSerialHM();
controlServos();
delay(10);
}
int angle2Value(int angle){
return map(angle, 0, 90, 750, 1500);
}
void readSerialHM(){
String reply;
String strX;
String strW;
String strY;
String strH;
while (hmSerial.available()) {
reply = hmSerial.readStringUntil('\n');
strX = getValue(reply,',',0);
strW = getValue(reply,',',1);
strY = getValue(reply,',',2);
strH = getValue(reply,',',3);
objCenterX = strX.toInt();
camW = strW.toInt();
objCenterY = strY.toInt();
camH = strH.toInt();
camCenterX = camW/2;
camCenterY = camH/2;
tracking = true;
}
#ifdef DEBUG
if(reply.length() > 0){
if(ledState==LOW){
digitalWrite(LED_BUILTIN, HIGH);
ledState = HIGH;
}else{
digitalWrite(LED_BUILTIN, LOW);
ledState = LOW;
}
receive = true;
Serial.println(reply);
// if(strX.equals("")==false)
// Serial.println(objCenterX);
// if(strW.equals("")==false)
// Serial.println(camW);
// if(strY.equals("")==false)
// Serial.println(objCenterY);
// if(strH.equals("")==false)
// Serial.println(camH);
}
#endif
}
void controlServos(){
if(tracking==true){
tracking=false;
int angleHorizon = INIT_ANGLE + float((camCenterX-objCenterX)*DELTA_ANGLE)*0.03f;
if(angleHorizon>90){
angleHorizon = 90;
}else if(angleHorizon<0){
angleHorizon = 0;
}
servoHorizon.write(angle2Value(angleHorizon));
int angleVertical = INIT_ANGLE + float((camCenterY-objCenterY)*DELTA_ANGLE)*0.03f;
if(angleVertical>90){
angleVertical = 90;
}else if(angleVertical<0){
angleVertical = 0;
}
servoVertical.write(angle2Value(angleVertical));
// Serial.print(angleHorizon);
// Serial.print('\t');
// Serial.println(angleVertical);
}
}
void sendCommand(String command){
if (command.length() > PACKAGE_LENGTH){
return;
}
// Serial.println("sendcommand");
// Serial.println(command);
command.toCharArray(buf,command.length());
buf[command.length()]='\r';
buf[command.length()+1]='\n';
hmSerial.println(buf);
receive = false;
for(int i=0 ; i < 20; i++){
buf[i] = '\0';
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}