Skip to content

Commit 71d58c9

Browse files
committed
wind_estimator move to new WQ (lp_default) and uORB::Subscription
1 parent e4ad994 commit 71d58c9

File tree

1 file changed

+30
-63
lines changed

1 file changed

+30
-63
lines changed

src/modules/wind_estimator/wind_estimator_main.cpp

+30-63
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include <perf/perf_counter.h>
3939
#include <px4_module.h>
4040
#include <px4_module_params.h>
41-
#include <px4_workqueue.h>
41+
#include <px4_work_queue/ScheduledWorkItem.hpp>
4242
#include <uORB/Subscription.hpp>
4343
#include <uORB/topics/airspeed.h>
4444
#include <uORB/topics/parameter_update.h>
@@ -49,14 +49,14 @@
4949

5050
using namespace time_literals;
5151

52-
#define SCHEDULE_INTERVAL 100000 /**< The schedule interval in usec (10 Hz) */
52+
static constexpr uint32_t SCHEDULE_INTERVAL{100_ms}; /**< The schedule interval in usec (10 Hz) */
5353

5454
using matrix::Dcmf;
5555
using matrix::Quatf;
5656
using matrix::Vector2f;
5757
using matrix::Vector3f;
5858

59-
class WindEstimatorModule : public ModuleBase<WindEstimatorModule>, public ModuleParams
59+
class WindEstimatorModule : public ModuleBase<WindEstimatorModule>, public ModuleParams, public px4::ScheduledWorkItem
6060
{
6161
public:
6262

@@ -74,20 +74,19 @@ class WindEstimatorModule : public ModuleBase<WindEstimatorModule>, public Modul
7474
static int print_usage(const char *reason = nullptr);
7575

7676
// run the main loop
77-
void cycle();
77+
void Run() override;
7878

7979
int print_status() override;
8080

8181
private:
82-
static struct work_s _work;
8382

8483
WindEstimator _wind_estimator;
8584
orb_advert_t _wind_est_pub{nullptr}; /**< wind estimate topic */
8685

87-
int _vehicle_attitude_sub{-1};
88-
int _vehicle_local_position_sub{-1};
89-
int _airspeed_sub{-1};
90-
int _param_sub{-1};
86+
uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)};
87+
uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)};
88+
uORB::Subscription _airspeed_sub{ORB_ID(airspeed)};
89+
uORB::Subscription _param_sub{ORB_ID(parameter_update)};
9190

9291
perf_counter_t _perf_elapsed{};
9392
perf_counter_t _perf_interval{};
@@ -103,24 +102,17 @@ class WindEstimatorModule : public ModuleBase<WindEstimatorModule>, public Modul
103102
(ParamInt<px4::params::WEST_BETA_GATE>) _param_west_beta_gate
104103
)
105104

106-
static void cycle_trampoline(void *arg);
107105
int start();
108106

109107
void update_params();
110108

111109
bool subscribe_topics();
112110
};
113111

