1717// Header includes
1818// //////////////////////////////////////////////////////////////////////////////
1919
20- // AVR runtime
21- #include < avr/io.h>
22- #include < avr/eeprom.h>
23- #include < avr/pgmspace.h>
24- #include < avr/wdt.h>
2520#include < math.h>
21+ #include < stdarg.h>
22+ #include < stdio.h>
2623
27- // Libraries
28- #include < FastSerial.h>
2924#include < AP_Common.h>
3025#include < AP_Progmem.h>
26+ #include < AP_HAL.h>
3127#include < AP_Menu.h>
3228#include < AP_Param.h>
33- #include < Arduino_Mega_ISR_Registry.h>
34- #include < APM_RC.h> // ArduPilot Mega RC Library
3529#include < AP_GPS.h> // ArduPilot GPS library
36- #include < I2C.h> // Wayne Truchsess I2C lib
37- #include < SPI.h> // Arduino SPI lib
38- #include < AP_Semaphore.h> // for removing conflict between optical flow and dataflash on SPI3 bus
39- #include < DataFlash.h> // ArduPilot Mega Flash Memory Library
40- #include < AP_ADC.h> // ArduPilot Mega Analog to Digital Converter Library
41- #include < AP_AnalogSource.h> // ArduPilot Mega polymorphic analog getter
42- #include < AP_PeriodicProcess.h> // ArduPilot Mega TimerProcess
4330#include < AP_Baro.h> // ArduPilot barometer library
4431#include < AP_Compass.h> // ArduPilot Mega Magnetometer Library
4532#include < AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
33+ #include < AP_ADC.h> // ArduPilot Mega Analog to Digital Converter Library
34+ #include < AP_ADC_AnalogSource.h>
4635#include < AP_InertialSensor.h> // Inertial Sensor Library
4736#include < AP_AHRS.h> // ArduPilot Mega DCM Library
4837#include < PID.h> // PID library
4938#include < RC_Channel.h> // RC Channel Library
5039#include < AP_RangeFinder.h> // Range finder library
5140#include < Filter.h> // Filter library
5241#include < AP_Buffer.h> // APM FIFO Buffer
53- #include < ModeFilter.h> // Mode Filter from Filter library
54- #include < LowPassFilter.h> // LowPassFilter class (inherits from Filter class)
5542#include < AP_Relay.h> // APM relay
5643#include < AP_Camera.h> // Photo or video camera
5744#include < AP_Airspeed.h>
5845#include < memcheck.h>
5946
47+ #include < APM_OBC.h>
48+ #include < APM_Control.h>
49+ #include < GCS_MAVLink.h> // MAVLink GCS definitions
50+ #include < AP_Mount.h> // Camera/Antenna mount
51+ #include < AP_Declination.h> // ArduPilot Mega Declination Helper Library
52+
53+
6054// optional new controller library
6155#if APM_CONTROL == ENABLED
6256#include < APM_Control.h>
6357#endif
6458
59+ // Pre-AP_HAL compatibility
60+ #include " compat.h"
61+
6562// Configuration
6663#include " config.h"
6764
68- #include < GCS_MAVLink.h> // MAVLink GCS definitions
69-
70- #include < AP_Mount.h> // Camera/Antenna mount
71-
7265// Local modules
7366#include " defines.h"
7467#include " Parameters.h"
7568#include " GCS.h"
7669
77- #include < AP_Declination .h> // ArduPilot Mega Declination Helper Library
70+ #include < AP_HAL_AVR .h>
7871
79- // //////////////////////////////////////////////////////////////////////////////
80- // Serial ports
81- // //////////////////////////////////////////////////////////////////////////////
82- //
83- // Note that FastSerial port buffers are allocated at ::begin time,
84- // so there is not much of a penalty to defining ports that we don't
85- // use.
86- //
87- FastSerialPort0 (Serial); // FTDI/console
88- FastSerialPort1 (Serial1); // GPS port
89- #if TELEMETRY_UART2 == ENABLED
90- // solder bridge set to enable UART2 instead of USB MUX
91- FastSerialPort2 (Serial3);
72+ AP_HAL::BetterStream* cliSerial;
73+
74+ #if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
75+ const AP_HAL::HAL& hal = AP_HAL_AVR_APM2;
9276#else
93- FastSerialPort3 (Serial3); // Telemetry port for APM1
77+ const AP_HAL::HAL& hal = AP_HAL_AVR_APM1;
9478#endif
9579
96- // port to use for command line interface
97- static FastSerial *cliSerial = &Serial;
9880
81+ // //////////////////////////////////////////////////////////////////////////////
82+ // AP_Param Loader
83+ // //////////////////////////////////////////////////////////////////////////////
9984// this sets up the parameter table, and sets the default values. This
10085// must be the first AP_Param variable declared to ensure its
10186// constructor runs before the constructors of the other AP_Param
10287// variables
10388AP_Param param_loader (var_info, WP_START_BYTE);
10489
105- // Outback Challenge failsafe support
90+ // //////////////////////////////////////////////////////////////////////////////
91+ // Outback Challenge Failsafe Support
92+ // //////////////////////////////////////////////////////////////////////////////
10693#if OBC_FAILSAFE == ENABLED
107- #include < APM_OBC.h>
10894APM_OBC obc;
10995#endif
11096
@@ -113,32 +99,6 @@ APM_OBC obc;
11399// //////////////////////////////////////////////////////////////////////////////
114100static const AP_InertialSensor::Sample_rate ins_sample_rate = AP_InertialSensor::RATE_50HZ;
115101
116-
117- // //////////////////////////////////////////////////////////////////////////////
118- // ISR Registry
119- // //////////////////////////////////////////////////////////////////////////////
120- Arduino_Mega_ISR_Registry isr_registry;
121-
122-
123- // //////////////////////////////////////////////////////////////////////////////
124- // APM_RC_Class Instance
125- // //////////////////////////////////////////////////////////////////////////////
126- #if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
127- APM_RC_APM2 APM_RC;
128- #else
129- APM_RC_APM1 APM_RC;
130- #endif
131-
132- // //////////////////////////////////////////////////////////////////////////////
133- // Dataflash
134- // //////////////////////////////////////////////////////////////////////////////
135- #if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
136- DataFlash_APM2 DataFlash;
137- #else
138- DataFlash_APM1 DataFlash;
139- #endif
140-
141-
142102// //////////////////////////////////////////////////////////////////////////////
143103// Parameters
144104// //////////////////////////////////////////////////////////////////////////////
@@ -147,7 +107,6 @@ DataFlash_APM1 DataFlash;
147107//
148108static Parameters g;
149109
150-
151110// //////////////////////////////////////////////////////////////////////////////
152111// prototypes
153112static void update_events (void );
@@ -201,22 +160,22 @@ static AP_Compass_HMC5843 compass;
201160
202161// real GPS selection
203162 #if GPS_PROTOCOL == GPS_PROTOCOL_AUTO
204- AP_GPS_Auto g_gps_driver (&Serial1 , &g_gps);
163+ AP_GPS_Auto g_gps_driver (hal.uartB , &g_gps);
205164
206165 #elif GPS_PROTOCOL == GPS_PROTOCOL_NMEA
207- AP_GPS_NMEA g_gps_driver (&Serial1 );
166+ AP_GPS_NMEA g_gps_driver (hal.uartB );
208167
209168 #elif GPS_PROTOCOL == GPS_PROTOCOL_SIRF
210- AP_GPS_SIRF g_gps_driver (&Serial1 );
169+ AP_GPS_SIRF g_gps_driver (hal.uartB );
211170
212171 #elif GPS_PROTOCOL == GPS_PROTOCOL_UBLOX
213- AP_GPS_UBLOX g_gps_driver (&Serial1 );
172+ AP_GPS_UBLOX g_gps_driver (hal.uartB );
214173
215174 #elif GPS_PROTOCOL == GPS_PROTOCOL_MTK
216- AP_GPS_MTK g_gps_driver (&Serial1 );
175+ AP_GPS_MTK g_gps_driver (hal.uartB );
217176
218177 #elif GPS_PROTOCOL == GPS_PROTOCOL_MTK16
219- AP_GPS_MTK16 g_gps_driver (&Serial1 );
178+ AP_GPS_MTK16 g_gps_driver (hal.uartB );
220179
221180 #elif GPS_PROTOCOL == GPS_PROTOCOL_NONE
222181AP_GPS_None g_gps_driver (NULL );
@@ -252,32 +211,30 @@ AP_AHRS_HIL ahrs(&ins, g_gps);
252211 #error Unrecognised HIL_MODE setting.
253212#endif // HIL MODE
254213
255- // we always have a timer scheduler
256- AP_TimerProcess timer_scheduler;
257-
258-
259214// //////////////////////////////////////////////////////////////////////////////
260215// GCS selection
261216// //////////////////////////////////////////////////////////////////////////////
262- //
263217GCS_MAVLINK gcs0;
264218GCS_MAVLINK gcs3;
265219
266220// //////////////////////////////////////////////////////////////////////////////
267- // PITOT selection
221+ // Analog Inputs
268222// //////////////////////////////////////////////////////////////////////////////
269- //
270223
271- #if CONFIG_PITOT_SOURCE == PITOT_SOURCE_ADC
272- AP_AnalogSource_ADC pitot_analog_source ( &adc,
273- CONFIG_PITOT_SOURCE_ADC_CHANNEL, 1.0 );
274- #elif CONFIG_PITOT_SOURCE == PITOT_SOURCE_ANALOG_PIN
275- AP_AnalogSource_Arduino pitot_analog_source (CONFIG_PITOT_SOURCE_ANALOG_PIN, 4.0 );
276- #endif
224+ AP_HAL::AnalogSource *pitot_analog_source;
277225
278226// a pin for reading the receiver RSSI voltage. The scaling by 0.25
279227// is to take the 0 to 1024 range down to an 8 bit range for MAVLink
280- AP_AnalogSource_Arduino RSSI_pin (-1 , 0.25 );
228+ AP_HAL::AnalogSource *rssi_analog_source;
229+
230+ AP_HAL::AnalogSource *vcc_pin;
231+
232+ AP_HAL::AnalogSource * batt_volt_pin;
233+ AP_HAL::AnalogSource * batt_curr_pin;
234+
235+ // //////////////////////////////////////////////////////////////////////////////
236+ // Relay
237+ // //////////////////////////////////////////////////////////////////////////////
281238
282239AP_Relay relay;
283240
@@ -314,7 +271,7 @@ static bool usb_connected;
314271enum FlightMode control_mode = INITIALISING;
315272// Used to maintain the state of the previous control switch position
316273// This is set to -1 when we need to re-read the switch
317- byte oldSwitchPosition;
274+ uint8_t oldSwitchPosition;
318275// This is used to enable the inverted flight feature
319276bool inverted_flight = false ;
320277// These are trim values used for elevon control
@@ -341,7 +298,7 @@ static int16_t failsafe;
341298static bool ch3_failsafe;
342299// A timer used to help recovery from unusual attitudes. If we enter an unusual attitude
343300// while in autonomous flight this variable is used to hold roll at 0 for a recovery period
344- static byte crash_timer;
301+ static uint8_t crash_timer;
345302// A timer used to track how long since we have received the last GCS heartbeat or rc override message
346303static uint32_t rc_override_fs_timer = 0 ;
347304
@@ -367,7 +324,7 @@ static const float t7 = 10000000.0;
367324// We use atan2 and other trig techniques to calaculate angles
368325// A counter used to count down valid gps fixes to allow the gps estimate to settle
369326// before recording our home position (and executing a ground start if we booted with an air start)
370- static byte ground_start_count = 5 ;
327+ static uint8_t ground_start_count = 5 ;
371328// Used to compute a speed estimate from the first valid gps fixes to decide if we are
372329// on the ground or in the air. Used to decide if a ground start is appropriate if we
373330// booted with an air start.
@@ -401,12 +358,12 @@ static int32_t hold_course = -1; // deg * 100 dir
401358
402359// There may be two active commands in Auto mode.
403360// This indicates the active navigation command by index number
404- static byte nav_command_index;
361+ static uint8_t nav_command_index;
405362// This indicates the active non-navigation command by index number
406- static byte non_nav_command_index;
363+ static uint8_t non_nav_command_index;
407364// This is the command type (eg navigate to waypoint) of the active navigation command
408- static byte nav_command_ID = NO_COMMAND;
409- static byte non_nav_command_ID = NO_COMMAND;
365+ static uint8_t nav_command_ID = NO_COMMAND;
366+ static uint8_t non_nav_command_ID = NO_COMMAND;
410367
411368// //////////////////////////////////////////////////////////////////////////////
412369// Airspeed
@@ -473,7 +430,7 @@ static float current_total1;
473430// //////////////////////////////////////////////////////////////////////////////
474431// Airspeed Sensors
475432// //////////////////////////////////////////////////////////////////////////////
476- AP_Airspeed airspeed (&pitot_analog_source) ;
433+ AP_Airspeed airspeed;
477434
478435// //////////////////////////////////////////////////////////////////////////////
479436// Altitude Sensor variables
@@ -645,16 +602,16 @@ static uint16_t mainLoop_count;
645602static uint32_t medium_loopTimer_ms;
646603
647604// Counters for branching from main control loop to slower loops
648- static byte medium_loopCounter;
605+ static uint8_t medium_loopCounter;
649606// Number of milliseconds used in last medium loop cycle
650607static uint8_t delta_ms_medium_loop;
651608
652609// Counters for branching from medium control loop to slower loops
653- static byte slow_loopCounter;
610+ static uint8_t slow_loopCounter;
654611// Counter to trigger execution of very low rate processes
655- static byte superslow_loopCounter;
612+ static uint8_t superslow_loopCounter;
656613// Counter to trigger execution of 1 Hz processes
657- static byte counter_one_herz;
614+ static uint8_t counter_one_herz;
658615
659616// % MCU cycles used
660617static float load;
@@ -683,6 +640,21 @@ AP_Mount camera_mount2(¤t_loc, g_gps, &ahrs, 1);
683640// //////////////////////////////////////////////////////////////////////////////
684641
685642void setup () {
643+ cliSerial = hal.console ;
644+ rssi_analog_source = hal.analogin ->channel (ANALOG_INPUT_NONE, 0.25 );
645+
646+ #if CONFIG_PITOT_SOURCE == PITOT_SOURCE_ADC
647+ pitot_analog_source = new AP_ADC_AnalogSource ( &adc,
648+ CONFIG_PITOT_SOURCE_ADC_CHANNEL, 1.0 );
649+ #elif CONFIG_PITOT_SOURCE == PITOT_SOURCE_ANALOG_PIN
650+ pitot_analog_source = hal.analogin ->channel (CONFIG_PITOT_SOURCE_ANALOG_PIN, 4.0 );
651+ #endif
652+ vcc_pin = hal.analogin ->channel (ANALOG_INPUT_BOARD_VCC);
653+
654+ batt_volt_pin = hal.analogin ->channel (g.battery_volt_pin );
655+ batt_curr_pin = hal.analogin ->channel (g.battery_curr_pin );
656+
657+ airspeed.init (pitot_analog_source);
686658 memcheck_init ();
687659 init_ardupilot ();
688660}
@@ -1248,3 +1220,5 @@ static void update_alt()
12481220 // if(medium_loopCounter == 0 && slow_loopCounter == 0)
12491221 // add_altitude_data(millis() / 100, g_gps->altitude / 10);
12501222}
1223+
1224+ AP_HAL_MAIN ();
0 commit comments