本文介绍 ROS-1 Hello Word in ROS

ROS-1 Hello Word in ROS

This article was original written by Jin Tian, welcome re-post, first come with https://jinfagang.github.io . but please keep this copyright info, thanks, any question could be asked via wechat: jintianiloveu

0. ROS WorkSpace

ok, before we get started, we should know how to start a workspace, simply using:

1
2
3
4
mkdir my_ws
mkdir -P my_ws/src
cd my_ws
catkin_init

these command will start a ws, and generate a workspace.

1. ROS IDE

I think the most convenient IDE is roboware-studio, this can be downloaded and install from official site. Run it in terminal after you source your ros enviroment. roboware-studio based on vscode, it’s very simple to use.

After you install roboware and you have your workspace, you should right run it after this:

1
2
source /opt/ros/kinetic/setup.sh
roboware-studio .

So you will see a beautiful interface of vscode, you haven’t add any packages right? Using this to create a ros package:

1
catkin_create_pkg tutor std_msgs rospy roscpp

this command will create a new package named: tutor, and the rest package are dependencies, std_msgs contains all message type of many languages, and rospy are the essential support of your package, you will have both python and C++ program in one package.

2. ROS Hello World

ok, once you have your package created, you should add a new main.cpp to your src/ folder, add there codes into it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "std_msgs/String.h"
#include <sstream>
#include "ros/ros.h"
#include "ros/time.h"
int main(int argc, char *argv[])
{
ros::init(argc, argv, "example_node");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("message", 1000);
ros::Rate loop_rate(10);
while (ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss << "Hello, this is me, your friend!";
msg.data = ss.str();
chatter_pub.publish(msg);
ROS_INFO("%s", msg.data.c_str());
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}

this will implement a message publisher, it will publish a message of : Hello, this is me ... in very 0.1 seconds which is in frequency of 10 Hz.

ok, back to your workspace root folder, and run:

1
catkin_make

this will make the whole workspace, include all your packages. After that you could run your node like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
rosrun tutor main#include "std_msgs/String.h"
#include <sstream>
#include "ros/ros.h"
#include "ros/time.h"
int main(int argc, char *argv[])
{
ros::init(argc, argv, "example_node");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("message", 1000);
ros::Rate loop_rate(10);
while (ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss << "Hello, this is me, your friend!";
msg.data = ss.str();
chatter_pub.publish(msg);
ROS_INFO("%s", msg.data.c_str());
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}

the tutoris your package name and of-course this is your node name, after that is the executable program name of your package, be caution that the executable program maybe Python and C++, so every package could have multiple executable program.