Guest User

Untitled

a guest
Nov 13th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import rospy
  3. from std_msgs.msg import Int32
  4. from geometry_msgs.msg import Twist
  5. from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
  6. from pi_trees_ros.pi_trees_ros import *
  7.  
  8. class Patrol_tree:
  9. def __init__(self):
  10. rospy.init_node("patrol_tree")
  11. rospy.on_shutdown(self.shutdown)
  12. self.nodo_raiz = Sequence("ARBOL")
  13.  
  14. nodo_healthy = Selector("STAY_HEALTHY")
  15. nodo_battery_OK = MonitorTask("BATTERY_OK?", "battery_level", Int32, self.checkBattery )
  16.  
  17. nodo_recharge = SimpleActionTask("RECHARGE_AT_HOME", "move_base", MoveBaseAction,
  18. self.createGoal(2,2), reset_after=True)
  19. nodo_healthy.add_child(nodo_battery_OK)
  20. nodo_healthy.add_child(nodo_recharge)
  21.  
  22. nodo_patrol = Sequence("PATROL")
  23. nodo_wp1 = SimpleActionTask("WP_1", "move_base", MoveBaseAction, self.createGoal(2,7))
  24. nodo_wp2 = SimpleActionTask("WP_2", "move_base", MoveBaseAction, self.createGoal(9,2))
  25. nodo_patrol.add_child(nodo_wp1)
  26. nodo_patrol.add_child(nodo_wp2)
  27.  
  28. self.nodo_raiz.add_child(nodo_healthy)
  29. self.nodo_raiz.add_child(nodo_patrol)
  30.  
  31. print_tree(self.nodo_raiz)
  32.  
  33. def checkBattery(self, msg):
  34. if msg.data is None:
  35. return TaskStatus.RUNNING
  36. else:
  37. if msg.data < 30:
  38. rospy.loginfo("BATERIA BAJA!: " + str(int(msg.data)))
  39. return TaskStatus.FAILURE
  40. else:
  41. rospy.loginfo("BATERIA OK!: " + str(int(msg.data)))
  42. return TaskStatus.SUCCESS
  43.  
  44. def createGoal(self, pos_x, pos_y):
  45. goal = MoveBaseGoal()
  46. goal.target_pose.header.frame_id = 'map'
  47. goal.target_pose.header.stamp = rospy.Time.now()
  48. goal.target_pose.pose.position.x = pos_x
  49. goal.target_pose.pose.position.y = pos_y
  50. goal.target_pose.pose.orientation.w = 1
  51. return goal
  52.  
  53. def shutdown(self):
  54. rospy.loginfo("Stopping the robot...")
  55. rospy.sleep(1)
  56.  
  57. def start(self):
  58. while not rospy.is_shutdown():
  59. self.nodo_raiz.run()
  60. rospy.sleep(0.1)
  61.  
  62.  
  63. if __name__ == '__main__':
  64. tree = Patrol_tree()
  65. tree.start()
Add Comment
Please, Sign In to add comment