ROS四元数、欧拉角互转

#include<ros/ros.h>
#include<geometry_msgs/PoseWithCovarianceStamped.h> //amcl_pose
#include<tf/transform_datatypes.h> //转换函数头文件
#include <tf/tf.h>

class RobotMove
{
public:
    RobotMove();
    void poseCallBack(geometry_msgs::PoseWithCovarianceStampedConstPtr &pose);

private:
    ros::NodeHandle n;
    ros::Subscriber pose_sub; //订阅amcl位置
    float pose_x,pose_y,pose_z;
};
//构造函数
RobotMove::RobotMove()
{  
	pose_sub=n.subscribe<geometry_msgs::PoseWithCovarianceStamped>("/amcl_pose",10,&RobotMove::poseCallBack,this);
}

//位置回调函数
void RobotMove::poseCallBack(geometry_msgs::PoseWithCovarianceStampedConstPtr &pose)
{
    //四元数转欧拉角
    tf::Quaternion quat; //定义一个四元数
    tf::quaternionMsgToTF(pose->pose.pose.orientation,quat); //取出方向存储于四元数
 
    double roll,pitch,yaw; //定义存储r\p\y的容器
    tf::Matrix3x3(quat).getRPY(roll,pitch,yaw); //进行转换
    pose_x=roll;
    pose_y=pitch;
    pose_z=yaw;

	//欧拉角转四元数,类型可以不同
	geometry_msgs::Quaternion quaternion;//定义四元数
	quaternion=tf::createQuaternionMsgFromRollPitchYaw(0,0,0); //欧拉角
}
int main(int argc, char *argv[])
{
    ros::init(argc, argv, "robotMoveAction");
    RobotMove robotmove;
    ros::spin();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值