ROS学习笔记-一个节点收发多个话题

 一个节点发布odom话题和订阅cmd_vel话题

int main(int argc, char **argv)
{
    ros::init(argc, argv, "turtlebot3_core"); //初始化节点
	ros::NodeHandle n;

	ros::Subscriber sub = n.subscribe("/cmd_vel", 50, callback); //订阅话题

	ros::Publisher odom_pub = n.advertise<nav_msgs::Odometry>("odom", 50); //发布话题
	ros::Duration(1.0).sleep(); //延时等待话题发布完成

	tf::TransformBroadcaster odom_broadcaster;

    double x = 0.0;
	double y = 0.0;
	double th = 0.0;

    double vx = 0.1;
    double vy = -0.1;
    double vth = 0.1;
 
    ros::Time current_time, last_time;
    current_time = ros::Time::now();
    last_time = ros::Time::now();
 
    ros::Rate r(1.0);
    while(n.ok()){
 
        ros::spinOnce();               // check for incoming messages
        current_time = ros::Time::now();
 
        //compute odometry in a typical way given the velocities of the robot
        double dt = (current_time - last_time).toSec();
        double delta_x = (vx * cos(th) - vy * sin(th)) * dt;
        double delta_y = (vx * sin(th) + vy * cos(th)) * dt;
        double delta_th = vth * dt;
 
        x += delta_x;
        y += delta_y;
        th += delta_th;
 
        //since all odometry is 6DOF we'll need a quaternion created from yaw
        geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromYaw(th);
 
        //first, we'll publish the transform over tf
        geometry_msgs::TransformStamped odom_trans;
        odom_trans.header.stamp = current_time;
        odom_trans.header.frame_id = "odom";
        odom_trans.child_frame_id = "base_link";
 
        odom_trans.transform.translation.x = x;
        odom_trans.transform.translation.y = y;
        odom_trans.transform.translation.z = 0.0;
        odom_trans.transform.rotation = odom_quat;
 
        //send the transform
        odom_broadcaster.sendTransform(odom_trans);
 
        //next, we'll publish the odometry message over ROS
        nav_msgs::Odometry odom;
        odom.header.stamp = current_time;
        odom.header.frame_id = "odom";
 
        //set the position
        odom.pose.pose.position.x = x;
        odom.pose.pose.position.y = y;
        odom.pose.pose.position.z = 0.0;
        odom.pose.pose.orientation = odom_quat;
 
        //set the velocity
        odom.child_frame_id = "base_link";
        odom.twist.twist.linear.x = vx;
        odom.twist.twist.linear.y = vy;
        odom.twist.twist.angular.z = vth;
 
        //publish the message
        odom_pub.publish(odom);
 
        last_time = current_time;
        r.sleep();
    }
    ros::spin();
	return 0;
}

订阅回调函数

void callback(const geometry_msgs::Twist &cmd_vel)
{
	// ROS_INFO("Received a /cmd_vel message!");
	ROS_INFO("Linear Components:[%f,%f,%f]", cmd_vel.linear.x, cmd_vel.linear.y, cmd_vel.linear.z);
	ROS_INFO("Angular Components:[%f,%f,%f]", cmd_vel.angular.x, cmd_vel.angular.y, cmd_vel.angular.z);
	// Do velocity processing here:
	// Use the kinematics of your robot to map linear and angular velocities into motor commands


	// Then set your wheel speeds (using wheel_left and wheel_right as examples)
	//    wheel_left.set_speed(v_l)
	//    wheel_right.set_speed(v_r)
	// std::cout<<"left"<<std::endl;
    // std::cout<<"right"<<std::endl;
}

参考资料

https://www.cnblogs.com/gary-guo/p/7215284.html

https://blog.csdn.net/JeSuisDavid/article/details/121304217

ROS学习笔记7 (zhaoxuhui.top)

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值