我在通过ros运行.cpp文件时遇到问题,cleanuer_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)
看看关于创建和运行包/节点的ROS教程
- 编写一个简单的发布服务器和订阅服务器
- 检查简单发布服务器和订阅服务器