mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-19 05:53:34 +08:00
correctly handle interval, call callback only when topic updated, add example for 2 topics
This commit is contained in:
@@ -36,10 +36,10 @@ void rc_channels_callback(const PX4_TOPIC_T(rc_channels) &msg)
|
||||
{
|
||||
PX4_INFO("I heard: [%llu]", msg.timestamp_last_valid);
|
||||
}
|
||||
// void rc_channels_callback(int i)
|
||||
// {
|
||||
// PX4_INFO("I heard: [%d]", i);
|
||||
// }
|
||||
void rc_channels_callback2(const PX4_TOPIC_T(rc_channels) &msg)
|
||||
{
|
||||
PX4_INFO("I heard2: [%llu]", msg.timestamp_last_valid);
|
||||
}
|
||||
namespace px4
|
||||
{
|
||||
bool task_should_exit = false;
|
||||
@@ -81,7 +81,8 @@ PX4_MAIN_FUNCTION(subscriber)
|
||||
* is the number of messages that will be buffered up before beginning to throw
|
||||
* away the oldest ones.
|
||||
*/
|
||||
PX4_SUBSCRIBE(n, rc_channels, rc_channels_callback, 1000);
|
||||
PX4_SUBSCRIBE(n, rc_channels, rc_channels_callback, 100);
|
||||
PX4_SUBSCRIBE(n, rc_channels, rc_channels_callback2, 1000);
|
||||
PX4_INFO("subscribed");
|
||||
|
||||
/**
|
||||
|
||||
@@ -114,7 +114,7 @@ public:
|
||||
SubscriberPX4<M> *sub_px4 = new SubscriberPX4<M>(meta, interval, callback, &_subs);
|
||||
|
||||
/* Check if this is the smallest interval so far and update _sub_min_interval */
|
||||
if (_sub_min_interval == nullptr || _sub_min_interval->getInterval() < sub_px4->getInterval()) {
|
||||
if (_sub_min_interval == nullptr || _sub_min_interval->getInterval() > sub_px4->getInterval()) {
|
||||
_sub_min_interval = sub_px4;
|
||||
}
|
||||
return (Subscriber*)sub_px4;
|
||||
|
||||
@@ -89,6 +89,11 @@ public:
|
||||
~SubscriberPX4() {};
|
||||
|
||||
void update() {
|
||||
if (!uORB::Subscription<M>::updated()) {
|
||||
/* Topic not updated, do not call callback */
|
||||
return;
|
||||
}
|
||||
|
||||
/* get latest data */
|
||||
uORB::Subscription<M>::update();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user