38
38
#include < perf/perf_counter.h>
39
39
#include < px4_module.h>
40
40
#include < px4_module_params.h>
41
- #include < px4_workqueue.h >
41
+ #include < px4_work_queue/ScheduledWorkItem.hpp >
42
42
#include < uORB/Subscription.hpp>
43
43
#include < uORB/topics/airspeed.h>
44
44
#include < uORB/topics/parameter_update.h>
49
49
50
50
using namespace time_literals ;
51
51
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) */
53
53
54
54
using matrix::Dcmf;
55
55
using matrix::Quatf;
56
56
using matrix::Vector2f;
57
57
using matrix::Vector3f;
58
58
59
- class WindEstimatorModule : public ModuleBase <WindEstimatorModule>, public ModuleParams
59
+ class WindEstimatorModule : public ModuleBase <WindEstimatorModule>, public ModuleParams, public px4::ScheduledWorkItem
60
60
{
61
61
public:
62
62
@@ -74,20 +74,19 @@ class WindEstimatorModule : public ModuleBase<WindEstimatorModule>, public Modul
74
74
static int print_usage (const char *reason = nullptr );
75
75
76
76
// run the main loop
77
- void cycle () ;
77
+ void Run () override ;
78
78
79
79
int print_status () override ;
80
80
81
81
private:
82
- static struct work_s _work;
83
82
84
83
WindEstimator _wind_estimator;
85
84
orb_advert_t _wind_est_pub{nullptr }; /* *< wind estimate topic */
86
85
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) };
91
90
92
91
perf_counter_t _perf_elapsed{};
93
92
perf_counter_t _perf_interval{};
@@ -103,24 +102,17 @@ class WindEstimatorModule : public ModuleBase<WindEstimatorModule>, public Modul
103
102
(ParamInt<px4::params::WEST_BETA_GATE>) _param_west_beta_gate
104
103
)
105
104
106
- static void cycle_trampoline (void *arg);
107
105
int start ();
108
106
109
107
void update_params ();
110
108
111
109
bool subscribe_topics ();
112
110
};
113
111
114
- work_s WindEstimatorModule::_work = {};
115
-
116
112
WindEstimatorModule::WindEstimatorModule ():
117
- ModuleParams(nullptr )
113
+ ModuleParams(nullptr ),
114
+ ScheduledWorkItem(px4::wq_configurations::lp_default)
118
115
{
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
-
124
116
// initialise parameters
125
117
update_params ();
126
118
@@ -130,10 +122,7 @@ WindEstimatorModule::WindEstimatorModule():
130
122
131
123
WindEstimatorModule::~WindEstimatorModule ()
132
124
{
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 ();
137
126
138
127
orb_unadvertise (_wind_est_pub);
139
128
@@ -145,52 +134,34 @@ int
145
134
WindEstimatorModule::task_spawn (int argc, char *argv[])
146
135
{
147
136
/* 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 ();
166
138
167
139
// check if the trampoline is called for the first time
168
140
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;
177
143
}
178
144
145
+ _object.store (dev);
146
+
179
147
if (dev) {
180
- dev->cycle ();
148
+ dev->ScheduleOnInterval (SCHEDULE_INTERVAL, 10000 );
149
+ _task_id = task_id_is_work_queue;
150
+ return PX4_OK;
181
151
}
152
+
153
+ return PX4_ERROR;
182
154
}
183
155
184
156
void
185
- WindEstimatorModule::cycle ()
157
+ WindEstimatorModule::Run ()
186
158
{
187
159
perf_count (_perf_interval);
188
160
perf_begin (_perf_elapsed);
189
161
190
- bool param_updated;
191
- orb_check (_param_sub, ¶m_updated);
162
+ parameter_update_s param{};
192
163
193
- if (param_updated ) {
164
+ if (_param_sub. update (¶m) ) {
194
165
update_params ();
195
166
}
196
167
@@ -201,15 +172,15 @@ WindEstimatorModule::cycle()
201
172
202
173
// validate required conditions for the filter to fuse measurements
203
174
204
- vehicle_attitude_s att = {};
175
+ vehicle_attitude_s att{};
205
176
206
- if (orb_copy ( ORB_ID (vehicle_attitude), _vehicle_attitude_sub, &att) == PX4_OK ) {
177
+ if (_vehicle_attitude_sub. update ( &att)) {
207
178
att_valid = (time_now_usec - att.timestamp < 1_s) && (att.timestamp > 0 );
208
179
}
209
180
210
- vehicle_local_position_s lpos = {};
181
+ vehicle_local_position_s lpos{};
211
182
212
- if (orb_copy ( ORB_ID (vehicle_local_position), _vehicle_local_position_sub, &lpos) == PX4_OK ) {
183
+ if (_vehicle_local_position_sub. update ( &lpos)) {
213
184
lpos_valid = (time_now_usec - lpos.timestamp < 1_s) && (lpos.timestamp > 0 ) && lpos.v_xy_valid ;
214
185
}
215
186
@@ -225,9 +196,9 @@ WindEstimatorModule::cycle()
225
196
_wind_estimator.fuse_beta (time_now_usec, vI, q);
226
197
227
198
// additionally, for airspeed fusion we need to have recent measurements
228
- airspeed_s airspeed = {};
199
+ airspeed_s airspeed{};
229
200
230
- if (orb_copy ( ORB_ID (airspeed), _airspeed_sub, &airspeed) == PX4_OK ) {
201
+ if (_airspeed_sub. update ( &airspeed)) {
231
202
if ((time_now_usec - airspeed.timestamp < 1_s) && (airspeed.timestamp > 0 )) {
232
203
const Vector3f vel_var{Dcmf (q) *Vector3f{lpos.evh , lpos.evh , lpos.evv }};
233
204
@@ -261,10 +232,6 @@ WindEstimatorModule::cycle()
261
232
262
233
if (should_exit ()) {
263
234
exit_and_cleanup ();
264
-
265
- } else {
266
- /* schedule next cycle */
267
- work_queue (LPWORK, &_work, (worker_t )&WindEstimatorModule::cycle_trampoline, this , USEC2TICK (SCHEDULE_INTERVAL));
268
235
}
269
236
}
270
237
0 commit comments