send_event: convert to use ModuleBase

This commit is contained in:
Beat Küng
2017-07-14 11:57:11 +02:00
parent 04c4339ca3
commit 5bdbfa9b5c
3 changed files with 44 additions and 88 deletions
+1 -1
View File
@@ -463,7 +463,7 @@ then
commander start commander start
fi fi
if send_event start_listening if send_event start
then then
fi fi
+36 -78
View File
@@ -38,14 +38,13 @@
#include <px4_log.h> #include <px4_log.h>
#include <drivers/drv_hrt.h> #include <drivers/drv_hrt.h>
static SendEvent *send_event_obj = nullptr;
struct work_s SendEvent::_work = {}; struct work_s SendEvent::_work = {};
// Run it at 30 Hz. // Run it at 30 Hz.
const unsigned SEND_EVENT_INTERVAL_US = 33000; const unsigned SEND_EVENT_INTERVAL_US = 33000;
int SendEvent::initialize() int SendEvent::task_spawn(int argc, char *argv[])
{ {
int ret = work_queue(LPWORK, &_work, (worker_t)&SendEvent::initialize_trampoline, nullptr, 0); int ret = work_queue(LPWORK, &_work, (worker_t)&SendEvent::initialize_trampoline, nullptr, 0);
@@ -59,13 +58,14 @@ int SendEvent::initialize()
/* wait up to 1s */ /* wait up to 1s */
usleep(2500); usleep(2500);
} while ((!send_event_obj || !send_event_obj->is_running()) && ++i < 400); } while (!_object && ++i < 400);
if (i == 400) { if (i == 400) {
PX4_ERR("failed to start");
return -1; return -1;
} }
_task_id = task_id_is_work_queue;
return 0; return 0;
} }
@@ -75,13 +75,10 @@ SendEvent::SendEvent()
int SendEvent::start() int SendEvent::start()
{ {
if (_task_is_running) { if (is_running()) {
return 0; return 0;
} }
_task_is_running = true;
_task_should_exit = false;
_vehicle_command_sub = orb_subscribe(ORB_ID(vehicle_command)); _vehicle_command_sub = orb_subscribe(ORB_ID(vehicle_command));
// Kick off the cycling. We can call it directly because we're already in the work queue context // Kick off the cycling. We can call it directly because we're already in the work queue context
@@ -90,37 +87,17 @@ int SendEvent::start()
return 0; return 0;
} }
void SendEvent::stop()
{
if (!_task_is_running) {
return;
}
_task_should_exit = true;
// Wait for task to exit
int i = 0;
do {
/* wait up to 3s */
usleep(100000);
} while (_task_is_running && ++i < 30);
if (i == 30) {
PX4_ERR("failed to stop");
}
}
void SendEvent::initialize_trampoline(void *arg) void SendEvent::initialize_trampoline(void *arg)
{ {
send_event_obj = new SendEvent(); SendEvent *send_event = new SendEvent();
if (!send_event_obj) { if (!send_event) {
PX4_ERR("alloc failed"); PX4_ERR("alloc failed");
return; return;
} }
send_event_obj->start(); send_event->start();
_object = send_event;
} }
void void
@@ -133,13 +110,13 @@ SendEvent::cycle_trampoline(void *arg)
void SendEvent::cycle() void SendEvent::cycle()
{ {
if (_task_should_exit) { if (should_exit()) {
if (_vehicle_command_sub >= 0) { if (_vehicle_command_sub >= 0) {
orb_unsubscribe(_vehicle_command_sub); orb_unsubscribe(_vehicle_command_sub);
_vehicle_command_sub = -1; _vehicle_command_sub = -1;
} }
_task_is_running = false; exit_and_cleanup();
return; return;
} }
@@ -213,63 +190,44 @@ void SendEvent::answer_command(const vehicle_command_s &cmd, unsigned result)
} }
void SendEvent::print_status()
{
PX4_INFO("running");
}
int SendEvent::print_usage(const char *reason)
static void print_usage(const char *reason = nullptr)
{ {
if (reason) { if (reason) {
PX4_WARN("%s\n", reason); printf("%s\n\n", reason);
} }
PX4_INFO("usage: send_event {start_listening|stop_listening|status|temperature_calibration}\n" PRINT_MODULE_DESCRIPTION(
"\tstart_listening: start background task to listen to events\n" R"DESCR_STR(
"\ttemperature_calibration [-g] [-a] [-b]: start temperature calibration task\n" Background process running periodically on the LP work queue to perform housekeeping tasks.
"\t all sensors if no option given, 1 or several of gyro, accel, baro otherwise\n" It is currently only responsible for temperature calibration.
);
}
The tasks can be started via CLI or uORB topics (vehicle_command from MAVLink, etc.).
)DESCR_STR");
PRINT_MODULE_USAGE_NAME("send_event", "system");
PRINT_MODULE_USAGE_COMMAND_DESCR("start", "Start the background task");
PRINT_MODULE_USAGE_COMMAND_DESCR("temperature_calibration", "Run temperature calibration process");
PRINT_MODULE_USAGE_PARAM_FLAG('g', "calibrate the gyro", true);
PRINT_MODULE_USAGE_PARAM_FLAG('a', "calibrate the accel", true);
PRINT_MODULE_USAGE_PARAM_FLAG('b', "calibrate the baro (if none of these is given, all will be calibrated)", true);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
return 0;
}
int send_event_main(int argc, char *argv[]) int send_event_main(int argc, char *argv[])
{ {
if (argc < 2) { return SendEvent::main(argc, argv);
print_usage(); }
return 1;
}
if (!strcmp(argv[1], "start_listening")) {
if (send_event_obj) { int SendEvent::custom_command(int argc, char *argv[])
PX4_INFO("already running"); {
return -1; if (!strcmp(argv[0], "temperature_calibration")) {
}
return SendEvent::initialize(); if (!is_running()) {
} else if (!strcmp(argv[1], "stop_listening")) {
if (send_event_obj) {
send_event_obj->stop();
delete send_event_obj;
send_event_obj = nullptr;
}
} else if (!strcmp(argv[1], "status")) {
if (send_event_obj) {
send_event_obj->print_status();
} else {
PX4_INFO("not running");
}
} else if (!strcmp(argv[1], "temperature_calibration")) {
if (!send_event_obj) {
PX4_ERR("background task not running"); PX4_ERR("background task not running");
return -1; return -1;
} }
+7 -9
View File
@@ -34,12 +34,13 @@
#pragma once #pragma once
#include <px4_workqueue.h> #include <px4_workqueue.h>
#include <px4_module.h>
#include <uORB/topics/vehicle_command.h> #include <uORB/topics/vehicle_command.h>
#include <uORB/topics/vehicle_command_ack.h> #include <uORB/topics/vehicle_command_ack.h>
extern "C" __EXPORT int send_event_main(int argc, char *argv[]); extern "C" __EXPORT int send_event_main(int argc, char *argv[]);
class SendEvent class SendEvent : public ModuleBase<SendEvent>
{ {
public: public:
SendEvent(); SendEvent();
@@ -47,14 +48,13 @@ public:
/** Initialize class in the same context as the work queue. And start the background listener. /** Initialize class in the same context as the work queue. And start the background listener.
* *
* @return 0 if successful, <0 on error */ * @return 0 if successful, <0 on error */
static int initialize(); static int task_spawn(int argc, char *argv[]);
/** Stop background listener */ /** @see ModuleBase */
void stop(); static int custom_command(int argc, char *argv[]);
bool is_running() { return _task_is_running; } /** @see ModuleBase */
static int print_usage(const char *reason = nullptr);
void print_status();
private: private:
@@ -78,8 +78,6 @@ private:
/** return an ACK to a vehicle_command */ /** return an ACK to a vehicle_command */
void answer_command(const vehicle_command_s &cmd, unsigned result); void answer_command(const vehicle_command_s &cmd, unsigned result);
volatile bool _task_should_exit = false;
volatile bool _task_is_running = false;
static struct work_s _work; static struct work_s _work;
int _vehicle_command_sub = -1; int _vehicle_command_sub = -1;
orb_advert_t _command_ack_pub = nullptr; orb_advert_t _command_ack_pub = nullptr;