Я столкнулся с проблемой при запуске файла .cpp, clean_bot.cpp (показан ниже) через запуск ros. Вот следующие детали:
#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 <[email protected]>
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 по созданию и запуску пакетов/узлов.