2011-03-19 07:20:11 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
/*
|
2011-05-07 16:41:55 -03:00
|
|
|
ArduCopter Version 2.0 Beta
|
2010-12-19 12:40:33 -04:00
|
|
|
Authors: Jason Short
|
|
|
|
Based on code and ideas from the Arducopter team: Jose Julio, Randy Mackay, Jani Hirvinen
|
|
|
|
Thanks to: Chris Anderson, Mike Smith, Jordi Munoz, Doug Weibel, James Goppert, Benjamin Pelletier
|
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
|
|
|
|
This firmware is free software; you can redistribute it and/or
|
2010-12-19 12:40:33 -04:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2011-06-24 14:09:59 -03:00
|
|
|
|
|
|
|
Special Thanks for Contributors:
|
|
|
|
|
|
|
|
Hein Hollander :Octo Support
|
|
|
|
Dani Saez :V Ocoto Support
|
|
|
|
Max Levine :Tri Support, Graphics
|
|
|
|
Jose Julio :Stabilization Control laws
|
|
|
|
Randy MacKay :Heli Support
|
|
|
|
Jani Hiriven :Testing feedback
|
|
|
|
Andrew Tridgell :Mavlink Support
|
|
|
|
James Goppert :Mavlink Support
|
|
|
|
Doug Weibel :Libraries
|
|
|
|
Mike Smith :Libraries, Coding support
|
|
|
|
HappyKillmore :Mavlink GCS
|
|
|
|
Micheal Oborne :Mavlink GCS
|
|
|
|
Jack Dunkle :Alpha testing
|
|
|
|
Christof Schmid :Alpha testing
|
|
|
|
|
|
|
|
And much more so PLEASE PM me on DIYDRONES to add your contribution to the List
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
*/
|
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Header includes
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// AVR runtime
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/eeprom.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include <FastSerial.h>
|
|
|
|
#include <AP_Common.h>
|
2011-02-24 01:56:59 -04:00
|
|
|
#include <APM_RC.h> // ArduPilot Mega RC Library
|
|
|
|
#include <AP_GPS.h> // ArduPilot GPS library
|
2010-12-19 12:40:33 -04:00
|
|
|
#include <Wire.h> // Arduino I2C lib
|
2011-06-12 09:14:10 -03:00
|
|
|
#include <SPI.h>
|
2011-02-24 01:56:59 -04:00
|
|
|
#include <DataFlash.h> // ArduPilot Mega Flash Memory Library
|
|
|
|
#include <AP_ADC.h> // ArduPilot Mega Analog to Digital Converter Library
|
|
|
|
#include <APM_BMP085.h> // ArduPilot Mega BMP085 Library
|
|
|
|
#include <AP_Compass.h> // ArduPilot Mega Magnetometer Library
|
|
|
|
#include <AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
|
|
|
|
#include <AP_IMU.h> // ArduPilot Mega IMU Library
|
|
|
|
#include <AP_DCM.h> // ArduPilot Mega DCM Library
|
|
|
|
#include <PID.h> // PID library
|
2011-02-19 22:03:01 -04:00
|
|
|
#include <RC_Channel.h> // RC Channel Library
|
2011-02-20 19:09:28 -04:00
|
|
|
#include <AP_RangeFinder.h> // Range finder library
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-03-17 22:50:33 -03:00
|
|
|
#define MAVLINK_COMM_NUM_BUFFERS 2
|
2011-02-19 22:03:01 -04:00
|
|
|
#include <GCS_MAVLink.h> // MAVLink GCS definitions
|
2011-04-13 13:33:06 -03:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Configuration
|
2011-06-24 01:37:54 -03:00
|
|
|
#include "defines.h"
|
2010-12-19 12:40:33 -04:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
// Local modules
|
2011-02-17 03:09:13 -04:00
|
|
|
#include "Parameters.h"
|
2011-02-17 05:36:33 -04:00
|
|
|
#include "GCS.h"
|
2011-02-24 01:56:59 -04:00
|
|
|
#include "HIL.h"
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2010-12-19 12:40:33 -04:00
|
|
|
// Serial ports
|
2011-02-17 03:09:13 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2010-12-19 12:40:33 -04:00
|
|
|
//
|
|
|
|
// Note that FastSerial port buffers are allocated at ::begin time,
|
|
|
|
// so there is not much of a penalty to defining ports that we don't
|
|
|
|
// use.
|
2011-02-17 03:09:13 -04:00
|
|
|
//
|
2011-02-19 22:03:01 -04:00
|
|
|
FastSerialPort0(Serial); // FTDI/console
|
|
|
|
FastSerialPort1(Serial1); // GPS port
|
|
|
|
FastSerialPort3(Serial3); // Telemetry port
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Parameters
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Global parameters are all contained within the 'g' class.
|
|
|
|
//
|
|
|
|
Parameters g;
|
|
|
|
|
2011-05-09 14:40:32 -03:00
|
|
|
|
2011-03-17 22:50:46 -03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// prototypes
|
|
|
|
void update_events(void);
|
|
|
|
|
2011-05-09 14:40:32 -03:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Sensors
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// There are three basic options related to flight sensor selection.
|
|
|
|
//
|
|
|
|
// - Normal flight mode. Real sensors are used.
|
|
|
|
// - HIL Attitude mode. Most sensors are disabled, as the HIL
|
|
|
|
// protocol supplies attitude information directly.
|
|
|
|
// - HIL Sensors mode. Synthetic sensors are configured that
|
|
|
|
// supply data from the simulation.
|
|
|
|
//
|
|
|
|
|
2011-02-19 03:44:44 -04:00
|
|
|
// All GPS access should be through this pointer.
|
2011-02-19 22:03:01 -04:00
|
|
|
GPS *g_gps;
|
2011-02-17 03:09:13 -04:00
|
|
|
|
2011-05-09 09:20:22 -03:00
|
|
|
#if HIL_MODE == HIL_MODE_DISABLED
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
// real sensors
|
|
|
|
AP_ADC_ADS7844 adc;
|
|
|
|
APM_BMP085_Class barometer;
|
2011-06-03 22:22:04 -03:00
|
|
|
// MAG PROTOCOL
|
|
|
|
#if MAG_PROTOCOL == MAG_PROTOCOL_5843
|
|
|
|
AP_Compass_HMC5843 compass(Parameters::k_param_compass);
|
|
|
|
#elif MAG_PROTOCOL == MAG_PROTOCOL_5883L
|
|
|
|
AP_Compass_HMC5883L compass(Parameters::k_param_compass);
|
|
|
|
#else
|
|
|
|
#error Unrecognised MAG_PROTOCOL setting.
|
2011-06-16 14:03:26 -03:00
|
|
|
#endif
|
2011-02-25 01:33:39 -04:00
|
|
|
|
|
|
|
// real GPS selection
|
|
|
|
#if GPS_PROTOCOL == GPS_PROTOCOL_AUTO
|
|
|
|
AP_GPS_Auto g_gps_driver(&Serial1, &g_gps);
|
|
|
|
|
|
|
|
#elif GPS_PROTOCOL == GPS_PROTOCOL_NMEA
|
|
|
|
AP_GPS_NMEA g_gps_driver(&Serial1);
|
|
|
|
|
|
|
|
#elif GPS_PROTOCOL == GPS_PROTOCOL_SIRF
|
|
|
|
AP_GPS_SIRF g_gps_driver(&Serial1);
|
|
|
|
|
|
|
|
#elif GPS_PROTOCOL == GPS_PROTOCOL_UBLOX
|
|
|
|
AP_GPS_UBLOX g_gps_driver(&Serial1);
|
|
|
|
|
|
|
|
#elif GPS_PROTOCOL == GPS_PROTOCOL_MTK
|
|
|
|
AP_GPS_MTK g_gps_driver(&Serial1);
|
|
|
|
|
|
|
|
#elif GPS_PROTOCOL == GPS_PROTOCOL_MTK16
|
|
|
|
AP_GPS_MTK16 g_gps_driver(&Serial1);
|
|
|
|
|
|
|
|
#elif GPS_PROTOCOL == GPS_PROTOCOL_NONE
|
|
|
|
AP_GPS_None g_gps_driver(NULL);
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error Unrecognised GPS_PROTOCOL setting.
|
|
|
|
#endif // GPS PROTOCOL
|
2011-02-19 03:21:42 -04:00
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
#elif HIL_MODE == HIL_MODE_SENSORS
|
2011-02-25 01:33:39 -04:00
|
|
|
// sensor emulators
|
|
|
|
AP_ADC_HIL adc;
|
|
|
|
APM_BMP085_HIL_Class barometer;
|
|
|
|
AP_Compass_HIL compass;
|
|
|
|
AP_GPS_HIL g_gps_driver(NULL);
|
2011-02-24 01:56:59 -04:00
|
|
|
|
|
|
|
#elif HIL_MODE == HIL_MODE_ATTITUDE
|
2011-02-25 01:33:39 -04:00
|
|
|
AP_DCM_HIL dcm;
|
|
|
|
AP_GPS_HIL g_gps_driver(NULL);
|
|
|
|
AP_Compass_HIL compass; // never used
|
|
|
|
AP_IMU_Shim imu; // never used
|
2011-02-17 03:09:13 -04:00
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
#else
|
2011-02-25 01:33:39 -04:00
|
|
|
#error Unrecognised HIL_MODE setting.
|
2011-02-24 01:56:59 -04:00
|
|
|
#endif // HIL MODE
|
|
|
|
|
|
|
|
#if HIL_MODE != HIL_MODE_DISABLED
|
|
|
|
#if HIL_PROTOCOL == HIL_PROTOCOL_MAVLINK
|
2011-05-09 09:20:22 -03:00
|
|
|
GCS_MAVLINK hil(Parameters::k_param_streamrates_port0);
|
2011-02-24 01:56:59 -04:00
|
|
|
#elif HIL_PROTOCOL == HIL_PROTOCOL_XPLANE
|
|
|
|
HIL_XPLANE hil;
|
|
|
|
#endif // HIL PROTOCOL
|
|
|
|
#endif // HIL_MODE
|
|
|
|
|
2011-05-09 09:20:22 -03:00
|
|
|
// We may have a hil object instantiated just for mission planning
|
|
|
|
#if HIL_MODE == HIL_MODE_DISABLED && HIL_PROTOCOL == HIL_PROTOCOL_MAVLINK && HIL_PORT == 0
|
|
|
|
GCS_MAVLINK hil(Parameters::k_param_streamrates_port0);
|
|
|
|
#endif
|
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
#if HIL_MODE != HIL_MODE_ATTITUDE
|
|
|
|
#if HIL_MODE != HIL_MODE_SENSORS
|
2011-03-02 22:32:50 -04:00
|
|
|
// Normal
|
|
|
|
AP_IMU_Oilpan imu(&adc, Parameters::k_param_IMU_calibration);
|
2011-02-24 01:56:59 -04:00
|
|
|
#else
|
2011-03-02 22:32:50 -04:00
|
|
|
// hil imu
|
|
|
|
AP_IMU_Shim imu;
|
2011-02-24 01:56:59 -04:00
|
|
|
#endif
|
2011-03-02 22:32:50 -04:00
|
|
|
// normal dcm
|
|
|
|
AP_DCM dcm(&imu, g_gps);
|
2011-02-24 01:56:59 -04:00
|
|
|
#endif
|
2010-12-30 03:40:57 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// GCS selection
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
#if GCS_PROTOCOL == GCS_PROTOCOL_MAVLINK
|
2011-05-09 09:20:22 -03:00
|
|
|
GCS_MAVLINK gcs(Parameters::k_param_streamrates_port3);
|
2011-02-17 03:09:13 -04:00
|
|
|
#else
|
2011-02-25 01:33:39 -04:00
|
|
|
// If we are not using a GCS, we need a stub that does nothing.
|
|
|
|
GCS_Class gcs;
|
2011-02-17 03:09:13 -04:00
|
|
|
#endif
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-04-14 02:56:39 -03:00
|
|
|
//#include <GCS_SIMPLE.h>
|
2011-04-10 20:20:22 -03:00
|
|
|
//GCS_SIMPLE gcs_simple(&Serial);
|
2011-03-26 03:35:52 -03:00
|
|
|
|
2011-06-16 14:03:26 -03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SONAR selection
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
#if SONAR_TYPE == MAX_SONAR_XL
|
|
|
|
AP_RangeFinder_MaxsonarXL sonar;//(SONAR_PORT, &adc);
|
|
|
|
#elif SONAR_TYPE == MAX_SONAR_LV
|
|
|
|
AP_RangeFinder_MaxsonarLV sonar;//(SONAR_PORT, &adc);
|
|
|
|
#endif
|
2011-02-20 19:09:28 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Global variables
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
byte control_mode = STABILIZE;
|
|
|
|
byte oldSwitchPosition; // for remembering the control mode switch
|
|
|
|
|
|
|
|
const char *comma = ",";
|
|
|
|
|
|
|
|
const char* flight_mode_strings[] = {
|
|
|
|
"STABILIZE",
|
2011-02-19 22:03:01 -04:00
|
|
|
"ACRO",
|
2010-12-19 12:40:33 -04:00
|
|
|
"ALT_HOLD",
|
2011-03-14 03:04:07 -03:00
|
|
|
"SIMPLE",
|
2010-12-19 12:40:33 -04:00
|
|
|
"AUTO",
|
2011-03-02 22:32:50 -04:00
|
|
|
"GCS_AUTO",
|
2011-03-09 02:37:09 -04:00
|
|
|
"LOITER",
|
|
|
|
"RTL"};
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
/* Radio values
|
2011-02-17 05:36:33 -04:00
|
|
|
Channel assignments
|
2010-12-19 12:40:33 -04:00
|
|
|
1 Ailerons (rudder if no ailerons)
|
|
|
|
2 Elevator
|
|
|
|
3 Throttle
|
|
|
|
4 Rudder (if we have ailerons)
|
|
|
|
5 Mode - 3 position switch
|
2011-02-25 01:33:39 -04:00
|
|
|
6 User assignable
|
2010-12-19 12:40:33 -04:00
|
|
|
7 trainer switch - sets throttle nominal (toggle switch), sets accels to Level (hold > 1 second)
|
|
|
|
8 TBD
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Radio
|
|
|
|
// -----
|
2011-03-11 17:37:37 -04:00
|
|
|
int motor_out[8];
|
2011-01-25 01:53:36 -04:00
|
|
|
Vector3f omega;
|
|
|
|
|
2011-06-12 09:14:10 -03:00
|
|
|
// Heli
|
|
|
|
// ----
|
|
|
|
float heli_rollFactor[3], heli_pitchFactor[3]; // only required for 3 swashplate servos
|
|
|
|
int heli_servo_min[3], heli_servo_max[3]; // same here. for yaw servo we use heli_servo4_min/max parameter directly
|
|
|
|
int heli_servo_out[4];
|
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
// Failsafe
|
|
|
|
// --------
|
|
|
|
boolean failsafe; // did our throttle dip below the failsafe value?
|
|
|
|
boolean ch3_failsafe;
|
|
|
|
boolean motor_armed;
|
2011-05-14 23:02:09 -03:00
|
|
|
boolean motor_auto_armed; // if true,
|
2011-02-25 01:33:39 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
// PIDs
|
2011-02-25 01:33:39 -04:00
|
|
|
// ----
|
2011-05-18 20:38:24 -03:00
|
|
|
//int max_stabilize_dampener; //
|
|
|
|
//int max_yaw_dampener; //
|
2011-02-17 03:09:13 -04:00
|
|
|
boolean rate_yaw_flag; // used to transition yaw control from Rate control to Yaw hold
|
2011-04-16 17:42:00 -03:00
|
|
|
byte yaw_debug;
|
2011-04-17 02:17:42 -03:00
|
|
|
bool did_clear_yaw_control;
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
// LED output
|
|
|
|
// ----------
|
|
|
|
boolean motor_light; // status of the Motor safety
|
|
|
|
boolean GPS_light; // status of the GPS light
|
2011-04-02 16:56:35 -03:00
|
|
|
boolean timer_light; // status of the Motor safety
|
2011-06-16 14:03:26 -03:00
|
|
|
byte led_mode = NORMAL_LEDS;
|
2011-01-22 22:36:22 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// GPS variables
|
|
|
|
// -------------
|
|
|
|
const float t7 = 10000000.0; // used to scale GPS values for EEPROM storage
|
2011-01-11 17:22:54 -04:00
|
|
|
float scaleLongUp = 1; // used to reverse longtitude scaling
|
|
|
|
float scaleLongDown = 1; // used to reverse longtitude scaling
|
2011-04-21 20:07:31 -03:00
|
|
|
byte ground_start_count = 10; // have we achieved first lock and set Home?
|
2011-02-17 03:09:13 -04:00
|
|
|
|
2011-02-17 05:36:33 -04:00
|
|
|
// Location & Navigation
|
2010-12-19 12:40:33 -04:00
|
|
|
// ---------------------
|
2011-02-25 01:33:39 -04:00
|
|
|
const float radius_of_earth = 6378100; // meters
|
|
|
|
const float gravity = 9.81; // meters/ sec^2
|
2010-12-19 12:40:33 -04:00
|
|
|
long nav_bearing; // deg * 100 : 0 to 360 current desired bearing to navigate
|
2011-02-25 01:33:39 -04:00
|
|
|
long target_bearing; // deg * 100 : 0 to 360 location of the plane to the target
|
|
|
|
long crosstrack_bearing; // deg * 100 : 0 to 360 desired angle of plane to target
|
|
|
|
int climb_rate; // m/s * 100 - For future implementation of controlled ascent/descent by rate
|
|
|
|
float nav_gain_scaler = 1; // Gain scaling for headwind/tailwind TODO: why does this variable need to be initialized to 1?
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
int last_ground_speed; // used to dampen navigation
|
2011-04-26 02:15:25 -03:00
|
|
|
byte wp_control; // used to control - navgation or loiter
|
2011-04-25 02:12:59 -03:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
byte command_must_index; // current command memory location
|
|
|
|
byte command_may_index; // current command memory location
|
|
|
|
byte command_must_ID; // current command ID
|
|
|
|
byte command_may_ID; // current command ID
|
2011-04-18 13:27:03 -03:00
|
|
|
byte wp_verify_byte; // used for tracking state of navigating waypoints
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
float cos_roll_x = 1;
|
|
|
|
float cos_pitch_x = 1;
|
|
|
|
float cos_yaw_x = 1;
|
|
|
|
float sin_pitch_y, sin_yaw_y, sin_roll_y;
|
2011-05-23 02:53:00 -03:00
|
|
|
float sin_nav_y, cos_nav_x; // used in calc_nav_output XXX move to local funciton
|
2011-04-03 01:44:00 -03:00
|
|
|
bool simple_bearing_is_set = false;
|
|
|
|
long initial_simple_bearing; // used for Simple mode
|
2011-02-10 03:09:32 -04:00
|
|
|
|
2011-04-17 20:12:27 -03:00
|
|
|
float Y6_scaling = Y6_MOTOR_SCALER;
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Airspeed
|
|
|
|
// --------
|
2011-02-25 01:33:39 -04:00
|
|
|
int airspeed; // m/s * 100
|
2011-01-25 01:53:36 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Location Errors
|
|
|
|
// ---------------
|
2011-02-25 01:33:39 -04:00
|
|
|
long bearing_error; // deg * 100 : 0 to 36000
|
|
|
|
long altitude_error; // meters * 100 we are off in altitude
|
2010-12-19 12:40:33 -04:00
|
|
|
float crosstrack_error; // meters we are off trackline
|
|
|
|
long distance_error; // distance to the WP
|
|
|
|
long yaw_error; // how off are we pointed
|
2011-02-25 01:33:39 -04:00
|
|
|
long long_error, lat_error; // temp for debugging
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
// Battery Sensors
|
|
|
|
// ---------------
|
2010-12-19 12:40:33 -04:00
|
|
|
float battery_voltage = LOW_VOLTAGE * 1.05; // Battery Voltage of total battery, initialized above threshold for filter
|
|
|
|
float battery_voltage1 = LOW_VOLTAGE * 1.05; // Battery Voltage of cell 1, initialized above threshold for filter
|
|
|
|
float battery_voltage2 = LOW_VOLTAGE * 1.05; // Battery Voltage of cells 1 + 2, initialized above threshold for filter
|
|
|
|
float battery_voltage3 = LOW_VOLTAGE * 1.05; // Battery Voltage of cells 1 + 2+3, initialized above threshold for filter
|
|
|
|
float battery_voltage4 = LOW_VOLTAGE * 1.05; // Battery Voltage of cells 1 + 2+3 + 4, initialized above threshold for filter
|
|
|
|
|
2011-01-16 23:44:12 -04:00
|
|
|
float current_amps;
|
2011-01-17 22:48:44 -04:00
|
|
|
float current_total;
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
// Airspeed Sensors
|
|
|
|
// ----------------
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
// Barometer Sensor variables
|
|
|
|
// --------------------------
|
2011-06-16 14:03:26 -03:00
|
|
|
long abs_pressure;
|
|
|
|
long ground_pressure;
|
2010-12-19 12:40:33 -04:00
|
|
|
int ground_temperature;
|
2011-06-16 14:03:26 -03:00
|
|
|
int32_t baro_filter[BARO_FILTER_SIZE];
|
|
|
|
byte baro_filter_index;
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
// Altitude Sensor variables
|
2011-02-20 19:09:28 -04:00
|
|
|
// ----------------------
|
2011-04-21 20:07:31 -03:00
|
|
|
int sonar_alt;
|
|
|
|
int baro_alt;
|
2011-04-28 02:43:35 -03:00
|
|
|
int baro_alt_offset;
|
2011-02-25 01:33:39 -04:00
|
|
|
byte altitude_sensor = BARO; // used to know which sensor is active, BARO or SONAR
|
2011-01-09 22:15:24 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// flight mode specific
|
|
|
|
// --------------------
|
2011-02-25 01:33:39 -04:00
|
|
|
boolean takeoff_complete; // Flag for using take-off controls
|
|
|
|
boolean land_complete;
|
2011-03-02 22:32:50 -04:00
|
|
|
//int takeoff_altitude;
|
2011-02-25 01:33:39 -04:00
|
|
|
int landing_distance; // meters;
|
2011-02-17 03:09:13 -04:00
|
|
|
long old_alt; // used for managing altitude rates
|
2011-02-17 05:36:33 -04:00
|
|
|
int velocity_land;
|
2011-06-16 14:03:26 -03:00
|
|
|
byte yaw_tracking = MAV_ROI_WPNEXT; // no tracking, point at next wp, or at a target
|
|
|
|
byte brake_timer;
|
2011-03-09 02:37:09 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Loiter management
|
|
|
|
// -----------------
|
2011-04-25 02:12:59 -03:00
|
|
|
long saved_target_bearing; // deg * 100
|
2011-06-16 14:03:26 -03:00
|
|
|
unsigned long loiter_time; // millis : when we started LOITER mode
|
|
|
|
unsigned long loiter_time_max; // millis : how long to stay in LOITER mode
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
// these are the values for navigation control functions
|
|
|
|
// ----------------------------------------------------
|
2011-02-25 01:33:39 -04:00
|
|
|
long nav_roll; // deg * 100 : target roll angle
|
|
|
|
long nav_pitch; // deg * 100 : target pitch angle
|
|
|
|
long nav_yaw; // deg * 100 : target yaw angle
|
|
|
|
long nav_lat; // for error calcs
|
|
|
|
long nav_lon; // for error calcs
|
|
|
|
int nav_throttle; // 0-1000 for throttle control
|
|
|
|
int nav_throttle_old; // for filtering
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-04-30 13:01:57 -03:00
|
|
|
long throttle_integrator; // used to control when we calculate nav_throttle
|
2011-04-25 02:12:59 -03:00
|
|
|
bool invalid_throttle; // used to control when we calculate nav_throttle
|
|
|
|
bool invalid_nav; // used to control when we calculate nav_throttle
|
2011-04-08 16:13:31 -03:00
|
|
|
bool set_throttle_cruise_flag = false; // used to track the throttle crouse value
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
long command_yaw_start; // what angle were we to begin with
|
2011-06-16 14:03:26 -03:00
|
|
|
unsigned long command_yaw_start_time; // when did we start turning
|
|
|
|
unsigned int command_yaw_time; // how long we are turning
|
2010-12-19 12:40:33 -04:00
|
|
|
long command_yaw_end; // what angle are we trying to be
|
|
|
|
long command_yaw_delta; // how many degrees will we turn
|
|
|
|
int command_yaw_speed; // how fast to turn
|
|
|
|
byte command_yaw_dir;
|
2011-03-26 03:35:52 -03:00
|
|
|
byte command_yaw_relative;
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-06-16 14:03:26 -03:00
|
|
|
int auto_level_counter;
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Waypoints
|
|
|
|
// ---------
|
2011-02-25 01:33:39 -04:00
|
|
|
long wp_distance; // meters - distance between plane and next waypoint
|
|
|
|
long wp_totalDistance; // meters - distance between old and next waypoint
|
|
|
|
byte next_wp_index; // Current active command index
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
// repeating event control
|
|
|
|
// -----------------------
|
|
|
|
byte event_id; // what to do - see defines
|
2011-06-16 14:03:26 -03:00
|
|
|
unsigned long event_timer; // when the event was asked for in ms
|
|
|
|
unsigned int event_delay; // how long to delay the next firing of event in millis
|
2010-12-19 12:40:33 -04:00
|
|
|
int event_repeat; // how many times to fire : 0 = forever, 1 = do once, 2 = do twice
|
|
|
|
int event_value; // per command value, such as PWM for servos
|
|
|
|
int event_undo_value; // the value used to undo commands
|
2011-02-17 05:36:33 -04:00
|
|
|
byte repeat_forever;
|
2010-12-19 12:40:33 -04:00
|
|
|
byte undo_event; // counter for timing the undo
|
|
|
|
|
|
|
|
// delay command
|
|
|
|
// --------------
|
2011-03-13 03:25:38 -03:00
|
|
|
long condition_value; // used in condition commands (eg delay, change alt, etc.)
|
|
|
|
long condition_start;
|
|
|
|
int condition_rate;
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-04-20 02:37:05 -03:00
|
|
|
// land command
|
|
|
|
// ------------
|
|
|
|
long land_start; // when we intiated command in millis()
|
|
|
|
long original_alt; // altitide reference for start of command
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// 3D Location vectors
|
|
|
|
// -------------------
|
2011-01-09 22:15:24 -04:00
|
|
|
struct Location home; // home location
|
|
|
|
struct Location prev_WP; // last waypoint
|
|
|
|
struct Location current_loc; // current location
|
|
|
|
struct Location next_WP; // next waypoint
|
2011-03-27 18:26:34 -03:00
|
|
|
struct Location target_WP; // where do we want to you towards?
|
2011-05-29 01:02:01 -03:00
|
|
|
struct Location simple_WP; //
|
2011-01-09 22:15:24 -04:00
|
|
|
struct Location next_command; // command preloaded
|
2011-02-17 05:36:33 -04:00
|
|
|
long target_altitude; // used for
|
|
|
|
boolean home_is_set; // Flag for if we have g_gps lock and have set the home location
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
// IMU variables
|
|
|
|
// -------------
|
2011-02-25 01:33:39 -04:00
|
|
|
float G_Dt = 0.02; // Integration time for the gyros (DCM algorithm)
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
// Performance monitoring
|
|
|
|
// ----------------------
|
|
|
|
long perf_mon_timer;
|
2011-02-25 01:33:39 -04:00
|
|
|
float imu_health; // Metric based on accel gain deweighting
|
|
|
|
int G_Dt_max; // Max main loop cycle time in milliseconds
|
2010-12-19 12:40:33 -04:00
|
|
|
int gps_fix_count;
|
|
|
|
byte gcs_messages_sent;
|
|
|
|
|
|
|
|
|
|
|
|
// GCS
|
|
|
|
// ---
|
|
|
|
char GCS_buffer[53];
|
2011-02-25 01:33:39 -04:00
|
|
|
char display_PID = -1; // Flag used by DebugTerminal to indicate that the next PID calculation with this index should be displayed
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
// System Timers
|
|
|
|
// --------------
|
|
|
|
unsigned long fast_loopTimer; // Time in miliseconds of main control loop
|
|
|
|
unsigned long fast_loopTimeStamp; // Time Stamp when fast loop was complete
|
2011-02-24 01:56:59 -04:00
|
|
|
uint8_t delta_ms_fast_loop; // Delta Time in miliseconds
|
2010-12-19 12:40:33 -04:00
|
|
|
int mainLoop_count;
|
|
|
|
|
|
|
|
unsigned long medium_loopTimer; // Time in miliseconds of navigation control loop
|
|
|
|
byte medium_loopCounter; // Counters for branching from main control loop to slower loops
|
2011-02-24 01:56:59 -04:00
|
|
|
uint8_t delta_ms_medium_loop;
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-05-09 21:00:05 -03:00
|
|
|
unsigned long fiftyhz_loopTimer;
|
|
|
|
uint8_t delta_ms_fiftyhz;
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
byte slow_loopCounter;
|
2011-03-16 02:43:48 -03:00
|
|
|
int superslow_loopCounter;
|
2011-03-30 02:27:13 -03:00
|
|
|
byte flight_timer; // for limiting the execution of flight mode thingys
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-01-17 22:48:44 -04:00
|
|
|
|
2011-05-16 01:59:06 -03:00
|
|
|
unsigned long dTnav; // Delta Time in milliseconds for navigation computations
|
2011-05-27 15:21:55 -03:00
|
|
|
unsigned long nav_loopTimer; // used to track the elapsed ime for GPS nav
|
2010-12-19 12:40:33 -04:00
|
|
|
unsigned long elapsedTime; // for doing custom events
|
|
|
|
float load; // % MCU cycles used
|
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
byte counter_one_herz;
|
2011-04-02 16:56:35 -03:00
|
|
|
bool GPS_enabled = false;
|
2011-06-16 14:03:26 -03:00
|
|
|
byte loop_step;
|
2011-03-09 02:37:09 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Top-level logic
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2010-12-23 10:05:59 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
void setup() {
|
|
|
|
init_ardupilot();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2011-05-02 01:34:15 -03:00
|
|
|
// We want this to execute fast
|
|
|
|
// ----------------------------
|
2011-05-13 16:29:45 -03:00
|
|
|
if (millis() - fast_loopTimer >= 5) {
|
2011-01-17 22:48:44 -04:00
|
|
|
delta_ms_fast_loop = millis() - fast_loopTimer;
|
2010-12-19 12:40:33 -04:00
|
|
|
fast_loopTimer = millis();
|
2011-01-17 22:48:44 -04:00
|
|
|
load = float(fast_loopTimeStamp - fast_loopTimer) / delta_ms_fast_loop;
|
|
|
|
G_Dt = (float)delta_ms_fast_loop / 1000.f; // used by DCM integrator
|
2010-12-19 12:40:33 -04:00
|
|
|
mainLoop_count++;
|
|
|
|
|
2011-06-16 14:03:26 -03:00
|
|
|
//if (delta_ms_fast_loop > 6)
|
|
|
|
// Log_Write_Performance();
|
|
|
|
|
2011-04-02 16:56:35 -03:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Execute the fast loop
|
|
|
|
// ---------------------
|
|
|
|
fast_loop();
|
|
|
|
fast_loopTimeStamp = millis();
|
|
|
|
}
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-05-09 21:00:05 -03:00
|
|
|
if (millis() - fiftyhz_loopTimer > 19) {
|
|
|
|
delta_ms_fiftyhz = millis() - fiftyhz_loopTimer;
|
|
|
|
fiftyhz_loopTimer = millis();
|
2011-02-25 01:33:39 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
medium_loop();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-05-09 21:00:05 -03:00
|
|
|
fifty_hz_loop();
|
2011-02-25 01:33:39 -04:00
|
|
|
counter_one_herz++;
|
2011-06-16 14:03:26 -03:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
if(counter_one_herz == 50){
|
|
|
|
super_slow_loop();
|
2011-03-20 09:55:51 -03:00
|
|
|
counter_one_herz = 0;
|
2011-02-25 01:33:39 -04:00
|
|
|
}
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
if (millis() - perf_mon_timer > 20000) {
|
2011-02-17 03:09:13 -04:00
|
|
|
if (mainLoop_count != 0) {
|
2011-05-09 01:16:58 -03:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
gcs.send_message(MSG_PERF_REPORT);
|
2011-05-09 01:16:58 -03:00
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
if (g.log_bitmask & MASK_LOG_PM)
|
2011-02-17 03:09:13 -04:00
|
|
|
Log_Write_Performance();
|
2011-02-24 01:56:59 -04:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
resetPerfData();
|
|
|
|
}
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-16 14:03:26 -03:00
|
|
|
// Main loop
|
2010-12-19 12:40:33 -04:00
|
|
|
void fast_loop()
|
|
|
|
{
|
|
|
|
// IMU DCM Algorithm
|
|
|
|
read_AHRS();
|
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
// This is the fast loop - we want it to execute at >= 100Hz
|
|
|
|
// ---------------------------------------------------------
|
2011-02-17 05:36:33 -04:00
|
|
|
if (delta_ms_fast_loop > G_Dt_max)
|
2011-01-17 22:48:44 -04:00
|
|
|
G_Dt_max = delta_ms_fast_loop;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-05-26 02:58:17 -03:00
|
|
|
// Read radio
|
|
|
|
// ----------
|
2011-06-16 14:03:26 -03:00
|
|
|
read_radio(); // read the radio first
|
2011-05-26 02:58:17 -03:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// custom code/exceptions for flight modes
|
|
|
|
// ---------------------------------------
|
|
|
|
update_current_flight_mode();
|
|
|
|
|
|
|
|
// write out the servo PWM values
|
|
|
|
// ------------------------------
|
|
|
|
set_servos_4();
|
2011-03-06 05:36:03 -04:00
|
|
|
|
2011-04-30 13:01:57 -03:00
|
|
|
// record throttle output
|
|
|
|
// ------------------------------
|
2011-05-13 16:29:45 -03:00
|
|
|
//throttle_integrator += g.rc_3.servo_out;
|
2011-04-30 13:01:57 -03:00
|
|
|
|
2011-05-10 07:26:32 -03:00
|
|
|
#if HIL_PROTOCOL == HIL_PROTOCOL_MAVLINK && HIL_MODE != HIL_MODE_DISABLED
|
2011-05-09 21:00:05 -03:00
|
|
|
// HIL for a copter needs very fast update of the servo values
|
2011-05-11 08:55:13 -03:00
|
|
|
hil.send_message(MSG_RADIO_OUT);
|
2011-05-09 21:00:05 -03:00
|
|
|
#endif
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void medium_loop()
|
|
|
|
{
|
|
|
|
|
2011-02-13 18:32:34 -04:00
|
|
|
// reads all of the necessary trig functions for cameras, throttle, etc.
|
|
|
|
update_trig();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// This is the start of the medium (10 Hz) loop pieces
|
|
|
|
// -----------------------------------------
|
|
|
|
switch(medium_loopCounter) {
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-03-15 02:54:48 -03:00
|
|
|
// This case deals with the GPS and Compass
|
|
|
|
//-----------------------------------------
|
2010-12-19 12:40:33 -04:00
|
|
|
case 0:
|
2011-06-16 14:03:26 -03:00
|
|
|
loop_step = 1;
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
medium_loopCounter++;
|
2011-03-09 02:37:09 -04:00
|
|
|
|
2011-04-02 16:56:35 -03:00
|
|
|
if(GPS_enabled){
|
2011-03-28 02:18:10 -03:00
|
|
|
update_GPS();
|
|
|
|
}
|
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
//readCommands();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-05-09 21:00:05 -03:00
|
|
|
#if HIL_MODE != HIL_MODE_ATTITUDE
|
|
|
|
if(g.compass_enabled){
|
|
|
|
compass.read(); // Read magnetometer
|
|
|
|
compass.calculate(dcm.get_dcm_matrix()); // Calculate heading
|
|
|
|
//compass.calculate(dcm.roll, dcm.pitch); // Calculate heading
|
|
|
|
compass.null_offsets(dcm.get_dcm_matrix());
|
|
|
|
}
|
|
|
|
#endif
|
2011-06-16 14:03:26 -03:00
|
|
|
|
|
|
|
// auto_trim, uses an auto_level algorithm
|
|
|
|
auto_trim();
|
2010-12-19 12:40:33 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
// This case performs some navigation computations
|
|
|
|
//------------------------------------------------
|
|
|
|
case 1:
|
2011-06-16 14:03:26 -03:00
|
|
|
loop_step = 2;
|
2010-12-19 12:40:33 -04:00
|
|
|
medium_loopCounter++;
|
|
|
|
|
2011-03-14 03:04:07 -03:00
|
|
|
// hack to stop navigation in Simple mode
|
2011-06-03 02:36:39 -03:00
|
|
|
if (control_mode == SIMPLE){
|
|
|
|
// clear GPS data
|
|
|
|
g_gps->new_data = false;
|
2011-03-14 03:04:07 -03:00
|
|
|
break;
|
2011-06-03 02:36:39 -03:00
|
|
|
}
|
2011-03-14 03:04:07 -03:00
|
|
|
|
2011-03-15 02:54:48 -03:00
|
|
|
// Auto control modes:
|
2011-06-16 14:03:26 -03:00
|
|
|
if(g_gps->new_data && g_gps->fix){
|
|
|
|
loop_step = 11;
|
|
|
|
|
|
|
|
// invalidate GPS data
|
2011-02-24 01:56:59 -04:00
|
|
|
g_gps->new_data = false;
|
2011-01-11 17:14:57 -04:00
|
|
|
|
2011-03-15 02:54:48 -03:00
|
|
|
// we are not tracking I term on navigation, so this isn't needed
|
2011-05-16 01:59:06 -03:00
|
|
|
dTnav = millis() - nav_loopTimer;
|
|
|
|
nav_loopTimer = millis();
|
2011-03-15 02:54:48 -03:00
|
|
|
|
|
|
|
// calculate the copter's desired bearing and WP distance
|
|
|
|
// ------------------------------------------------------
|
2011-01-11 17:14:57 -04:00
|
|
|
navigate();
|
2011-04-03 18:11:14 -03:00
|
|
|
|
2011-05-15 00:00:05 -03:00
|
|
|
// control mode specific updates to nav_bearing
|
|
|
|
// --------------------------------------------
|
|
|
|
update_navigation();
|
2011-05-31 02:29:06 -03:00
|
|
|
|
|
|
|
if (g.log_bitmask & MASK_LOG_NTUN)
|
|
|
|
Log_Write_Nav_Tuning();
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-05-14 23:02:09 -03:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
// command processing
|
|
|
|
//-------------------
|
|
|
|
case 2:
|
2011-06-16 14:03:26 -03:00
|
|
|
loop_step = 3;
|
2010-12-19 12:40:33 -04:00
|
|
|
medium_loopCounter++;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-02-21 00:30:56 -04:00
|
|
|
// Read altitude from sensors
|
2011-03-15 02:54:48 -03:00
|
|
|
// --------------------------
|
2011-02-21 00:30:56 -04:00
|
|
|
update_alt();
|
2011-02-20 19:09:28 -04:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// altitude smoothing
|
|
|
|
// ------------------
|
|
|
|
//calc_altitude_smoothing_error();
|
|
|
|
|
|
|
|
calc_altitude_error();
|
|
|
|
|
|
|
|
// invalidate the throttle hold value
|
|
|
|
// ----------------------------------
|
|
|
|
invalid_throttle = true;
|
2011-06-16 14:03:26 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
// This case deals with sending high rate telemetry
|
|
|
|
//-------------------------------------------------
|
|
|
|
case 3:
|
|
|
|
loop_step = 4;
|
|
|
|
medium_loopCounter++;
|
2011-04-25 02:12:59 -03:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// perform next command
|
|
|
|
// --------------------
|
2011-05-14 23:02:09 -03:00
|
|
|
if(control_mode == AUTO){
|
|
|
|
//if(home_is_set){
|
2011-04-25 02:12:59 -03:00
|
|
|
update_commands();
|
2011-05-14 23:02:09 -03:00
|
|
|
//}
|
2011-03-02 22:32:50 -04:00
|
|
|
}
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-03-15 02:54:48 -03:00
|
|
|
#if HIL_MODE != HIL_MODE_ATTITUDE
|
2011-05-31 02:29:06 -03:00
|
|
|
if (g.log_bitmask & MASK_LOG_ATTITUDE_MED)
|
2011-05-09 21:00:05 -03:00
|
|
|
Log_Write_Attitude();
|
|
|
|
|
|
|
|
if (g.log_bitmask & MASK_LOG_CTUN)
|
|
|
|
Log_Write_Control_Tuning();
|
2011-03-15 02:54:48 -03:00
|
|
|
#endif
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-03-15 02:54:48 -03:00
|
|
|
|
2011-03-20 09:55:51 -03:00
|
|
|
// XXX this should be a "GCS medium loop" interface
|
|
|
|
#if GCS_PROTOCOL == GCS_PROTOCOL_MAVLINK
|
|
|
|
gcs.data_stream_send(5,45);
|
|
|
|
// send all requested output streams with rates requested
|
|
|
|
// between 5 and 45 Hz
|
|
|
|
#else
|
|
|
|
gcs.send_message(MSG_ATTITUDE); // Sends attitude data
|
|
|
|
#endif
|
2011-05-09 21:00:05 -03:00
|
|
|
|
|
|
|
#if HIL_PROTOCOL == HIL_PROTOCOL_MAVLINK && (HIL_MODE != HIL_MODE_DISABLED || HIL_PORT == 0)
|
|
|
|
hil.data_stream_send(5,45);
|
|
|
|
#endif
|
2011-06-16 14:03:26 -03:00
|
|
|
|
|
|
|
if (g.log_bitmask & MASK_LOG_MOTORS)
|
|
|
|
Log_Write_Motors();
|
|
|
|
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
break;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// This case controls the slow loop
|
|
|
|
//---------------------------------
|
|
|
|
case 4:
|
2011-06-16 14:03:26 -03:00
|
|
|
loop_step = 5;
|
2011-03-15 02:54:48 -03:00
|
|
|
medium_loopCounter = 0;
|
2011-05-27 15:21:55 -03:00
|
|
|
|
2011-05-09 21:00:05 -03:00
|
|
|
delta_ms_medium_loop = millis() - medium_loopTimer;
|
|
|
|
medium_loopTimer = millis();
|
2011-03-15 02:54:48 -03:00
|
|
|
|
2011-05-09 12:46:56 -03:00
|
|
|
if (g.battery_monitoring != 0){
|
|
|
|
read_battery();
|
2011-01-16 23:44:12 -04:00
|
|
|
}
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-03-15 02:54:48 -03:00
|
|
|
// Accel trims = hold > 2 seconds
|
|
|
|
// Throttle cruise = switch less than 1 second
|
|
|
|
// --------------------------------------------
|
2010-12-19 12:40:33 -04:00
|
|
|
read_trim_switch();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-03-15 02:54:48 -03:00
|
|
|
// Check for engine arming
|
|
|
|
// -----------------------
|
2010-12-19 12:40:33 -04:00
|
|
|
arm_motors();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-06-16 14:03:26 -03:00
|
|
|
// should we be in DCM Fast recovery?
|
|
|
|
// ----------------------------------
|
|
|
|
check_DCM();
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
slow_loop();
|
|
|
|
break;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
default:
|
2011-03-15 02:54:48 -03:00
|
|
|
// this is just a catch all
|
|
|
|
// ------------------------
|
2010-12-19 12:40:33 -04:00
|
|
|
medium_loopCounter = 0;
|
|
|
|
break;
|
|
|
|
}
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-05-09 21:00:05 -03:00
|
|
|
}
|
2011-04-17 20:08:16 -03:00
|
|
|
|
2011-05-09 21:00:05 -03:00
|
|
|
// stuff that happens at 50 hz
|
|
|
|
// ---------------------------
|
|
|
|
void fifty_hz_loop()
|
|
|
|
{
|
2010-12-19 12:40:33 -04:00
|
|
|
// use Yaw to find our bearing error
|
|
|
|
calc_bearing_error();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// guess how close we are - fixed observer calc
|
2011-02-24 01:56:59 -04:00
|
|
|
//calc_distance_error();
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-05-09 21:00:05 -03:00
|
|
|
# if HIL_MODE == HIL_MODE_DISABLED
|
|
|
|
if (g.log_bitmask & MASK_LOG_ATTITUDE_FAST)
|
|
|
|
Log_Write_Attitude();
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-03-15 02:54:48 -03:00
|
|
|
if (g.log_bitmask & MASK_LOG_RAW)
|
|
|
|
Log_Write_Raw();
|
|
|
|
#endif
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-05-31 02:29:06 -03:00
|
|
|
#if CAMERA_STABILIZER == ENABLED
|
|
|
|
camera_stabilization();
|
|
|
|
#endif
|
2011-05-09 21:00:05 -03:00
|
|
|
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-05-09 21:00:05 -03:00
|
|
|
// XXX is it appropriate to be doing the comms below on the fast loop?
|
|
|
|
|
|
|
|
#if HIL_MODE != HIL_MODE_DISABLED && HIL_PORT != GCS_PORT
|
|
|
|
// kick the HIL to process incoming sensor packets
|
|
|
|
hil.update();
|
|
|
|
|
|
|
|
#if HIL_PROTOCOL == HIL_PROTOCOL_MAVLINK
|
|
|
|
hil.data_stream_send(45,1000);
|
|
|
|
#else
|
|
|
|
hil.send_message(MSG_SERVO_OUT);
|
|
|
|
#endif
|
|
|
|
#elif HIL_PROTOCOL == HIL_PROTOCOL_MAVLINK && HIL_MODE == HIL_MODE_DISABLED && HIL_PORT == 0
|
|
|
|
// Case for hil object on port 0 just for mission planning
|
|
|
|
hil.update();
|
|
|
|
hil.data_stream_send(45,1000);
|
2010-12-27 19:09:08 -04:00
|
|
|
#endif
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-05-09 21:00:05 -03:00
|
|
|
// kick the GCS to process uplink data
|
|
|
|
gcs.update();
|
2011-05-13 16:29:45 -03:00
|
|
|
|
2011-03-20 09:55:51 -03:00
|
|
|
#if GCS_PROTOCOL == GCS_PROTOCOL_MAVLINK
|
|
|
|
gcs.data_stream_send(45,1000);
|
|
|
|
#endif
|
2011-05-09 21:00:05 -03:00
|
|
|
// XXX this should be absorbed into the above,
|
|
|
|
// or be a "GCS fast loop" interface
|
2011-05-14 01:45:28 -03:00
|
|
|
|
2011-06-16 14:03:26 -03:00
|
|
|
#if FRAME_CONFIG == TRI_FRAME
|
2011-05-14 23:02:09 -03:00
|
|
|
// Hack - had to move to 50hz loop to test a theory
|
2011-05-14 01:45:28 -03:00
|
|
|
// servo Yaw
|
2011-06-16 14:03:26 -03:00
|
|
|
g.rc_4.calc_pwm();
|
2011-05-14 01:45:28 -03:00
|
|
|
APM_RC.OutputCh(CH_7, g.rc_4.radio_out);
|
2011-05-14 23:02:09 -03:00
|
|
|
#endif
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
|
2011-05-09 21:00:05 -03:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
void slow_loop()
|
|
|
|
{
|
|
|
|
// This is the slow (3 1/3 Hz) loop pieces
|
|
|
|
//----------------------------------------
|
|
|
|
switch (slow_loopCounter){
|
|
|
|
case 0:
|
2011-06-16 14:03:26 -03:00
|
|
|
loop_step = 6;
|
2010-12-19 12:40:33 -04:00
|
|
|
slow_loopCounter++;
|
|
|
|
superslow_loopCounter++;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-05-25 02:48:33 -03:00
|
|
|
if(superslow_loopCounter > 800){ // every 4 minutes
|
2011-02-24 01:56:59 -04:00
|
|
|
#if HIL_MODE != HIL_MODE_ATTITUDE
|
2011-03-16 02:43:48 -03:00
|
|
|
if(g.rc_3.control_in == 0 && g.compass_enabled){
|
2011-02-24 01:56:59 -04:00
|
|
|
compass.save_offsets();
|
2011-03-16 02:43:48 -03:00
|
|
|
superslow_loopCounter = 0;
|
2011-02-24 01:56:59 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
break;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
case 1:
|
2011-06-16 14:03:26 -03:00
|
|
|
loop_step = 7;
|
2010-12-19 12:40:33 -04:00
|
|
|
slow_loopCounter++;
|
2011-02-17 03:09:13 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Read 3-position switch on radio
|
|
|
|
// -------------------------------
|
2011-02-17 05:36:33 -04:00
|
|
|
read_control_switch();
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Read main battery voltage if hooked up - does not read the 5v from radio
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
#if BATTERY_EVENT == 1
|
|
|
|
read_battery();
|
|
|
|
#endif
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-06-01 02:50:17 -03:00
|
|
|
#if AUTO_RESET_LOITER == 1
|
|
|
|
if(control_mode == LOITER){
|
|
|
|
if((abs(g.rc_2.control_in) + abs(g.rc_1.control_in)) > 500){
|
|
|
|
// reset LOITER to current position
|
|
|
|
long temp = next_WP.alt;
|
|
|
|
next_WP = current_loc;
|
|
|
|
next_WP.alt = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
break;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
case 2:
|
2011-06-16 14:03:26 -03:00
|
|
|
loop_step = 8;
|
2010-12-19 12:40:33 -04:00
|
|
|
slow_loopCounter = 0;
|
|
|
|
update_events();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-03-15 02:54:48 -03:00
|
|
|
// blink if we are armed
|
2011-06-16 14:03:26 -03:00
|
|
|
update_lights();
|
2011-03-15 02:54:48 -03:00
|
|
|
|
2011-02-17 03:09:13 -04:00
|
|
|
// XXX this should be a "GCS slow loop" interface
|
|
|
|
#if GCS_PROTOCOL == GCS_PROTOCOL_MAVLINK
|
|
|
|
gcs.data_stream_send(1,5);
|
|
|
|
// send all requested output streams with rates requested
|
|
|
|
// between 1 and 5 Hz
|
|
|
|
#else
|
|
|
|
gcs.send_message(MSG_LOCATION);
|
2011-05-09 21:00:05 -03:00
|
|
|
gcs.send_message(MSG_CPU_LOAD, load*100);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HIL_PROTOCOL == HIL_PROTOCOL_MAVLINK && (HIL_MODE != HIL_MODE_DISABLED || HIL_PORT == 0)
|
|
|
|
hil.data_stream_send(1,5);
|
2011-02-17 03:09:13 -04:00
|
|
|
#endif
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-04-17 20:08:16 -03:00
|
|
|
#if CHANNEL_6_TUNING != CH6_NONE
|
|
|
|
tuning();
|
|
|
|
#endif
|
2011-01-18 02:12:11 -04:00
|
|
|
|
2011-04-28 02:43:35 -03:00
|
|
|
|
|
|
|
// filter out the baro offset.
|
2011-06-16 14:03:26 -03:00
|
|
|
//if(baro_alt_offset > 0) baro_alt_offset--;
|
|
|
|
//if(baro_alt_offset < 0) baro_alt_offset++;
|
2011-04-28 02:43:35 -03:00
|
|
|
|
2011-05-18 20:38:24 -03:00
|
|
|
|
|
|
|
#if MOTOR_LEDS == 1
|
2011-05-23 23:14:18 -03:00
|
|
|
update_motor_leds();
|
2011-05-18 20:38:24 -03:00
|
|
|
#endif
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
slow_loopCounter = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
// 1Hz loop
|
2011-02-25 01:33:39 -04:00
|
|
|
void super_slow_loop()
|
|
|
|
{
|
2011-06-16 14:03:26 -03:00
|
|
|
loop_step = 9;
|
2011-04-13 13:33:06 -03:00
|
|
|
if (g.log_bitmask & MASK_LOG_CURRENT)
|
2011-02-25 01:33:39 -04:00
|
|
|
Log_Write_Current();
|
2011-03-09 02:37:09 -04:00
|
|
|
|
|
|
|
gcs.send_message(MSG_HEARTBEAT); // XXX This is running at 3 1/3 Hz instead of 1 Hz
|
2011-05-09 21:00:05 -03:00
|
|
|
|
|
|
|
#if HIL_PROTOCOL == HIL_PROTOCOL_MAVLINK && (HIL_MODE != HIL_MODE_DISABLED || HIL_PORT == 0)
|
|
|
|
hil.send_message(MSG_HEARTBEAT);
|
|
|
|
#endif
|
2011-03-09 02:37:09 -04:00
|
|
|
|
2011-03-26 03:35:52 -03:00
|
|
|
//if(gcs_simple.read()){
|
|
|
|
// Serial.print("!");
|
|
|
|
/*
|
|
|
|
Location temp;
|
|
|
|
temp.id = gcs_simple.id;
|
|
|
|
temp.p1 = gcs_simple.p1;
|
|
|
|
temp.alt = gcs_simple.altitude;
|
|
|
|
temp.lat = gcs_simple.latitude;
|
|
|
|
temp.lng = gcs_simple.longitude;
|
2011-04-25 02:12:59 -03:00
|
|
|
set_command_with_index(temp, gcs_simple.index);
|
2011-03-26 03:35:52 -03:00
|
|
|
gcs_simple.ack();
|
|
|
|
*/
|
|
|
|
//}
|
2011-06-01 02:50:17 -03:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
}
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
void update_GPS(void)
|
|
|
|
{
|
2011-06-16 14:03:26 -03:00
|
|
|
loop_step = 10;
|
2011-02-17 05:36:33 -04:00
|
|
|
g_gps->update();
|
2010-12-19 12:40:33 -04:00
|
|
|
update_GPS_light();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-04-17 02:17:42 -03:00
|
|
|
//current_loc.lng = 377697000; // Lon * 10 * *7
|
|
|
|
//current_loc.lat = -1224318000; // Lat * 10 * *7
|
|
|
|
//current_loc.alt = 100; // alt * 10 * *7
|
|
|
|
//return;
|
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
if (g_gps->new_data && g_gps->fix) {
|
2011-03-09 02:37:09 -04:00
|
|
|
|
2011-02-19 22:03:01 -04:00
|
|
|
// XXX We should be sending GPS data off one of the regular loops so that we send
|
|
|
|
// no-GPS-fix data too
|
|
|
|
#if GCS_PROTOCOL != GCS_PROTOCOL_MAVLINK
|
|
|
|
gcs.send_message(MSG_LOCATION);
|
|
|
|
#endif
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
// for performance
|
|
|
|
// ---------------
|
|
|
|
gps_fix_count++;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
if(ground_start_count > 1){
|
|
|
|
ground_start_count--;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
} else if (ground_start_count == 1) {
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// We countdown N number of good GPS fixes
|
|
|
|
// so that the altitude is more accurate
|
|
|
|
// -------------------------------------
|
|
|
|
if (current_loc.lat == 0) {
|
2011-02-17 03:09:13 -04:00
|
|
|
SendDebugln("!! bad loc");
|
2010-12-19 12:40:33 -04:00
|
|
|
ground_start_count = 5;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-02-24 01:56:59 -04:00
|
|
|
}else{
|
2011-02-17 03:09:13 -04:00
|
|
|
//Serial.printf("init Home!");
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-01-17 22:48:44 -04:00
|
|
|
// reset our nav loop timer
|
2011-03-15 02:54:48 -03:00
|
|
|
//nav_loopTimer = millis();
|
2010-12-19 12:40:33 -04:00
|
|
|
init_home();
|
2011-02-19 22:03:01 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// init altitude
|
2011-04-21 02:15:45 -03:00
|
|
|
current_loc.alt = home.alt;
|
2010-12-19 12:40:33 -04:00
|
|
|
ground_start_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-17 05:36:33 -04:00
|
|
|
current_loc.lng = g_gps->longitude; // Lon * 10 * *7
|
|
|
|
current_loc.lat = g_gps->latitude; // Lat * 10 * *7
|
2011-05-31 02:29:06 -03:00
|
|
|
|
|
|
|
if (g.log_bitmask & MASK_LOG_GPS){
|
|
|
|
Log_Write_GPS();
|
|
|
|
}
|
2011-02-17 03:09:13 -04:00
|
|
|
}
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
void update_current_flight_mode(void)
|
|
|
|
{
|
|
|
|
if(control_mode == AUTO){
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// this is a hack to prevent run up of the throttle I term for alt hold
|
|
|
|
if(command_must_ID == MAV_CMD_NAV_TAKEOFF){
|
|
|
|
invalid_throttle = (g.rc_3.control_in != 0);
|
|
|
|
// make invalid_throttle false if we are waiting to take off.
|
|
|
|
}
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
switch(command_must_ID){
|
|
|
|
default:
|
|
|
|
// Output Pitch, Roll, Yaw and Throttle
|
2011-02-17 05:36:33 -04:00
|
|
|
// ------------------------------------
|
2011-01-02 16:34:42 -04:00
|
|
|
auto_yaw();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-25 01:53:36 -04:00
|
|
|
// mix in user control
|
|
|
|
control_nav_mixer();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// perform stabilzation
|
2011-01-25 01:53:36 -04:00
|
|
|
output_stabilize_roll();
|
|
|
|
output_stabilize_pitch();
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
if(invalid_throttle)
|
|
|
|
calc_nav_throttle();
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// apply throttle control
|
|
|
|
output_auto_throttle();
|
|
|
|
break;
|
|
|
|
}
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
}else{
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
switch(control_mode){
|
2011-01-25 01:53:36 -04:00
|
|
|
case ACRO:
|
|
|
|
// clear any AP naviagtion values
|
|
|
|
nav_pitch = 0;
|
|
|
|
nav_roll = 0;
|
|
|
|
|
|
|
|
// Yaw control
|
|
|
|
output_manual_yaw();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-25 01:53:36 -04:00
|
|
|
// apply throttle control
|
|
|
|
output_manual_throttle();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-25 01:53:36 -04:00
|
|
|
// mix in user control
|
|
|
|
control_nav_mixer();
|
|
|
|
|
|
|
|
// perform rate or stabilzation
|
|
|
|
// ----------------------------
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-25 01:53:36 -04:00
|
|
|
// Roll control
|
2011-02-17 03:09:13 -04:00
|
|
|
if(abs(g.rc_1.control_in) >= ACRO_RATE_TRIGGER){
|
2011-01-25 01:53:36 -04:00
|
|
|
output_rate_roll(); // rate control yaw
|
|
|
|
}else{
|
|
|
|
output_stabilize_roll(); // hold yaw
|
|
|
|
}
|
2011-01-11 17:14:57 -04:00
|
|
|
|
2011-01-25 01:53:36 -04:00
|
|
|
// Roll control
|
2011-02-17 03:09:13 -04:00
|
|
|
if(abs(g.rc_2.control_in) >= ACRO_RATE_TRIGGER){
|
2011-01-25 01:53:36 -04:00
|
|
|
output_rate_pitch(); // rate control yaw
|
|
|
|
}else{
|
|
|
|
output_stabilize_pitch(); // hold yaw
|
|
|
|
}
|
|
|
|
break;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
case STABILIZE:
|
|
|
|
// clear any AP naviagtion values
|
|
|
|
nav_pitch = 0;
|
|
|
|
nav_roll = 0;
|
2010-12-26 15:49:11 -04:00
|
|
|
|
2011-05-26 02:58:17 -03:00
|
|
|
if(g.rc_3.control_in == 0){
|
2011-05-31 02:29:06 -03:00
|
|
|
clear_yaw_control();
|
|
|
|
}else{
|
|
|
|
// Yaw control
|
|
|
|
output_manual_yaw();
|
2011-05-26 02:58:17 -03:00
|
|
|
}
|
2010-12-26 15:49:11 -04:00
|
|
|
|
|
|
|
// apply throttle control
|
|
|
|
output_manual_throttle();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-25 01:53:36 -04:00
|
|
|
// mix in user control
|
|
|
|
control_nav_mixer();
|
2010-12-26 15:49:11 -04:00
|
|
|
|
|
|
|
// perform stabilzation
|
2011-01-25 01:53:36 -04:00
|
|
|
output_stabilize_roll();
|
|
|
|
output_stabilize_pitch();
|
2010-12-26 15:49:11 -04:00
|
|
|
break;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-03-14 03:04:07 -03:00
|
|
|
case SIMPLE:
|
2011-03-30 02:27:13 -03:00
|
|
|
flight_timer++;
|
2011-03-14 03:04:07 -03:00
|
|
|
// 25 hz
|
2011-03-30 02:27:13 -03:00
|
|
|
if(flight_timer > 4){
|
|
|
|
flight_timer = 0;
|
2011-03-15 02:54:48 -03:00
|
|
|
|
2011-05-06 21:21:16 -03:00
|
|
|
simple_WP.lat = 0;
|
|
|
|
simple_WP.lng = 0;
|
2011-03-15 02:54:48 -03:00
|
|
|
|
|
|
|
next_WP.lng = (float)g.rc_1.control_in *.4; // X: 4500 / 2 = 2250 = 25 meteres
|
|
|
|
next_WP.lat = -((float)g.rc_2.control_in *.4); // Y: 4500 / 2 = 2250 = 25 meteres
|
2011-03-14 03:04:07 -03:00
|
|
|
|
|
|
|
// calc a new bearing
|
2011-05-06 21:21:16 -03:00
|
|
|
nav_bearing = get_bearing(&simple_WP, &next_WP) + initial_simple_bearing;
|
2011-03-14 03:04:07 -03:00
|
|
|
nav_bearing = wrap_360(nav_bearing);
|
2011-05-06 21:21:16 -03:00
|
|
|
wp_distance = get_distance(&simple_WP, &next_WP);
|
2011-03-14 03:04:07 -03:00
|
|
|
calc_bearing_error();
|
2011-03-15 02:54:48 -03:00
|
|
|
/*
|
|
|
|
Serial.printf("lat: %ld lon:%ld, bear:%ld, dist:%ld, init:%ld, err:%ld ",
|
|
|
|
next_WP.lat,
|
|
|
|
next_WP.lng,
|
|
|
|
nav_bearing,
|
|
|
|
wp_distance,
|
|
|
|
initial_simple_bearing,
|
|
|
|
bearing_error);
|
|
|
|
*/
|
2011-03-14 03:04:07 -03:00
|
|
|
// get nav_pitch and nav_roll
|
2011-04-25 02:12:59 -03:00
|
|
|
calc_simple_nav();
|
|
|
|
calc_nav_output();
|
|
|
|
limit_nav_pitch_roll(4500);
|
2011-03-14 03:04:07 -03:00
|
|
|
}
|
|
|
|
|
2011-05-26 02:58:17 -03:00
|
|
|
// are we at rest? reset nav_yaw
|
|
|
|
if(g.rc_3.control_in == 0){
|
2011-05-31 02:29:06 -03:00
|
|
|
clear_yaw_control();
|
|
|
|
}else{
|
|
|
|
// Yaw control
|
|
|
|
output_manual_yaw();
|
2011-05-26 02:58:17 -03:00
|
|
|
}
|
|
|
|
|
2011-03-14 03:04:07 -03:00
|
|
|
// apply throttle control
|
|
|
|
output_manual_throttle();
|
|
|
|
|
|
|
|
// apply nav_pitch and nav_roll to output
|
2011-04-10 17:31:33 -03:00
|
|
|
simple_mixer();
|
2011-03-14 03:04:07 -03:00
|
|
|
|
|
|
|
// perform stabilzation
|
|
|
|
output_stabilize_roll();
|
|
|
|
output_stabilize_pitch();
|
|
|
|
break;
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
case ALT_HOLD:
|
|
|
|
// clear any AP naviagtion values
|
|
|
|
nav_pitch = 0;
|
|
|
|
nav_roll = 0;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-05-26 02:58:17 -03:00
|
|
|
// allow interactive changing of atitude
|
2011-04-10 20:07:24 -03:00
|
|
|
adjust_altitude();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-02 16:34:42 -04:00
|
|
|
// Yaw control
|
|
|
|
// -----------
|
|
|
|
output_manual_yaw();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// Output Pitch, Roll, Yaw and Throttle
|
|
|
|
// ------------------------------------
|
2011-04-25 02:12:59 -03:00
|
|
|
|
|
|
|
if(invalid_throttle)
|
|
|
|
calc_nav_throttle();
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// apply throttle control
|
|
|
|
output_auto_throttle();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-01-25 01:53:36 -04:00
|
|
|
// mix in user control
|
|
|
|
control_nav_mixer();
|
2010-12-19 12:40:33 -04:00
|
|
|
|
|
|
|
// perform stabilzation
|
2011-01-25 01:53:36 -04:00
|
|
|
output_stabilize_roll();
|
|
|
|
output_stabilize_pitch();
|
2010-12-19 12:40:33 -04:00
|
|
|
break;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
|
|
|
case RTL:
|
2010-12-19 12:40:33 -04:00
|
|
|
// Output Pitch, Roll, Yaw and Throttle
|
|
|
|
// ------------------------------------
|
2011-01-02 16:34:42 -04:00
|
|
|
auto_yaw();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
if(invalid_throttle)
|
|
|
|
calc_nav_throttle();
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// apply throttle control
|
|
|
|
output_auto_throttle();
|
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
// mix in user control with Nav control
|
2011-01-25 01:53:36 -04:00
|
|
|
control_nav_mixer();
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// perform stabilzation
|
2011-01-25 01:53:36 -04:00
|
|
|
output_stabilize_roll();
|
|
|
|
output_stabilize_pitch();
|
2010-12-19 12:40:33 -04:00
|
|
|
break;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-03-09 02:37:09 -04:00
|
|
|
case LOITER:
|
2011-05-26 02:58:17 -03:00
|
|
|
// allow interactive changing of atitude
|
2011-04-10 20:07:24 -03:00
|
|
|
adjust_altitude();
|
2011-03-30 02:27:13 -03:00
|
|
|
|
2011-04-10 20:07:24 -03:00
|
|
|
// Output Pitch, Roll, Yaw and Throttle
|
|
|
|
// ------------------------------------
|
2011-03-30 02:27:13 -03:00
|
|
|
|
2011-01-02 16:34:42 -04:00
|
|
|
// Yaw control
|
|
|
|
// -----------
|
|
|
|
output_manual_yaw();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
if(invalid_throttle)
|
|
|
|
calc_nav_throttle();
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// apply throttle control
|
|
|
|
output_auto_throttle();
|
|
|
|
|
2011-03-02 22:32:50 -04:00
|
|
|
// mix in user control with Nav control
|
2011-01-25 01:53:36 -04:00
|
|
|
control_nav_mixer();
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
// perform stabilzation
|
2011-01-25 01:53:36 -04:00
|
|
|
output_stabilize_roll();
|
|
|
|
output_stabilize_pitch();
|
2010-12-19 12:40:33 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
//Serial.print("$");
|
|
|
|
break;
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// called after a GPS read
|
|
|
|
void update_navigation()
|
|
|
|
{
|
|
|
|
// wp_distance is in ACTUAL meters, not the *100 meters we get from the GPS
|
|
|
|
// ------------------------------------------------------------------------
|
2011-05-23 02:53:00 -03:00
|
|
|
switch(control_mode){
|
|
|
|
case AUTO:
|
|
|
|
verify_commands();
|
2010-12-19 12:40:33 -04:00
|
|
|
|
2011-05-23 02:53:00 -03:00
|
|
|
// note: wp_control is handled by commands_logic
|
2011-03-05 01:12:16 -04:00
|
|
|
|
2011-05-23 02:53:00 -03:00
|
|
|
// calculates desired Yaw
|
|
|
|
update_nav_yaw();
|
2011-04-25 02:12:59 -03:00
|
|
|
|
2011-05-23 02:53:00 -03:00
|
|
|
// calculates the desired Roll and Pitch
|
|
|
|
update_nav_wp();
|
|
|
|
break;
|
2011-05-14 23:02:09 -03:00
|
|
|
|
2011-05-23 02:53:00 -03:00
|
|
|
case RTL:
|
|
|
|
// calculates desired Yaw
|
|
|
|
update_nav_yaw();
|
2011-05-23 23:14:18 -03:00
|
|
|
case LOITER:
|
|
|
|
// are we Traversing or Loitering?
|
2011-05-31 02:29:06 -03:00
|
|
|
wp_control = (wp_distance < 50) ? LOITER_MODE : WP_MODE;
|
|
|
|
//wp_control = LOITER_MODE;
|
2011-04-26 02:15:25 -03:00
|
|
|
|
2011-05-23 02:53:00 -03:00
|
|
|
// calculates the desired Roll and Pitch
|
|
|
|
update_nav_wp();
|
|
|
|
break;
|
|
|
|
|
2011-05-29 01:02:01 -03:00
|
|
|
/*#if YAW_HACK == 1
|
2011-05-23 02:53:00 -03:00
|
|
|
case SIMPLE:
|
|
|
|
// calculates desired Yaw
|
|
|
|
// exprimental_hack
|
|
|
|
if(g.rc_6.control_in > 900)
|
|
|
|
update_nav_yaw();
|
|
|
|
if(g.rc_6.control_in < 100){
|
|
|
|
nav_yaw = dcm.yaw_sensor;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2011-05-29 01:02:01 -03:00
|
|
|
*/
|
2010-12-19 12:40:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_AHRS(void)
|
|
|
|
{
|
|
|
|
// Perform IMU calculations and get attitude info
|
2011-01-25 01:53:36 -04:00
|
|
|
//-----------------------------------------------
|
2011-05-09 21:00:05 -03:00
|
|
|
#if HIL_MODE == HIL_MODE_SENSORS
|
|
|
|
// update hil before dcm update
|
|
|
|
hil.update();
|
|
|
|
#endif
|
|
|
|
|
2010-12-19 12:40:33 -04:00
|
|
|
dcm.update_DCM(G_Dt);
|
2011-02-17 03:09:13 -04:00
|
|
|
omega = dcm.get_gyro();
|
2011-01-23 12:40:03 -04:00
|
|
|
}
|
2011-02-13 18:32:34 -04:00
|
|
|
|
|
|
|
void update_trig(void){
|
|
|
|
Vector2f yawvector;
|
|
|
|
Matrix3f temp = dcm.get_dcm_matrix();
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-02-13 18:32:34 -04:00
|
|
|
yawvector.x = temp.a.x; // sin
|
|
|
|
yawvector.y = temp.b.x; // cos
|
|
|
|
yawvector.normalize();
|
|
|
|
|
|
|
|
cos_yaw_x = yawvector.y; // 0 x = north
|
2011-02-17 05:36:33 -04:00
|
|
|
sin_yaw_y = yawvector.x; // 1 y
|
|
|
|
|
2011-02-13 18:32:34 -04:00
|
|
|
sin_pitch_y = -temp.c.x;
|
|
|
|
cos_pitch_x = sqrt(1 - (temp.c.x * temp.c.x));
|
2011-02-17 05:36:33 -04:00
|
|
|
|
2011-02-13 18:32:34 -04:00
|
|
|
cos_roll_x = temp.c.z / cos_pitch_x;
|
|
|
|
sin_roll_y = temp.c.y / cos_pitch_x;
|
2011-02-17 05:36:33 -04:00
|
|
|
}
|
2011-02-20 19:09:28 -04:00
|
|
|
|
2011-06-16 14:03:26 -03:00
|
|
|
// updated at 10hz
|
2011-02-21 00:30:56 -04:00
|
|
|
void update_alt()
|
|
|
|
{
|
2011-04-25 02:12:59 -03:00
|
|
|
altitude_sensor = BARO;
|
|
|
|
|
2011-06-16 14:03:26 -03:00
|
|
|
#if HIL_MODE == HIL_MODE_ATTITUDE
|
2011-03-03 07:39:52 -04:00
|
|
|
current_loc.alt = g_gps->altitude;
|
2011-06-16 14:03:26 -03:00
|
|
|
#else
|
|
|
|
|
|
|
|
if(g.sonar_enabled){
|
|
|
|
// filter out offset
|
|
|
|
float scale;
|
|
|
|
|
|
|
|
// read barometer
|
|
|
|
baro_alt = read_barometer();
|
|
|
|
int temp_sonar = sonar.read();
|
|
|
|
|
|
|
|
// spike filter
|
|
|
|
//if((temp_sonar - sonar_alt) < 50){
|
|
|
|
sonar_alt = temp_sonar;
|
|
|
|
//}
|
|
|
|
|
|
|
|
scale = (sonar_alt - 300) / 300;
|
|
|
|
scale = constrain(scale, 0, 1);
|
|
|
|
|
|
|
|
current_loc.alt = ((float)sonar_alt * (1.0 - scale)) + ((float)baro_alt * scale) + home.alt;
|
|
|
|
|
|
|
|
}else{
|
|
|
|
baro_alt = read_barometer();
|
|
|
|
// no sonar altitude
|
|
|
|
current_loc.alt = baro_alt + home.alt;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// updated at 10hz
|
|
|
|
void update_alt2()
|
|
|
|
{
|
|
|
|
altitude_sensor = BARO;
|
|
|
|
|
|
|
|
#if HIL_MODE == HIL_MODE_ATTITUDE
|
|
|
|
current_loc.alt = g_gps->altitude;
|
|
|
|
#else
|
|
|
|
|
2011-02-21 00:30:56 -04:00
|
|
|
if(g.sonar_enabled){
|
2011-04-28 02:43:35 -03:00
|
|
|
// filter out offset
|
|
|
|
|
2011-04-21 20:07:31 -03:00
|
|
|
// read barometer
|
2011-05-29 16:32:55 -03:00
|
|
|
baro_alt = read_barometer();
|
2011-05-06 21:21:16 -03:00
|
|
|
int temp_sonar = sonar.read();
|
2011-04-21 20:07:31 -03:00
|
|
|
|
2011-05-29 16:32:55 -03:00
|
|
|
// spike filter
|
|
|
|
if((temp_sonar - sonar_alt) < 50){
|
2011-05-06 21:21:16 -03:00
|
|
|
sonar_alt = temp_sonar;
|
2011-04-28 02:43:35 -03:00
|
|
|
}
|
2011-04-21 20:07:31 -03:00
|
|
|
|
2011-04-25 02:12:59 -03:00
|
|
|
// decide if we're using sonar
|
2011-06-03 02:36:39 -03:00
|
|
|
if ((baro_alt < 1200) || sonar_alt < 300){
|
2011-04-25 02:12:59 -03:00
|
|
|
// correct alt for angle of the sonar
|
2011-04-26 02:15:25 -03:00
|
|
|
float temp = cos_pitch_x * cos_roll_x;
|
|
|
|
temp = max(temp, 0.707);
|
|
|
|
|
|
|
|
sonar_alt = (float)sonar_alt * temp;
|
2011-04-21 20:07:31 -03:00
|
|
|
|
2011-06-03 02:36:39 -03:00
|
|
|
if(sonar_alt < 450){
|
2011-04-25 02:12:59 -03:00
|
|
|
altitude_sensor = SONAR;
|
2011-04-28 02:43:35 -03:00
|
|
|
baro_alt_offset = sonar_alt - baro_alt;
|
2011-04-25 02:12:59 -03:00
|
|
|
}
|
2011-02-21 00:30:56 -04:00
|
|
|
}
|
|
|
|
|
2011-04-21 20:07:31 -03:00
|
|
|
// calculate our altitude
|
2011-02-21 00:30:56 -04:00
|
|
|
if(altitude_sensor == BARO){
|
2011-04-28 02:43:35 -03:00
|
|
|
current_loc.alt = baro_alt + baro_alt_offset + home.alt;
|
2011-02-21 00:30:56 -04:00
|
|
|
}else{
|
|
|
|
current_loc.alt = sonar_alt + home.alt;
|
|
|
|
}
|
2011-02-21 00:46:26 -04:00
|
|
|
|
2011-02-20 19:09:28 -04:00
|
|
|
}else{
|
2011-04-21 20:07:31 -03:00
|
|
|
baro_alt = read_barometer();
|
2011-02-21 00:30:56 -04:00
|
|
|
// no sonar altitude
|
|
|
|
current_loc.alt = baro_alt + home.alt;
|
2011-02-20 19:09:28 -04:00
|
|
|
}
|
2011-04-21 02:15:45 -03:00
|
|
|
|
2011-02-25 01:33:39 -04:00
|
|
|
//Serial.printf("b_alt: %ld, home: %ld ", baro_alt, home.alt);
|
2011-06-16 14:03:26 -03:00
|
|
|
#endif
|
2011-03-26 03:35:52 -03:00
|
|
|
}
|
|
|
|
|
2011-04-10 20:07:24 -03:00
|
|
|
void
|
|
|
|
adjust_altitude()
|
|
|
|
{
|
|
|
|
flight_timer++;
|
|
|
|
if(flight_timer >= 2){
|
|
|
|
flight_timer = 0;
|
|
|
|
|
|
|
|
if(g.rc_3.control_in <= 200){
|
2011-05-31 02:29:06 -03:00
|
|
|
next_WP.alt -= 1; // 1 meter per second
|
|
|
|
next_WP.alt = max(next_WP.alt, (current_loc.alt - 100)); // don't go more than 4 meters below current location
|
2011-04-10 20:07:24 -03:00
|
|
|
next_WP.alt = max(next_WP.alt, 100); // no lower than 1 meter?
|
2011-05-31 02:29:06 -03:00
|
|
|
|
2011-04-10 20:07:24 -03:00
|
|
|
}else if (g.rc_3.control_in > 700){
|
2011-05-31 02:29:06 -03:00
|
|
|
next_WP.alt += 2; // 1 meter per second
|
2011-04-13 13:33:06 -03:00
|
|
|
//next_WP.alt = min((current_loc.alt + 400), next_WP.alt); // don't go more than 4 meters below current location
|
2011-05-31 02:29:06 -03:00
|
|
|
next_WP.alt = min(next_WP.alt, (current_loc.alt + 200)); // don't go more than 4 meters below current location
|
2011-04-10 20:07:24 -03:00
|
|
|
}
|
|
|
|
}
|
2011-04-16 17:42:00 -03:00
|
|
|
}
|
|
|
|
|
2011-04-17 20:08:16 -03:00
|
|
|
void tuning(){
|
|
|
|
|
|
|
|
#if CHANNEL_6_TUNING == CH6_STABLIZE_KP
|
|
|
|
g.pid_stabilize_roll.kP((float)g.rc_6.control_in / 1000.0);
|
|
|
|
g.pid_stabilize_pitch.kP((float)g.rc_6.control_in / 1000.0);
|
|
|
|
|
|
|
|
#elif CHANNEL_6_TUNING == CH6_STABLIZE_KD
|
|
|
|
// uncomment me out to try in flight dampening, 0 = unflyable, .2 = unfun, .13 worked for me.
|
|
|
|
// use test,radio to get the value to use in your config.
|
2011-05-18 20:38:24 -03:00
|
|
|
g.pid_stabilize_pitch.kD((float)g.rc_6.control_in / 1000.0);
|
|
|
|
g.pid_stabilize_roll.kD((float)g.rc_6.control_in / 1000.0);
|
2011-04-17 20:08:16 -03:00
|
|
|
|
|
|
|
#elif CHANNEL_6_TUNING == CH6_BARO_KP
|
|
|
|
g.pid_baro_throttle.kP((float)g.rc_6.control_in / 1000.0);
|
|
|
|
|
|
|
|
#elif CHANNEL_6_TUNING == CH6_BARO_KD
|
|
|
|
g.pid_baro_throttle.kD((float)g.rc_6.control_in / 1000.0); // 0 to 1
|
|
|
|
|
|
|
|
#elif CHANNEL_6_TUNING == CH6_SONAR_KP
|
|
|
|
g.pid_sonar_throttle.kP((float)g.rc_6.control_in / 1000.0);
|
|
|
|
|
|
|
|
#elif CHANNEL_6_TUNING == CH6_SONAR_KD
|
|
|
|
g.pid_sonar_throttle.kD((float)g.rc_6.control_in / 1000.0); // 0 to 1
|
|
|
|
|
|
|
|
#elif CHANNEL_6_TUNING == CH6_Y6_SCALING
|
2011-04-17 20:12:27 -03:00
|
|
|
Y6_scaling = (float)g.rc_6.control_in / 1000.0;
|
2011-04-21 20:07:31 -03:00
|
|
|
|
|
|
|
#elif CHANNEL_6_TUNING == CH6_PMAX
|
|
|
|
g.pitch_max.set(g.rc_6.control_in * 2); // 0 to 2000
|
2011-06-16 14:03:26 -03:00
|
|
|
|
|
|
|
#elif CHANNEL_6_TUNING == CH6_DCM_RP
|
|
|
|
dcm.kp_roll_pitch((float)g.rc_6.control_in / 5000.0);
|
|
|
|
|
|
|
|
#elif CHANNEL_6_TUNING == CH6_DCM_Y
|
|
|
|
dcm.kp_yaw((float)g.rc_6.control_in / 1000.0);
|
|
|
|
|
2011-06-12 09:14:10 -03:00
|
|
|
#elif CHANNEL_6_TUNING == CH6_YAW_KP
|
|
|
|
// yaw heading
|
|
|
|
g.pid_yaw.kP((float)g.rc_6.control_in / 200.0); // range from 0.0 ~ 5.0
|
2011-06-16 14:03:26 -03:00
|
|
|
|
2011-06-12 09:14:10 -03:00
|
|
|
#elif CHANNEL_6_TUNING == CH6_YAW_KD
|
|
|
|
// yaw heading
|
|
|
|
g.pid_yaw.kD((float)g.rc_6.control_in / 1000.0);
|
2011-06-16 14:03:26 -03:00
|
|
|
|
2011-06-12 09:14:10 -03:00
|
|
|
#elif CHANNEL_6_TUNING == CH6_YAW_RATE_KP
|
|
|
|
// yaw rate
|
|
|
|
g.pid_acro_rate_yaw.kP((float)g.rc_6.control_in / 1000.0);
|
2011-06-16 14:03:26 -03:00
|
|
|
|
2011-06-12 09:14:10 -03:00
|
|
|
#elif CHANNEL_6_TUNING == CH6_YAW_RATE_KD
|
|
|
|
// yaw rate
|
|
|
|
g.pid_acro_rate_yaw.kD((float)g.rc_6.control_in / 1000.0);
|
2011-04-21 20:07:31 -03:00
|
|
|
|
2011-04-17 20:08:16 -03:00
|
|
|
#endif
|
2011-05-09 14:40:32 -03:00
|
|
|
}
|
|
|
|
|
2011-05-23 02:53:00 -03:00
|
|
|
void update_nav_wp()
|
|
|
|
{
|
|
|
|
if(wp_control == LOITER_MODE){
|
|
|
|
// calc a pitch to the target
|
|
|
|
calc_loiter_nav();
|
|
|
|
|
|
|
|
// rotate pitch and roll to the copter frame of reference
|
|
|
|
calc_loiter_output();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// how far are we from the ideal trajectory?
|
|
|
|
// this pushes us back on course
|
|
|
|
update_crosstrack();
|
|
|
|
|
|
|
|
// calc a rate dampened pitch to the target
|
|
|
|
calc_rate_nav();
|
|
|
|
|
|
|
|
// rotate that pitch to the copter frame of reference
|
|
|
|
calc_nav_output();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_nav_yaw()
|
|
|
|
{
|
|
|
|
// this tracks a location so the copter is always pointing towards it.
|
|
|
|
if(yaw_tracking == MAV_ROI_LOCATION){
|
|
|
|
nav_yaw = get_bearing(¤t_loc, &target_WP);
|
|
|
|
|
|
|
|
}else if(yaw_tracking == MAV_ROI_WPNEXT){
|
|
|
|
nav_yaw = target_bearing;
|
|
|
|
}
|
2011-06-16 14:03:26 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void check_DCM()
|
|
|
|
{
|
|
|
|
#if DYNAMIC_DRIFT == 1
|
|
|
|
if(g.rc_1.control_in == 0 && g.rc_2.control_in == 0){
|
|
|
|
if(g.rc_3.control_in < (g.throttle_cruise + 20)){
|
|
|
|
//dcm.kp_roll_pitch(dcm.kDCM_kp_rp_high);
|
|
|
|
dcm.kp_roll_pitch(0.15);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
dcm.kp_roll_pitch(0.05967);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|