From abeae7b6f6d70042c6b0bed493ade52727648c2d Mon Sep 17 00:00:00 2001 From: Thomas Gubler Date: Mon, 26 Jan 2015 14:50:23 +0100 Subject: extend subscriber/publisher example --- src/examples/publisher/publisher_example.cpp | 25 ++++++++++++++++++++----- src/examples/publisher/publisher_example.h | 2 ++ src/examples/subscriber/subscriber_example.cpp | 26 +++++++++++++++++++++++--- src/examples/subscriber/subscriber_example.h | 3 ++- 4 files changed, 47 insertions(+), 9 deletions(-) (limited to 'src/examples') diff --git a/src/examples/publisher/publisher_example.cpp b/src/examples/publisher/publisher_example.cpp index 9952a16e4..e7ab9eb5a 100644 --- a/src/examples/publisher/publisher_example.cpp +++ b/src/examples/publisher/publisher_example.cpp @@ -45,7 +45,9 @@ using namespace px4; PublisherExample::PublisherExample() : _n(), - _rc_channels_pub(_n.advertise()) + _rc_channels_pub(_n.advertise()), + _v_att_pub(_n.advertise()), + _parameter_update_pub(_n.advertise()) { } @@ -57,10 +59,23 @@ int PublisherExample::main() loop_rate.sleep(); _n.spinOnce(); - px4_rc_channels msg; - msg.data().timestamp_last_valid = px4::get_time_micros(); - PX4_INFO("%llu", msg.data().timestamp_last_valid); - _rc_channels_pub->publish(msg); + /* Publish example message */ + px4_rc_channels rc_channels_msg; + rc_channels_msg.data().timestamp_last_valid = px4::get_time_micros(); + PX4_INFO("rc: %llu", rc_channels_msg.data().timestamp_last_valid); + _rc_channels_pub->publish(rc_channels_msg); + + /* Publish example message */ + px4_vehicle_attitude v_att_msg; + v_att_msg.data().timestamp = px4::get_time_micros(); + PX4_INFO("att: %llu", v_att_msg.data().timestamp); + _v_att_pub->publish(v_att_msg); + + /* Publish example message */ + px4_parameter_update parameter_update_msg; + parameter_update_msg.data().timestamp = px4::get_time_micros(); + PX4_INFO("param update: %llu", parameter_update_msg.data().timestamp); + _parameter_update_pub->publish(parameter_update_msg); } diff --git a/src/examples/publisher/publisher_example.h b/src/examples/publisher/publisher_example.h index 4ff59a226..8165b0ffc 100644 --- a/src/examples/publisher/publisher_example.h +++ b/src/examples/publisher/publisher_example.h @@ -50,4 +50,6 @@ public: protected: px4::NodeHandle _n; px4::Publisher * _rc_channels_pub; + px4::Publisher * _v_att_pub; + px4::Publisher * _parameter_update_pub; }; diff --git a/src/examples/subscriber/subscriber_example.cpp b/src/examples/subscriber/subscriber_example.cpp index 781dde486..e1292f788 100644 --- a/src/examples/subscriber/subscriber_example.cpp +++ b/src/examples/subscriber/subscriber_example.cpp @@ -67,9 +67,15 @@ SubscriberExample::SubscriberExample() : /* No callback */ _sub_rc_chan = _n.subscribe(500); - /* Class Method */ + /* Class method */ _n.subscribe(&SubscriberExample::rc_channels_callback, this, 1000); + /* Another class method */ + _n.subscribe(&SubscriberExample::vehicle_attitude_callback, this, 1000); + + /* Yet antoher class method */ + _n.subscribe(&SubscriberExample::parameter_update_callback, this, 1000); + PX4_INFO("subscribed"); } @@ -78,8 +84,22 @@ SubscriberExample::SubscriberExample() : * Also the current value of the _sub_rc_chan subscription is printed */ void SubscriberExample::rc_channels_callback(const px4_rc_channels &msg) { - PX4_INFO("Callback (method): [%llu]", + PX4_INFO("rc_channels_callback (method): [%llu]", msg.data().timestamp_last_valid); - PX4_INFO("Callback (method): value of _sub_rc_chan: [%llu]", + PX4_INFO("rc_channels_callback (method): value of _sub_rc_chan: [%llu]", _sub_rc_chan->data().timestamp_last_valid); } + +void SubscriberExample::vehicle_attitude_callback(const px4_vehicle_attitude &msg) { + PX4_INFO("vehicle_attitude_callback (method): [%llu]", + msg.data().timestamp); +} + +void SubscriberExample::parameter_update_callback(const px4_parameter_update &msg) { + PX4_INFO("parameter_update_callback (method): [%llu]", + msg.data().timestamp); + PX4_PARAM_GET(_p_sub_interv, &_interval); + PX4_INFO("Param SUB_INTERV = %d", _interval); + PX4_PARAM_GET(_p_test_float, &_test_float); + PX4_INFO("Param SUB_TESTF = %.3f", (double)_test_float); +} diff --git a/src/examples/subscriber/subscriber_example.h b/src/examples/subscriber/subscriber_example.h index 8da3df438..9b6d890e3 100644 --- a/src/examples/subscriber/subscriber_example.h +++ b/src/examples/subscriber/subscriber_example.h @@ -59,6 +59,7 @@ protected: px4::Subscriber * _sub_rc_chan; void rc_channels_callback(const px4_rc_channels &msg); - + void vehicle_attitude_callback(const px4_vehicle_attitude &msg); + void parameter_update_callback(const px4_parameter_update &msg); }; -- cgit v1.2.3