Skip to content

Commit b7424c6

Browse files
committed
send_event: move to new WQ and uORB::Subscription
1 parent 90bf26b commit b7424c6

9 files changed

+51
-285
lines changed

src/modules/events/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ px4_add_module(
4141
send_event.cpp
4242
set_leds.cpp
4343
status_display.cpp
44-
subscriber_handler.cpp
4544
temperature_calibration/accel.cpp
4645
temperature_calibration/baro.cpp
4746
temperature_calibration/gyro.cpp

src/modules/events/rc_loss_alarm.cpp

+7-20
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,27 @@ namespace events
5252
namespace rc_loss
5353
{
5454

55-
RC_Loss_Alarm::RC_Loss_Alarm(const events::SubscriberHandler &subscriber_handler)
56-
: _subscriber_handler(subscriber_handler)
57-
{
58-
}
59-
60-
bool RC_Loss_Alarm::check_for_updates()
61-
{
62-
if (_subscriber_handler.vehicle_status_updated()) {
63-
orb_copy(ORB_ID(vehicle_status), _subscriber_handler.get_vehicle_status_sub(), &_vehicle_status);
64-
return true;
65-
}
66-
67-
return false;
68-
}
69-
7055
void RC_Loss_Alarm::process()
7156
{
72-
if (!check_for_updates()) {
57+
vehicle_status_s status{};
58+
59+
if (!_vehicle_status_sub.update(&status)) {
7360
return;
7461
}
7562

7663
if (!_was_armed &&
77-
_vehicle_status.arming_state == vehicle_status_s::ARMING_STATE_ARMED) {
64+
status.arming_state == vehicle_status_s::ARMING_STATE_ARMED) {
7865

7966
_was_armed = true; // Once true, impossible to go back to false
8067
}
8168

82-
if (!_had_rc && !_vehicle_status.rc_signal_lost) {
69+
if (!_had_rc && !status.rc_signal_lost) {
8370

8471
_had_rc = true;
8572
}
8673

87-
if (_was_armed && _had_rc && _vehicle_status.rc_signal_lost &&
88-
_vehicle_status.arming_state != vehicle_status_s::ARMING_STATE_ARMED) {
74+
if (_was_armed && _had_rc && status.rc_signal_lost &&
75+
status.arming_state != vehicle_status_s::ARMING_STATE_ARMED) {
8976
play_tune();
9077
_alarm_playing = true;
9178

src/modules/events/rc_loss_alarm.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939

4040
#pragma once
4141

42-
#include "subscriber_handler.h"
43-
44-
#include <uORB/uORB.h>
42+
#include <uORB/Subscription.hpp>
4543
#include <uORB/topics/vehicle_status.h>
4644

4745
namespace events
@@ -53,7 +51,7 @@ class RC_Loss_Alarm
5351
{
5452
public:
5553

56-
RC_Loss_Alarm(const events::SubscriberHandler &subscriber_handler);
54+
RC_Loss_Alarm() = default;
5755

5856
/** regularily called to handle state updates */
5957
void process();
@@ -71,12 +69,12 @@ class RC_Loss_Alarm
7169
/** Publish tune control to interrupt any sound */
7270
void stop_tune();
7371

74-
struct vehicle_status_s _vehicle_status = {};
72+
uORB::Subscription _vehicle_status_sub{ORB_ID(vehicle_status)};
73+
7574
bool _was_armed = false;
7675
bool _had_rc = false; // Don't trigger alarm for systems without RC
7776
bool _alarm_playing = false;
7877
orb_advert_t _tune_control_pub = nullptr;
79-
const events::SubscriberHandler &_subscriber_handler;
8078
};
8179

8280
} /* namespace rc_loss */

src/modules/events/send_event.cpp

+23-58
Original file line numberDiff line numberDiff line change
@@ -40,47 +40,48 @@
4040
#include <px4_log.h>
4141
#include <drivers/drv_hrt.h>
4242

43+
using namespace time_literals;
44+
4345
namespace events
4446
{
4547

46-
struct work_s SendEvent::_work = {};
47-
4848
// Run it at 30 Hz.
49-
const unsigned SEND_EVENT_INTERVAL_US = 33000;
50-
49+
static constexpr uint32_t SEND_EVENT_INTERVAL_US{1_s / 30};
5150

5251
int SendEvent::task_spawn(int argc, char *argv[])
5352
{
54-
int ret = work_queue(LPWORK, &_work, (worker_t)&SendEvent::initialize_trampoline, nullptr, 0);
55-
56-
if (ret < 0) {
57-
return ret;
58-
}
59-
60-
ret = wait_until_running();
53+
SendEvent *send_event = new SendEvent();
6154

62-
if (ret < 0) {
63-
return ret;
55+
if (!send_event) {
56+
PX4_ERR("alloc failed");
57+
return PX4_ERROR;
6458
}
6559

60+
_object.store(send_event);
6661
_task_id = task_id_is_work_queue;
6762

63+
send_event->start();
64+
6865
return 0;
6966
}
7067

71-
SendEvent::SendEvent() : ModuleParams(nullptr)
68+
SendEvent::SendEvent() :
69+
ModuleParams(nullptr),
70+
ScheduledWorkItem(px4::wq_configurations::lp_default)
7271
{
7372
if (_param_ev_tsk_stat_dis.get()) {
74-
_status_display = new status::StatusDisplay(_subscriber_handler);
73+
_status_display = new status::StatusDisplay();
7574
}
7675

7776
if (_param_ev_tsk_rc_loss.get()) {
78-
_rc_loss_alarm = new rc_loss::RC_Loss_Alarm(_subscriber_handler);
77+
_rc_loss_alarm = new rc_loss::RC_Loss_Alarm();
7978
}
8079
}
8180

8281
SendEvent::~SendEvent()
8382
{
83+
ScheduleClear();
84+
8485
if (_status_display != nullptr) {
8586
delete _status_display;
8687
}
@@ -92,49 +93,18 @@ SendEvent::~SendEvent()
9293

9394
int SendEvent::start()
9495
{
95-
if (is_running()) {
96-
return 0;
97-
}
98-
99-
// Subscribe to the topics.
100-
_subscriber_handler.subscribe();
101-
102-
// Kick off the cycling. We can call it directly because we're already in the work queue context.
103-
cycle();
104-
105-
return 0;
106-
}
107-
108-
void SendEvent::initialize_trampoline(void *arg)
109-
{
110-
SendEvent *send_event = new SendEvent();
111-
112-
if (!send_event) {
113-
PX4_ERR("alloc failed");
114-
return;
115-
}
96+
ScheduleOnInterval(SEND_EVENT_INTERVAL_US, 10000);
11697

117-
send_event->start();
118-
_object.store(send_event);
98+
return PX4_OK;
11999
}
120100

121-
void SendEvent::cycle_trampoline(void *arg)
122-
{
123-
SendEvent *obj = reinterpret_cast<SendEvent *>(arg);
124-
125-
obj->cycle();
126-
}
127-
128-
void SendEvent::cycle()
101+
void SendEvent::Run()
129102
{
130103
if (should_exit()) {
131-
_subscriber_handler.unsubscribe();
132104
exit_and_cleanup();
133105
return;
134106
}
135107

136-
_subscriber_handler.check_for_updates();
137-
138108
process_commands();
139109

140110
if (_status_display != nullptr) {
@@ -144,21 +114,16 @@ void SendEvent::cycle()
144114
if (_rc_loss_alarm != nullptr) {
145115
_rc_loss_alarm->process();
146116
}
147-
148-
work_queue(LPWORK, &_work, (worker_t)&SendEvent::cycle_trampoline, this,
149-
USEC2TICK(SEND_EVENT_INTERVAL_US));
150117
}
151118

152119
void SendEvent::process_commands()
153120
{
154-
if (!_subscriber_handler.vehicle_command_updated()) {
121+
vehicle_command_s cmd{};
122+
123+
if (!_vehicle_command_sub.update(&cmd)) {
155124
return;
156125
}
157126

158-
struct vehicle_command_s cmd;
159-
160-
orb_copy(ORB_ID(vehicle_command), _subscriber_handler.get_vehicle_command_sub(), &cmd);
161-
162127
bool got_temperature_calibration_command = false, accel = false, baro = false, gyro = false;
163128

164129
switch (cmd.command) {

src/modules/events/send_event.h

+4-21
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@
3333

3434
#pragma once
3535

36-
#include "subscriber_handler.h"
3736
#include "status_display.h"
3837
#include "rc_loss_alarm.h"
3938

40-
#include <px4_workqueue.h>
39+
#include <px4_work_queue/ScheduledWorkItem.hpp>
4140
#include <px4_module.h>
4241
#include <px4_module_params.h>
4342
#include <uORB/topics/vehicle_command.h>
@@ -49,7 +48,7 @@ namespace events
4948
extern "C" __EXPORT int send_event_main(int argc, char *argv[]);
5049

5150
/** @class SendEvent The SendEvent class manages the RC loss audible alarm, LED status display, and thermal calibration. */
52-
class SendEvent : public ModuleBase<SendEvent>, public ModuleParams
51+
class SendEvent : public ModuleBase<SendEvent>, public ModuleParams, public px4::ScheduledWorkItem
5352
{
5453
public:
5554

@@ -92,22 +91,10 @@ class SendEvent : public ModuleBase<SendEvent>, public ModuleParams
9291
*/
9392
void answer_command(const vehicle_command_s &cmd, unsigned result);
9493

95-
/**
96-
* @brief Process cycle trampoline for the work queue.
97-
* @param arg Pointer to the task startup arguments.
98-
*/
99-
static void cycle_trampoline(void *arg);
100-
10194
/**
10295
* @brief Calls process_commands() and schedules the next cycle.
10396
*/
104-
void cycle();
105-
106-
/**
107-
* @brief Trampoline for initialisation.
108-
* @param arg Pointer to the task startup arguments.
109-
*/
110-
static void initialize_trampoline(void *arg);
97+
void Run() override;
11198

11299
/**
113100
* @brief Checks for new commands and processes them.
@@ -120,11 +107,7 @@ class SendEvent : public ModuleBase<SendEvent>, public ModuleParams
120107
*/
121108
int start();
122109

123-
/** @struct _work The work queue struct. */
124-
static struct work_s _work;
125-
126-
/** @var _subscriber_handler The uORB subscriber handler. */
127-
SubscriberHandler _subscriber_handler;
110+
uORB::Subscription _vehicle_command_sub{ORB_ID(vehicle_command)};
128111

129112
/** @var _status_display Pointer to the status display object. */
130113
status::StatusDisplay *_status_display = nullptr;

src/modules/events/status_display.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ namespace events
4848
namespace status
4949
{
5050

51-
StatusDisplay::StatusDisplay(const events::SubscriberHandler &subscriber_handler)
52-
: _subscriber_handler(subscriber_handler)
51+
StatusDisplay::StatusDisplay()
5352
{
5453
// set the base color
5554
_led_control.priority = 0;
@@ -66,23 +65,19 @@ bool StatusDisplay::check_for_updates()
6665
{
6766
bool got_updates = false;
6867

69-
if (_subscriber_handler.battery_status_updated()) {
70-
orb_copy(ORB_ID(battery_status), _subscriber_handler.get_battery_status_sub(), &_battery_status);
68+
if (_battery_status_sub.update()) {
7169
got_updates = true;
7270
}
7371

74-
if (_subscriber_handler.cpuload_updated()) {
75-
orb_copy(ORB_ID(cpuload), _subscriber_handler.get_cpuload_sub(), &_cpu_load);
72+
if (_cpu_load_sub.update()) {
7673
got_updates = true;
7774
}
7875

79-
if (_subscriber_handler.vehicle_status_flags_updated()) {
80-
orb_copy(ORB_ID(vehicle_status_flags), _subscriber_handler.get_vehicle_status_flags_sub(), &_vehicle_status_flags);
76+
if (_vehicle_status_flags_sub.update()) {
8177
got_updates = true;
8278
}
8379

84-
if (_subscriber_handler.vehicle_status_updated()) {
85-
orb_copy(ORB_ID(vehicle_status), _subscriber_handler.get_vehicle_status_sub(), &_vehicle_status);
80+
if (_vehicle_status_sub.update()) {
8681
got_updates = true;
8782
}
8883

src/modules/events/status_display.h

+8-10
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@
4040

4141
#pragma once
4242

43-
#include "subscriber_handler.h"
44-
4543
#include <drivers/drv_hrt.h>
4644

47-
#include <uORB/uORB.h>
45+
#include <uORB/Subscription.hpp>
4846
#include <uORB/topics/battery_status.h>
4947
#include <uORB/topics/cpuload.h>
5048
#include <uORB/topics/led_control.h>
49+
#include <uORB/topics/vehicle_attitude.h>
5150
#include <uORB/topics/vehicle_status.h>
5251
#include <uORB/topics/vehicle_status_flags.h>
5352

@@ -60,7 +59,7 @@ class StatusDisplay
6059
{
6160
public:
6261

63-
StatusDisplay(const events::SubscriberHandler &subscriber_handler);
62+
StatusDisplay();
6463

6564
/** regularily called to handle state updates */
6665
void process();
@@ -81,11 +80,11 @@ class StatusDisplay
8180
void publish();
8281

8382
// TODO: review if there is a better variant that allocates this in the memory
84-
struct battery_status_s _battery_status = {};
85-
struct cpuload_s _cpu_load = {};
86-
struct vehicle_status_s _vehicle_status = {};
87-
struct vehicle_status_flags_s _vehicle_status_flags = {};
88-
struct vehicle_attitude_s _vehicle_attitude = {};
83+
uORB::SubscriptionData<battery_status_s> _battery_status_sub{ORB_ID(battery_status)};
84+
uORB::SubscriptionData<cpuload_s> _cpu_load_sub{ORB_ID(cpuload)};
85+
uORB::SubscriptionData<vehicle_status_s> _vehicle_status_sub{ORB_ID(vehicle_status)};
86+
uORB::SubscriptionData<vehicle_status_flags_s> _vehicle_status_flags_sub{ORB_ID(vehicle_status_flags)};
87+
uORB::SubscriptionData<vehicle_attitude_s> _vehicle_attitude_sub{ORB_ID(vehicle_attitude)};
8988

9089
struct led_control_s _led_control = {};
9190

@@ -97,7 +96,6 @@ class StatusDisplay
9796
int _old_nav_state = -1;
9897
int _old_battery_status_warning = -1;
9998
orb_advert_t _led_control_pub = nullptr;
100-
const events::SubscriberHandler &_subscriber_handler;
10199
};
102100

103101
} /* namespace status */

0 commit comments

Comments
 (0)