The first program developed by Ros

Posted by Joefunkx on Wed, 22 Jan 2020 16:50:45 +0100

Engineering creation

  • Create and enter the catkin folder
wilson@ubuntu:~/code$ mkdir catkin
wilson@ubuntu:~/code$ cd catkin/
  • Create and enter src folder
wilson@ubuntu:~/code/catkin$ mkdir src
wilson@ubuntu:~/code/catkin$ cd src/
wilson@ubuntu:~/code/catkin/src$ 
  • Initializing the workspace in the src directory
wilson@ubuntu:~/code/catkin/src$ catkin_init_workspace 
Creating symlink "/home/wilson/code/catkin/src/CMakeLists.txt" pointing to "/opt/ros/melodic/share/catkin/cmake/toplevel.cmake"
wilson@ubuntu:~/code/catkin/src$ ls
CMakeLists.txt
  • Switch back to the project directory and execute catkin? Make to generate the development environment
wilson@ubuntu:~/code/catkin/src$ cd ..
wilson@ubuntu:~/code/catkin$ ls
src
wilson@ubuntu:~/code/catkin$ catkin_make
Base path: /home/wilson/code/catkin
Source space: /home/wilson/code/catkin/src
Build space: /home/wilson/code/catkin/build
Devel space: /home/wilson/code/catkin/devel
Install space: /home/wilson/code/catkin/install
  • Activate the program to ensure that the terminal is available elsewhere
wilson@ubuntu:~/code/catkin$ ls
build  devel  src
wilson@ubuntu:~/code/catkin$ source devel/setup.bash
  • Enter the src folder and create the Hello? Demo project, which depends on STD? Msgs Rossy roscpp
wilson@ubuntu:~/code/catkin/src$ catkin_create_pkg hello_demo std_msgs rospy roscpp
Created file hello_demo/package.xml
Created file hello_demo/CMakeLists.txt
Created folder hello_demo/include/hello_demo
Created folder hello_demo/src
Successfully created files in /home/wilson/code/catkin/src/hello_demo. Please adjust the values in package.xml.

Writing code

  • Write subscriber and publisher code in project directory src
wilson@ubuntu:~/code/catkin/src/hello_demo/src$ ls
publisher.cpp  subscriber.cpp

publisher.cpp node name is publisher, session name is chatter

#include "ros/ros.h"
#include "std_msgs/String.h"

#include <sstream>

int main(int argc, char **argv)
{
    ros::init(argc, argv, "publisher");

    ros::NodeHandle n;

    ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);

    ros::Rate loop_rate(10);

    int count = 0;
    while(ros::ok())
    {
        std_msgs::String msg;

        std::stringstream ss;
        ss<<"hello, "<<count;
        msg.data = ss.str();

        ROS_INFO("%s", msg.data.c_str());

        chatter_pub.publish(msg);

        ros::spinOnce();

        loop_rate.sleep();
        ++count;
    }

    return 0;
}

subscriber.cpp node is subscriber and session is chatter

#include "ros/ros.h"
#include "std_msgs/String.h"

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
    ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "subscriber");

    ros::NodeHandle n;

    ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

    ros::spin();

    return 0;
}
  • Modify CMakelist.txt file
wilson@ubuntu:~/code/catkin/src/hello_demo$ ls
CMakeLists.txt  include  package.xml  src
wilson@ubuntu:~/code/catkin/src/hello_demo$ gedit CMakeLists.txt

Add the following definition

add_executable(publisher src/publisher.cpp)
target_link_libraries(publisher ${catkin_LIBRARIES})
add_dependencies(publisher hello_demo_generate_messages_cpp)

add_executable(subscriber src/subscriber.cpp)
target_link_libraries(subscriber ${catkin_LIBRARIES})
add_dependencies(subscriber hello_demo_generate_messages_cpp)
  • Switch back to working directory
wilson@ubuntu:~/code/catkin/src/hello_demo$ cd ..
wilson@ubuntu:~/code/catkin/src$ cd ..
wilson@ubuntu:~/code/catkin$ ls
build  devel  src
  • Compile
wilson@ubuntu:~/code/catkin$ catkin_make
  • Activate the program to ensure that the terminal is available elsewhere
wilson@ubuntu:~/code/catkin$ source devel/setup.bash

Test verification

  • Run core node
    wilson@ubuntu:~/code/catkin$ roscore
  • Run publisher
wilson@ubuntu:~/code/catkin$ rosrun hello_demo publisher 
[ INFO] [1579615615.820928731]: hello, 0
[ INFO] [1579615615.921658189]: hello, 1
  • Run subscribers
wilson@ubuntu:~/code/catkin$ rosrun hello_demo subscriber 
[ INFO] [1579615616.122224767]: I heard: [hello, 3]
[ INFO] [1579615616.221957346]: I heard: [hello, 4]
131 original articles published, praised 81, visited 70000+
Private letter follow

Topics: Ubuntu xml cmake Session