-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmission_state_machine.py
More file actions
executable file
·86 lines (67 loc) · 3.1 KB
/
mission_state_machine.py
File metadata and controls
executable file
·86 lines (67 loc) · 3.1 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
79
80
81
82
83
84
85
86
#!/usr/bin/env python
from multiprocessing.connection import Listener
import rospy
import math
import tf2_ros
import tf2_geometry_msgs
import geometry_msgs.msg
import tf
from gazebo_msgs.msg import ModelStates
from nav_msgs.msg import Odometry
from std_msgs.msg import Float32
from geometry_msgs.msg import Pose
import tf2_geometry_msgs
#class that has robots controlle
class MissionStateMachine:
#the insitaiser
def __init__(self):
# Creates a node with subscriber
rospy.init_node('mission_state_machine')
self.x = 0.
self.y = 0.
self.z = 0.
self.threshold = 0.2
self.pose_subscriber = rospy.Subscriber('/mavros/global_position/local', Odometry, self.position_callback)
self.goal_publisher = rospy.Publisher('goal', Pose, queue_size=10)
self.tf_buffer = tf2_ros.Buffer()
self.listener = tf2_ros.TransformListener(self.tf_buffer)
self.waypoints = [[2, 2, 0], [2, -2, 0], [-2, -2, 0], [-2, 2, 0], [2, 2, 0], [0, 0, 2], [0, 0, 0]]
self.curr_waypoint_idx = 0
def position_callback(self, data):
self.x = data.pose.pose.position.x
self.y = data.pose.pose.position.y
self.z = data.pose.pose.position.z
mission_pose = Pose()
mission_pose.position.x = self.waypoints[self.curr_waypoint_idx][0]
mission_pose.position.y = self.waypoints[self.curr_waypoint_idx][1]
mission_pose.position.z = self.waypoints[self.curr_waypoint_idx][2]
# only convert first 4 points, last two are already in map fram
if(self.curr_waypoint_idx < len(self.waypoints)-2):
# converting coordinates from inspection fram to map frame
mission_pose = self.transform_pose(mission_pose, "inspection", "map")
print(mission_pose)
self.goal_publisher.publish(mission_pose)
#if x y z values are within
if((abs(self.x - mission_pose.position.x) < self.threshold) and
(abs(self.y - mission_pose.position.y) < self.threshold) and
(abs(self.z - mission_pose.position.z) < self.threshold) and
(self.curr_waypoint_idx < len(self.waypoints))):
self.curr_waypoint_idx += 1
# reference: https://answers.ros.org/question/323075/transform-the-coordinate-frame-of-a-pose-from-one-fixed-frame-to-another/
def transform_pose(self, input_pose, from_frame, to_frame):
pose_stamped = tf2_geometry_msgs.PoseStamped()
pose_stamped.pose = input_pose
pose_stamped.header.frame_id = from_frame
pose_stamped.header.stamp = rospy.Time(0)
try:
# ** It is important to wait for the listener to start listening. Hence the rospy.Duration(1)
output_pose_stamped = self.tf_buffer.transform(pose_stamped, to_frame, rospy.Duration(1))
return output_pose_stamped.pose
except (tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException):
raise
if __name__ == '__main__':
try:
MissionStateMachine()
rospy.spin()
except rospy.ROSInterruptException:
print ("error!")