|
| 1 | +import time |
| 2 | +import socket |
| 3 | +from threading import Thread |
| 4 | + |
| 5 | +from sympy import Point |
| 6 | +import rclpy |
| 7 | +from rclpy.duration import Duration |
| 8 | +from rclpy.node import Node |
| 9 | +from geometry_msgs.msg import PoseStamped |
| 10 | +import numpy as np |
| 11 | +from std_msgs.msg import String, Header |
| 12 | +from nav_msgs.msg import Odometry, Path |
| 13 | +from nav2_msgs.action import NavigateThroughPoses |
| 14 | +from nav2_simple_commander.robot_navigator import BasicNavigator, TaskResult |
| 15 | +import rove_navigation.behavior.mule_constants as consts |
| 16 | + |
| 17 | +# Node to handle when the robot loses connection with internet, stores waypoints to fall back to to try to get back connection |
| 18 | +class MuleBehavior(Node): |
| 19 | + def __init__(self): |
| 20 | + super().__init__('mule_behavior') |
| 21 | + self.subscription_person_position = self.create_subscription( |
| 22 | + String, |
| 23 | + '/tracking/state', |
| 24 | + self.state_listener, |
| 25 | + 10) |
| 26 | + self.pub_mule_state = self.create_publisher( |
| 27 | + String, |
| 28 | + '/mule_state', |
| 29 | + 10) |
| 30 | + self.odom_subscriber = self.create_subscription( |
| 31 | + Odometry, |
| 32 | + '/odometry/local', |
| 33 | + self.odom_listener, |
| 34 | + 10) |
| 35 | + |
| 36 | + # nav variables |
| 37 | + self.startPoint: PoseStamped |
| 38 | + self.endpoint: PoseStamped |
| 39 | + self.goal_point: PoseStamped |
| 40 | + self.currentPos: PoseStamped |
| 41 | + self.navigator = BasicNavigator() |
| 42 | + |
| 43 | + |
| 44 | + self.state = '' |
| 45 | + self.change_state(consts.START) |
| 46 | + self.navigate_to_goal_point() # returns only when goal reached/cancelled/failed |
| 47 | + |
| 48 | + self.get_logger().info('Initiating MuleBehavior node') |
| 49 | + |
| 50 | + # Thread to execute goal pose posting for rewind |
| 51 | + self.connection_thread = Thread(target=self.rewind_behavior) |
| 52 | + self.connection_thread.start() |
| 53 | + |
| 54 | + def change_state(self, state): |
| 55 | + if(state != self.state): |
| 56 | + self.state = state |
| 57 | + self.pub_mule_state.publish(String(data=state)) |
| 58 | + |
| 59 | + # Subscribed to /odometry/local |
| 60 | + def odom_listener(self, msg:Odometry): |
| 61 | + self.currentPos = PoseStamped() |
| 62 | + self.currentPos.header.frame_id = msg.header.frame_id |
| 63 | + self.currentPos.header.stamp = msg.header.stamp |
| 64 | + self.currentPos.pose = msg.pose.pose |
| 65 | + |
| 66 | + # Subscribed to /tracking/state, handles state/figure changes |
| 67 | + def state_listener(self, msg:String): |
| 68 | + if self.state != msg.data: |
| 69 | + returnState = msg.data |
| 70 | + match msg.data: |
| 71 | + case consts.FIGURE_TPOSE: |
| 72 | + if self.state == consts.START: |
| 73 | + self.startPoint = self.currentPos |
| 74 | + self.navigator.setInitialPose(self.startPoint) |
| 75 | + self.get_logger().info(f'Mule behavior engaged, point A noted at coordonates {self.startPoint.pose.position}') |
| 76 | + returnState = consts.FOLLOWING |
| 77 | + |
| 78 | + case consts.FIGURE_IDLE: |
| 79 | + if self.state != consts.REWIND and self.state != consts.PROCESSED_REWIND: |
| 80 | + returnState=self.state |
| 81 | + |
| 82 | + case consts.FIGURE_UP: |
| 83 | + if self.state != consts.REWIND and self.state != consts.PROCESSED_REWIND: |
| 84 | + if self.state == consts.PAUSE: returnState = consts.FOLLOWING |
| 85 | + else: returnState = consts.PAUSE |
| 86 | + |
| 87 | + case consts.FIGURE_BUCKET: |
| 88 | + if self.state != consts.REWIND and self.state != consts.PROCESSED_REWIND: |
| 89 | + self.endpoint = self.currentPos |
| 90 | + returnState = consts.REWIND |
| 91 | + |
| 92 | + case _: |
| 93 | + self.get_logger().error(f'Unimplemented pose returned: {msg.data}') |
| 94 | + |
| 95 | + self.change_state(returnState) |
| 96 | + |
| 97 | + # runs on a thread continuously |
| 98 | + def rewind_behavior(self): |
| 99 | + while self.state != consts.END: |
| 100 | + if self.state == consts.REWIND: |
| 101 | + self.get_logger().info(f'Rewind phase engaged, point B noted at coordonates {self.endpoint.pose.position}') |
| 102 | + self.change_state(consts.PROCESSED_REWIND) |
| 103 | + self.goal_point = self.startPoint |
| 104 | + while self.state == consts.PROCESSED_REWIND: |
| 105 | + self.navigate_to_goal_point() # returns only when goal reached/cancelled/failed |
| 106 | + result = self.navigator.getResult() |
| 107 | + goal = 'A' |
| 108 | + nextgoal = 'B' |
| 109 | + if self.goal_point == self.startPoint: |
| 110 | + goal = 'B' |
| 111 | + nextgoal = 'A' |
| 112 | + if result == TaskResult.SUCCEEDED: |
| 113 | + self.get_logger().info(f'Goal {goal} reached! Initiating nav towards point {nextgoal}') |
| 114 | + self.goal_point = self.startPoint if self.goal_point == self.endpoint else self.endpoint |
| 115 | + elif result == TaskResult.FAILED: |
| 116 | + self.get_logger().error(f'Failed to reach {goal}! Initiating nav towards point {nextgoal}') |
| 117 | + self.goal_point = self.startPoint if self.goal_point == self.endpoint else self.endpoint |
| 118 | + elif result == TaskResult.CANCELED: |
| 119 | + self.get_logger().error(f'Rewinding to point {goal} was cancelled! Initiating nav towards point {nextgoal}') |
| 120 | + else : |
| 121 | + self.get_logger().warn(f'nav2 navigator task returned with unknown TaskResult') |
| 122 | + |
| 123 | + # Tells navigator to go to current goal Point and processes feedback |
| 124 | + def navigate_to_goal_point(self): |
| 125 | + i = 0 |
| 126 | + self.navigator.goToPose(self.goal_point) |
| 127 | + while not self.navigator.isTaskComplete(): |
| 128 | + if self.state != consts.PROCESSED_REWIND: |
| 129 | + self.navigator.cancelTask() |
| 130 | + |
| 131 | + feedback:NavigateThroughPoses.Feedback = self.navigator.getFeedback() |
| 132 | + self.poses_remaining = feedback.number_of_poses_remaining |
| 133 | + if feedback and i % 5 == 0: |
| 134 | + # navigation timeout |
| 135 | + if Duration.from_msg(feedback.navigation_time) > 1.5 * Duration(seconds=feedback.estimated_time_remaining): |
| 136 | + self.navigator.cancelTask() |
| 137 | + i+=1 |
| 138 | + |
| 139 | +def main(args=None): |
| 140 | + rclpy.init(args=args) |
| 141 | + node = MuleBehavior() |
| 142 | + rclpy.spin(node) |
| 143 | + node.destroy_node() |
| 144 | + rclpy.shutdown() |
| 145 | + |
| 146 | +if __name__ == '__main__': |
| 147 | + main() |
0 commit comments