|
| 1 | +/* |
| 2 | + AeroQuad v3.0 - April 2011 |
| 3 | + www.AeroQuad.com |
| 4 | + Copyright (c) 2011 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 | +#ifndef _AQ_GLOBAL_HEADER_DEFINITION_H_ |
| 22 | +#define _AQ_GLOBAL_HEADER_DEFINITION_H_ |
| 23 | + |
| 24 | + |
| 25 | +#include <stdlib.h> |
| 26 | +#include <math.h> |
| 27 | +#include "Arduino.h" |
| 28 | +#include "pins_arduino.h" |
| 29 | + |
| 30 | +// Flight Software Version |
| 31 | +#define SOFTWARE_VERSION 3.0 |
| 32 | + |
| 33 | +#define BAUD 115200 |
| 34 | +//#define BAUD 111111 // use this to be compatible with USB and XBee connections |
| 35 | +//#define BAUD 57600 |
| 36 | + |
| 37 | +// Analog Reference Value |
| 38 | +// This value provided from Configurator |
| 39 | +// Use a DMM to measure the voltage between AREF and GND |
| 40 | +// Enter the measured voltage below to define your value for aref |
| 41 | +// If you don't have a DMM use the following: |
| 42 | +// AeroQuad Shield v1.7, aref = 3.0 |
| 43 | +// AeroQuad Shield v1.6 or below, aref = 2.8 |
| 44 | +float aref; // Read in from EEPROM |
| 45 | +////////////////////////////////////////////////////// |
| 46 | + |
| 47 | +/** |
| 48 | + * Heading and heading hold global declaration section |
| 49 | + */ |
| 50 | +byte headingHoldConfig = 0; |
| 51 | +float headingHold = 0; // calculated adjustment for quad to go to heading (PID output) |
| 52 | +float heading = 0; // measured heading from yaw gyro (process variable) |
| 53 | +float relativeHeading = 0; // current heading the quad is set to (set point) |
| 54 | +byte headingHoldState = OFF; |
| 55 | +////////////////////////////////////////////////////// |
| 56 | + |
| 57 | +/** |
| 58 | + * battery monitor and battery monitor throttle correction global declaration section |
| 59 | + */ |
| 60 | +int batteyMonitorThrottleCorrection = 0; |
| 61 | +#if defined (BattMonitor) |
| 62 | + #define BattMonitorAlarmVoltage 10.0 // required by battery monitor macro, this is overriden by readEEPROM() |
| 63 | + float batteryMonitorAlarmVoltage = 10.0; |
| 64 | + int batteryMonitorStartThrottle = 0; |
| 65 | + int batteryMonitorThrottleTarget = 1450; |
| 66 | + unsigned long batteryMonitorStartTime = 0; |
| 67 | + unsigned long batteryMonitorGoinDownTime = 60000; |
| 68 | + |
| 69 | + #if defined BattMonitorAutoDescent |
| 70 | + int batteryMonitorAlarmCounter = 0; |
| 71 | + #define BATTERY_MONITOR_MAX_ALARM_COUNT 50 |
| 72 | + #endif |
| 73 | +#endif |
| 74 | +////////////////////////////////////////////////////// |
| 75 | + |
| 76 | +/** |
| 77 | + * ESC calibration process global declaration |
| 78 | + */ |
| 79 | +byte calibrateESC = 0; |
| 80 | +int testCommand = 1000; |
| 81 | +////////////////////////////////////////////////////// |
| 82 | + |
| 83 | +/** |
| 84 | + * Flight control global declaration |
| 85 | + */ |
| 86 | +#define RATE_FLIGHT_MODE 0 |
| 87 | +#define ATTITUDE_FLIGHT_MODE 1 |
| 88 | +byte flightMode = RATE_FLIGHT_MODE; |
| 89 | +unsigned long frameCounter = 0; // main loop executive frame counter |
| 90 | +int minArmedThrottle = MIN_ARMED_THROTTLE; // Read in from EEPROM, defines min throttle during flips |
| 91 | + |
| 92 | +float G_Dt = 0.002; |
| 93 | +int throttle = 1000; |
| 94 | +byte motorArmed = OFF; |
| 95 | +byte safetyCheck = OFF; |
| 96 | +// main loop time variable |
| 97 | +unsigned long previousTime = 0; |
| 98 | +unsigned long currentTime = 0; |
| 99 | +unsigned long deltaTime = 0; |
| 100 | +// sub loop time variable |
| 101 | +unsigned long tenHZpreviousTime = 0; |
| 102 | +unsigned long fiftyHZpreviousTime = 0; |
| 103 | +unsigned long hundredHZpreviousTime = 0; |
| 104 | + |
| 105 | +void readPilotCommands(); |
| 106 | +void calculateFlightError(); |
| 107 | +void processHeading(); |
| 108 | +void processAltitudeHold(); |
| 109 | +void processCalibrateESC(); |
| 110 | +void processFlightControl(); |
| 111 | +void processAltitudeHold(); |
| 112 | +////////////////////////////////////////////////////// |
| 113 | + |
| 114 | +/** |
| 115 | + * Altitude control global declaration |
| 116 | + */ |
| 117 | +#if defined AltitudeHoldBaro || defined AltitudeHoldRangeFinder |
| 118 | + // special state that allows immediate turn off of Altitude hold if large throttle changesa are made at the TX |
| 119 | + int altitudeHoldBump = 90; |
| 120 | + int altitudeHoldPanicStickMovement = 250; |
| 121 | + |
| 122 | + float altitudeToHoldTarget = 0.0; |
| 123 | + int altitudeHoldThrottle = 1000; |
| 124 | + boolean isStoreAltitudeNeeded = false; |
| 125 | + boolean altitudeHoldState = OFF; // ON, OFF or ALTPANIC |
| 126 | +#endif |
| 127 | +int minThrottleAdjust = -50; |
| 128 | +int maxThrottleAdjust = 50; |
| 129 | + |
| 130 | +float getAltitudeFromSensors(); |
| 131 | +////////////////////////////////////////////////////// |
| 132 | + |
| 133 | + |
| 134 | +/** |
| 135 | + * Serial communication global declaration |
| 136 | + */ |
| 137 | +#define SERIAL_PRINT SERIAL_PORT.print |
| 138 | +#define SERIAL_PRINTLN SERIAL_PORT.println |
| 139 | +#define SERIAL_AVAILABLE SERIAL_PORT.available |
| 140 | +#define SERIAL_READ SERIAL_PORT.read |
| 141 | +#define SERIAL_FLUSH SERIAL_PORT.flush |
| 142 | +#define SERIAL_BEGIN SERIAL_PORT.begin |
| 143 | + |
| 144 | +HardwareSerial *binaryPort; |
| 145 | + |
| 146 | +void readSerialCommand(); |
| 147 | +void sendSerialTelemetry(); |
| 148 | +void printInt(int data); |
| 149 | +float readFloatSerial(); |
| 150 | +void sendBinaryFloat(float); |
| 151 | +void sendBinaryuslong(unsigned long); |
| 152 | +void fastTelemetry(); |
| 153 | +void comma(); |
| 154 | +void reportVehicleState(); |
| 155 | +////////////////////////////////////////////////////// |
| 156 | + |
| 157 | +/** |
| 158 | + * EEPROM global section |
| 159 | + */ |
| 160 | +typedef struct { |
| 161 | + float p; |
| 162 | + float i; |
| 163 | + float d; |
| 164 | +} t_NVR_PID; |
| 165 | + |
| 166 | +typedef struct { |
| 167 | + float slope; |
| 168 | + float offset; |
| 169 | + float smooth_factor; |
| 170 | +} t_NVR_Receiver; |
| 171 | + |
| 172 | +typedef struct { |
| 173 | + t_NVR_PID ROLL_PID_GAIN_ADR; |
| 174 | + t_NVR_PID LEVELROLL_PID_GAIN_ADR; |
| 175 | + t_NVR_PID YAW_PID_GAIN_ADR; |
| 176 | + t_NVR_PID PITCH_PID_GAIN_ADR; |
| 177 | + t_NVR_PID LEVELPITCH_PID_GAIN_ADR; |
| 178 | + t_NVR_PID HEADING_PID_GAIN_ADR; |
| 179 | + t_NVR_PID LEVEL_GYRO_ROLL_PID_GAIN_ADR; |
| 180 | + t_NVR_PID LEVEL_GYRO_PITCH_PID_GAIN_ADR; |
| 181 | + t_NVR_PID ALTITUDE_PID_GAIN_ADR; |
| 182 | + t_NVR_PID ZDAMP_PID_GAIN_ADR; |
| 183 | + t_NVR_Receiver RECEIVER_DATA[LASTCHANNEL]; |
| 184 | + |
| 185 | + float SOFTWARE_VERSION_ADR; |
| 186 | + float WINDUPGUARD_ADR; |
| 187 | + float XMITFACTOR_ADR; |
| 188 | + float GYROSMOOTH_ADR; |
| 189 | + float ACCSMOOTH_ADR; |
| 190 | + float AREF_ADR; |
| 191 | + float FLIGHTMODE_ADR; |
| 192 | + float HEADINGHOLD_ADR; |
| 193 | + float ACCEL_1G_ADR; |
| 194 | + float ALTITUDE_MAX_THROTTLE_ADR; |
| 195 | + float ALTITUDE_MIN_THROTTLE_ADR; |
| 196 | + float ALTITUDE_SMOOTH_ADR; |
| 197 | + float ALTITUDE_WINDUP_ADR; |
| 198 | + float ALTITUDE_BUMP_ADR; |
| 199 | + float ALTITUDE_PANIC_ADR; |
| 200 | + float SERVOMINPITCH_ADR; |
| 201 | + float SERVOMINROLL_ADR; |
| 202 | + float GYRO_ROLL_ZERO_ADR; |
| 203 | + float GYRO_PITCH_ZERO_ADR; |
| 204 | + float GYRO_YAW_ZERO_ADR; |
| 205 | + // Accel Calibration |
| 206 | + float XAXIS_ACCEL_BIAS_ADR; |
| 207 | + float XAXIS_ACCEL_SCALE_FACTOR_ADR; |
| 208 | + float YAXIS_ACCEL_BIAS_ADR; |
| 209 | + float YAXIS_ACCEL_SCALE_FACTOR_ADR; |
| 210 | + float ZAXIS_ACCEL_BIAS_ADR; |
| 211 | + float ZAXIS_ACCEL_SCALE_FACTOR_ADR; |
| 212 | + // Mag Calibration |
| 213 | + float XAXIS_MAG_BIAS_ADR; |
| 214 | + float YAXIS_MAG_BIAS_ADR; |
| 215 | + float ZAXIS_MAG_BIAS_ADR; |
| 216 | + // Battery Monitor |
| 217 | + float BATT_ALARM_VOLTAGE_ADR; |
| 218 | + float BATT_THROTTLE_TARGET_ADR; |
| 219 | + float BATT_DOWN_TIME_ADR; |
| 220 | +} t_NVR_Data; |
| 221 | + |
| 222 | + |
| 223 | +void readEEPROM(); |
| 224 | +void initSensorsZeroFromEEPROM(); |
| 225 | +void storeSensorsZeroToEEPROM(); |
| 226 | +void initReceiverFromEEPROM(); |
| 227 | + |
| 228 | +float nvrReadFloat(int address); // defined in DataStorage.h |
| 229 | +void nvrWriteFloat(float value, int address); // defined in DataStorage.h |
| 230 | +void nvrReadPID(unsigned char IDPid, unsigned int IDEeprom); |
| 231 | +void nvrWritePID(unsigned char IDPid, unsigned int IDEeprom); |
| 232 | + |
| 233 | +#define GET_NVR_OFFSET(param) ((int)&(((t_NVR_Data*) 0)->param)) |
| 234 | +#define readFloat(addr) nvrReadFloat(GET_NVR_OFFSET(addr)) |
| 235 | +#define writeFloat(value, addr) nvrWriteFloat(value, GET_NVR_OFFSET(addr)) |
| 236 | +#define readPID(IDPid, addr) nvrReadPID(IDPid, GET_NVR_OFFSET(addr)) |
| 237 | +#define writePID(IDPid, addr) nvrWritePID(IDPid, GET_NVR_OFFSET(addr)) |
| 238 | + |
| 239 | + |
| 240 | +/** |
| 241 | + * Debug utility global declaration |
| 242 | + * Debug code should never be part of a release sofware |
| 243 | + * @see Kenny |
| 244 | + */ |
| 245 | +//#define DEBUG |
| 246 | +byte fastTransfer = OFF; // Used for troubleshooting |
| 247 | +//unsigned long fastTelemetryTime = 0; |
| 248 | +//byte testSignal = LOW; |
| 249 | +////////////////////////////////////////////////////// |
| 250 | + |
| 251 | +#endif // _AQ_GLOBAL_HEADER_DEFINITION_H_ |
| 252 | + |
0 commit comments