<SOLVED> How to subscribe to IMU data? Offboard control, PX4, MAVROS
Sorry I couldn't find out how to paste code properly. I'm trying to get IMU data from px4 through MAVROS. I am doing offboard control.
I referred from this website to make an IMU listener. (Writing a Simple Subscriber for IMU)
Although I am not sure how to find type in classes that are used in this code.
Right now, it isn't even going through chatterCallback even though I move px4 around and changing IMU values. Also when I run rqt_graph
, it will give me imu_listener standing alone and not connected to MAVROS.
Can anyone give me advise how to make this IMUlistener work?
Thankyou, Sarah
.#include "ros/ros.h" //take away period in the beggining
.#include "sensor_msgs/Imu.h"
.#include "mavros_msgs/State.h"
void chatterCallback(const sensor_msgs::Imu::ConstPtr& msg)
{
ROS_INFO("Hello");
ROS_INFO("Imu Seq: [%d]", msg->header.seq);
ROS_INFO("Imu Orientation x: [%f], y: [%f], z: [%f], w: [%f]", msg->orientation.x,msg->orientation.y,msg->orientation.z,msg->orientation.w);
}
mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
current_state = *msg;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "imu_listener");
ros::NodeHandle n;
ros::Subscriber state_sub = n.subscribe<mavros_msgs::State>
("mavros/state", 10, state_cb);
ros::Subscriber sub = n.subscribe<sensor_msgs::Imu>
("mavros/imu/data", 1000, chatterCallback);
ros::Rate rate(20.0);
while(ros::ok() && current_state.connected){
ros::spinOnce();
rate.sleep();
}
for(int i = 100; ros::ok() && i > 0; --i){
ros::spinOnce();
rate.sleep();
}
ros::spin();
return 0;
}
Welcome! In order to get your the code formatted correctly, you don't use the
<code>
or<pre>
tags. What you do is paste your code, highlight it, then click the preformatted text (101010
) button.Oh ok thank you for the information!