mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-04-14 01:57:37 +08:00
fix: template_module update to best practise
This commit is contained in:
parent
348a558a15
commit
6f18fa39e8
4
Kconfig
4
Kconfig
@ -220,6 +220,10 @@ menu "examples"
|
||||
source "src/examples/Kconfig"
|
||||
endmenu
|
||||
|
||||
menu "templates"
|
||||
source "src/templates/Kconfig"
|
||||
endmenu
|
||||
|
||||
menu "platforms"
|
||||
depends on PLATFORM_QURT || PLATFORM_POSIX || PLATFORM_NUTTX
|
||||
source "platforms/Kconfig"
|
||||
|
||||
1
src/templates/Kconfig
Normal file
1
src/templates/Kconfig
Normal file
@ -0,0 +1 @@
|
||||
rsource "*/Kconfig"
|
||||
5
src/templates/template_module/Kconfig
Normal file
5
src/templates/template_module/Kconfig
Normal file
@ -0,0 +1,5 @@
|
||||
menuconfig TEMPLATES_TEMPLATE_MODULE
|
||||
bool "template_module"
|
||||
default n
|
||||
---help---
|
||||
Enable support for template_module
|
||||
@ -38,7 +38,6 @@
|
||||
#include <px4_platform_common/posix.h>
|
||||
|
||||
#include <uORB/topics/parameter_update.h>
|
||||
#include <uORB/topics/sensor_combined.h>
|
||||
|
||||
ModuleBase::Descriptor TemplateModule::desc{task_spawn, custom_command, print_usage};
|
||||
|
||||
@ -145,42 +144,20 @@ TemplateModule::TemplateModule(int example_param, bool example_flag)
|
||||
|
||||
void TemplateModule::run()
|
||||
{
|
||||
// Example: run the loop synchronized to the sensor_combined topic publication
|
||||
int sensor_combined_sub = orb_subscribe(ORB_ID(sensor_combined));
|
||||
|
||||
px4_pollfd_struct_t fds[1];
|
||||
fds[0].fd = sensor_combined_sub;
|
||||
fds[0].events = POLLIN;
|
||||
|
||||
// initialize parameters
|
||||
parameters_update(true);
|
||||
|
||||
while (!should_exit()) {
|
||||
|
||||
// wait for up to 1000ms for data
|
||||
int pret = px4_poll(fds, (sizeof(fds) / sizeof(fds[0])), 1000);
|
||||
|
||||
if (pret == 0) {
|
||||
// Timeout: let the loop run anyway, don't do `continue` here
|
||||
|
||||
} else if (pret < 0) {
|
||||
// this is undesirable but not much we can do
|
||||
PX4_ERR("poll error %d, %d", pret, errno);
|
||||
px4_usleep(50000);
|
||||
continue;
|
||||
|
||||
} else if (fds[0].revents & POLLIN) {
|
||||
|
||||
struct sensor_combined_s sensor_combined;
|
||||
orb_copy(ORB_ID(sensor_combined), sensor_combined_sub, &sensor_combined);
|
||||
if (_sensor_accel_sub.updated()) {
|
||||
sensor_accel_s accel{};
|
||||
_sensor_accel_sub.copy(&accel);
|
||||
// TODO: do something with the data...
|
||||
|
||||
}
|
||||
|
||||
parameters_update();
|
||||
px4_usleep(10_ms);
|
||||
}
|
||||
|
||||
orb_unsubscribe(sensor_combined_sub);
|
||||
}
|
||||
|
||||
void TemplateModule::parameters_update(bool force)
|
||||
|
||||
@ -35,8 +35,10 @@
|
||||
|
||||
#include <px4_platform_common/module.h>
|
||||
#include <px4_platform_common/module_params.h>
|
||||
#include <uORB/Subscription.hpp>
|
||||
#include <uORB/SubscriptionInterval.hpp>
|
||||
#include <uORB/topics/parameter_update.h>
|
||||
#include <uORB/topics/sensor_accel.h>
|
||||
|
||||
using namespace time_literals;
|
||||
|
||||
@ -89,5 +91,6 @@ private:
|
||||
|
||||
// Subscriptions
|
||||
uORB::SubscriptionInterval _parameter_update_sub{ORB_ID(parameter_update), 1_s};
|
||||
uORB::Subscription _sensor_accel_sub{ORB_ID(sensor_accel)};
|
||||
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user