mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-23 22:58:10 +08:00
extend subscriber/publisher example
This commit is contained in:
@@ -45,7 +45,9 @@ using namespace px4;
|
||||
|
||||
PublisherExample::PublisherExample() :
|
||||
_n(),
|
||||
_rc_channels_pub(_n.advertise<px4_rc_channels>())
|
||||
_rc_channels_pub(_n.advertise<px4_rc_channels>()),
|
||||
_v_att_pub(_n.advertise<px4_vehicle_attitude>()),
|
||||
_parameter_update_pub(_n.advertise<px4_parameter_update>())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -50,4 +50,6 @@ public:
|
||||
protected:
|
||||
px4::NodeHandle _n;
|
||||
px4::Publisher<px4::px4_rc_channels> * _rc_channels_pub;
|
||||
px4::Publisher<px4::px4_vehicle_attitude> * _v_att_pub;
|
||||
px4::Publisher<px4::px4_parameter_update> * _parameter_update_pub;
|
||||
};
|
||||
|
||||
@@ -67,9 +67,15 @@ SubscriberExample::SubscriberExample() :
|
||||
/* No callback */
|
||||
_sub_rc_chan = _n.subscribe<px4_rc_channels>(500);
|
||||
|
||||
/* Class Method */
|
||||
/* Class method */
|
||||
_n.subscribe<px4_rc_channels>(&SubscriberExample::rc_channels_callback, this, 1000);
|
||||
|
||||
/* Another class method */
|
||||
_n.subscribe<px4_vehicle_attitude>(&SubscriberExample::vehicle_attitude_callback, this, 1000);
|
||||
|
||||
/* Yet antoher class method */
|
||||
_n.subscribe<px4_parameter_update>(&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);
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ protected:
|
||||
px4::Subscriber<px4_rc_channels> * _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);
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user