root / quad1 / AeroQuad / Template.h @ 9240aaa3
History | View | Annotate | Download (6.06 KB)
1 | 9240aaa3 | Alex | /*
|
---|---|---|---|
2 | AeroQuad v2.1 - October 2010
|
||
3 | www.AeroQuad.com
|
||
4 | Copyright (c) 2010 Ted Carancho. All rights reserved.
|
||
5 | An Open Source Arduino based multicopter.
|
||
6 |
|
||
7 | This program is free software: you can redistribute it and/or modify
|
||
8 | it under the terms of the GNU General Public License as published by
|
||
9 | the Free Software Foundation, either version 3 of the License, or
|
||
10 | (at your option) any later version.
|
||
11 | |||
12 | This program is distributed in the hope that it will be useful,
|
||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
15 | GNU General Public License for more details.
|
||
16 | |||
17 | You should have received a copy of the GNU General Public License
|
||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
19 | */
|
||
20 | |||
21 | // User this as a template for new classes or subclasses
|
||
22 | |||
23 | // ***********************************************************************
|
||
24 | // ************************** Example Class ******************************
|
||
25 | // ***********************************************************************
|
||
26 | class exampleClass { |
||
27 | public:
|
||
28 | int exampleVariable;
|
||
29 | float exampleData[3]; |
||
30 | exampleClass(void) {
|
||
31 | // this is the constructor of the object and must have the same name
|
||
32 | // can be used to initialize any of the variables declared above
|
||
33 | } |
||
34 | |||
35 | // **********************************************************************
|
||
36 | // The following function calls must be defined inside any new subclasses
|
||
37 | // **********************************************************************
|
||
38 | virtual void initialize(void); |
||
39 | virtual void exampleFunction(int); |
||
40 | virtual const int getExampleData(byte); |
||
41 | |||
42 | // *********************************************************
|
||
43 | // The following functions are common between all subclasses
|
||
44 | // *********************************************************
|
||
45 | void examplePublicFunction(byte axis, int value) { |
||
46 | // insert common code here
|
||
47 | } |
||
48 | const int getPublicData(byte axis) { |
||
49 | return exampleData[axis];
|
||
50 | } |
||
51 | }; |
||
52 | |||
53 | // ***********************************************************************
|
||
54 | // ************************ Example Subclass *****************************
|
||
55 | // ***********************************************************************
|
||
56 | class exampleSubClass : public exampleClass { |
||
57 | private:
|
||
58 | int exampleArray[3]; // only for use inside this subclass |
||
59 | int examplePrivateData; // only for use inside this subclass |
||
60 | void examplePrivateFunction(int functionVariable) { |
||
61 | // it’s possible to declare functions just for this subclass
|
||
62 | } |
||
63 | |||
64 | public:
|
||
65 | exampleSubClass() : exampleClass(){ |
||
66 | // this is the constructor of the object and must have the same name
|
||
67 | // can be used to initialize any of the variables declared above
|
||
68 | } |
||
69 | |||
70 | // ***********************************************************
|
||
71 | // Define all the virtual functions declared in the main class
|
||
72 | // ***********************************************************
|
||
73 | void initialize(void) { |
||
74 | // insert code here
|
||
75 | } |
||
76 | void exampleFunction(int someVariable) { |
||
77 | // insert code here
|
||
78 | examplePrivateFunction(someVariable); |
||
79 | } |
||
80 | const int getExampleData(byte axis) { |
||
81 | // insert code here
|
||
82 | return exampleArray[axis];
|
||
83 | } |
||
84 | }; |
||
85 | |||
86 | // Example implementation of a class and subclass for a magnetometer
|
||
87 | // ***********************************************************************
|
||
88 | // ************************** Compass Class ******************************
|
||
89 | // ***********************************************************************
|
||
90 | class CompassExample { |
||
91 | private: // not found in the example above, but it's possible to declare private variables only seen in the main class |
||
92 | float cosRoll;
|
||
93 | float sinRoll;
|
||
94 | float cosPitch;
|
||
95 | float sinPitch;
|
||
96 | float magX;
|
||
97 | float magY;
|
||
98 | |||
99 | public:
|
||
100 | int compassAddress;
|
||
101 | float heading;
|
||
102 | int measuredMagX;
|
||
103 | int measuredMagY;
|
||
104 | int measuredMagZ;
|
||
105 | |||
106 | CompassExample(void) {
|
||
107 | // this is the constructor of the object and must have the same name
|
||
108 | // can be used to initialize any of the variables declared above
|
||
109 | } |
||
110 | |||
111 | // **********************************************************************
|
||
112 | // The following function calls must be defined inside any new subclasses
|
||
113 | // **********************************************************************
|
||
114 | virtual void initialize(void); |
||
115 | virtual void measure(void); |
||
116 | |||
117 | // *********************************************************
|
||
118 | // The following functions are common between all subclasses
|
||
119 | // *********************************************************
|
||
120 | const float getHeading(void) { |
||
121 | // Heading calculation based on code written by FabQuad
|
||
122 | // http://aeroquad.com/showthread.php?691-Hold-your-heading-with-HMC5843-Magnetometer
|
||
123 | cosRoll = cos(radians(flightAngle.getData(ROLL))); |
||
124 | sinRoll = sin(radians(flightAngle.getData(ROLL))); |
||
125 | cosPitch = cos(radians(flightAngle.getData(PITCH))); |
||
126 | sinPitch = sin(radians(flightAngle.getData(PITCH))); |
||
127 | magX = measuredMagX * cosPitch + measuredMagY * sinRoll * sinPitch + measuredMagZ * cosRoll * sinPitch; |
||
128 | magY = measuredMagY * cosRoll - measuredMagZ * sinRoll; |
||
129 | return degrees(atan2(-magY, magX));
|
||
130 | } |
||
131 | }; |
||
132 | |||
133 | // ***********************************************************************
|
||
134 | // ************************ Example Subclass *****************************
|
||
135 | // ***********************************************************************
|
||
136 | class Compass_AeroQuad_v2 : public CompassExample { |
||
137 | // This sets up the HMC5843 from Sparkfun
|
||
138 | public:
|
||
139 | Compass_AeroQuad_v2() : CompassExample(){ |
||
140 | compassAddress = 0x1E;
|
||
141 | } |
||
142 | |||
143 | // ***********************************************************
|
||
144 | // Define all the virtual functions declared in the main class
|
||
145 | // ***********************************************************
|
||
146 | void initialize(void) { |
||
147 | updateRegisterI2C(compassAddress, 0x02, 0x00); // continuous 10Hz mode |
||
148 | delay(100);
|
||
149 | } |
||
150 | |||
151 | void measure(void) { |
||
152 | sendByteI2C(compassAddress, 0x03);
|
||
153 | Wire.requestFrom(compassAddress, 6);
|
||
154 | measuredMagX = (Wire.receive() << 8) | Wire.receive();
|
||
155 | measuredMagY = (Wire.receive() << 8) | Wire.receive();
|
||
156 | measuredMagZ = (Wire.receive() << 8) | Wire.receive();
|
||
157 | Wire.endTransmission(); |
||
158 | } |
||
159 | }; |