Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
demo_security.py
1 #! /usr/bin/env python3
2 # Copyright 2021 Samsung Research America
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from copy import deepcopy
17 
18 from geometry_msgs.msg import PoseStamped
19 from nav2_simple_commander.robot_navigator import BasicNavigator, TaskResult
20 
21 import rclpy
22 from rclpy.duration import Duration
23 
24 
25 """
26 Basic security route patrol demo. In this demonstration, the expectation
27 is that there are security cameras mounted on the robots recording or being
28 watched live by security staff.
29 """
30 
31 
32 def main():
33  rclpy.init()
34 
35  navigator = BasicNavigator()
36 
37  # Security route, probably read in from a file for a real application
38  # from either a map or drive and repeat.
39  security_route = [
40  [10.15, -0.77],
41  [17.86, -0.77],
42  [21.58, -3.5],
43  [19.4, -6.4],
44  [-6.0, 5.0],
45  ]
46 
47  # Set our demo's initial pose
48  initial_pose = PoseStamped()
49  initial_pose.header.frame_id = 'map'
50  initial_pose.header.stamp = navigator.get_clock().now().to_msg()
51  initial_pose.pose.position.x = 0.0
52  initial_pose.pose.position.y = 0.0
53  initial_pose.pose.orientation.z = 0.0
54  initial_pose.pose.orientation.w = 1.0
55  navigator.setInitialPose(initial_pose)
56 
57  # Wait for navigation to fully activate
58  navigator.waitUntilNav2Active()
59 
60  # Do security route until dead
61  while rclpy.ok():
62  # Send our route
63  route_poses = []
64  pose = PoseStamped()
65  pose.header.frame_id = 'map'
66  pose.header.stamp = navigator.get_clock().now().to_msg()
67  pose.pose.orientation.w = 1.0
68  for pt in security_route:
69  pose.pose.position.x = pt[0]
70  pose.pose.position.y = pt[1]
71  route_poses.append(deepcopy(pose))
72  navigator.goThroughPoses(route_poses)
73 
74  # Do something during our route (e.x. AI detection on camera images for anomalies)
75  # Simply print ETA for the demonstation
76  i = 0
77  while not navigator.isTaskComplete():
78  i += 1
79  feedback = navigator.getFeedback()
80  if feedback and i % 5 == 0:
81  print(
82  'Estimated time to complete current route: '
83  + '{0:.0f}'.format(
84  Duration.from_msg(feedback.estimated_time_remaining).nanoseconds
85  / 1e9
86  )
87  + ' seconds.'
88  )
89 
90  # Some failure mode, must stop since the robot is clearly stuck
91  if Duration.from_msg(feedback.navigation_time) > Duration(
92  seconds=180.0
93  ):
94  print('Navigation has exceeded timeout of 180s, canceling request.')
95  navigator.cancelTask()
96 
97  # If at end of route, reverse the route to restart
98  security_route.reverse()
99 
100  result = navigator.getResult()
101  if result == TaskResult.SUCCEEDED:
102  print('Route complete! Restarting...')
103  elif result == TaskResult.CANCELED:
104  print('Security route was canceled, exiting.')
105  exit(1)
106  elif result == TaskResult.FAILED:
107  print('Security route failed! Restarting from other side...')
108 
109  exit(0)
110 
111 
112 if __name__ == '__main__':
113  main()