-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_types.cpp
86 lines (79 loc) · 1.83 KB
/
data_types.cpp
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
#include "Arduino.h"
#include "data_types.h"
void serialPrintLocation(char* prefix, Location l) {
Serial.print(prefix);
Serial.print("(");
Serial.print(l.x);
Serial.print(",");
Serial.print(l.y);
Serial.print(")");
}
void serialPrintlnLocation(char* prefix, Location l) {
serialPrintLocation(prefix, l);
Serial.println();
}
void serialPrintWaypoint(char* prefix, int index, Waypoint w) {
Serial.print(prefix);
Serial.print(index);
Serial.print(" -- (");
Serial.print(w.x);
Serial.print(",");
Serial.print(w.y);
Serial.print(")");
}
void serialPrintlnWaypoint(char* prefix, int index, Waypoint w) {
serialPrintWaypoint(prefix, index, w);
Serial.println();
}
void serialPrintPosition(char* prefix, Position p) {
Serial.print(prefix);
Serial.print("(");
Serial.print(p.x);
Serial.print(",");
Serial.print(p.y);
Serial.print("):");
Serial.print(p.r * 180.0 / PI);
}
void serialPrintlnPosition(char* prefix, Position p) {
serialPrintPosition(prefix, p);
Serial.println();
}
void serialPrintVector(char* prefix, Vector v) {
Serial.print(prefix);
Serial.print(v.d);
Serial.print("@");
Serial.print(v.r * 180.0 / PI);
}
void serialPrintlnVector(char* prefix, Vector v) {
serialPrintVector(prefix, v);
Serial.println();
}
void blink(int pin, int count, int onMs, int offMs) {
int i = 0;
while (i < count) {
digitalWrite(pin, HIGH);
delay(onMs);
digitalWrite(pin, LOW);
i++;
if (i < count) {
delay(offMs);
}
}
}
Vector getVector(float x0, float y0, float x1, float y1) {
float deltaY = y1 - y0;
float deltaX = x1 - x0;
Vector v;
v.d = sqrt(pow(deltaX, 2) + pow(deltaY, 2));
v.r = normalizeRadians(atan2(deltaY, deltaX));
return v;
}
float normalizeRadians(float r) {
while (r > PI) {
r -= 2 * PI;
}
while (r < -PI) {
r += 2 * PI;
}
return r;
}