2013-01-11 20:59:20 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2013-01-11 20:59:20 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* main loop scheduler for APM
|
|
|
|
* Author: Andrew Tridgell, January 2013
|
|
|
|
*
|
|
|
|
*/
|
2016-02-17 21:25:48 -04:00
|
|
|
#pragma once
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2015-08-11 03:28:45 -03:00
|
|
|
#include <AP_Param/AP_Param.h>
|
2016-04-21 01:50:15 -03:00
|
|
|
#include <AP_HAL/Util.h>
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2015-07-28 15:20:38 -03:00
|
|
|
#define AP_SCHEDULER_NAME_INITIALIZER(_name) .name = #_name,
|
|
|
|
|
2015-11-30 15:59:07 -04:00
|
|
|
/*
|
|
|
|
useful macro for creating scheduler task table
|
|
|
|
*/
|
|
|
|
#define SCHED_TASK_CLASS(classname, classptr, func, _rate_hz, _max_time_micros) { \
|
|
|
|
.function = FUNCTOR_BIND(classptr, &classname::func, void),\
|
|
|
|
AP_SCHEDULER_NAME_INITIALIZER(func)\
|
|
|
|
.rate_hz = _rate_hz,\
|
|
|
|
.max_time_micros = _max_time_micros\
|
|
|
|
}
|
|
|
|
|
2013-01-11 20:59:20 -04:00
|
|
|
/*
|
|
|
|
A task scheduler for APM main loops
|
|
|
|
|
|
|
|
Sketches should call scheduler.init() on startup, then call
|
2015-05-24 16:04:34 -03:00
|
|
|
scheduler.tick() at regular intervals (typically every 10ms).
|
2013-01-11 20:59:20 -04:00
|
|
|
|
|
|
|
To run tasks use scheduler.run(), passing the amount of time that
|
|
|
|
the scheduler is allowed to use before it must return
|
|
|
|
*/
|
|
|
|
|
2015-08-11 03:28:45 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Vehicle/AP_Vehicle.h>
|
2015-05-12 03:59:38 -03:00
|
|
|
|
2013-01-11 20:59:20 -04:00
|
|
|
class AP_Scheduler
|
|
|
|
{
|
|
|
|
public:
|
2015-11-30 15:59:07 -04:00
|
|
|
// constructor
|
|
|
|
AP_Scheduler(void);
|
|
|
|
|
2015-05-24 18:55:06 -03:00
|
|
|
FUNCTOR_TYPEDEF(task_fn_t, void);
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2015-05-24 16:04:34 -03:00
|
|
|
struct Task {
|
|
|
|
task_fn_t function;
|
2015-07-28 13:21:52 -03:00
|
|
|
const char *name;
|
2015-11-30 15:59:07 -04:00
|
|
|
float rate_hz;
|
2015-05-24 16:04:34 -03:00
|
|
|
uint16_t max_time_micros;
|
|
|
|
};
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2015-05-24 16:04:34 -03:00
|
|
|
// initialise scheduler
|
2015-05-26 01:33:30 -03:00
|
|
|
void init(const Task *tasks, uint8_t num_tasks);
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2015-05-24 16:04:34 -03:00
|
|
|
// call when one tick has passed
|
|
|
|
void tick(void);
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2015-05-24 16:04:34 -03:00
|
|
|
// run the tasks. Call this once per 'tick'.
|
|
|
|
// time_available is the amount of time available to run
|
|
|
|
// tasks in microseconds
|
|
|
|
void run(uint16_t time_available);
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2015-05-24 16:04:34 -03:00
|
|
|
// return the number of microseconds available for the current task
|
|
|
|
uint16_t time_available_usec(void);
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2013-01-11 21:06:55 -04:00
|
|
|
// return debug parameter
|
|
|
|
uint8_t debug(void) { return _debug; }
|
|
|
|
|
2013-07-23 02:00:00 -03:00
|
|
|
// return load average, as a number between 0 and 1. 1 means
|
|
|
|
// 100% load. Calculated from how much spare time we have at the
|
|
|
|
// end of a run()
|
|
|
|
float load_average(uint32_t tick_time_usec) const;
|
|
|
|
|
2015-11-30 15:59:07 -04:00
|
|
|
// get the configured main loop rate
|
|
|
|
uint16_t get_loop_rate_hz(void) const {
|
|
|
|
return _loop_rate_hz;
|
|
|
|
}
|
|
|
|
|
2015-05-24 16:04:34 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2014-07-02 20:09:11 -03:00
|
|
|
// current running task, or -1 if none. Used to debug stuck tasks
|
|
|
|
static int8_t current_task;
|
|
|
|
|
2013-01-11 20:59:20 -04:00
|
|
|
private:
|
2015-05-24 16:04:34 -03:00
|
|
|
// used to enable scheduler debugging
|
|
|
|
AP_Int8 _debug;
|
|
|
|
|
2015-11-30 15:59:07 -04:00
|
|
|
// overall scheduling rate in Hz
|
|
|
|
AP_Int16 _loop_rate_hz;
|
|
|
|
|
2015-05-24 16:04:34 -03:00
|
|
|
// progmem list of tasks to run
|
|
|
|
const struct Task *_tasks;
|
|
|
|
|
|
|
|
// number of tasks in _tasks list
|
|
|
|
uint8_t _num_tasks;
|
|
|
|
|
|
|
|
// number of 'ticks' that have passed (number of times that
|
|
|
|
// tick() has been called
|
|
|
|
uint16_t _tick_counter;
|
|
|
|
|
|
|
|
// tick counter at the time we last ran each task
|
|
|
|
uint16_t *_last_run;
|
|
|
|
|
|
|
|
// number of microseconds allowed for the current task
|
|
|
|
uint32_t _task_time_allowed;
|
|
|
|
|
|
|
|
// the time in microseconds when the task started
|
|
|
|
uint32_t _task_time_started;
|
2013-07-23 02:00:00 -03:00
|
|
|
|
|
|
|
// number of spare microseconds accumulated
|
|
|
|
uint32_t _spare_micros;
|
|
|
|
|
|
|
|
// number of ticks that _spare_micros is counted over
|
|
|
|
uint8_t _spare_ticks;
|
2016-04-21 01:50:15 -03:00
|
|
|
|
|
|
|
// performance counters
|
|
|
|
AP_HAL::Util::perf_counter_t *_perf_counters;
|
2013-01-11 20:59:20 -04:00
|
|
|
};
|