forked from AeroQuad/AeroQuad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Template.h
161 lines (141 loc) · 6.05 KB
/
Template.h
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
/*
AeroQuad v2.3 - March 2011
www.AeroQuad.com
Copyright (c) 2011 Ted Carancho. All rights reserved.
An Open Source Arduino based multicopter.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// User this as a template for new classes or subclasses
// ***********************************************************************
// ************************** Example Class ******************************
// ***********************************************************************
class exampleClass {
public:
int exampleVariable;
float exampleData[3];
exampleClass(void) {
// this is the constructor of the object and must have the same name
// can be used to initialize any of the variables declared above
}
// **********************************************************************
// The following function calls must be defined inside any new subclasses
// **********************************************************************
virtual void initialize(void);
virtual void exampleFunction(int);
virtual const int getExampleData(byte);
// *********************************************************
// The following functions are common between all subclasses
// *********************************************************
void examplePublicFunction(byte axis, int value) {
// insert common code here
}
const int getPublicData(byte axis) {
return exampleData[axis];
}
};
// ***********************************************************************
// ************************ Example Subclass *****************************
// ***********************************************************************
class exampleSubClass : public exampleClass {
private:
int exampleArray[3]; // only for use inside this subclass
int examplePrivateData; // only for use inside this subclass
void examplePrivateFunction(int functionVariable) {
// it's possible to declare functions just for this subclass
}
public:
exampleSubClass() : exampleClass(){
// this is the constructor of the object and must have the same name
// can be used to initialize any of the variables declared above
}
// ***********************************************************
// Define all the virtual functions declared in the main class
// ***********************************************************
void initialize(void) {
// insert code here
}
void exampleFunction(int someVariable) {
// insert code here
examplePrivateFunction(someVariable);
}
const int getExampleData(byte axis) {
// insert code here
return exampleArray[axis];
}
};
// Example implementation of a class and subclass for a magnetometer
// ***********************************************************************
// ************************** Compass Class ******************************
// ***********************************************************************
class CompassExample {
private: // not found in the example above, but it's possible to declare private variables only seen in the main class
float cosRoll;
float sinRoll;
float cosPitch;
float sinPitch;
float magX;
float magY;
public:
int compassAddress;
float heading;
int measuredMagX;
int measuredMagY;
int measuredMagZ;
CompassExample(void) {
// this is the constructor of the object and must have the same name
// can be used to initialize any of the variables declared above
}
// **********************************************************************
// The following function calls must be defined inside any new subclasses
// **********************************************************************
virtual void initialize(void);
virtual void measure(void);
// *********************************************************
// The following functions are common between all subclasses
// *********************************************************
const float getHeading(void) {
// Heading calculation based on code written by FabQuad
// http://aeroquad.com/showthread.php?691-Hold-your-heading-with-HMC5843-Magnetometer
cosRoll = cos(radians(flightAngle.getData(ROLL)));
sinRoll = sin(radians(flightAngle.getData(ROLL)));
cosPitch = cos(radians(flightAngle.getData(PITCH)));
sinPitch = sin(radians(flightAngle.getData(PITCH)));
magX = measuredMagX * cosPitch + measuredMagY * sinRoll * sinPitch + measuredMagZ * cosRoll * sinPitch;
magY = measuredMagY * cosRoll - measuredMagZ * sinRoll;
return degrees(atan2(-magY, magX));
}
};
// ***********************************************************************
// ************************ Example Subclass *****************************
// ***********************************************************************
class Compass_AeroQuad_v2 : public CompassExample {
// This sets up the HMC5843 from Sparkfun
public:
Compass_AeroQuad_v2() : CompassExample(){
compassAddress = 0x1E;
}
// ***********************************************************
// Define all the virtual functions declared in the main class
// ***********************************************************
void initialize(void) {
updateRegisterI2C(compassAddress, 0x02, 0x00); // continuous 10Hz mode
delay(100);
}
void measure(void) {
sendByteI2C(compassAddress, 0x03);
Wire.requestFrom(compassAddress, 6);
measuredMagX = (Wire.receive() << 8) | Wire.receive();
measuredMagY = (Wire.receive() << 8) | Wire.receive();
measuredMagZ = (Wire.receive() << 8) | Wire.receive();
Wire.endTransmission();
}
};