反光兽 2021-06-26 15:34 采纳率: 25%
浏览 1200
已结题

ros中怎么用c++让turtlesim海龟画一个爱心

#include "ros/ros.h"
#include<geometry_msgs/Twist.h> 
 
int main(int argc, char *argv[])
{
    int PI = 3.141592653589793653589793;
    ros::init(argc, argv, "heart_shape");  
    ros::NodeHandle n;         
    ros::Publisher vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);
    geometry_msgs::Twist vel_cmd; 
    ros::Rate loopRate(2);
     int count = 0;  
     int i = 0;
    while(ros::ok())
    {
        vel_cmd.linear.x = 1.0;
        vel_cmd.linear.y = 0.0;  
        vel_cmd.linear.z = 0.0;
 
        vel_cmd.angular.x = 0;
        vel_cmd.angular.y = 0;
        vel_cmd.angular.z = 1; 
        count++;

        while (count==9&&i==0)
        {
                count = 0;
                i=1;
                vel_cmd.linear.x = 0.0;
                vel_cmd.angular.z = 2*PI;
        }

        while (count==7&&i==1)
        {

                vel_cmd.linear.x = 1.0;
                vel_cmd.angular.z = 2;
        }
        vel_pub.publish(vel_cmd); 
        ros::spinOnce();
        loopRate.sleep();
    }
    return 0;
}

我的想法是先画两个半圆,再画一个大的半圆,但代码怎么改写都没办法实现,下面是基本思路,没有写完但调试出现问题
     

  • 写回答

2条回答 默认 最新

  • 等xin 2022-03-31 19:07
    关注

    #include "ros/ros.h"
    #include <geometry_msgs/Twist.h>

    int main(int argc, char *argv[])
    {
    double PI = 3.141592653589793653589793;
    ros::init(argc, argv, "heart_shape");
    ros::NodeHandle n;
    ros::Publisher vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);
    geometry_msgs::Twist vel_cmd;
    ros::Rate loopRate(2);
    int count = 0;
    int i = 0;
    while (ros::ok())
    {
    geometry_msgs::Twist msg;
    msg.linear.x = 1;
    msg.angular.z = 1;
    i++;

        if (i ==9 )
        {
    
    
            msg.linear.x = 0;
            msg.angular.z = 2*PI;
        }
        
        if (i == 16)
        {
            ;
            msg.linear.x = 1.0;
            msg.angular.z = 2;
        }
    
        if (i >= 17)
        {
            ;
            msg.linear.x = 1;
            msg.angular.z = 0;
            i++;
            if (i >= 27)
            {    
                msg.linear.x = 0.15;
                msg.linear.y = 1;
                i++;
                if (i >= 43)
                {
                    msg.linear.x = 0;
                    msg.linear.y = 0;
            }
        }
            
            
        }
    
        vel_pub.publish(msg);
    
        //发布消息
        ROS_INFO_STREAM("Sending random velocity command: "
                        << "linear = " << msg.linear.x
                        << " angular = " << msg.angular.z);
        //按照循环频率延时
        //ros::spinOnce();
        loopRate.sleep();
    }
    
        return 0;
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月8日
  • 已采纳回答 3月31日
  • 请回答用户的提问 1月20日

悬赏问题

  • ¥15 mysql sum函数优化
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题
  • ¥20 基于matlab的航迹融合 航迹关联 航迹插补
  • ¥15 用Matlab实现图中的光线追迹