ROS basic notes

ros workspace

The workspace can be compared with the project in vs. the src folder under the workspace stores various ros packages. ros packages can be considered as function packages to realize specific functions, such as robot_description describes the robot, and navigation plans the path.
1. Create workspace:

mkdir -p practice_ws/src
cd practice_ws
catkin_init_workspace

baxter_ws is a customizable space name.
2. Create function package

cd src
catkin_creat_pkg topic_demo roscpp rospy std_msgs

roscpp rospy std_msgs is a function package dependency.

Generation of ros node and implementation of topic communication

1. Introduction
The ros node is an executable file. It is an executable file or python script generated by c + + compilation. It is usually developed by editing the c + + source file or python script, which are placed in the src folder and script folder of the function package respectively.

2. Editing and compiling (c + +)
Write c + + topic publisher talker:

#include<ros/ros.h>
#include<topic_demo/gps.h>
int main(int argc, char**argv){
  ros::init(argc,argv,"talker");
  ros::NodeHandle nh;
 //To prevent the loss of initial data, ROS:: duration (1) is usually required here Sleep() waits for successful registration
 //Message content definition and assignment
  topic_demo::gps msg;//Custom msg
  msg.x=1.0;
  msg.y=1.0;
  msg.state="working";
  //Publisher definition
  ros::Publisher pub = nh.advertise<topic_demo::gps>("gps_info",1);
                                   //Template, defining message type / / topic name / / queue
  ros::Rate loop_rate(1.0);//1HZ

  while(ros::ok()){
    msg.x = 1.03*msg.x;
    msg.y = 1.01*msg.y;
    ROS_INFO("Talker:GPS:x=%f,y=%f",msg.x,msg.y);
    pub.publish(msg);//Release news
    loop_rate.sleep();//Sleep at the rate. The last rate shall prevail. If the set time is not exceeded, wait until the set time
	}
  return 0;
}

Custom message GPS MSG is as follows

float32 x
float32 y
string state

Write a c + + subscriber listener:

#include<ros/ros.h>
#include<topic_demo/gps.h>
#include<std_msgs/Float32.h>

void gpsCallback(const topic_demo::gps::ConstPtr &msg)
{
  //The callback function processes the received data through the constant reference pointer msg
  std_msgs::Float32 distance;
  distance.data = sqrt(pow(msg->x,2)+pow(msg->y,2));
  ROS_INFO("Listener:Distance to origin = %f, state = %s", distance.data,msg->state.c_str());
}

int main(int argc, char** argv){
  ros::init(argc,argv,"listener");//Initialize the node, define the node name, and register
  ros::NodeHandle n;//ros handle
  ros::Subscriber sub = n.subscribe("gps_info", 1,gpsCallback);
  									//Topic name / / queue length / / callback function
  ros::spin();//Call callback function later
  return 0;
}

For c + + source files, they can only be executed after compilation. The c + + compilation rules are defined in cmakelist. Therefore, to generate an executable file, you need to edit the cmakelist file and add:

add_message_files(FILES gps.msg)##Add custom message
generate_messages(DEPENDENCIES std_msgs)

add_executable(Executable file name
  src/Source file name.cpp
)##This sentence can be understood as generating an executable file
target_link_libraries(Executable file name
  ${catkin_LIBRARIES}
)## Add a link library to link to the official catkin library

Then cd to the workspace directory and use catkin_ The make command compiles.
Edit python files
Note: #/ usr/bin/env python must be placed in the first line, and errors will be reported in the second line:

3. Implementation
cd to workspace
Remember source before executing/ devel/setup. bash

rosrun Package name executable name

Example:

rosrun turtlesim turtlesim_node

It should be noted here that many places think it is the rosrun package name node name, but actually the startup is an executable file (node). The executable file name is not necessarily the node name. The node name is named in the node initialization, but if the launch file is started, the name in init() will be overwritten.
3.launch file starts multiple nodes

<launch>
    <node pkg="Package name" type="Executable file name" name="Node name" />

</launch>

Summary of ros auxiliary commands

View message delivery: rosrun rqt_graph rqt_graph
View topic list: rostopic list
View data published by topic: rostopic echo [topic]
View the message type of the published topic: rostopic type [topic]
View message details: rosmsg show [msg]
Rate of report data publishing: rostopic hz [topic]
The dynamic time chart shows the data published to a topic: rosrun rqt_plot rqt_plot

Keywords: Machine Learning AI Autonomous vehicles

Added by trev on Tue, 28 Dec 2021 15:20:21 +0200