~lenzgr/+junk/QuaduIMU

22 by Lenz Grimmer
- Updated ADC.pde with the code from Easy IMU Pilot
1
/* *********************************************************************** */
2
/*                    QuaduIMU Quadcopter                                  */
3
/*                                                                         */
4
/* Code based on ArduIMU DCM code from Diydrones.com  (r20)                */
5
/* and Jose Julio's JJ ArduIMU Quadcopter (Ver. 1.15)                      */
6
/* Author:  Lenz Grimmer                                                   */
36 by Lenz Grimmer
- Incorporated and adapted pitch, yaw and roll stick interpretation code from
7
#define SOFTWARE_VER "0.4"
22 by Lenz Grimmer
- Updated ADC.pde with the code from Easy IMU Pilot
8
/* This version supports the HMC5843 Triple Axis Magnetometer (Optional)   */
9
/* Merged and adapted from Jose's v1.27 (mini) code                        */
10
/* Merged Motor and RX control from Paul René Jørgensen's Quaduino Code    */
11
/* Merged ADC improvements and loop control from Loris Rion's EasyIMUPilot */
12
/* *********************************************************************** */
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
13
14
#include <inttypes.h>
15
#include <math.h>
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
16
#include <Wire.h>   // For magnetometer readings
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
17
#include <ServoDecode.h>
18
#include <ServoTimer2.h> 
19
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
20
/* ***************************************************************************** */
21
/*  CONFIGURATION PART                                                           */
22
/* ***************************************************************************** */
23
35 by Lenz Grimmer
- Allow enabling/disabling the compass in flight (using the AUX2 switch)
24
#define HAVE_MAGNETOMETER 1  // 0 : No magnetometer    1: Magnetometer
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
25
26
// QuadCopter Attitude control PID GAINS
36 by Lenz Grimmer
- Incorporated and adapted pitch, yaw and roll stick interpretation code from
27
#define KP_QUAD_ROLL 2.0
15 by Lenz Grimmer
Found stable flight parameters again:
28
#define KD_QUAD_ROLL 0.4
13 by Lenz Grimmer
Revert back to the previous PID values as a starting point (still quite unstable)
29
#define KI_QUAD_ROLL 0.5 
36 by Lenz Grimmer
- Incorporated and adapted pitch, yaw and roll stick interpretation code from
30
#define KP_QUAD_PITCH 2.0 //1.75 //1.6 // 2.2   //1.75
15 by Lenz Grimmer
Found stable flight parameters again:
31
#define KD_QUAD_PITCH 0.4 //0.4 //0.42 // 0.54  //0.45
13 by Lenz Grimmer
Revert back to the previous PID values as a starting point (still quite unstable)
32
#define KI_QUAD_PITCH 0.5 //0.42 // 0.4  //0.5
12 by Lenz Grimmer
Merged changes from Jose's version 1.27 in the hope of
33
#define KP_QUAD_YAW 3.5 // 4.6  //3.2 //2.6
34
#define KD_QUAD_YAW 1.0 // 0.7  //0.8 //0.4
35
#define KI_QUAD_YAW 0.15 // 0.2  //0.15
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
36
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
37
#define KD_QUAD_COMMAND_PART 13.0   // for special KD implementation (in two parts). Higher values makes the quadcopter more responsive to user inputs
38
36 by Lenz Grimmer
- Incorporated and adapted pitch, yaw and roll stick interpretation code from
39
// Maximum Roll and Pitch angles (in °)
40
#define MAX_ROLL 45
41
#define MAX_PITCH 45
42
// Maximum absolute yaw speed (in °/s)
43
#define MAX_YAW_SPEED 8
44
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
45
// The IMU should be correctly adjusted : Gyro Gains and also initial IMU offsets:
46
// We have to take this values with the IMU flat (0º roll, 0ºpitch)
47
#define acc_offset_x 508 
48
#define acc_offset_y 504
49
#define acc_offset_z 501       // We need to rotate the IMU exactly 90º to take this value  
33 by Lenz Grimmer
- Slightly updated the roll and pitch gyro offsets
50
#define gyro_offset_roll 365
51
#define gyro_offset_pitch 370
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
52
#define gyro_offset_yaw 380
53
28 by Lenz Grimmer
- Merged more improvements from EasyIMU Pilot
54
// RX pulse range (in ms)
36 by Lenz Grimmer
- Incorporated and adapted pitch, yaw and roll stick interpretation code from
55
#define MIN_CHANN 1160      // RX Throttle pulse width at minimum...
56
#define MAX_CHANN 1830
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
57
#define CHANN_CENTER 1500
36 by Lenz Grimmer
- Incorporated and adapted pitch, yaw and roll stick interpretation code from
58
#define MINCHECK MIN_CHANN+40
59
#define MAXCHECK MAX_CHANN-40
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
60
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
61
//Loop speed - AHRS_LOOP must be a multiple of all other loop rates
62
#define AHRS_LOOP 100           // IMU main loop (in Hz)
63
#define MAGNETOMETER_LOOP 10    // IMU magnetometer loop (in Hz)
64
#define TELEMETRY_LOOP 20       // Telemetry loop (in Hz)
65
#define RADIO_LOOP 50           // Radio RX loop (in Hz)
66
#define CONTROL_LOOP 100        // Control (PID + servos update) loop (in Hz)
67
33 by Lenz Grimmer
- Slightly updated the roll and pitch gyro offsets
68
#define ARM_DELAY 2 // Motor arming delay (in seconds)
32 by Lenz Grimmer
- Added some (configurable) delay to the arming/disarming of motors
69
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
70
/* *************************************************** */
71
/*           END OF CONFIGURATION PART                 */
72
/* *************************************************** */
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
73
74
// ADC : Voltage reference 3.3v / 10bits(1024 steps) => 3.22mV/ADC step
75
// ADXL335 Sensitivity(from datasheet) => 330mV/g, 3.22mV/ADC step => 330/3.22 = 102.48
76
// Tested value : 101
77
#define GRAVITY 101 //this equivalent to 1G in the raw data coming from the accelerometer 
78
#define Accel_Scale(x) x*(GRAVITY/9.81)//Scaling the raw data of the accel to actual acceleration in meters for seconds square
79
80
#define ToRad(x) (x*0.01745329252)  // *pi/180
81
#define ToDeg(x) (x*57.2957795131)  // *180/pi
82
83
// LPR530 & LY530 Sensitivity (from datasheet) => 3.33mV/º/s, 3.22mV/ADC step => 1.03
84
// Tested values : 0.96,0.96,0.94
85
#define Gyro_Gain_X 0.92 //X axis Gyro gain
86
#define Gyro_Gain_Y 0.92 //Y axis Gyro gain
87
#define Gyro_Gain_Z 0.94 //Z axis Gyro gain
88
#define Gyro_Scaled_X(x) x*ToRad(Gyro_Gain_X) //Return the scaled ADC raw data of the gyro in radians for second
89
#define Gyro_Scaled_Y(x) x*ToRad(Gyro_Gain_Y) //Return the scaled ADC raw data of the gyro in radians for second
90
#define Gyro_Scaled_Z(x) x*ToRad(Gyro_Gain_Z) //Return the scaled ADC raw data of the gyro in radians for second
91
13 by Lenz Grimmer
Revert back to the previous PID values as a starting point (still quite unstable)
92
#define Kp_ROLLPITCH 0.0125 //0.008  //0.0125 //0.010 // Pitch&Roll Proportional Gain
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
93
#define Ki_ROLLPITCH 0.000010 // Pitch&Roll Integrator Gain
12 by Lenz Grimmer
Merged changes from Jose's version 1.27 in the hope of
94
#define Kp_YAW 1.0 // Yaw Porportional Gain  
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
95
#define Ki_YAW 0.00005 // Yaw Integrator Gain
96
97
//Sensor: GYROX, GYROY, GYROZ, ACCELX, ACCELY, ACCELZ
30 by Lenz Grimmer
- Code cleanup: removed some unused variables/defines
98
int SENSOR_SIGN[]={ 1, -1, -1, 1, -1, 1, -1, -1, -1};
99
// Sensor pin order (for ArduIMU v2 flat)
100
uint8_t sensors[6] = {6, 7, 3, 0, 1, 2};
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
101
22 by Lenz Grimmer
- Updated ADC.pde with the code from Easy IMU Pilot
102
volatile unsigned int AN_raw[8]; //store the ADC raw data in 12 bits
103
unsigned int AN_OFFSET[6]; //Array that stores the Offset
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
104
float AN[6]; //array that store the 6 ADC filtered data
105
106
float Accel_Vector[3]= {0,0,0}; //Store the acceleration in a vector
107
float Accel_Vector_unfiltered[3]= {0,0,0}; //Store the acceleration in a vector
108
float Accel_magnitude;
109
float Accel_weight;
110
float Gyro_Vector[3]= {0,0,0};//Store the gyros rutn rate in a vector
111
float Omega_Vector[3]= {0,0,0}; //Corrected Gyro_Vector data
112
float Omega_P[3]= {0,0,0};//Omega Proportional correction
113
float Omega_I[3]= {0,0,0};//Omega Integrator
114
float Omega[3]= {0,0,0};
115
116
float errorRollPitch[3]= {0,0,0};
117
float errorYaw[3]= {0,0,0};
118
119
float roll=0;
120
float pitch=0;
121
float yaw=0;
122
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
123
//Magnetometer variables
35 by Lenz Grimmer
- Allow enabling/disabling the compass in flight (using the AUX2 switch)
124
boolean magnetom_enabled=true;
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
125
int magnetom_x;
126
int magnetom_y;
127
int magnetom_z;
128
float MAG_Heading;
129
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
130
float DCM_Matrix[3][3]= {
131
  {
132
    1,0,0  }
133
  ,{
134
    0,1,0  }
135
  ,{
136
    0,0,1  }
137
}; 
138
34 by Lenz Grimmer
- Slightly reduced pitch and roll P value to 2.1 to avoid wobbling on descent
139
//  RX PPM channel order for DX7 and Spektrum2PPM
140
// ===============================================
141
//  1: Throttle (left stick down->up)
142
//  2: Roll (right stick right->left)
143
//  3: Pitch/Nick (right stick down->up)
144
//  4: Yaw (left stick right->left)
145
//  5: Gear (Switch 1->0)
146
//  6: Flt Mode (Switch 2->1->N)
147
//  7: Aux2 (Switch 1->0)
148
9 by Lenz Grimmer
- Make yaw stick more sensitive
149
#define RX_THROTTLE 1
150
#define RX_ROLL 2
151
#define RX_PITCH 3
152
#define RX_YAW 4
153
#define RX_GEAR 5
34 by Lenz Grimmer
- Slightly reduced pitch and roll P value to 2.1 to avoid wobbling on descent
154
#define RX_FLTMODE 6
155
#define RX_AUX 7
9 by Lenz Grimmer
- Make yaw stick more sensitive
156
36 by Lenz Grimmer
- Incorporated and adapted pitch, yaw and roll stick interpretation code from
157
// Configure stick behaviour
158
#define REVERSE_ROLL -          // Reverse roll  (+ or -)
159
#define REVERSE_PITCH -         // Reverse pitch (+ or -)
160
#define REVERSE_YAW -           // Reverse yaw (+ or -)
161
#define REVERSE_THROTTLE +      // Reverse throttle (+ or -)
162
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
163
#define THROTTLE RX_THROTTLE-1
164
#define ROLL RX_ROLL-1
165
#define PITCH RX_PITCH-1
166
#define YAW RX_YAW-1
167
#define GEAR RX_GEAR-1
34 by Lenz Grimmer
- Slightly reduced pitch and roll P value to 2.1 to avoid wobbling on descent
168
#define FLTMODE RX_FLTMODE-1
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
169
#define AUX RX_AUX-1
170
14.1.1 by Lenz Grimmer
Temporary commit to merge changes from another branch. Work in progress! (Does not even compile yet)
171
// PPM Rx signal read (ICP) constants and variables
172
// RX command states
173
#define RX_NOT_SYNCHED 0
174
#define RX_ACQUIRING 1
175
#define RX_READY 2
176
#define RX_IN_FAILSAFE 3
177
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
178
// Define LEDs
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
179
// Red indicates motors are armed
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
180
#define REDLEDPIN 5
181
#define REDLEDON digitalWrite(REDLEDPIN, HIGH)
182
#define REDLEDOFF digitalWrite(REDLEDPIN, LOW)
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
183
// Blue indicates RX readiness
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
184
#define BLUELEDPIN 6
185
#define BLUELEDON digitalWrite(BLUELEDPIN, HIGH)
186
#define BLUELEDOFF digitalWrite(BLUELEDPIN, LOW)
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
187
// Yellow indicates IMU activity and commands being sent to the motors
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
188
#define YELLOWLEDPIN 7
189
#define YELLOWLEDON digitalWrite(YELLOWLEDPIN, HIGH)
190
#define YELLOWLEDOFF digitalWrite(YELLOWLEDPIN, LOW)
191
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
192
#define MOTOR_FRONT 0
193
#define MOTOR_REAR 1
194
#define MOTOR_LEFT 2
195
#define MOTOR_RIGHT 3
196
14.1.1 by Lenz Grimmer
Temporary commit to merge changes from another branch. Work in progress! (Does not even compile yet)
197
struct RX {
35 by Lenz Grimmer
- Allow enabling/disabling the compass in flight (using the AUX2 switch)
198
  int raw[7];        // Raw 
199
  int value[7];      // Smoothed
200
  float command[7];  // Processed
201
  float diff[7];     // Delta between two samples
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
202
  int state;         // RX command state (e.g. RX_NOT_SYNCHED or RX_READY
203
} rx = {
35 by Lenz Grimmer
- Allow enabling/disabling the compass in flight (using the AUX2 switch)
204
  { 0, 0, 0, 0, 0, 0, 0 },
205
  { 0, 0, 0, 0, 0, 0, 0 },
206
  { 0, 0, 0, 0, 0, 0, 0 },
207
  { 0, 0, 0, 0, 0, 0, 0 },
208
  RX_NOT_SYNCHED
14.1.1 by Lenz Grimmer
Temporary commit to merge changes from another branch. Work in progress! (Does not even compile yet)
209
};
210
211
struct Motor {
212
  int command[4];
213
  boolean armed;
214
} motor = {
36 by Lenz Grimmer
- Incorporated and adapted pitch, yaw and roll stick interpretation code from
215
  { MIN_CHANN, MIN_CHANN, MIN_CHANN, MIN_CHANN },
14.1.1 by Lenz Grimmer
Temporary commit to merge changes from another branch. Work in progress! (Does not even compile yet)
216
  false,
217
};
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
218
219
// ADC variables
220
volatile uint8_t MuxSel=0;
221
volatile uint8_t analog_reference = DEFAULT;
222
volatile uint16_t analog_buffer[8];
223
volatile uint8_t analog_count[8];
224
int an_count;
225
226
// Attitude control variables
227
int control_roll;           // Control results
228
int control_pitch;
229
int control_yaw;
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
230
231
// Attitude control
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
232
float roll_I=0;
233
float roll_D;
234
float err_roll;
235
float pitch_I=0;
236
float pitch_D;
237
float err_pitch;
238
float yaw_I=0;
239
float yaw_D;
240
float err_yaw;
241
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
242
// timer variables
243
unsigned int counter = 0;
244
float G_Dt;    // Integration time for the gyros (DCM algorithm)
245
long timer = 0; //general purpose timer 
246
long timer_old = 0;
32 by Lenz Grimmer
- Added some (configurable) delay to the arming/disarming of motors
247
unsigned int arming_counter = 0;
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
248
249
void setup(){
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
250
  
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
251
  Serial.begin(38400);
252
  Serial.println();
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
253
  Serial.print("QuaduIMU v");
254
  Serial.println(SOFTWARE_VER);
255
27 by Lenz Grimmer
Checkin after successful flight tests:
256
  pinMode(BLUELEDPIN, OUTPUT); // Blue LED
3 by Lenz Grimmer
Make use of all the LEDs on the ArduIMU board
257
  pinMode(REDLEDPIN, OUTPUT);  // Red LED
5 by Lenz Grimmer
If throttle is all the way down (and we're likely on the ground),
258
  pinMode(YELLOWLEDPIN, OUTPUT); // Yellow LED
27 by Lenz Grimmer
Checkin after successful flight tests:
259
  BLUELEDON;
260
  REDLEDON;
261
  YELLOWLEDON;
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
262
32 by Lenz Grimmer
- Added some (configurable) delay to the arming/disarming of motors
263
  // Attach ESCs and set to minimum throttle. Motors are disarmed.
14.1.1 by Lenz Grimmer
Temporary commit to merge changes from another branch. Work in progress! (Does not even compile yet)
264
  setupMotors();
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
265
 
266
  Analog_Reference(EXTERNAL);
267
  Analog_Init();
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
268
 
35 by Lenz Grimmer
- Allow enabling/disabling the compass in flight (using the AUX2 switch)
269
#if (HAVE_MAGNETOMETER==1)
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
270
  // Magnetometer initialization
271
  I2C_Init();
272
  Compass_Init();
273
#endif
274
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
275
  Read_adc_raw();
276
  delay(20);
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
277
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
278
  // Offset values for accels and gyros...
22 by Lenz Grimmer
- Updated ADC.pde with the code from Easy IMU Pilot
279
  AN_OFFSET[0] = gyro_offset_roll;
280
  AN_OFFSET[1] = gyro_offset_pitch;
281
  AN_OFFSET[2] = gyro_offset_yaw;
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
282
  AN_OFFSET[3] = acc_offset_x;
283
  AN_OFFSET[4] = acc_offset_y;
284
  AN_OFFSET[5] = acc_offset_z;
285
 
286
  // Take the gyro offset values
22 by Lenz Grimmer
- Updated ADC.pde with the code from Easy IMU Pilot
287
  for(int i=0;i<300;i++)
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
288
  {
289
    Read_adc_raw();
290
    for(int y=0; y<=2; y++)   // Read initial ADC values for offset.
291
      AN_OFFSET[y]=AN_OFFSET[y]*0.8 + AN[y]*0.2;
292
    delay(20);
293
  }
294
 
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
295
  Serial.print("AN:");
22 by Lenz Grimmer
- Updated ADC.pde with the code from Easy IMU Pilot
296
  for (int i=0; i<6; i++)
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
297
  {
298
    Serial.print(",");
299
    Serial.println(AN_OFFSET[i]);
300
  }
27 by Lenz Grimmer
Checkin after successful flight tests:
301
  YELLOWLEDOFF;
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
302
14.1.1 by Lenz Grimmer
Temporary commit to merge changes from another branch. Work in progress! (Does not even compile yet)
303
  setupRX();
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
304
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
305
  Read_adc_raw();   // Start ADC readings...
306
  timer = millis();
307
  delay(20);
27 by Lenz Grimmer
Checkin after successful flight tests:
308
  BLUELEDOFF;
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
309
}
310
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
311
/* MAIN LOOP */
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
312
void loop() {
313
  
314
  if((DIYmillis()-timer) >= (1000/AHRS_LOOP)) //AHRS LOOP
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
315
  {
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
316
    counter++;
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
317
    timer_old = timer;
318
    timer=millis();
319
    G_Dt = (timer-timer_old)/1000.0;      // Real time of loop run 
320
	
35 by Lenz Grimmer
- Allow enabling/disabling the compass in flight (using the AUX2 switch)
321
#if (HAVE_MAGNETOMETER==1)
322
    if (counter % (AHRS_LOOP/MAGNETOMETER_LOOP) == 0 && magnetom_enabled) //MAGNETOMETER LOOP
11 by Lenz Grimmer
- Added initial support for the HMC5843 Triple Axis Magnetometer (optional)
323
    {
324
      Read_Compass();    // Read I2C magnetometer
325
      Compass_Heading(); // Calculate magnetic heading  
326
    }
327
#endif
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
328
    
329
    //Read ADC (accelero & gyros)
330
    Read_adc_raw();
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
331
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
332
    // IMU DCM Algorithm
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
333
    DCM(); 
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
334
   
335
    if (counter % (AHRS_LOOP/RADIO_LOOP) == 0) //CONTROL LOOP
336
    {
337
       updateRX();
338
    }
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
339
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
340
    if (counter % (AHRS_LOOP/CONTROL_LOOP) == 0) //CONTROL LOOP
341
    {
342
      if (rx.state==RX_READY) {
343
        Attitude_control();
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
344
      
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
345
        // Quadcopter mix
26 by Lenz Grimmer
- Don't update motors if they're not armed
346
        if (rx.raw[THROTTLE] > (MINCHECK) && motor.armed)  // Minimum throttle to start control
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
347
        {
348
          YELLOWLEDON; 
349
          motor.command[MOTOR_RIGHT]=rx.raw[THROTTLE] - control_roll + control_yaw;    // Right motor
350
          motor.command[MOTOR_LEFT]=rx.raw[THROTTLE] + control_roll + control_yaw;    // Left motor
351
          motor.command[MOTOR_FRONT]=rx.raw[THROTTLE] + control_pitch - control_yaw;   // Front motor
352
          motor.command[MOTOR_REAR]=rx.raw[THROTTLE] - control_pitch - control_yaw;   // rear motor
353
          updateMotors();
354
        } else {
355
          YELLOWLEDOFF; 
356
          rx.command[YAW] = ToDeg(yaw);
357
          roll_I = 0;  // reset I terms...
358
          pitch_I = 0;
359
          yaw_I = 0; 
360
          // Motors stopped
36 by Lenz Grimmer
- Incorporated and adapted pitch, yaw and roll stick interpretation code from
361
          setAllMotors(MIN_CHANN);
26 by Lenz Grimmer
- Don't update motors if they're not armed
362
          updateMotors();
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
363
        }
364
      } else {  // rx.state!=RX_READY => Lost radio signal => Descend slowly...
365
        // Descend  (Reduce throttle)
36 by Lenz Grimmer
- Incorporated and adapted pitch, yaw and roll stick interpretation code from
366
        if (rx.raw[THROTTLE]>MIN_CHANN)
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
367
          rx.raw[THROTTLE]--;
368
        rx.command[ROLL] = 0;     // Stabilize to roll=0, pitch=0, yaw not important
369
        rx.command[PITCH] = 0;
370
        Attitude_control();
371
        // Quadcopter mix, no yaw
372
        motor.command[MOTOR_RIGHT]=rx.raw[THROTTLE] - control_roll;    // Right motor
373
        motor.command[MOTOR_LEFT]=rx.raw[THROTTLE] + control_roll;    // Left motor
374
        motor.command[MOTOR_FRONT]=rx.raw[THROTTLE] + control_pitch;   // Front motor
375
        motor.command[MOTOR_REAR]=rx.raw[THROTTLE] - control_pitch;   // rear motor
20 by Lenz Grimmer
After merge cleanup, now it even compiles, but has not been tested at all!
376
        updateMotors();
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
377
      }
21 by Lenz Grimmer
- Merged Loop control and millis() replacement function from Easy IMU Pilot
378
    }
379
380
    if (counter % (AHRS_LOOP/TELEMETRY_LOOP) == 0) //TELEMETRY LOOP
381
    {
382
      // Telemetry data...
383
      printdata();
384
    }
385
  }
2 by Lenz Grimmer
Renamed Project to "QuaduIMU"
386
}