ROS实战_1.5 ROS调试与可视化

0. 常用功能

roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
rosrun rqt_graph rqt_graph
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

    如果你将鼠标放在/turtle1/command_velocity上方,相应的ROS节点(蓝色和绿色)和话题(红色)就会高亮显示。

正如你所看到的,turtlesim_node和turtle_teleop_key节点正通过一个名为 /turtle1/command_velocity的话题来互相通信。 
这里写图片描述

输出某个话题的 消息:

 rostopic echo /turtle1/cmd_vel
  • 1
  • 1

    你可能看不到任何东西因为现在还没有数据发布到该话题上。接下来我们通过按下方向键使turtle_teleop_key节点发布数据。记住如果turtle没有动起来的话就需要你重新选中turtle_teleop_key节点运行时所在的终端窗口。 
现在当你按下向上方向键时应该会看到下面的信息:

linear: 2.0
angular: 0.0
---
linear: 2.0
angular: 0.0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
使用 rostopic list  能够列出所有当前订阅和发布的话题 ;
使用 rostopic info /topic 输出活动主题,发布主题,主题订阅者和服务的信息;
使用 rostopic type /topic 用来查看所发布话题的消息类型。
rostopic type /turtle1/cmd_vel
  • 1
  • 1

你应该会看到:geometry_msgs/Twist 
使用rosmsg命令来查看消息的详细情况

rosmsg show geometry_msgs/Twist
  • 1
  • 1

使用 rostopic pub 可以把数据发布到当前某个正在广播的话题上。

rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'
  • 1
  • 1

Note:如果是在pub后面添加-1的话,只能吧一个数据发布3秒;-1 使rostopic发布一条消息后马上退出 ;/turtle1/command_velocity 这是消息所发布到的话题名称 ;turtlesim/Velocity 这是所发布消息的类型。

他的格式:

rostopic pub [topic] [msg_type] [args]
  • 1
  • 1

    正如之前提到的,在一个turtlesim/Velocity消息里面包含有两个浮点型元素:linear和angular。 在本例中,2.0是linear的值,1.8是angular的值。这些参数其实是按照YAML语法格式编写的,这在YAML文档中有更多的描述。 

    你可能已经注意到turtle已经停止移动了。这是因为turtle需要一个稳定的频率为1Hz的命令流来保持移动状态。我们可以使用rostopicpub-r命令来发布一个稳定的命令,在数据类型的后面,添加-r和1 然后就可以持续的发送数据:

rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'
  • 1
  • 1

这里写图片描述 
使用 rostopic hz 用来查看数据发布的频率

rostopic hz /turtle1/pose
  • 1
  • 1

你会看到:

subscribed to [/turtle1/pose]
average rate: 59.354
        min: 0.005s max: 0.027s std dev: 0.00284s window: 58
average rate: 59.459
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

    现在我们可以知道了turtlesim正以大约60Hz的频率发布数据给turtle 


1. 例程

    本节通过让先锋机器人做圆周运动并实时输出位置信息的例子,实例化讲解ROS调试工具的使用;

circle_sim.cpp

#include
#include
#include 
#include  // for std :: setprecision and std :: fixed

/* checks pose messages and outputs them to user */
void poseMessageReceived(const nav_msgs::Odometry& msg) 
{
	std::cout << std::setprecision(2) <("RosAria/cmd_vel", 1000);
  geometry_msgs::Twist msg;
  
  // Create a subscriber object .
	ros::Subscriber pose;
	pose = nh.subscribe("RosAria/pose", 1000, &poseMessageReceived) ; //固定语法:ros::Subscriber **=nh.subscribe("topic type",buff,&callback) 建立订阅关系并执行一些操作
	// nh.subscribe() print_aria_state 节点订阅了RosAria/pose 话题,此话题类型为nav_msgs/Odometry,执行回调函数,结果返回给Subscriber类型的 pose对象
	// 与发布者不同,发布者建立发布关系后,一般无回调函数处理,所以后续需要 ros::Publisher类型的对象执行一些操作
  double CLOCK_SPEED = 10;
  ros::Rate rate(CLOCK_SPEED);

  // Make the robot stop (robot perhaps has a speed already) 
  msg.linear.x = 0;
  msg.linear.y = 0;
  msg.linear.z = 0;
  msg.angular.x = 0;
  msg.angular.y = 0;
  msg.angular.z = 0;
  pub.publish(msg);
// f=CLOCK_SPEED 循环频率;循环一次需要的时间T=1/f;
// count=t/f+1 程序的循环次数(7次 14s),机器人动作循环t/f 次;(6次)
// t/f*T 机器人运动时间(12s)
// msg.angular.z 单位 rad/s
  while(ros::ok())
  {
	  msg.linear.x = 0.5;
    msg.angular.z = 0.3;//逆时针方向为正
	  pub.publish(msg);

    ros::spinOnce();
    rate.sleep();//可看做延时函数,0.5hz,延时2秒
      //频率越高,指令发布的越连续,相当于采样频率越高;对速度和角速度没有影响;
  }
}

