forked from Archive/PX4-Autopilot
Simplify the PX4IO main loop to cut down on memory consumption.
This commit is contained in:
parent
5aa5645fb0
commit
8c7e2546ed
|
@ -39,7 +39,6 @@
|
|||
|
||||
#include <nuttx/config.h>
|
||||
#include <stdbool.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <systemlib/perf_counter.h>
|
||||
|
@ -53,21 +52,18 @@
|
|||
|
||||
static bool ppm_input(uint16_t *values, uint16_t *num_values);
|
||||
|
||||
void
|
||||
controls_main(void)
|
||||
{
|
||||
struct pollfd fds[2];
|
||||
static perf_counter_t c_gather_dsm;
|
||||
static perf_counter_t c_gather_sbus;
|
||||
static perf_counter_t c_gather_ppm;
|
||||
|
||||
void
|
||||
controls_init(void)
|
||||
{
|
||||
/* DSM input */
|
||||
fds[0].fd = dsm_init("/dev/ttyS0");
|
||||
fds[0].events = POLLIN;
|
||||
dsm_init("/dev/ttyS0");
|
||||
|
||||
/* S.bus input */
|
||||
fds[1].fd = sbus_init("/dev/ttyS2");
|
||||
fds[1].events = POLLIN;
|
||||
|
||||
ASSERT(fds[0].fd >= 0);
|
||||
ASSERT(fds[1].fd >= 0);
|
||||
sbus_init("/dev/ttyS2");
|
||||
|
||||
/* default to a 1:1 input map, all enabled */
|
||||
for (unsigned i = 0; i < MAX_CONTROL_CHANNELS; i++) {
|
||||
|
@ -82,11 +78,13 @@ controls_main(void)
|
|||
r_page_rc_input_config[base + PX4IO_P_RC_CONFIG_OPTIONS] = PX4IO_P_RC_CONFIG_OPTIONS_ENABLED;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
/* run this loop at ~100Hz */
|
||||
int result = poll(fds, 2, 10);
|
||||
c_gather_dsm = perf_alloc(PC_ELAPSED, "c_gather_dsm");
|
||||
c_gather_sbus = perf_alloc(PC_ELAPSED, "c_gather_sbus");
|
||||
c_gather_ppm = perf_alloc(PC_ELAPSED, "c_gather_ppm");
|
||||
}
|
||||
|
||||
ASSERT(result >= 0);
|
||||
void
|
||||
controls_tick() {
|
||||
|
||||
/*
|
||||
* Gather R/C control inputs from supported sources.
|
||||
|
@ -96,25 +94,32 @@ controls_main(void)
|
|||
* other. Don't do that.
|
||||
*/
|
||||
|
||||
perf_begin(c_gather_dsm);
|
||||
bool dsm_updated = dsm_input(r_raw_rc_values, &r_raw_rc_count);
|
||||
if (dsm_updated)
|
||||
r_status_flags |= PX4IO_P_STATUS_FLAGS_RC_DSM;
|
||||
perf_end(c_gather_dsm);
|
||||
|
||||
perf_begin(c_gather_sbus);
|
||||
bool sbus_updated = sbus_input(r_raw_rc_values, &r_raw_rc_count);
|
||||
if (sbus_updated)
|
||||
r_status_flags |= PX4IO_P_STATUS_FLAGS_RC_SBUS;
|
||||
perf_end(c_gather_sbus);
|
||||
|
||||
/*
|
||||
* XXX each S.bus frame will cause a PPM decoder interrupt
|
||||
* storm (lots of edges). It might be sensible to actually
|
||||
* disable the PPM decoder completely if we have S.bus signal.
|
||||
*/
|
||||
perf_begin(c_gather_ppm);
|
||||
bool ppm_updated = ppm_input(r_raw_rc_values, &r_raw_rc_count);
|
||||
if (ppm_updated)
|
||||
r_status_flags |= PX4IO_P_STATUS_FLAGS_RC_PPM;
|
||||
perf_end(c_gather_ppm);
|
||||
|
||||
ASSERT(r_raw_rc_count <= MAX_CONTROL_CHANNELS);
|
||||
|
||||
|
||||
/*
|
||||
* In some cases we may have received a frame, but input has still
|
||||
* been lost.
|
||||
|
@ -275,8 +280,6 @@ controls_main(void)
|
|||
r_status_flags &= ~PX4IO_P_STATUS_FLAGS_OVERRIDE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
|
|
|
@ -68,14 +68,8 @@ static struct hrt_call serial_dma_call;
|
|||
volatile uint8_t debug_level = 0;
|
||||
volatile uint32_t i2c_loop_resets = 0;
|
||||
|
||||
struct hrt_call loop_overtime_call;
|
||||
|
||||
// this allows wakeup of the main task via a signal
|
||||
static pid_t daemon_pid;
|
||||
|
||||
|
||||
/*
|
||||
a set of debug buffers to allow us to send debug information from ISRs
|
||||
* a set of debug buffers to allow us to send debug information from ISRs
|
||||
*/
|
||||
|
||||
static volatile uint32_t msg_counter;
|
||||
|
@ -91,9 +85,10 @@ static volatile uint8_t msg_next_out, msg_next_in;
|
|||
static char msg[NUM_MSG][40];
|
||||
|
||||
/*
|
||||
add a debug message to be printed on the console
|
||||
* add a debug message to be printed on the console
|
||||
*/
|
||||
void isr_debug(uint8_t level, const char *fmt, ...)
|
||||
void
|
||||
isr_debug(uint8_t level, const char *fmt, ...)
|
||||
{
|
||||
if (level > debug_level) {
|
||||
return;
|
||||
|
@ -107,9 +102,10 @@ void isr_debug(uint8_t level, const char *fmt, ...)
|
|||
}
|
||||
|
||||
/*
|
||||
show all pending debug messages
|
||||
* show all pending debug messages
|
||||
*/
|
||||
static void show_debug_messages(void)
|
||||
static void
|
||||
show_debug_messages(void)
|
||||
{
|
||||
if (msg_counter != last_msg_counter) {
|
||||
uint32_t n = msg_counter - last_msg_counter;
|
||||
|
@ -122,36 +118,9 @@ static void show_debug_messages(void)
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
catch I2C lockups
|
||||
*/
|
||||
static void loop_overtime(void *arg)
|
||||
int
|
||||
user_start(int argc, char *argv[])
|
||||
{
|
||||
lowsyslog("I2C RESET\n");
|
||||
i2c_loop_resets++;
|
||||
i2c_dump();
|
||||
i2c_reset();
|
||||
hrt_call_after(&loop_overtime_call, 50000, (hrt_callout)loop_overtime, NULL);
|
||||
}
|
||||
|
||||
static void wakeup_handler(int signo, siginfo_t *info, void *ucontext)
|
||||
{
|
||||
/* nothing to do - we just want poll() to return */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
wakeup the main task using a signal
|
||||
*/
|
||||
void daemon_wakeup(void)
|
||||
{
|
||||
kill(daemon_pid, SIGUSR1);
|
||||
}
|
||||
|
||||
int user_start(int argc, char *argv[])
|
||||
{
|
||||
daemon_pid = getpid();
|
||||
|
||||
/* run C++ ctors before we go any further */
|
||||
up_cxxinitialize();
|
||||
|
||||
|
@ -184,18 +153,27 @@ int user_start(int argc, char *argv[])
|
|||
/* configure the first 8 PWM outputs (i.e. all of them) */
|
||||
up_pwm_servo_init(0xff);
|
||||
|
||||
/* start the flight control signal handler */
|
||||
int ret = task_create("FCon",
|
||||
SCHED_PRIORITY_DEFAULT,
|
||||
1024,
|
||||
(main_t)controls_main,
|
||||
NULL);
|
||||
/* initialise the control inputs */
|
||||
controls_init();
|
||||
|
||||
/* start the i2c handler */
|
||||
i2c_init();
|
||||
|
||||
/* add a performance counter for mixing */
|
||||
perf_counter_t mixer_perf = perf_alloc(PC_ELAPSED, "mix");
|
||||
|
||||
/* add a performance counter for controls */
|
||||
perf_counter_t controls_perf = perf_alloc(PC_ELAPSED, "controls");
|
||||
|
||||
/* and one for measuring the loop rate */
|
||||
perf_counter_t loop_perf = perf_alloc(PC_INTERVAL, "loop");
|
||||
|
||||
struct mallinfo minfo = mallinfo();
|
||||
lowsyslog("MEM: free %u, largest %u\n", minfo.mxordblk, minfo.fordblks);
|
||||
|
||||
#if 0
|
||||
/* not enough memory, lock down */
|
||||
if (ret != OK || minfo.mxordblk < 500) {
|
||||
if (minfo.mxordblk < 500) {
|
||||
lowsyslog("ERR: not enough MEM");
|
||||
bool phase = false;
|
||||
|
||||
|
@ -210,46 +188,33 @@ int user_start(int argc, char *argv[])
|
|||
phase = !phase;
|
||||
usleep(300000);
|
||||
}
|
||||
|
||||
/* start the i2c handler */
|
||||
i2c_init();
|
||||
|
||||
/* add a performance counter for mixing */
|
||||
perf_counter_t mixer_perf = perf_alloc(PC_ELAPSED, "mix");
|
||||
#endif
|
||||
|
||||
/*
|
||||
* setup a null handler for SIGUSR1 - we will use this for wakeup from poll()
|
||||
* Run everything in a tight loop.
|
||||
*/
|
||||
struct sigaction sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_sigaction = wakeup_handler;
|
||||
sigfillset(&sa.sa_mask);
|
||||
sigdelset(&sa.sa_mask, SIGUSR1);
|
||||
if (sigaction(SIGUSR1, &sa, NULL) != OK) {
|
||||
lowsyslog("SIGUSR1 init fail\n");
|
||||
}
|
||||
|
||||
/*
|
||||
run the mixer at ~50Hz, using signals to run it early if
|
||||
need be
|
||||
*/
|
||||
uint64_t last_debug_time = 0;
|
||||
for (;;) {
|
||||
/*
|
||||
if we are not scheduled for 30ms then reset the I2C bus
|
||||
*/
|
||||
hrt_call_after(&loop_overtime_call, 30000, (hrt_callout)loop_overtime, NULL);
|
||||
|
||||
// we use usleep() instead of poll() as poll() is not
|
||||
// interrupted by signals in nuttx, whereas usleep() is
|
||||
usleep(20000);
|
||||
/* track the rate at which the loop is running */
|
||||
perf_count(loop_perf);
|
||||
|
||||
/* kick the mixer */
|
||||
perf_begin(mixer_perf);
|
||||
mixer_tick();
|
||||
perf_end(mixer_perf);
|
||||
|
||||
/* kick the control inputs */
|
||||
perf_begin(controls_perf);
|
||||
controls_tick();
|
||||
perf_end(controls_perf);
|
||||
|
||||
/* check for debug activity */
|
||||
show_debug_messages();
|
||||
if (hrt_absolute_time() - last_debug_time > 1000000) {
|
||||
|
||||
/* post debug state at ~1Hz */
|
||||
if (hrt_absolute_time() - last_debug_time > (1000 * 1000)) {
|
||||
isr_debug(1, "d:%u s=0x%x a=0x%x f=0x%x r=%u",
|
||||
(unsigned)debug_level,
|
||||
(unsigned)r_status_flags,
|
||||
|
|
|
@ -170,7 +170,8 @@ extern uint16_t adc_measure(unsigned channel);
|
|||
*
|
||||
* Input functions return true when they receive an update from the RC controller.
|
||||
*/
|
||||
extern void controls_main(void);
|
||||
extern void controls_init(void);
|
||||
extern void controls_tick(void);
|
||||
extern int dsm_init(const char *device);
|
||||
extern bool dsm_input(uint16_t *values, uint16_t *num_values);
|
||||
extern int sbus_init(const char *device);
|
||||
|
@ -179,11 +180,6 @@ extern bool sbus_input(uint16_t *values, uint16_t *num_values);
|
|||
/** global debug level for isr_debug() */
|
||||
extern volatile uint8_t debug_level;
|
||||
|
||||
/**
|
||||
* Wake up mixer.
|
||||
*/
|
||||
void daemon_wakeup(void);
|
||||
|
||||
/* send a debug message to the console */
|
||||
extern void isr_debug(uint8_t level, const char *fmt, ...);
|
||||
|
||||
|
|
|
@ -203,8 +203,6 @@ registers_set(uint8_t page, uint8_t offset, const uint16_t *values, unsigned num
|
|||
r_status_flags |= PX4IO_P_STATUS_FLAGS_FMU_OK;
|
||||
r_status_flags &= ~PX4IO_P_STATUS_FLAGS_RAW_PWM;
|
||||
|
||||
// wake up daemon to trigger mixer
|
||||
daemon_wakeup();
|
||||
break;
|
||||
|
||||
/* handle raw PWM input */
|
||||
|
@ -224,8 +222,6 @@ registers_set(uint8_t page, uint8_t offset, const uint16_t *values, unsigned num
|
|||
system_state.fmu_data_received_time = hrt_absolute_time();
|
||||
r_status_flags |= PX4IO_P_STATUS_FLAGS_FMU_OK | PX4IO_P_STATUS_FLAGS_RAW_PWM;
|
||||
|
||||
// wake up the main thread to trigger mixer
|
||||
daemon_wakeup();
|
||||
break;
|
||||
|
||||
/* handle setup for servo failsafe values */
|
||||
|
|
|
@ -401,11 +401,11 @@ CONFIG_SCHED_ATEXIT=n
|
|||
CONFIG_DISABLE_CLOCK=n
|
||||
CONFIG_DISABLE_POSIX_TIMERS=y
|
||||
CONFIG_DISABLE_PTHREAD=y
|
||||
CONFIG_DISABLE_SIGNALS=n
|
||||
CONFIG_DISABLE_SIGNALS=y
|
||||
CONFIG_DISABLE_MQUEUE=y
|
||||
CONFIG_DISABLE_MOUNTPOINT=y
|
||||
CONFIG_DISABLE_ENVIRON=y
|
||||
CONFIG_DISABLE_POLL=n
|
||||
CONFIG_DISABLE_POLL=y
|
||||
|
||||
#
|
||||
# Misc libc settings
|
||||
|
@ -541,7 +541,7 @@ CONFIG_BOOT_COPYTORAM=n
|
|||
CONFIG_CUSTOM_STACK=n
|
||||
CONFIG_STACK_POINTER=
|
||||
CONFIG_IDLETHREAD_STACKSIZE=1024
|
||||
CONFIG_USERMAIN_STACKSIZE=1024
|
||||
CONFIG_USERMAIN_STACKSIZE=800
|
||||
CONFIG_PTHREAD_STACK_MIN=512
|
||||
CONFIG_PTHREAD_STACK_DEFAULT=1024
|
||||
CONFIG_HEAP_BASE=
|
||||
|
|
Loading…
Reference in New Issue