2018-09-17 19:41:31 +02:00
|
|
|
#!/usr/bin/env python
|
2018-09-18 01:46:10 +02:00
|
|
|
import rospy
|
|
|
|
import rospkg
|
|
|
|
|
|
|
|
from std_msgs.msg import Bool
|
2018-09-28 14:34:55 +02:00
|
|
|
from rosaria_msgs.msg import RobotInfoMsg
|
|
|
|
from rosaria_msgs.msg import RestrictionsMsg
|
2018-09-18 01:46:10 +02:00
|
|
|
|
|
|
|
from robot_info import RobotInfo
|
|
|
|
from restrictions import Restrictions
|
|
|
|
|
|
|
|
from enums.clutch_state import ClutchState as CS
|
|
|
|
from enums.stop_state import StopState as SS
|
2018-09-17 18:29:00 +02:00
|
|
|
|
|
|
|
class ROSWrapper:
|
|
|
|
|
2018-09-17 21:39:35 +02:00
|
|
|
def __init__(self):
|
2018-09-18 01:46:10 +02:00
|
|
|
self.robots_list_update_callback = None
|
|
|
|
self.selected_robot_info_update_callback = None
|
|
|
|
self.master_stop_update_callback = None
|
|
|
|
self.restrictions_update_callback = None
|
|
|
|
|
|
|
|
self.get_user_stop_state = None
|
|
|
|
self.get_clutch_state = None
|
|
|
|
|
|
|
|
self.robot_info_subscriber = None
|
|
|
|
self.master_stop_subscriber = None
|
|
|
|
self.restrictions_subscriber = None
|
|
|
|
|
|
|
|
self.user_stop_publisher = None
|
|
|
|
self.clutch_publisher = None
|
|
|
|
|
|
|
|
self.periodic_timer = None
|
2018-09-18 02:29:14 +02:00
|
|
|
self.robots_list_timer = None
|
2018-09-18 14:56:21 +02:00
|
|
|
self.connection_timer = None
|
2018-09-25 13:31:56 +02:00
|
|
|
self.master_connection_timer = None
|
2018-09-18 14:56:21 +02:00
|
|
|
|
2018-09-26 17:25:38 +02:00
|
|
|
# def setup_node(self):
|
|
|
|
# rospy.init_node('safety_user_plugin', anonymous=True)
|
2018-09-18 01:46:10 +02:00
|
|
|
|
|
|
|
def setup_subscribers_and_publishers(self,robot_id):
|
|
|
|
robot_name = 'PIONIER' + str(robot_id)
|
|
|
|
|
2018-09-28 17:59:45 +02:00
|
|
|
self.robot_info_subscriber = rospy.Subscriber('/{0}/RosAria/robot_info'.format(robot_name), RobotInfoMsg, self.handle_selected_robot_info_update)
|
|
|
|
self.master_stop_subscriber = rospy.Subscriber('/PIONIER/master_stop', Bool, self.handle_master_stop_update)
|
|
|
|
self.restrictions_subscriber = rospy.Subscriber('/PIONIER/restrictions', RestrictionsMsg, self.handle_restrictions_update)
|
2018-09-18 01:46:10 +02:00
|
|
|
|
2018-09-28 17:59:45 +02:00
|
|
|
self.user_stop_publisher = rospy.Publisher('/{0}/RosAria/user_stop'.format(robot_name),Bool,queue_size = 1)
|
|
|
|
self.clutch_publisher = rospy.Publisher('/{0}/RosAria/clutch'.format(robot_name),Bool,queue_size = 1)
|
2018-09-18 01:46:10 +02:00
|
|
|
|
2018-09-27 12:43:52 +02:00
|
|
|
if self.periodic_timer != None:
|
|
|
|
self.shutdown_timer(self.periodic_timer)
|
2018-09-18 01:46:10 +02:00
|
|
|
self.periodic_timer = rospy.Timer(rospy.Duration(0.2),self.publish_periodic_update)
|
2018-09-27 12:43:52 +02:00
|
|
|
|
|
|
|
if self.connection_timer != None:
|
|
|
|
self.shutdown_timer(self.connection_timer)
|
2018-09-18 14:56:21 +02:00
|
|
|
self.connection_timer = rospy.Timer(rospy.Duration(1.5),self.handle_robot_connection_lost)
|
2018-09-27 12:43:52 +02:00
|
|
|
|
|
|
|
if self.master_connection_timer != None:
|
|
|
|
self.shutdown_timer(self.master_connection_timer)
|
2018-09-25 13:31:56 +02:00
|
|
|
self.master_connection_timer = rospy.Timer(rospy.Duration(1.5),self.handle_master_connection_lost)
|
2018-09-18 01:46:10 +02:00
|
|
|
|
|
|
|
def unsubscribe(self,subscriber):
|
|
|
|
if subscriber != None:
|
2018-09-18 19:09:59 +02:00
|
|
|
subscriber.unregister()
|
2018-09-18 01:46:10 +02:00
|
|
|
|
|
|
|
def unregister_publisher(self,publisher):
|
|
|
|
if publisher != None:
|
|
|
|
publisher.unregister()
|
|
|
|
|
|
|
|
def shutdown_timer(self,timer):
|
|
|
|
if timer != None:
|
|
|
|
timer.shutdown()
|
|
|
|
|
|
|
|
def publish_periodic_update(self,event):
|
|
|
|
stop_state = self.get_user_stop_state()
|
2018-09-28 17:59:45 +02:00
|
|
|
# clutch_state = self.get_clutch_state()
|
2018-09-18 01:46:10 +02:00
|
|
|
|
|
|
|
if stop_state == SS.RUNNING:
|
|
|
|
self.user_started()
|
|
|
|
elif stop_state == SS.STOPPED:
|
|
|
|
self.user_stopped()
|
|
|
|
else:
|
2018-09-19 21:20:04 +02:00
|
|
|
raise ValueError('stop_state UNKNOWN when attempting to publish periodic update')
|
2018-09-18 01:46:10 +02:00
|
|
|
|
2018-09-28 17:59:45 +02:00
|
|
|
# if clutch_state == CS.ENGAGED:
|
|
|
|
# self.engage_clutch()
|
|
|
|
# elif clutch_state == CS.DISENGAGED:
|
|
|
|
# self.disengage_clutch()
|
|
|
|
# else:
|
|
|
|
# raise ValueError('clutch_state UNKNOWN when attempting to publish periodic update')
|
2018-09-18 01:46:10 +02:00
|
|
|
|
2018-09-18 14:56:21 +02:00
|
|
|
def handle_robot_connection_lost(self,event):
|
2018-09-18 18:11:39 +02:00
|
|
|
self.shutdown_timer(self.connection_timer)
|
2018-09-18 14:56:21 +02:00
|
|
|
self.robot_connection_lost_callback()
|
|
|
|
|
2018-09-25 13:31:56 +02:00
|
|
|
def handle_master_connection_lost(self,event):
|
|
|
|
self.shutdown_timer(self.master_connection_timer)
|
|
|
|
self.master_connection_lost_callback()
|
|
|
|
|
2018-09-18 01:46:10 +02:00
|
|
|
def cancel_subscribers_and_publishers(self):
|
|
|
|
self.shutdown_timer(self.periodic_timer)
|
|
|
|
self.unsubscribe(self.robot_info_subscriber)
|
|
|
|
self.unsubscribe(self.master_stop_subscriber)
|
|
|
|
self.unsubscribe(self.restrictions_subscriber)
|
|
|
|
self.unregister_publisher(self.user_stop_publisher)
|
|
|
|
self.unregister_publisher(self.clutch_publisher)
|
|
|
|
|
|
|
|
self.periodic_timer = None
|
|
|
|
self.robot_info_subscriber = None
|
|
|
|
self.master_stop_subscriber = None
|
|
|
|
self.restrictions_subscriber = None
|
|
|
|
self.user_stop_publisher = None
|
|
|
|
self.clutch_publisher = None
|
|
|
|
|
2018-09-18 14:56:21 +02:00
|
|
|
def restart_connection_timer(self):
|
|
|
|
if self.connection_timer != None:
|
|
|
|
self.connection_timer.shutdown()
|
|
|
|
self.connection_timer = rospy.Timer(rospy.Duration(1.5),self.handle_robot_connection_lost)
|
|
|
|
else:
|
2018-09-19 21:20:04 +02:00
|
|
|
raise RuntimeError('Attempting to restart connection_timer when it is not initialized')
|
2018-09-18 14:56:21 +02:00
|
|
|
|
2018-09-25 13:31:56 +02:00
|
|
|
def restart_master_connection_timer(self):
|
|
|
|
if self.master_connection_timer != None:
|
|
|
|
self.master_connection_timer.shutdown()
|
|
|
|
self.master_connection_timer = rospy.Timer(rospy.Duration(1.5),self.handle_master_connection_lost)
|
|
|
|
else:
|
|
|
|
raise RuntimeError('Attempting to restart master_connection_timer when it is not initialized')
|
|
|
|
|
2018-09-18 01:46:10 +02:00
|
|
|
def unregister_node(self):
|
|
|
|
self.cancel_subscribers_and_publishers()
|
|
|
|
rospy.signal_shutdown('Closing safety user plugin')
|
2018-09-17 19:41:31 +02:00
|
|
|
|
2018-09-17 21:39:35 +02:00
|
|
|
def select_robot(self,robot_id):
|
2018-09-18 01:46:10 +02:00
|
|
|
self.setup_subscribers_and_publishers(robot_id)
|
|
|
|
|
|
|
|
def setup_get_user_stop_state_function(self,function):
|
|
|
|
self.get_user_stop_state = function
|
|
|
|
|
|
|
|
def setup_get_clutch_state_function(self,function):
|
|
|
|
self.get_clutch_state = function
|
|
|
|
|
|
|
|
# ROSWrapper Callbacks
|
2018-09-18 02:29:14 +02:00
|
|
|
def get_robots_list(self,event):
|
2018-09-18 01:46:10 +02:00
|
|
|
robots_id_list = []
|
|
|
|
|
|
|
|
published_topics_list = rospy.get_published_topics(namespace='/')
|
|
|
|
published_topics = []
|
|
|
|
|
|
|
|
for list_ in published_topics_list:
|
|
|
|
published_topics.append(list_[0])
|
|
|
|
|
|
|
|
for topic in published_topics:
|
2018-09-18 02:29:14 +02:00
|
|
|
if topic.find('RosAria') ==-1 or topic.find('robot_info') == -1:
|
|
|
|
pass
|
|
|
|
else:
|
2018-09-18 01:46:10 +02:00
|
|
|
robot_number = topic.split('/')
|
2018-09-28 17:59:45 +02:00
|
|
|
robot_number = robot_number[1]
|
2018-09-18 01:46:10 +02:00
|
|
|
robot_number = robot_number[7:]
|
|
|
|
if len(robot_number) > 0:
|
|
|
|
robot_number = int(robot_number)
|
|
|
|
robots_id_list.append(robot_number)
|
|
|
|
|
|
|
|
self.robots_list_update_callback(robots_id_list)
|
|
|
|
|
2018-09-18 19:09:59 +02:00
|
|
|
|
|
|
|
def release_robot(self):
|
|
|
|
self.unsubscribe(self.robot_info_subscriber)
|
|
|
|
self.shutdown_timer(self.periodic_timer)
|
|
|
|
self.shutdown_timer(self.connection_timer)
|
2018-09-27 12:43:52 +02:00
|
|
|
self.shutdown_timer(self.master_connection_timer)
|
2018-09-18 19:09:59 +02:00
|
|
|
|
2018-09-18 01:46:10 +02:00
|
|
|
def handle_selected_robot_info_update(self,msg):
|
2018-09-18 14:56:21 +02:00
|
|
|
# Restarting connection timer to avoid raising robot_connection_lost_callback
|
|
|
|
self.restart_connection_timer()
|
|
|
|
|
2018-09-18 21:47:49 +02:00
|
|
|
_robot_info = RobotInfo(0)
|
|
|
|
_robot_info.update_robot_info(msg)
|
2018-09-18 01:46:10 +02:00
|
|
|
self.selected_robot_info_update_callback(_robot_info)
|
|
|
|
|
|
|
|
def handle_master_stop_update(self,msg):
|
2018-09-25 13:31:56 +02:00
|
|
|
# Restarting master connection timer to avoid raising master_connection_lost_callback
|
|
|
|
self.restart_master_connection_timer()
|
|
|
|
|
2018-09-18 01:46:10 +02:00
|
|
|
master_stop_state = SS.UNKNOWN
|
|
|
|
if msg.data == True:
|
|
|
|
master_stop_state = SS.RUNNING
|
|
|
|
else:
|
|
|
|
master_stop_state = SS.STOPPED
|
|
|
|
|
|
|
|
self.master_stop_update_callback(master_stop_state)
|
|
|
|
|
|
|
|
def handle_restrictions_update(self,msg):
|
|
|
|
restrictions = Restrictions(msg)
|
2018-09-19 21:48:09 +02:00
|
|
|
self.restrictions_update_callback(restrictions)
|
2018-09-18 01:46:10 +02:00
|
|
|
|
|
|
|
# UserPlugin Callbacks
|
2018-09-17 21:39:35 +02:00
|
|
|
def set_robots_list_update_callback(self,callback_function):
|
|
|
|
self.robots_list_update_callback = callback_function
|
2018-09-18 02:29:14 +02:00
|
|
|
self.robots_list_timer = rospy.Timer(rospy.Duration(0.5),self.get_robots_list)
|
2018-09-17 21:39:35 +02:00
|
|
|
|
2018-09-18 01:46:10 +02:00
|
|
|
def set_selected_robot_info_update_callback(self,callback_function):
|
|
|
|
self.selected_robot_info_update_callback = callback_function
|
|
|
|
|
|
|
|
def set_master_stop_update_callback(self,callback_function):
|
|
|
|
self.master_stop_update_callback = callback_function
|
|
|
|
|
|
|
|
def set_restrictions_update_callback(self,callback_function):
|
|
|
|
self.restrictions_update_callback = callback_function
|
|
|
|
|
2018-09-18 14:56:21 +02:00
|
|
|
def set_robot_connection_lost_callback(self,callback_function):
|
|
|
|
self.robot_connection_lost_callback = callback_function
|
2018-09-17 21:39:35 +02:00
|
|
|
|
2018-09-25 13:31:56 +02:00
|
|
|
def set_master_connection_lost_callback(self,callback_function):
|
|
|
|
self.master_connection_lost_callback = callback_function
|
|
|
|
|
2018-09-17 21:39:35 +02:00
|
|
|
def engage_clutch(self):
|
2018-09-18 01:46:10 +02:00
|
|
|
msg = Bool()
|
|
|
|
msg.data = True
|
2018-09-18 14:56:21 +02:00
|
|
|
self.clutch_publisher.publish(msg)
|
2018-09-17 21:39:35 +02:00
|
|
|
|
|
|
|
def disengage_clutch(self):
|
2018-09-18 01:46:10 +02:00
|
|
|
msg = Bool()
|
|
|
|
msg.data = False
|
2018-09-18 14:56:21 +02:00
|
|
|
self.clutch_publisher.publish(msg)
|
2018-09-18 01:46:10 +02:00
|
|
|
|
2018-09-18 14:56:21 +02:00
|
|
|
def user_started(self):
|
2018-09-18 01:46:10 +02:00
|
|
|
msg = Bool()
|
|
|
|
msg.data = True
|
2018-09-18 14:56:21 +02:00
|
|
|
self.user_stop_publisher.publish(msg)
|
2018-09-17 21:39:35 +02:00
|
|
|
|
2018-09-18 14:56:21 +02:00
|
|
|
def user_stopped(self):
|
2018-09-18 01:46:10 +02:00
|
|
|
msg = Bool()
|
|
|
|
msg.data = False
|
2018-09-18 14:56:21 +02:00
|
|
|
self.user_stop_publisher.publish(msg)
|
2018-09-17 21:39:35 +02:00
|
|
|
|