-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_waypoints.py
More file actions
78 lines (73 loc) · 2.91 KB
/
python_waypoints.py
File metadata and controls
78 lines (73 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from copy import deepcopy
from geometry_msgs.msg import PoseStamped
from nav2_simple_commander.robot_navigator import BasicNavigator, TaskResult
import rclpy
import math
"""
Basic security route patrol demo. In this demonstration, the expectation
is that there are security cameras mounted on the robots recording or being
watched live by security staff.
From Ros2_learners repo
This version uses a text file to input waypoints. Robot heading is in degrees
North = 0, E = 90, S = 180, W = 270, matching nautical.
Simply put in x and y positions and rotation, separated by one space, on each line.
Line 38 has data file name in home folder. Position data must be float. Rotation is integer.
Do not end data file with blank line
"""
def lineToData(line):
"""Converts a raw line list into an
appropriate data format."""
return (float(line[0]), float(line[1]), int(line[2]))
def readData(fileName):
"""Generic data reading function:
reads lines in a text file and
splits them into lists."""
data = []
with open(fileName) as f:
for line in f.readlines():
data.append(lineToData(line.split()))
return data
#pairs = readData('upper_playground_data.txt')
#for x, y in pairs:
# print("pose position.", x)
# print("Pose position.", y)
def main():
rclpy.init()
navigator = BasicNavigator()
triples = readData('security_route_data.txt')
# Security route, read in from text file
# Set robot's initial pose in Rviz
# Wait for navigation to fully activate
navigator.waitUntilNav2Active()
# Do security route once
while rclpy.ok():
# Send our route
poses = []
pose = PoseStamped()
pose.header.frame_id = 'map'
pose.header.stamp = navigator.get_clock().now().to_msg()
for x, y, rotation in triples:
rotrot = 360 - rotation
# print(pairs)
pose.pose.position.x = x
pose.pose.position.y = y
# Convert Z rotation to quaternion
pose.pose.orientation.z = math.sin(math.radians(rotrot) / 2)
pose.pose.orientation.w = math.cos(math.radians(rotrot) / 2)
poses.append(deepcopy(pose))
# print(pose)
nav_start = navigator.get_clock().now()
navigator.followWaypoints(poses)
# Do something during our route (e.x. AI detection on camera images for anomalies)
# Simply print number of waypoint for the demonstation
i = 0
while not navigator.isTaskComplete():
i += 1
feedback = navigator.getFeedback()
if feedback and i % 5 == 0:
print('Executing current waypoint: : {0}/{1: <5}'.format(
str(feedback.current_waypoint + 1), str(len(poses))), end='\r')
# my_file.close()
exit(0)
if __name__ == '__main__':
main()