114-
work_s WindEstimatorModule::_work = {};
115-
116112
WindEstimatorModule::WindEstimatorModule():
117-
ModuleParams(nullptr)
113+
ModuleParams(nullptr),
114+
ScheduledWorkItem(px4::wq_configurations::lp_default)
118115
{
119-
_vehicle_attitude_sub = orb_subscribe(ORB_ID(vehicle_attitude));
120-
_vehicle_local_position_sub = orb_subscribe(ORB_ID(vehicle_local_position));
121-
_airspeed_sub = orb_subscribe(ORB_ID(airspeed));
122-
_param_sub = orb_subscribe(ORB_ID(parameter_update));
123-
124116
// initialise parameters
125117
update_params();
126118

@@ -130,10 +122,7 @@ WindEstimatorModule::WindEstimatorModule():
130122

131123
WindEstimatorModule::~WindEstimatorModule()
132124
{
133-
orb_unsubscribe(_vehicle_attitude_sub);
134-
orb_unsubscribe(_vehicle_local_position_sub);
135-
orb_unsubscribe(_airspeed_sub);
136-
orb_unsubscribe(_param_sub);
125+
ScheduleClear();
137126

138127
orb_unadvertise(_wind_est_pub);
139128

@@ -145,52 +134,34 @@ int
145134
WindEstimatorModule::task_spawn(int argc, char *argv[])
146135
{
147136
/* schedule a cycle to start things */
148-
work_queue(LPWORK, &_work, (worker_t)&WindEstimatorModule::cycle_trampoline, nullptr, 0);
149-
150-
// wait until task is up & running
151-
if (wait_until_running() < 0) {
152-
_task_id = -1;
153-
154-
} else {
155-
_task_id = task_id_is_work_queue;
156-
return PX4_OK;
157-
}
158-
159-
return PX4_ERROR;
160-
}
161-
162-
void
163-
WindEstimatorModule::cycle_trampoline(void *arg)
164-
{
165-
WindEstimatorModule *dev = reinterpret_cast<WindEstimatorModule *>(arg);
137+
WindEstimatorModule *dev = new WindEstimatorModule();
166138

167139
// check if the trampoline is called for the first time
168140
if (!dev) {
169-
dev = new WindEstimatorModule();
170-
171-
if (!dev) {
172-
PX4_ERR("alloc failed");
173-
return;
174-
}
175-
176-
_object.store(dev);
141+
PX4_ERR("alloc failed");
142+
return PX4_ERROR;
177143
}
178144

145+
_object.store(dev);
146+
179147
if (dev) {
180-
dev->cycle();
148+
dev->ScheduleOnInterval(SCHEDULE_INTERVAL, 10000);
149+
_task_id = task_id_is_work_queue;
150+
return PX4_OK;
181151
}
152+
153+
return PX4_ERROR;
182154
}
183155

184156
void
185-
WindEstimatorModule::cycle()
157+
WindEstimatorModule::Run()
186158
{
187159
perf_count(_perf_interval);
188160
perf_begin(_perf_elapsed);
189161

190-
bool param_updated;
191-
orb_check(_param_sub, &param_updated);
162+
parameter_update_s param{};
192163

193-
if (param_updated) {
164+
if (_param_sub.update(&param)) {
194165
update_params();
195166
}
196167

@@ -201,15 +172,15 @@ WindEstimatorModule::cycle()
201172

202173
// validate required conditions for the filter to fuse measurements
203174

204-
vehicle_attitude_s att = {};
175+
vehicle_attitude_s att{};
205176

206-
if (orb_copy(ORB_ID(vehicle_attitude), _vehicle_attitude_sub, &att) == PX4_OK) {
177+
if (_vehicle_attitude_sub.update(&att)) {
207178
att_valid = (time_now_usec - att.timestamp < 1_s) && (att.timestamp > 0);
208179
}
209180

210-
vehicle_local_position_s lpos = {};
181+
vehicle_local_position_s lpos{};
211182

212-
if (orb_copy(ORB_ID(vehicle_local_position), _vehicle_local_position_sub, &lpos) == PX4_OK) {
183+
if (_vehicle_local_position_sub.update(&lpos)) {
213184
lpos_valid = (time_now_usec - lpos.timestamp < 1_s) && (lpos.timestamp > 0) && lpos.v_xy_valid;
214185
}
215186

@@ -225,9 +196,9 @@ WindEstimatorModule::cycle()
225196
_wind_estimator.fuse_beta(time_now_usec, vI, q);
226197

227198
// additionally, for airspeed fusion we need to have recent measurements
228-
airspeed_s airspeed = {};
199+
airspeed_s airspeed{};
229200

230-
if (orb_copy(ORB_ID(airspeed), _airspeed_sub, &airspeed) == PX4_OK) {
201+
if (_airspeed_sub.update(&airspeed)) {
231202
if ((time_now_usec - airspeed.timestamp < 1_s) && (airspeed.timestamp > 0)) {
232203
const Vector3f vel_var{Dcmf(q) *Vector3f{lpos.evh, lpos.evh, lpos.evv}};
233204

@@ -261,10 +232,6 @@ WindEstimatorModule::cycle()
261232

262233
if (should_exit()) {
263234
exit_and_cleanup();
264-
265-
} else {
266-
/* schedule next cycle */
267-
work_queue(LPWORK, &_work, (worker_t)&WindEstimatorModule::cycle_trampoline, this, USEC2TICK(SCHEDULE_INTERVAL));
268235
}
269236
}
270237

0 commit comments

Comments
 (0)