2.Using rqt_console and rqt_logger_level to modify the debugging level on the fly

     rqt_logger_level to set the logging level of the nodes or named loggers and rqt_console to visualize, filter, and analyze the logging messages.

    Run roscore and rqt_console to see the logging messages:

$rosrun rqt_console rqt_console

    Inorder to set the severity of the loggers, we must run the following command:

$rosrun rqt_logger_level rqt_logger_level

    Here,we can select the node, then the named logger, and finally itsseverity. Once  we modify it, the new messages received with a severity below the desired one will  not appear in rqt_console.


2.Inspecting what is going on

2.1 Listing nodes, topics, services, and parameters

• rosnode info NODE: This prints information about a node
• rosnode kill NODE: This kills a running node or sends a given signal
• rosnode list: This lists the active nodes
• rosnode machine hostname This lists the nodes running on a particular machine or lists machines
• rosnode ping NODE: This tests the connectivity to the node.
• rosnode cleanup: This purges the registration information from unreachable nodes

• rostopic bw /topic: This displays the bandwidth used by the topic.
• rostopic echo /topic: This prints messages to the screen.
• rostopic find message_type: This finds topics by their type.
• rostopic hz /topic: This displays the publishing rate of the topic.
• rostopic info /topic: This prints information about the active topic,topics published, ones it is subscribed to, and services.
• rostopic list: This prints information about active topics.
• rostopic pub /topic type args: This publishes data to the topic. It allows us to create and publish data in whatever topic we want,
directlyfrom the command line.
• rostopic type /topic: This prints the topic type, that is, the type of message it publishes.

• rosmsg show: This displays the fields of a message
• rosmsg list: This lists all messages
• rosmsg package: This lists all of the messages in a package
• rosmsg packages: This lists all of the packages that have the message
• rosmsg users: This searches for code files that use the message type
• rosmsg md5: This displays the MD5 sum of a message

• ros servicecall /service args: This calls the service with the arguments provided
• ros servicefind msg-type:  This finds services by service type
• ros serviceinfo /service:  This prints information about the service
• ros servicelist:This lists the active services
• ros servicetype /service:This prints the service type
• ros serviceuri /service:  This prints the ROSRPC URI service

• rosparam list: This lists all the parameters in the server
• rosparam get parameter: This gets the value of a parameter
• rosparam set parameter value: This sets the value of a parameter
• rosparam delete parameter:  This deletes a parameter
• rosparam dump file: This saves Parameter Server to a file
• rosparam load file: This loads a file (with parameters) on Parameter Server

Note: Any of these commands can be combined with regular bash commands, such as grep,to look for the desired nodes, topics, services, or parameters. For example, action goal topics can be found using the following command:

$rostopic list | grep pose

2.2 rqt_topic

    ROS provides several GUIs to play with topics and services. First, rqt_top shows the nodes running in an interface similar to a table of processes (ToP), which allows us to rapidly see all the nodes and resources they are using.

rosrunrqt_top rqt_top

    On the other hand, rqt_topic shows the debugging information about topics,  including publishers, subscribers, the publishing rate, and messages published. You can view the message fields topic and select the topics you want to subscribe  to,to analyze their bandwidth and rate (Hz) and see the latest message published; note that latched topics usually do not publish continuously, so you will not see any  information about them.

rosrun rqt_topic rqt_topic

Note: 选中某一话题后,不仅能实时检测话题发布的消息值,还能查看话题发布的消息类型的详细情况,类似于以下命令的组合使用:

$ rostopic type /RosAria/pose

nav_msgs/Odometry

