2016-07-07 05:26:11 -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/>.
|
|
|
|
*/
|
|
|
|
|
2018-02-09 19:08:54 -04:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
#if AP_MODULE_SUPPORTED
|
|
|
|
|
2016-07-07 05:26:11 -03:00
|
|
|
/*
|
|
|
|
support for external modules
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#if defined(HAVE_LIBDL)
|
2017-08-28 11:05:04 -03:00
|
|
|
#include <dirent.h>
|
2016-07-07 05:26:11 -03:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
#include <AP_Module/AP_Module.h>
|
|
|
|
#include <AP_Module/AP_Module_Structures.h>
|
|
|
|
|
|
|
|
struct AP_Module::hook_list *AP_Module::hooks[NUM_HOOKS];
|
|
|
|
|
|
|
|
const char *AP_Module::hook_names[AP_Module::NUM_HOOKS] = {
|
2016-07-13 22:30:34 -03:00
|
|
|
"ap_hook_setup_start",
|
|
|
|
"ap_hook_setup_complete",
|
|
|
|
"ap_hook_AHRS_update",
|
|
|
|
"ap_hook_gyro_sample",
|
|
|
|
"ap_hook_accel_sample",
|
2016-07-07 05:26:11 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
scan a module for hook symbols
|
|
|
|
*/
|
|
|
|
void AP_Module::module_scan(const char *path)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_LIBDL)
|
|
|
|
void *m = dlopen(path, RTLD_NOW);
|
|
|
|
if (m == nullptr) {
|
|
|
|
printf("dlopen(%s) -> %s\n", path, dlerror());
|
|
|
|
return;
|
|
|
|
}
|
2016-10-16 19:31:51 -03:00
|
|
|
uint8_t found_hooks = 0;
|
2016-07-07 05:26:11 -03:00
|
|
|
for (uint16_t i=0; i<NUM_HOOKS; i++) {
|
|
|
|
void *s = dlsym(m, hook_names[i]);
|
|
|
|
if (s != nullptr) {
|
|
|
|
// found a hook in this module, add it to the list
|
|
|
|
struct hook_list *h = new hook_list;
|
|
|
|
if (h == nullptr) {
|
|
|
|
AP_HAL::panic("Failed to allocate hook for %s", hook_names[i]);
|
|
|
|
}
|
|
|
|
h->next = hooks[i];
|
|
|
|
h->symbol = s;
|
|
|
|
hooks[i] = h;
|
2016-10-16 19:31:51 -03:00
|
|
|
found_hooks++;
|
2016-07-07 05:26:11 -03:00
|
|
|
}
|
|
|
|
}
|
2016-10-16 19:31:51 -03:00
|
|
|
if (found_hooks == 0) {
|
2016-07-07 05:26:11 -03:00
|
|
|
// we don't need this module
|
|
|
|
dlclose(m);
|
2016-10-16 19:31:51 -03:00
|
|
|
} else {
|
|
|
|
printf("AP_Module: Loaded %u hooks from %s\n", (unsigned)found_hooks, path);
|
2016-07-07 05:26:11 -03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
initialise AP_Module, looking for shared libraries in the given module path
|
|
|
|
*/
|
|
|
|
void AP_Module::init(const char *module_path)
|
|
|
|
{
|
2017-08-28 11:05:04 -03:00
|
|
|
#if AP_MODULE_SUPPORTED
|
2016-07-07 05:26:11 -03:00
|
|
|
// scan through module directory looking for *.so
|
|
|
|
DIR *d;
|
|
|
|
struct dirent *de;
|
|
|
|
d = opendir(module_path);
|
|
|
|
if (d == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while ((de = readdir(d))) {
|
|
|
|
const char *extension = strrchr(de->d_name, '.');
|
|
|
|
if (extension == nullptr || strcmp(extension, ".so") != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char *path = nullptr;
|
|
|
|
if (asprintf(&path, "%s/%s", module_path, de->d_name) == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
module_scan(path);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
closedir(d);
|
2017-08-28 11:05:04 -03:00
|
|
|
#endif
|
2016-07-07 05:26:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
call any setup_start hooks
|
|
|
|
*/
|
|
|
|
void AP_Module::call_hook_setup_start(void)
|
|
|
|
{
|
2016-07-13 22:39:54 -03:00
|
|
|
#if AP_MODULE_SUPPORTED
|
2016-07-07 05:26:11 -03:00
|
|
|
uint64_t now = AP_HAL::micros64();
|
|
|
|
for (const struct hook_list *h=hooks[HOOK_SETUP_START]; h; h=h->next) {
|
2016-07-13 22:30:34 -03:00
|
|
|
ap_hook_setup_start_fn_t fn = reinterpret_cast<ap_hook_setup_start_fn_t>(h->symbol);
|
2016-07-07 05:26:11 -03:00
|
|
|
fn(now);
|
|
|
|
}
|
2016-07-13 22:39:54 -03:00
|
|
|
#endif
|
2016-07-07 05:26:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
call any setup_complete hooks
|
|
|
|
*/
|
|
|
|
void AP_Module::call_hook_setup_complete(void)
|
|
|
|
{
|
2016-07-13 22:39:54 -03:00
|
|
|
#if AP_MODULE_SUPPORTED
|
2016-07-07 05:26:11 -03:00
|
|
|
uint64_t now = AP_HAL::micros64();
|
|
|
|
for (const struct hook_list *h=hooks[HOOK_SETUP_COMPLETE]; h; h=h->next) {
|
2016-07-13 22:30:34 -03:00
|
|
|
ap_hook_setup_complete_fn_t fn = reinterpret_cast<ap_hook_setup_complete_fn_t>(h->symbol);
|
2016-07-07 05:26:11 -03:00
|
|
|
fn(now);
|
2016-07-13 22:39:54 -03:00
|
|
|
}
|
|
|
|
#endif
|
2016-07-07 05:26:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
call any AHRS_update hooks
|
|
|
|
*/
|
2021-07-20 09:16:30 -03:00
|
|
|
void AP_Module::call_hook_AHRS_update(const AP_AHRS &ahrs)
|
2016-07-07 05:26:11 -03:00
|
|
|
{
|
2016-07-13 22:39:54 -03:00
|
|
|
#if AP_MODULE_SUPPORTED
|
2016-07-07 05:26:11 -03:00
|
|
|
if (hooks[HOOK_AHRS_UPDATE] == nullptr) {
|
|
|
|
// avoid filling in AHRS_state
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
construct AHRS_state structure
|
|
|
|
*/
|
|
|
|
struct AHRS_state state {};
|
|
|
|
state.structure_version = AHRS_state_version;
|
|
|
|
state.time_us = AP_HAL::micros64();
|
|
|
|
|
|
|
|
if (!ahrs.initialised()) {
|
|
|
|
state.status = AHRS_STATUS_INITIALISING;
|
|
|
|
} else if (ahrs.healthy()) {
|
|
|
|
state.status = AHRS_STATUS_HEALTHY;
|
|
|
|
} else {
|
|
|
|
state.status = AHRS_STATUS_UNHEALTHY;
|
|
|
|
}
|
|
|
|
|
|
|
|
Quaternion q;
|
|
|
|
q.from_rotation_matrix(ahrs.get_rotation_body_to_ned());
|
|
|
|
state.quat[0] = q[0];
|
|
|
|
state.quat[1] = q[1];
|
|
|
|
state.quat[2] = q[2];
|
|
|
|
state.quat[3] = q[3];
|
|
|
|
|
|
|
|
state.eulers[0] = ahrs.roll;
|
|
|
|
state.eulers[1] = ahrs.pitch;
|
|
|
|
state.eulers[2] = ahrs.yaw;
|
|
|
|
|
|
|
|
Location loc;
|
|
|
|
if (ahrs.get_origin(loc)) {
|
|
|
|
state.origin.initialised = true;
|
|
|
|
state.origin.latitude = loc.lat;
|
|
|
|
state.origin.longitude = loc.lng;
|
|
|
|
state.origin.altitude = loc.alt*0.01f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ahrs.get_position(loc)) {
|
|
|
|
state.position.available = true;
|
|
|
|
state.position.latitude = loc.lat;
|
|
|
|
state.position.longitude = loc.lng;
|
|
|
|
state.position.altitude = loc.alt*0.01f;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3f pos;
|
2017-01-30 15:07:25 -04:00
|
|
|
if (ahrs.get_relative_position_NED_origin(pos)) {
|
2016-07-07 05:26:11 -03:00
|
|
|
state.relative_position[0] = pos[0];
|
|
|
|
state.relative_position[1] = pos[1];
|
|
|
|
state.relative_position[2] = pos[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vector3f &gyro = ahrs.get_gyro();
|
|
|
|
state.gyro_rate[0] = gyro[0];
|
|
|
|
state.gyro_rate[1] = gyro[1];
|
|
|
|
state.gyro_rate[2] = gyro[2];
|
|
|
|
|
|
|
|
const Vector3f &accel_ef = ahrs.get_accel_ef();
|
|
|
|
state.accel_ef[0] = accel_ef[0];
|
|
|
|
state.accel_ef[1] = accel_ef[0];
|
|
|
|
state.accel_ef[2] = accel_ef[0];
|
2016-09-03 05:00:04 -03:00
|
|
|
|
|
|
|
state.primary_accel = ahrs.get_primary_accel_index();
|
|
|
|
state.primary_gyro = ahrs.get_primary_gyro_index();
|
|
|
|
|
|
|
|
const Vector3f &gyro_bias = ahrs.get_gyro_drift();
|
|
|
|
state.gyro_bias[0] = gyro_bias[0];
|
|
|
|
state.gyro_bias[1] = gyro_bias[1];
|
|
|
|
state.gyro_bias[2] = gyro_bias[2];
|
2016-09-20 04:34:39 -03:00
|
|
|
|
|
|
|
Vector3f vel;
|
|
|
|
if (ahrs.get_velocity_NED(vel)) {
|
|
|
|
state.velocity_ned[0] = vel.x;
|
|
|
|
state.velocity_ned[1] = vel.y;
|
|
|
|
state.velocity_ned[2] = vel.z;
|
|
|
|
}
|
2016-07-07 05:26:11 -03:00
|
|
|
|
|
|
|
for (const struct hook_list *h=hooks[HOOK_AHRS_UPDATE]; h; h=h->next) {
|
2016-07-13 22:30:34 -03:00
|
|
|
ap_hook_AHRS_update_fn_t fn = reinterpret_cast<ap_hook_AHRS_update_fn_t>(h->symbol);
|
2016-07-07 05:26:11 -03:00
|
|
|
fn(&state);
|
2016-07-13 22:39:54 -03:00
|
|
|
}
|
|
|
|
#endif
|
2016-07-07 05:26:11 -03:00
|
|
|
}
|
2016-07-12 08:50:25 -03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
call any gyro_sample hooks
|
|
|
|
*/
|
|
|
|
void AP_Module::call_hook_gyro_sample(uint8_t instance, float dt, const Vector3f &gyro)
|
|
|
|
{
|
2016-07-13 22:39:54 -03:00
|
|
|
#if AP_MODULE_SUPPORTED
|
2016-07-12 08:50:25 -03:00
|
|
|
if (hooks[HOOK_GYRO_SAMPLE] == nullptr) {
|
|
|
|
// avoid filling in struct
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
construct gyro_sample structure
|
|
|
|
*/
|
|
|
|
struct gyro_sample state {};
|
|
|
|
state.structure_version = gyro_sample_version;
|
|
|
|
state.time_us = AP_HAL::micros64();
|
|
|
|
state.instance = instance;
|
|
|
|
state.delta_time = dt;
|
|
|
|
state.gyro[0] = gyro[0];
|
|
|
|
state.gyro[1] = gyro[1];
|
|
|
|
state.gyro[2] = gyro[2];
|
|
|
|
|
|
|
|
for (const struct hook_list *h=hooks[HOOK_GYRO_SAMPLE]; h; h=h->next) {
|
2016-07-13 22:30:34 -03:00
|
|
|
ap_hook_gyro_sample_fn_t fn = reinterpret_cast<ap_hook_gyro_sample_fn_t>(h->symbol);
|
2016-07-12 08:50:25 -03:00
|
|
|
fn(&state);
|
2016-07-13 22:39:54 -03:00
|
|
|
}
|
|
|
|
#endif
|
2016-07-12 08:50:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
call any accel_sample hooks
|
|
|
|
*/
|
2016-08-31 01:56:44 -03:00
|
|
|
void AP_Module::call_hook_accel_sample(uint8_t instance, float dt, const Vector3f &accel, bool fsync_set)
|
2016-07-12 08:50:25 -03:00
|
|
|
{
|
2016-07-13 22:39:54 -03:00
|
|
|
#if AP_MODULE_SUPPORTED
|
2016-07-12 08:50:25 -03:00
|
|
|
if (hooks[HOOK_ACCEL_SAMPLE] == nullptr) {
|
|
|
|
// avoid filling in struct
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
construct accel_sample structure
|
|
|
|
*/
|
|
|
|
struct accel_sample state {};
|
|
|
|
state.structure_version = accel_sample_version;
|
|
|
|
state.time_us = AP_HAL::micros64();
|
|
|
|
state.instance = instance;
|
|
|
|
state.delta_time = dt;
|
|
|
|
state.accel[0] = accel[0];
|
|
|
|
state.accel[1] = accel[1];
|
|
|
|
state.accel[2] = accel[2];
|
2016-08-31 01:56:44 -03:00
|
|
|
state.fsync_set = fsync_set;
|
2016-07-12 08:50:25 -03:00
|
|
|
|
|
|
|
for (const struct hook_list *h=hooks[HOOK_ACCEL_SAMPLE]; h; h=h->next) {
|
2016-07-13 22:30:34 -03:00
|
|
|
ap_hook_accel_sample_fn_t fn = reinterpret_cast<ap_hook_accel_sample_fn_t>(h->symbol);
|
2016-07-12 08:50:25 -03:00
|
|
|
fn(&state);
|
2016-07-13 22:39:54 -03:00
|
|
|
}
|
|
|
|
#endif
|
2016-07-12 08:50:25 -03:00
|
|
|
}
|
2018-02-09 19:08:54 -04:00
|
|
|
|
|
|
|
#endif // AP_MODULE_SUPPORTED
|