I facing an issue while running a .cpp file, cleaner_bot.cpp (shown below) through ros run. Here are the following details:
#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);
}
I am receiving this message when I am trying to execute rosrun
, however doing catkin_make
is not giving me any issues.
Can anyone help find what has been missed? The below is what I am getting after executing 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);'
System details:
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
Thanks all in advance.
You should not (ros)run the source files of your project but rather the executable created by catkin_make
.
For example:
rosrun turtlesim_custom_cleaner cleaner_robot
Where cleaner_robot
is the name of your executable which depends on what you have defined in your CMakeLists.txt
but in general, you should have something like this in there:
add_executable(cleaner_robot src/cleaner_robot.cpp)
Have a look a the ROS tutorials on creating and running packages/nodes