2012-12-30 04:56:57 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
#include <AP_HAL.h>
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
|
|
|
|
|
|
|
|
#include <AP_HAL_PX4.h>
|
|
|
|
#include "AP_HAL_PX4_Namespace.h"
|
|
|
|
#include "HAL_PX4_Class.h"
|
2013-01-02 06:39:26 -04:00
|
|
|
#include "Scheduler.h"
|
2013-01-02 20:03:05 -04:00
|
|
|
#include "UARTDriver.h"
|
2013-01-03 04:35:05 -04:00
|
|
|
#include "Storage.h"
|
2013-01-03 06:30:35 -04:00
|
|
|
#include "RCInput.h"
|
2013-01-04 07:25:36 -04:00
|
|
|
#include "RCOutput.h"
|
2013-01-20 22:56:57 -04:00
|
|
|
#include "AnalogIn.h"
|
2013-01-21 02:10:42 -04:00
|
|
|
#include "Util.h"
|
2013-07-11 00:01:59 -03:00
|
|
|
#include "GPIO.h"
|
2012-12-30 04:56:57 -04:00
|
|
|
|
|
|
|
#include <AP_HAL_Empty.h>
|
|
|
|
#include <AP_HAL_Empty_Private.h>
|
|
|
|
|
2013-01-03 20:14:35 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <systemlib/systemlib.h>
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <unistd.h>
|
2013-01-01 03:15:43 -04:00
|
|
|
#include <stdio.h>
|
2013-01-22 16:36:13 -04:00
|
|
|
#include <pthread.h>
|
2013-01-24 00:03:48 -04:00
|
|
|
#include <poll.h>
|
|
|
|
#include <drivers/drv_hrt.h>
|
2013-01-01 03:15:43 -04:00
|
|
|
|
2012-12-30 04:56:57 -04:00
|
|
|
using namespace PX4;
|
|
|
|
|
2013-01-05 07:45:53 -04:00
|
|
|
static Empty::EmptySemaphore i2cSemaphore;
|
|
|
|
static Empty::EmptyI2CDriver i2cDriver(&i2cSemaphore);
|
2012-12-30 04:56:57 -04:00
|
|
|
static Empty::EmptySPIDeviceManager spiDeviceManager;
|
2013-07-11 00:01:59 -03:00
|
|
|
//static Empty::EmptyGPIO gpioDriver;
|
2012-12-30 04:56:57 -04:00
|
|
|
|
2013-01-02 06:39:26 -04:00
|
|
|
static PX4Scheduler schedulerInstance;
|
2013-01-23 01:37:11 -04:00
|
|
|
static PX4Storage storageDriver;
|
2013-01-03 06:30:35 -04:00
|
|
|
static PX4RCInput rcinDriver;
|
2013-01-04 07:25:36 -04:00
|
|
|
static PX4RCOutput rcoutDriver;
|
2013-01-20 22:56:57 -04:00
|
|
|
static PX4AnalogIn analogIn;
|
2013-01-21 02:10:42 -04:00
|
|
|
static PX4Util utilInstance;
|
2013-07-11 00:01:59 -03:00
|
|
|
static PX4GPIO gpioDriver;
|
2012-12-30 04:56:57 -04:00
|
|
|
|
2013-12-21 07:25:15 -04:00
|
|
|
#if defined(CONFIG_ARCH_BOARD_PX4FMU_V2)
|
2013-02-17 22:55:33 -04:00
|
|
|
#define UARTA_DEFAULT_DEVICE "/dev/ttyACM0"
|
2013-01-14 00:59:54 -04:00
|
|
|
#define UARTB_DEFAULT_DEVICE "/dev/ttyS3"
|
2013-02-17 22:55:33 -04:00
|
|
|
#define UARTC_DEFAULT_DEVICE "/dev/ttyS1"
|
2013-12-21 07:25:15 -04:00
|
|
|
#define UARTD_DEFAULT_DEVICE "/dev/ttyS2"
|
2014-03-04 05:34:44 -04:00
|
|
|
#define UARTE_DEFAULT_DEVICE "/dev/ttyS6"
|
2013-12-21 07:25:15 -04:00
|
|
|
#else
|
|
|
|
#define UARTA_DEFAULT_DEVICE "/dev/ttyACM0"
|
|
|
|
#define UARTB_DEFAULT_DEVICE "/dev/ttyS3"
|
|
|
|
#define UARTC_DEFAULT_DEVICE "/dev/ttyS2"
|
2013-11-22 04:16:51 -04:00
|
|
|
#define UARTD_DEFAULT_DEVICE "/dev/null"
|
2013-12-21 07:25:15 -04:00
|
|
|
#define UARTE_DEFAULT_DEVICE "/dev/null"
|
|
|
|
#endif
|
2013-01-03 20:14:35 -04:00
|
|
|
|
2013-02-17 22:55:33 -04:00
|
|
|
// 3 UART drivers, for GPS plus two mavlink-enabled devices
|
2013-01-24 00:03:48 -04:00
|
|
|
static PX4UARTDriver uartADriver(UARTA_DEFAULT_DEVICE, "APM_uartA");
|
|
|
|
static PX4UARTDriver uartBDriver(UARTB_DEFAULT_DEVICE, "APM_uartB");
|
2013-02-17 22:55:33 -04:00
|
|
|
static PX4UARTDriver uartCDriver(UARTC_DEFAULT_DEVICE, "APM_uartC");
|
2013-11-22 04:16:51 -04:00
|
|
|
static PX4UARTDriver uartDDriver(UARTD_DEFAULT_DEVICE, "APM_uartD");
|
2013-12-21 07:25:15 -04:00
|
|
|
static PX4UARTDriver uartEDriver(UARTE_DEFAULT_DEVICE, "APM_uartE");
|
2013-01-02 20:03:05 -04:00
|
|
|
|
2012-12-30 04:56:57 -04:00
|
|
|
HAL_PX4::HAL_PX4() :
|
|
|
|
AP_HAL::HAL(
|
2013-07-11 00:01:59 -03:00
|
|
|
&uartADriver, /* uartA */
|
|
|
|
&uartBDriver, /* uartB */
|
|
|
|
&uartCDriver, /* uartC */
|
2013-11-22 04:16:51 -04:00
|
|
|
&uartDDriver, /* uartD */
|
2013-12-21 07:25:15 -04:00
|
|
|
&uartEDriver, /* uartE */
|
2012-12-30 04:56:57 -04:00
|
|
|
&i2cDriver, /* i2c */
|
2015-06-15 14:46:31 -03:00
|
|
|
NULL, /* only one i2c */
|
|
|
|
NULL, /* only one i2c */
|
2012-12-30 04:56:57 -04:00
|
|
|
&spiDeviceManager, /* spi */
|
|
|
|
&analogIn, /* analogin */
|
|
|
|
&storageDriver, /* storage */
|
2013-10-05 05:33:07 -03:00
|
|
|
&uartADriver, /* console */
|
2012-12-30 04:56:57 -04:00
|
|
|
&gpioDriver, /* gpio */
|
|
|
|
&rcinDriver, /* rcinput */
|
|
|
|
&rcoutDriver, /* rcoutput */
|
|
|
|
&schedulerInstance, /* scheduler */
|
|
|
|
&utilInstance) /* util */
|
|
|
|
{}
|
|
|
|
|
2013-07-11 00:01:59 -03:00
|
|
|
bool _px4_thread_should_exit = false; /**< Daemon exit flag */
|
|
|
|
static bool thread_running = false; /**< Daemon status flag */
|
|
|
|
static int daemon_task; /**< Handle of daemon task / thread */
|
2014-07-02 20:09:51 -03:00
|
|
|
bool px4_ran_overtime;
|
2013-01-03 20:14:35 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2013-01-25 05:43:06 -04:00
|
|
|
/*
|
|
|
|
set the priority of the main APM task
|
|
|
|
*/
|
2015-02-15 20:53:08 -04:00
|
|
|
void hal_px4_set_priority(uint8_t priority)
|
2013-01-25 05:43:06 -04:00
|
|
|
{
|
|
|
|
struct sched_param param;
|
|
|
|
param.sched_priority = priority;
|
|
|
|
sched_setscheduler(daemon_task, SCHED_FIFO, ¶m);
|
|
|
|
}
|
|
|
|
|
2013-01-24 00:03:48 -04:00
|
|
|
/*
|
|
|
|
this is called when loop() takes more than 1 second to run. If that
|
|
|
|
happens then something is blocking for a long time in the main
|
|
|
|
sketch - probably waiting on a low priority driver. Set the priority
|
|
|
|
of the APM task low to let the driver run.
|
|
|
|
*/
|
|
|
|
static void loop_overtime(void *)
|
|
|
|
{
|
2015-02-15 20:53:08 -04:00
|
|
|
hal_px4_set_priority(APM_OVERTIME_PRIORITY);
|
2014-07-02 20:09:51 -03:00
|
|
|
px4_ran_overtime = true;
|
2013-01-24 00:03:48 -04:00
|
|
|
}
|
|
|
|
|
2013-01-03 20:14:35 -04:00
|
|
|
static int main_loop(int argc, char **argv)
|
|
|
|
{
|
|
|
|
extern void setup(void);
|
|
|
|
extern void loop(void);
|
2013-01-04 07:25:36 -04:00
|
|
|
|
2013-01-24 00:03:48 -04:00
|
|
|
|
2013-02-17 22:55:33 -04:00
|
|
|
hal.uartA->begin(115200);
|
|
|
|
hal.uartB->begin(38400);
|
|
|
|
hal.uartC->begin(57600);
|
2013-11-22 04:16:51 -04:00
|
|
|
hal.uartD->begin(57600);
|
2013-12-21 07:25:15 -04:00
|
|
|
hal.uartE->begin(57600);
|
2013-01-03 20:14:35 -04:00
|
|
|
hal.scheduler->init(NULL);
|
|
|
|
hal.rcin->init(NULL);
|
2013-01-04 07:25:36 -04:00
|
|
|
hal.rcout->init(NULL);
|
2013-01-20 22:56:57 -04:00
|
|
|
hal.analogin->init(NULL);
|
2013-07-11 00:01:59 -03:00
|
|
|
hal.gpio->init();
|
|
|
|
|
2013-01-03 20:14:35 -04:00
|
|
|
|
2013-01-25 05:43:06 -04:00
|
|
|
/*
|
|
|
|
run setup() at low priority to ensure CLI doesn't hang the
|
|
|
|
system, and to allow initial sensor read loops to run
|
|
|
|
*/
|
2015-02-15 20:53:08 -04:00
|
|
|
hal_px4_set_priority(APM_STARTUP_PRIORITY);
|
2013-01-25 05:43:06 -04:00
|
|
|
|
2013-10-28 02:10:51 -03:00
|
|
|
schedulerInstance.hal_initialized();
|
|
|
|
|
2013-01-03 20:14:35 -04:00
|
|
|
setup();
|
2013-01-10 21:20:43 -04:00
|
|
|
hal.scheduler->system_initialized();
|
2013-01-03 20:14:35 -04:00
|
|
|
|
2013-01-24 00:03:48 -04:00
|
|
|
perf_counter_t perf_loop = perf_alloc(PC_ELAPSED, "APM_loop");
|
|
|
|
perf_counter_t perf_overrun = perf_alloc(PC_COUNT, "APM_overrun");
|
|
|
|
struct hrt_call loop_overtime_call;
|
|
|
|
|
2013-01-20 06:14:18 -04:00
|
|
|
thread_running = true;
|
2013-01-24 00:03:48 -04:00
|
|
|
|
2013-01-25 05:43:06 -04:00
|
|
|
/*
|
|
|
|
switch to high priority for main loop
|
|
|
|
*/
|
2015-02-15 20:53:08 -04:00
|
|
|
hal_px4_set_priority(APM_MAIN_PRIORITY);
|
2013-01-24 00:03:48 -04:00
|
|
|
|
2013-01-20 22:56:57 -04:00
|
|
|
while (!_px4_thread_should_exit) {
|
2013-01-24 00:03:48 -04:00
|
|
|
perf_begin(perf_loop);
|
|
|
|
|
|
|
|
/*
|
|
|
|
this ensures a tight loop waiting on a lower priority driver
|
|
|
|
will eventually give up some time for the driver to run. It
|
|
|
|
will only ever be called if a loop() call runs for more than
|
2013-02-19 20:30:54 -04:00
|
|
|
0.1 second
|
2013-01-24 00:03:48 -04:00
|
|
|
*/
|
2013-02-19 20:30:54 -04:00
|
|
|
hrt_call_after(&loop_overtime_call, 100000, (hrt_callout)loop_overtime, NULL);
|
2013-01-24 00:03:48 -04:00
|
|
|
|
2013-07-11 00:01:59 -03:00
|
|
|
loop();
|
2013-01-24 00:03:48 -04:00
|
|
|
|
2014-07-02 20:09:51 -03:00
|
|
|
if (px4_ran_overtime) {
|
2013-01-24 00:03:48 -04:00
|
|
|
/*
|
|
|
|
we ran over 1s in loop(), and our priority was lowered
|
|
|
|
to let a driver run. Set it back to high priority now.
|
|
|
|
*/
|
2015-02-15 20:53:08 -04:00
|
|
|
hal_px4_set_priority(APM_MAIN_PRIORITY);
|
2013-01-24 00:03:48 -04:00
|
|
|
perf_count(perf_overrun);
|
2014-07-02 20:09:51 -03:00
|
|
|
px4_ran_overtime = false;
|
2013-01-24 00:03:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
perf_end(perf_loop);
|
|
|
|
|
|
|
|
/*
|
2015-02-12 20:35:21 -04:00
|
|
|
give up 250 microseconds of time, to ensure drivers get a
|
2013-10-13 18:29:30 -03:00
|
|
|
chance to run. This relies on the accurate semaphore wait
|
|
|
|
using hrt in semaphore.cpp
|
2013-01-24 00:03:48 -04:00
|
|
|
*/
|
2015-02-12 20:35:21 -04:00
|
|
|
hal.scheduler->delay_microseconds(250);
|
2013-07-11 00:01:59 -03:00
|
|
|
}
|
2013-01-14 00:59:54 -04:00
|
|
|
thread_running = false;
|
2013-01-03 20:14:35 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-14 00:59:54 -04:00
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: %s [options] {start,stop,status}\n", SKETCHNAME);
|
|
|
|
printf("Options:\n");
|
2013-02-17 22:55:33 -04:00
|
|
|
printf("\t-d DEVICE set terminal device (default %s)\n", UARTA_DEFAULT_DEVICE);
|
|
|
|
printf("\t-d2 DEVICE set second terminal device (default %s)\n", UARTC_DEFAULT_DEVICE);
|
2013-12-21 07:25:15 -04:00
|
|
|
printf("\t-d3 DEVICE set 3rd terminal device (default %s)\n", UARTD_DEFAULT_DEVICE);
|
|
|
|
printf("\t-d4 DEVICE set 2nd GPS device (default %s)\n", UARTE_DEFAULT_DEVICE);
|
2013-01-14 00:59:54 -04:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2013-01-03 20:14:35 -04:00
|
|
|
|
2012-12-30 04:56:57 -04:00
|
|
|
void HAL_PX4::init(int argc, char * const argv[]) const
|
|
|
|
{
|
2013-01-14 00:59:54 -04:00
|
|
|
int i;
|
2013-02-17 22:55:33 -04:00
|
|
|
const char *deviceA = UARTA_DEFAULT_DEVICE;
|
|
|
|
const char *deviceC = UARTC_DEFAULT_DEVICE;
|
2013-11-22 04:16:51 -04:00
|
|
|
const char *deviceD = UARTD_DEFAULT_DEVICE;
|
2013-12-21 07:25:15 -04:00
|
|
|
const char *deviceE = UARTE_DEFAULT_DEVICE;
|
2013-01-14 00:59:54 -04:00
|
|
|
|
2013-01-03 20:14:35 -04:00
|
|
|
if (argc < 1) {
|
2013-07-11 00:01:59 -03:00
|
|
|
printf("%s: missing command (try '%s start')",
|
2013-01-03 20:14:35 -04:00
|
|
|
SKETCHNAME, SKETCHNAME);
|
2013-01-14 00:59:54 -04:00
|
|
|
usage();
|
2013-01-03 20:14:35 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-01-14 00:59:54 -04:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
if (strcmp(argv[i], "start") == 0) {
|
|
|
|
if (thread_running) {
|
|
|
|
printf("%s already running\n", SKETCHNAME);
|
|
|
|
/* this is not an error */
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2013-02-17 22:55:33 -04:00
|
|
|
uartADriver.set_device_path(deviceA);
|
|
|
|
uartCDriver.set_device_path(deviceC);
|
2013-11-22 04:16:51 -04:00
|
|
|
uartDDriver.set_device_path(deviceD);
|
2013-12-21 07:25:15 -04:00
|
|
|
uartEDriver.set_device_path(deviceE);
|
|
|
|
printf("Starting %s uartA=%s uartC=%s uartD=%s uartE=%s\n",
|
|
|
|
SKETCHNAME, deviceA, deviceC, deviceD, deviceE);
|
2013-01-14 00:59:54 -04:00
|
|
|
|
2013-01-20 22:56:57 -04:00
|
|
|
_px4_thread_should_exit = false;
|
2015-05-31 01:49:27 -03:00
|
|
|
daemon_task = px4_task_spawn_cmd(SKETCHNAME,
|
|
|
|
SCHED_FIFO,
|
|
|
|
APM_MAIN_PRIORITY,
|
|
|
|
APM_MAIN_THREAD_STACK_SIZE,
|
|
|
|
main_loop,
|
|
|
|
NULL);
|
2013-01-14 00:59:54 -04:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[i], "stop") == 0) {
|
2013-01-20 22:56:57 -04:00
|
|
|
_px4_thread_should_exit = true;
|
2013-01-14 00:59:54 -04:00
|
|
|
exit(0);
|
|
|
|
}
|
2013-01-03 20:14:35 -04:00
|
|
|
|
2013-01-14 00:59:54 -04:00
|
|
|
if (strcmp(argv[i], "status") == 0) {
|
2013-01-20 22:56:57 -04:00
|
|
|
if (_px4_thread_should_exit && thread_running) {
|
|
|
|
printf("\t%s is exiting\n", SKETCHNAME);
|
|
|
|
} else if (thread_running) {
|
2013-01-14 00:59:54 -04:00
|
|
|
printf("\t%s is running\n", SKETCHNAME);
|
|
|
|
} else {
|
|
|
|
printf("\t%s is not started\n", SKETCHNAME);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2013-07-11 00:01:59 -03:00
|
|
|
if (strcmp(argv[i], "-d") == 0) {
|
2013-01-14 00:59:54 -04:00
|
|
|
// set terminal device
|
2013-07-11 00:01:59 -03:00
|
|
|
if (argc > i + 1) {
|
2013-02-17 22:55:33 -04:00
|
|
|
deviceA = strdup(argv[i+1]);
|
2013-07-11 00:01:59 -03:00
|
|
|
} else {
|
|
|
|
printf("missing parameter to -d DEVICE\n");
|
2013-01-14 00:59:54 -04:00
|
|
|
usage();
|
|
|
|
exit(1);
|
2013-07-11 00:01:59 -03:00
|
|
|
}
|
|
|
|
}
|
2013-02-17 22:55:33 -04:00
|
|
|
|
2013-07-11 00:01:59 -03:00
|
|
|
if (strcmp(argv[i], "-d2") == 0) {
|
2013-02-17 22:55:33 -04:00
|
|
|
// set uartC terminal device
|
2013-07-11 00:01:59 -03:00
|
|
|
if (argc > i + 1) {
|
2013-02-17 22:55:33 -04:00
|
|
|
deviceC = strdup(argv[i+1]);
|
2013-07-11 00:01:59 -03:00
|
|
|
} else {
|
|
|
|
printf("missing parameter to -d2 DEVICE\n");
|
2013-02-17 22:55:33 -04:00
|
|
|
usage();
|
|
|
|
exit(1);
|
2013-07-11 00:01:59 -03:00
|
|
|
}
|
|
|
|
}
|
2013-11-22 04:16:51 -04:00
|
|
|
|
|
|
|
if (strcmp(argv[i], "-d3") == 0) {
|
|
|
|
// set uartD terminal device
|
|
|
|
if (argc > i + 1) {
|
|
|
|
deviceD = strdup(argv[i+1]);
|
|
|
|
} else {
|
|
|
|
printf("missing parameter to -d3 DEVICE\n");
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2013-12-21 07:25:15 -04:00
|
|
|
|
|
|
|
if (strcmp(argv[i], "-d4") == 0) {
|
|
|
|
// set uartE 2nd GPS device
|
|
|
|
if (argc > i + 1) {
|
|
|
|
deviceE = strdup(argv[i+1]);
|
|
|
|
} else {
|
|
|
|
printf("missing parameter to -d4 DEVICE\n");
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2013-01-14 00:59:54 -04:00
|
|
|
}
|
2013-01-03 20:14:35 -04:00
|
|
|
|
2013-01-14 00:59:54 -04:00
|
|
|
usage();
|
2013-07-11 00:01:59 -03:00
|
|
|
exit(1);
|
2012-12-30 04:56:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const HAL_PX4 AP_HAL_PX4;
|
|
|
|
|
|
|
|
#endif // CONFIG_HAL_BOARD == HAL_BOARD_PX4
|
|
|
|
|