$ rosmsg show nav_msgs/Odometry


3.Inspecting the node's graph online with rqt_graph

$rosrun rqt_graph rqt_graph

Note: 通过菜单栏的 group 选项选择显示的内容;

    When running nodes in different machines, rqt_graph shows its great high-level debugging capabilities since it shows whether the nodes see each other from one machine to the other, enumerating the connections.


4.Plotting scalar data

4.1Creating a time series plot with rqt_plot

    To specify topic(s) to plot, you need full path name of the topics published.

$ rostopic list

/RosAria/battery_recharge_state
/RosAria/battery_state_of_charge
/RosAria/battery_voltage
/RosAria/bumper_state
/RosAria/cmd_vel
/RosAria/motors_state
/RosAria/parameter_descriptions
/RosAria/parameter_updates
/RosAria/pose
/RosAria/sonar
/RosAria/sonar_pointcloud2
/rosout
/rosout_agg
/tf

$rostopic type /RosAria/cmd_vel
geometry_msgs/Twist

   To plot a message, we must know its format; use rosmsg show  if you do not know it.

$ rosmsg show geometry_msgs/Twist
geometry_msgs/Vector3 linear
float64 x
float64 y
float64 z
geometry_msgs/Vector3 angular
float64 x
float64 y
float64 z 

rosrunrqt_plot rqt_plot /RosAria/cmd_vel/linear/x:y:z

robot@robot:~/catkin_ws$rostopic type /RosAria/pose

nav_msgs/Odometry

robot@robot:~/catkin_ws$rosmsg show nav_msgs/Odometry

std_msgs/Headerheader

  uint32seq

  timestamp

  stringframe_id

  stringchild_frame_id

geometry_msgs/Pose With Covariance pose
 
  geometry_msgs/Pose pose

    geometry_msgs/Point position

      float64 x

      float64 y

      float64 z

    geometry_msgs/Quaternion orientation

      float64x

      float64y

      float64z

      float64w

  float64[36] covariance

  geometry_msgs/Twist With Covariance twist

    geometry_msgs/Twist twist

     geometry_msgs/Vector3 linear

       float64x

       float64y

       float64z

      geometry_msgs/Vector3 angular

       float64x

       float64y

       float64z

     float64[36] covariance

注意格式!!

robot@robot:~/catkin_ws$rosrun rqt_plot rqt_plot /RosAria/pose/pose/pose/position/x:y:z


5.Saving and playing back data

5.1 What is a bag file?

    A bag file is a container of messages sent by topics that were recorded during a session using a robot or nodes. In brief, they are the logging files for the messages transferred during the execution of our system, and they allow us to play back everything

5.2 Recording data in a bag file with rosbag

    First,we can record all the topics with the following command:

$rosbag record -a

    Otherwise,we can record only specific topics.

$rosbag record /RosAria/pose

    By default, when we run the above command, the rosbag program subscribes to  the node and starts recording the message in a bag file in the current directory with  the data as the name.

    You can see more options with rosbag help record

5.3 Playing back a bag file 

    Now that we have a bag file recorded, we can use it to play back all the messages of the topics inside it; note that we need roscore running and usually nothing else.

    Then,we will move to the folder with the bag file we want to play

$rosbag play 2017-07-30-20-44-00.bag

5.4 Inspecting all the topics and messages in a bag file

    There are two main ways to see what we have inside a bag file. The first one is very simple.We just type、

 rosbag info ,

    The second way to inspect a bag file is extremely powerful. It is a GUI named rqt_bag that also allows playing back the files, viewing the images (if any),plotting scalar data, and also the RAW structure of the messages; it is there placement of rxbag. We only have to pass the name of the bag file,

rqt_bag 2017-07-30-20-44-00.bag


6.Using the rqt_gui and rqt plugins

    In ROS hydro, there are even more standalone plugins, such as a shell(rqt_shell), at opic publisher (rqt_publisher), a message type viewer (rqt_msg), and much more(the most important ones have been covered in this chapter). Even rqt_viz is a plugin, which replaces rviz, and can also be integrated into the new rqt_gui interface.We can run this GUI and add and arrange several plugins manually on the window,as it has been seen in several examples in this chapter:

$rosrun rqt_gui rqt_gui

   注意菜单栏的使用!!


你可能感兴趣的:(ROS)