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
|
|
|
|
2023-06-15 06:50:02 -03:00
|
|
|
#include "AP_Scheduler_config.h"
|
2021-05-19 23:34:14 -03:00
|
|
|
|
2024-03-07 03:52:58 -04:00
|
|
|
#if AP_SCHEDULER_ENABLED
|
|
|
|
|
2015-08-11 03:28:45 -03:00
|
|
|
#include <AP_Param/AP_Param.h>
|
2022-02-28 21:19:19 -04:00
|
|
|
#include <AP_HAL/Semaphores.h>
|
2016-04-21 01:50:15 -03:00
|
|
|
#include <AP_HAL/Util.h>
|
2017-08-29 09:42:55 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2017-11-13 03:11:27 -04:00
|
|
|
#include "PerfInfo.h" // loop perf monitoring
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2023-06-15 06:55:49 -03:00
|
|
|
#if AP_SCHEDULER_EXTENDED_TASKINFO_ENABLED
|
2020-09-04 15:55:45 -03:00
|
|
|
#define AP_SCHEDULER_NAME_INITIALIZER(_clazz,_name) .name = #_clazz "::" #_name,
|
2022-04-01 16:57:03 -03:00
|
|
|
#define AP_FAST_NAME_INITIALIZER(_clazz,_name) .name = #_clazz "::" #_name "*",
|
2023-06-15 06:55:49 -03:00
|
|
|
#else
|
|
|
|
#define AP_SCHEDULER_NAME_INITIALIZER(_clazz,_name) .name = #_name,
|
|
|
|
#define AP_FAST_NAME_INITIALIZER(_clazz,_name) .name = #_name "*",
|
2020-09-04 15:55:45 -03:00
|
|
|
#endif
|
2020-01-01 16:36:19 -04:00
|
|
|
#define LOOP_RATE 0
|
2015-07-28 15:20:38 -03:00
|
|
|
|
2015-11-30 15:59:07 -04:00
|
|
|
/*
|
|
|
|
useful macro for creating scheduler task table
|
|
|
|
*/
|
2021-11-13 23:18:09 -04:00
|
|
|
#define SCHED_TASK_CLASS(classname, classptr, func, _rate_hz, _max_time_micros, _priority) { \
|
2015-11-30 15:59:07 -04:00
|
|
|
.function = FUNCTOR_BIND(classptr, &classname::func, void),\
|
2020-09-04 15:55:45 -03:00
|
|
|
AP_SCHEDULER_NAME_INITIALIZER(classname, func)\
|
2015-11-30 15:59:07 -04:00
|
|
|
.rate_hz = _rate_hz,\
|
2021-11-13 23:18:09 -04:00
|
|
|
.max_time_micros = _max_time_micros, \
|
|
|
|
.priority = _priority \
|
2015-11-30 15:59:07 -04:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:57:03 -03:00
|
|
|
/*
|
|
|
|
useful macro for creating the fastloop task table
|
|
|
|
*/
|
|
|
|
#define FAST_TASK_CLASS(classname, classptr, func) { \
|
|
|
|
.function = FUNCTOR_BIND(classptr, &classname::func, void),\
|
|
|
|
AP_FAST_NAME_INITIALIZER(classname, func)\
|
|
|
|
.rate_hz = 0,\
|
|
|
|
.max_time_micros = 0,\
|
|
|
|
.priority = AP_Scheduler::FAST_TASK_PRI0 \
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
class AP_Scheduler
|
|
|
|
{
|
|
|
|
public:
|
2022-04-01 16:57:03 -03:00
|
|
|
AP_Scheduler();
|
2017-08-23 04:40:41 -03:00
|
|
|
|
|
|
|
/* Do not allow copies */
|
2022-09-30 06:50:43 -03:00
|
|
|
CLASS_NO_COPY(AP_Scheduler);
|
2017-08-23 04:40:41 -03:00
|
|
|
|
2019-02-10 01:03:20 -04:00
|
|
|
static AP_Scheduler *get_singleton();
|
|
|
|
static AP_Scheduler *_singleton;
|
2018-05-03 21:44:50 -03:00
|
|
|
|
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;
|
2021-11-13 23:18:09 -04:00
|
|
|
uint8_t priority; // task priority
|
2015-05-24 16:04:34 -03:00
|
|
|
};
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2020-09-04 15:55:45 -03:00
|
|
|
enum class Options : uint8_t {
|
|
|
|
RECORD_TASK_INFO = 1 << 0
|
|
|
|
};
|
|
|
|
|
2022-04-01 16:57:03 -03:00
|
|
|
enum FastTaskPriorities {
|
|
|
|
FAST_TASK_PRI0 = 0,
|
|
|
|
FAST_TASK_PRI1 = 1,
|
|
|
|
FAST_TASK_PRI2 = 2,
|
|
|
|
MAX_FAST_TASK_PRIORITIES = 3
|
|
|
|
};
|
|
|
|
|
2015-05-24 16:04:34 -03:00
|
|
|
// initialise scheduler
|
2018-02-09 18:38:17 -04:00
|
|
|
void init(const Task *tasks, uint8_t num_tasks, uint32_t log_performance_bit);
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2017-11-13 03:11:27 -04:00
|
|
|
// called by vehicle's main loop - which should be the only thing
|
|
|
|
// that function does
|
|
|
|
void loop();
|
|
|
|
|
2017-11-13 04:25:54 -04:00
|
|
|
// call to update any logging the scheduler might do; call at 1Hz
|
2018-02-09 18:38:17 -04:00
|
|
|
void update_logging();
|
2017-11-15 23:20:04 -04:00
|
|
|
|
2019-02-11 04:13:35 -04:00
|
|
|
// write out PERF message to logger
|
2017-11-15 23:20:04 -04:00
|
|
|
void Log_Write_Performance();
|
2017-11-13 04:25:54 -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
|
|
|
|
2017-11-13 01:58:19 -04:00
|
|
|
// return current tick counter
|
|
|
|
uint16_t ticks() const { return _tick_counter; }
|
2023-01-28 05:42:04 -04:00
|
|
|
uint32_t ticks32() const { return _tick_counter32; }
|
2017-11-13 01:58:19 -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
|
2016-07-25 19:27:24 -03:00
|
|
|
void run(uint32_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
|
2021-02-01 12:26:34 -04:00
|
|
|
uint16_t time_available_usec(void) const;
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2013-01-11 21:06:55 -04:00
|
|
|
// return debug parameter
|
2018-02-08 21:47:24 -04:00
|
|
|
uint8_t debug_flags(void) { return _debug; }
|
2013-01-11 21:06:55 -04:00
|
|
|
|
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()
|
2018-01-17 03:20:44 -04:00
|
|
|
float load_average();
|
|
|
|
|
|
|
|
// get the active main loop rate
|
|
|
|
uint16_t get_loop_rate_hz(void) {
|
|
|
|
if (_active_loop_rate_hz == 0) {
|
|
|
|
_active_loop_rate_hz = _loop_rate_hz;
|
|
|
|
}
|
|
|
|
return _active_loop_rate_hz;
|
2015-11-30 15:59:07 -04:00
|
|
|
}
|
2018-01-17 03:20:44 -04:00
|
|
|
// get the time-allowed-per-loop in microseconds
|
|
|
|
uint32_t get_loop_period_us() {
|
|
|
|
if (_loop_period_us == 0) {
|
|
|
|
_loop_period_us = 1000000UL / _loop_rate_hz;
|
|
|
|
}
|
|
|
|
return _loop_period_us;
|
2017-07-30 02:02:40 -03:00
|
|
|
}
|
2018-01-17 03:20:44 -04:00
|
|
|
// get the time-allowed-per-loop in seconds
|
|
|
|
float get_loop_period_s() {
|
|
|
|
if (is_zero(_loop_period_s)) {
|
2019-04-04 01:59:12 -03:00
|
|
|
_loop_period_s = 1.0f / _loop_rate_hz;
|
2018-01-17 03:20:44 -04:00
|
|
|
}
|
|
|
|
return _loop_period_s;
|
2017-08-29 09:42:55 -03:00
|
|
|
}
|
2017-07-30 02:02:40 -03:00
|
|
|
|
2022-11-29 23:27:06 -04:00
|
|
|
// get the filtered main loop time in seconds
|
2018-02-09 20:48:20 -04:00
|
|
|
float get_filtered_loop_time(void) const {
|
|
|
|
return perf_info.get_filtered_time();
|
|
|
|
}
|
2018-02-11 23:42:54 -04:00
|
|
|
|
2022-11-29 23:27:06 -04:00
|
|
|
// get the filtered active main loop rate
|
|
|
|
float get_filtered_loop_rate_hz() {
|
|
|
|
return perf_info.get_filtered_loop_rate_hz();
|
|
|
|
}
|
|
|
|
|
2018-02-11 23:42:54 -04:00
|
|
|
// get the time in seconds that the last loop took
|
|
|
|
float get_last_loop_time_s(void) const {
|
|
|
|
return _last_loop_time_s;
|
|
|
|
}
|
2019-09-17 05:13:28 -03:00
|
|
|
|
2024-08-28 17:19:38 -03:00
|
|
|
// get the time in microseconds that the current loop started
|
|
|
|
uint64_t get_loop_start_time_us(void) const {
|
|
|
|
return _loop_sample_time_us;
|
|
|
|
}
|
|
|
|
|
2019-09-17 05:13:28 -03:00
|
|
|
// get the amount of extra time being added on each loop
|
|
|
|
uint32_t get_extra_loop_us(void) const {
|
|
|
|
return extra_loop_us;
|
|
|
|
}
|
|
|
|
|
2020-02-11 19:10:45 -04:00
|
|
|
HAL_Semaphore &get_semaphore(void) { return _rsem; }
|
|
|
|
|
2020-12-30 02:23:27 -04:00
|
|
|
void task_info(ExpandingString &str);
|
2020-09-04 15:55:45 -03:00
|
|
|
|
2015-05-24 16:04:34 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
2013-01-11 20:59:20 -04:00
|
|
|
|
2017-11-13 03:11:27 -04:00
|
|
|
// loop performance monitoring:
|
|
|
|
AP::PerfInfo perf_info;
|
|
|
|
|
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
|
2017-08-29 09:42:55 -03:00
|
|
|
AP_Int16 _loop_rate_hz;
|
2017-08-23 04:40:41 -03:00
|
|
|
|
2018-01-17 03:20:44 -04:00
|
|
|
// loop rate in Hz as set at startup
|
|
|
|
AP_Int16 _active_loop_rate_hz;
|
2020-09-04 15:55:45 -03:00
|
|
|
|
|
|
|
// scheduler options
|
|
|
|
AP_Int8 _options;
|
2018-01-17 03:20:44 -04:00
|
|
|
|
|
|
|
// calculated loop period in usec
|
|
|
|
uint16_t _loop_period_us;
|
|
|
|
|
|
|
|
// calculated loop period in seconds
|
|
|
|
float _loop_period_s;
|
|
|
|
|
2021-11-13 23:18:09 -04:00
|
|
|
// list of tasks to run
|
|
|
|
const struct Task *_vehicle_tasks;
|
|
|
|
uint8_t _num_vehicle_tasks;
|
2015-05-24 16:04:34 -03:00
|
|
|
|
2021-11-13 23:18:09 -04:00
|
|
|
// list of common tasks to run
|
2019-12-18 18:35:05 -04:00
|
|
|
const struct Task *_common_tasks;
|
2021-11-13 23:18:09 -04:00
|
|
|
uint8_t _num_common_tasks;
|
2019-12-18 18:35:05 -04:00
|
|
|
|
|
|
|
// total number of tasks in _tasks and _common_tasks list
|
2015-05-24 16:04:34 -03:00
|
|
|
uint8_t _num_tasks;
|
|
|
|
|
|
|
|
// number of 'ticks' that have passed (number of times that
|
|
|
|
// tick() has been called
|
|
|
|
uint16_t _tick_counter;
|
2023-01-28 05:42:04 -04:00
|
|
|
uint32_t _tick_counter32;
|
2015-05-24 16:04:34 -03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2024-08-28 17:19:38 -03:00
|
|
|
// start of previous loop
|
2018-02-11 23:42:54 -04:00
|
|
|
uint32_t _loop_timer_start_us;
|
2017-11-13 03:11:27 -04:00
|
|
|
|
2018-02-11 23:42:54 -04:00
|
|
|
// time of last loop in seconds
|
2018-02-13 00:27:00 -04:00
|
|
|
float _last_loop_time_s;
|
2024-08-28 17:19:38 -03:00
|
|
|
|
|
|
|
// start of current loop
|
|
|
|
uint64_t _loop_sample_time_us;
|
|
|
|
|
2019-02-11 04:13:35 -04:00
|
|
|
// bitmask bit which indicates if we should log PERF message
|
2018-02-09 18:38:17 -04:00
|
|
|
uint32_t _log_performance_bit;
|
2019-09-17 05:13:28 -03:00
|
|
|
|
|
|
|
// maximum task slowdown compared to desired task rate before we
|
|
|
|
// start giving extra time per loop
|
|
|
|
const uint8_t max_task_slowdown = 4;
|
|
|
|
|
|
|
|
// counters to handle dynamically adjusting extra loop time to
|
|
|
|
// cope with low CPU conditions
|
|
|
|
uint32_t task_not_achieved;
|
|
|
|
uint32_t task_all_achieved;
|
|
|
|
|
|
|
|
// extra time available for each loop - used to dynamically adjust
|
|
|
|
// the loop rate in case we are well over budget
|
|
|
|
uint32_t extra_loop_us;
|
2020-02-11 19:10:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
// semaphore that is held while not waiting for ins samples
|
|
|
|
HAL_Semaphore _rsem;
|
2013-01-11 20:59:20 -04:00
|
|
|
};
|
2018-05-03 21:44:50 -03:00
|
|
|
|
|
|
|
namespace AP {
|
|
|
|
AP_Scheduler &scheduler();
|
|
|
|
};
|
2024-03-07 03:52:58 -04:00
|
|
|
|
|
|
|
#endif // AP_SCHEDULER_ENABLED
|