消息格式
上ROS官网查看👉ROS
ROS官网给定的主题,一般我们使用第二个。
实现思路
(1)mawei@Ubuntu:~/catkin_ws/src$ catkin_create_pkg imu_pkg roscpp rospy sensor_msgs
实现代码👇
//imu_node.cpp
#include<ros/ros.h>
#include<sensor_msgs/Imu.h>
#include<tf/tf.h>
void IMUCallback(const sensor_msgs::Imu msg)
{
if(msg.orientation_covariance[0]<0)//如果第一个元素为0,表示没有意义,直接推出
return;
//使用tf工具进行四元数转换
tf::Quaternion quaternion(
msg.orientation.x,
msg.orientation.y,
msg.orientation.z,
msg.orientation.w
);
double roll, pitch, yam;
tf::Matrix3x3(quaternion).getRPY(roll, pitch, yam);
roll=roll*180/M_PI;
pitch=pitch*180/M_PI;
yam=yam*180/M_PI;
ROS_INFO("滚转=%.0f 仰角=%.0f 朝向=%.0f ",roll, pitch, yam);
}
int main(int argc, char *argv[])
{
/* code */
setlocale(LC_ALL,"");
ros::init(argc,argv,"imu_node");
ros::NodeHandle nh;
ros::Subscriber sub=nh.subscribe("/imu/data",10, &IMUCallback);
ros::spin();
return 0;
}
备注:✨编译规则不要忘了,.txt文件末尾添加👇
add_executable(imu_node src/imu_node.cpp)
add_dependencies(imu_node KaTeX parse error: Expected '}', got 'EOF' at end of input: {{PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(imu_node
${catkin_LIBRARIES}
)