ros::Publisher: 找不到命令? | ROS 旋律 | Ubuntu 18.04

ros::Publisher: command not found? | ROS Melodic | Ubuntu 18.04

我在 运行 通过 ros 运行 创建 .cpp 文件时遇到问题,cleaner_bot.cpp(如下所示)。以下是详细信息:

    #include "ros/ros.h"
    #include "geometry_msgs/Twist.h"
    #include "std_msgs/String.h"
    
    #include <sstream>
    
    ros::Publisher vel_publish;
    
    void move(double speed, double direction, bool is_forward);
    
    int main(int argc, char **argv)
    {
        ros::init(argc, argv, "Cleaner Bot");
        ros::NodeHandle n;
    
        vel_publish = n.advertise<geometry_msgs::Twist>("/turtle/cmd_vel", 10);
        move(2.0, 5.0, 1.0);
    }
    
    void move(double speed, double distance, bool is_forward)
    {
        geometry_msgs::Twist vel_msg;
    
        // double time = distance/speed;
    
        if(is_forward)
        {
            // Setting a random linear velocity in the x direction
            vel_msg.linear.x = abs(speed);
        }
    
        else
        {
            vel_msg.linear.x = -abs(speed);
        }
    
        vel_msg.linear.y = 0;
        vel_msg.linear.z = 0;
    
        vel_msg.angular.x = 0;
        vel_msg.angular.y = 0;
        vel_msg.angular.z = 0;
    
        // Starting the loop with time, t = 0
        double time_prev = ros::Time::now().toSec();
    
        // Current distance is 0
        double dist_now = 0;
        
        ros::Rate loop_rate(10);
        do
        {
            vel_publish.publish(vel_msg);
            double time_now = ros::Time::now().toSec();
            double dist_now = speed * (time_now - time_prev); 
            ros::spinOnce();
        
        }while(dist_now < distance);
        
        vel_msg.linear.x = 0;
        vel_publish.publish(vel_msg);
    }

我在尝试执行 rosrun 时收到此消息,但是执行 catkin_make 没有给我任何问题。

谁能帮忙找到遗漏的东西?以下是我在执行 rosrun

后得到的结果
rosrun turtlesim_custom_cleaner src/cleaner_robot.cpp

/home/autoai/catkin_ws/src/turtlesim_custom_cleaner/src/cleaner_robot.cpp: line 7: ros::Publisher: command not found
/home/autoai/catkin_ws/src/turtlesim_custom_cleaner/src/cleaner_robot.cpp: line 9: syntax error near unexpected token `('
/home/autoai/catkin_ws/src/turtlesim_custom_cleaner/src/cleaner_robot.cpp: line 9: `void move(double speed, double direction, bool is_forward);'

系统详情:

Distributor ID: Ubuntu
Description:    Ubuntu 18.04.6 LTS
Release:    18.04
Codename:   bionic

Package: ros-melodic-ros
Status: install ok installed
Priority: optional
Section: misc
Installed-Size: 14
Maintainer: Dirk Thomas <dthomas@osrfoundation.org>
Architecture: amd64
Version: 1.14.9-1bionic.20210505.012339
Depends: ros-melodic-catkin, ros-melodic-mk, ros-melodic-rosbash, ros-melodic-rosboost-cfg, ros-melodic-rosbuild, ros-melodic-rosclean, ros-melodic-roscreate, ros-melodic-roslang, ros-melodic-roslib, ros-melodic-rosmake, ros-melodic-rosunit
Description: ROS packaging system

提前致谢。

你不应该 (ros)运行 你的项目的源文件,而是由 catkin_make 创建的可执行文件。

例如:

rosrun turtlesim_custom_cleaner cleaner_robot

其中 cleaner_robot 是您的可执行文件的名称,这取决于您在 CMakeLists.txt 中定义的内容,但一般来说,您应该在其中包含如下内容:

add_executable(cleaner_robot src/cleaner_robot.cpp)

查看有关创建和 运行ning packages/nodes

的 ROS 教程