This repository has been archived by the owner on Dec 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantenna_tracker.ino
187 lines (159 loc) · 5.08 KB
/
antenna_tracker.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
/**
* File: antenna_tracker.sketch
* Purpose: Arduino C program for tracking a device.
* Author: Abdul. D
* Date: 2016-10-20
*
* TODO: optimize to use as little memory as possible
* Changes:
* - v1.1 - added home bearing support, inverted the pitch, adding a command line based processing system; verbosity; compartmentalized some tasks.
* - v1.0 - initial release
*/
#include <Servo.h>
#include <Math.h>
#define PIN_GPS 1
#define PIN_SERVO_YAW 10
#define PIN_SERVO_PITCH 11
#define SERVO_PWM_MIN 1000
#define SERVO_PWM_MAX 2000
#define GPS_LAT 43.659334
#define GPS_LONG -79.400632
#define EARTH_RADIUS_METERS 6367990
#define VERBOSE
// dif in radius of earth at 43.6 to 43.7 (11km) is 6368.012 - 6367.974km is 38meters
// so have a setup function to calculate radius at given lat and long
// 43.659962 -79.400639
#define EARTH_RADIUS 6378137
Servo servoYaw; // Servo yaw
Servo servoPitch; // Servo pitch
// default GPS data
float gpsLat = GPS_LAT * 71 / 4068;
float gpsLong = GPS_LONG * 71 / 4068;
float homeBearing = 45;
struct Point
{
float x, y, z = 0; // lat, long, height
float bearing = 0; // considered over a sphere
float distance = 0; // considered over a sphere
};
void printDecimalPoint(float &toPrint, const char *msg);
void computePoint(Point &input, Point &output);
void printInput(float &toPrint);
void handleCoords();
void updateServos();
Point latLong; // IN DEGREES
Point quadPt;
String command;
void loop()
{
while (Serial && Serial.available() > 0)
{
command = Serial.readStringUntil(' ');
#ifdef VERBOSE
Serial.print("Processing Command: ");
Serial.println(command);
#endif
if (command == "coords")
{
handleCoords();
updateServos();
}
else if (command == "homebearing")
{
homeBearing = Serial.parseFloat();
#ifdef VERBOSE
Serial.println("----- UPDATING HOME BEARING -----");
printDecimalPoint(homeBearing, "New home bearing: ");
;
Serial.println("--------------------------------");
#endif
updateServos();
}
#ifdef VERBOSE
else
{
Serial.println("Unsupported operation.");
}
#endif
}
}
void handleCoords()
{
// read coords from serial
latLong.x = Serial.parseFloat(); // lat
latLong.y = Serial.parseFloat(); // lon
latLong.z = Serial.parseFloat(); // height ABOVE the ground
#ifdef VERBOSE
Serial.println("----- HANDLING COORDINATES -----");
printDecimalPoint(latLong.x, "Latitude: ");
printDecimalPoint(latLong.y, "Longitude: ");
printDecimalPoint(latLong.z, "Height: ");
Serial.println("--------------------------------");
#endif
// Radians conversion
latLong.x = latLong.x * 71 / 4068;
latLong.y = latLong.y * 71 / 4068;
// compute x y given lat long
computePoint(latLong, quadPt);
}
void updateServos()
{
// calculate pitch and yaw
float pitch = atan2(latLong.z, quadPt.distance) * (4068 / 71);
float yaw = quadPt.bearing;
#ifdef VERBOSE
printDecimalPoint(quadPt.distance, "Quadcopter distance: ");
printDecimalPoint(quadPt.bearing, "Bearing from geographic north: ");
printDecimalPoint(pitch, "Pitch (deg) from ground: ");
#endif
// move servos
// centered at homebearing, we go 90 less and 90 more, where home bearing is CW from geographic north
// so 90 less is 180 (all the way to the left) and 90 more is 0 (all the way to the right).
servoYaw.write(map(quadPt.bearing, homeBearing - 90, homeBearing + 90, 180, 0));
servoPitch.write(pitch);
}
/*
Point &input - the latitude and logitude that the quadcopter is at
return: mutates output's x and y to the cartesian coordinate x,y
*/
void computePoint(Point &input, Point &output)
{
// using equirectangular approximation because of arduino's limited precision
// works for small distances; max of 3/6371 is what I call small.
output.x = (input.y - gpsLong) * cos((input.x + gpsLat) / 2);
output.y = (input.x - gpsLat);
output.distance = sqrt((output.x * output.x) + (output.y * output.y)) * EARTH_RADIUS_METERS;
output.bearing = atan2(sin(input.y - gpsLong) * cos(input.x),
cos(gpsLat) * sin(input.x) -
sin(gpsLat) * cos(input.x) * cos(input.y - gpsLong));
output.bearing = (output.bearing * 4068) / 71; // to degrees
}
void setup()
{
// Servo setup
servoYaw.attach(PIN_SERVO_YAW);
servoYaw.write(0);
servoPitch.attach(PIN_SERVO_PITCH);
servoPitch.write(0);
delay(2000);
// GPS setup
// Optional functionality, for now we omitted; assume constant GPS
// Coordinates in #define header
//
Serial.begin(9600); // baud rate
Serial.print("Current coordinates: ");
printDecimalPoint(gpsLat, "(");
printDecimalPoint(gpsLong, ")");
Serial.println("Ready");
}
void printDecimalPoint(float &toPrint, const char *msg)
{
static char outstr[9];
Serial.print(msg);
dtostrf(toPrint, 9, 6, outstr);
Serial.println(outstr);
}
void printInput(float &toPrint)
{
printDecimalPoint(toPrint, "Recieved: ");
}