├── LICENSE ├── ObjectTrackingCamera.ino └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2020, ANM-P4F 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /ObjectTrackingCamera.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define PACKAGE_LENGTH 20 5 | #define DEBUG 6 | 7 | // Declare the Servo pin 8 | int servoPinHorizon = 3; 9 | int servoPinVertical = 5; 10 | // Create a servo object 11 | ServoTimer2 servoHorizon ; 12 | ServoTimer2 servoVertical; 13 | int DELTA_ANGLE = 1; 14 | int INIT_ANGLE = 45; 15 | 16 | // AltSoftSerial always uses these pins: 17 | // 18 | // Board Transmit Receive PWM Unusable 19 | // ----- -------- ------- ------------ 20 | // Arduino Uno 9 8 10 21 | 22 | // Arduino Uno Pin 9 <==> Bluetooth 4.0 UART CC2541 HM-10 RX 23 | // Arduino Uno Pin 8 <==> Bluetooth 4.0 UART CC2541 HM-10 TX 24 | // Arduino Uno Pin VCC <==> Bluetooth 4.0 UART CC2541 HM-10 VCC 25 | // Arduino Uno Pin GND <==> Bluetooth 4.0 UART CC2541 HM-10 GND 26 | 27 | AltSoftSerial hmSerial; 28 | int en_pin = 10; 29 | char buf[PACKAGE_LENGTH]; 30 | bool receive = false; 31 | int ledState = LOW; 32 | bool tracking = false; 33 | int objCenterX; 34 | int camW; 35 | int objCenterY; 36 | int camH; 37 | int camCenterX; 38 | int camCenterY; 39 | 40 | void setup() { 41 | // put your setup code here, to run once: 42 | hmSerial.begin(19200); 43 | Serial.begin(9600); 44 | pinMode(LED_BUILTIN, OUTPUT); 45 | 46 | for(int i=0 ; i < 20; i++){ 47 | buf[i] = '\0'; 48 | } 49 | 50 | servoHorizon.attach(servoPinHorizon); 51 | servoVertical.attach(servoPinVertical); 52 | servoHorizon.write(angle2Value(INIT_ANGLE)); 53 | servoVertical.write(angle2Value(INIT_ANGLE)); 54 | } 55 | 56 | void loop() { 57 | if (Serial.available()) { 58 | String data = Serial.readString(); 59 | sendCommand(data); 60 | } 61 | readSerialHM(); 62 | controlServos(); 63 | delay(10); 64 | } 65 | 66 | int angle2Value(int angle){ 67 | return map(angle, 0, 90, 750, 1500); 68 | } 69 | 70 | void readSerialHM(){ 71 | String reply; 72 | String strX; 73 | String strW; 74 | String strY; 75 | String strH; 76 | while (hmSerial.available()) { 77 | reply = hmSerial.readStringUntil('\n'); 78 | strX = getValue(reply,',',0); 79 | strW = getValue(reply,',',1); 80 | strY = getValue(reply,',',2); 81 | strH = getValue(reply,',',3); 82 | objCenterX = strX.toInt(); 83 | camW = strW.toInt(); 84 | objCenterY = strY.toInt(); 85 | camH = strH.toInt(); 86 | camCenterX = camW/2; 87 | camCenterY = camH/2; 88 | tracking = true; 89 | } 90 | 91 | #ifdef DEBUG 92 | if(reply.length() > 0){ 93 | if(ledState==LOW){ 94 | digitalWrite(LED_BUILTIN, HIGH); 95 | ledState = HIGH; 96 | }else{ 97 | digitalWrite(LED_BUILTIN, LOW); 98 | ledState = LOW; 99 | } 100 | receive = true; 101 | Serial.println(reply); 102 | // if(strX.equals("")==false) 103 | // Serial.println(objCenterX); 104 | // if(strW.equals("")==false) 105 | // Serial.println(camW); 106 | // if(strY.equals("")==false) 107 | // Serial.println(objCenterY); 108 | // if(strH.equals("")==false) 109 | // Serial.println(camH); 110 | } 111 | #endif 112 | } 113 | 114 | void controlServos(){ 115 | if(tracking==true){ 116 | tracking=false; 117 | int angleHorizon = INIT_ANGLE + float((camCenterX-objCenterX)*DELTA_ANGLE)*0.03f; 118 | if(angleHorizon>90){ 119 | angleHorizon = 90; 120 | }else if(angleHorizon<0){ 121 | angleHorizon = 0; 122 | } 123 | servoHorizon.write(angle2Value(angleHorizon)); 124 | 125 | int angleVertical = INIT_ANGLE + float((camCenterY-objCenterY)*DELTA_ANGLE)*0.03f; 126 | if(angleVertical>90){ 127 | angleVertical = 90; 128 | }else if(angleVertical<0){ 129 | angleVertical = 0; 130 | } 131 | servoVertical.write(angle2Value(angleVertical)); 132 | 133 | // Serial.print(angleHorizon); 134 | // Serial.print('\t'); 135 | // Serial.println(angleVertical); 136 | } 137 | } 138 | 139 | void sendCommand(String command){ 140 | if (command.length() > PACKAGE_LENGTH){ 141 | return; 142 | } 143 | // Serial.println("sendcommand"); 144 | // Serial.println(command); 145 | command.toCharArray(buf,command.length()); 146 | buf[command.length()]='\r'; 147 | buf[command.length()+1]='\n'; 148 | hmSerial.println(buf); 149 | receive = false; 150 | for(int i=0 ; i < 20; i++){ 151 | buf[i] = '\0'; 152 | } 153 | } 154 | 155 | String getValue(String data, char separator, int index) 156 | { 157 | int found = 0; 158 | int strIndex[] = { 0, -1 }; 159 | int maxIndex = data.length() - 1; 160 | 161 | for (int i = 0; i <= maxIndex && found <= index; i++) { 162 | if (data.charAt(i) == separator || i == maxIndex) { 163 | found++; 164 | strIndex[0] = strIndex[1] + 1; 165 | strIndex[1] = (i == maxIndex) ? i+1 : i; 166 | } 167 | } 168 | return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; 169 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Object Tracking Camera (Arduino part) 2 | 3 | This sw control servo motors to make the android camera track the target object. 4 | [![Simple Object Tracking Camera DIY 5 | ](http://img.youtube.com/vi/J4Bvrvfg920/0.jpg)](https://www.youtube.com/watch?v=J4Bvrvfg920 "Simple Object Tracking Camera DIY") 6 | 7 | ### Dependencies: 8 | 1.Download and install library: https://github.com/nabontra/ServoTimer2 via Sketch/Include library/Add .Zip library ...
9 | 2.Install AltSoftSerial via Libray Manager
10 | 11 | ### License 12 | 13 | BSD 2-Clause "Simplified" License 14 | 15 | --------------------------------------------------------------------------------