00001
00002
00003
00004
00005 import roslib; roslib.load_manifest('clearpath_base')
00006 import rospy
00007
00008 from clearpath_base.msg import ClearpathRobot
00009 from geometry_msgs.msg import Twist
00010 from std_msgs.msg import String, Float32
00011
00012
00013 from clearpath.horizon import Horizon
00014 from clearpath.horizon import transports
00015 from clearpath import utils
00016 import data
00017
00018
00019 import sys
00020 import logging
00021
00022
00023 class Clearpath:
00024 def __init__(self):
00025 rospy.init_node('clearpath_base')
00026
00027
00028 self.port = rospy.get_param('~port', '')
00029
00030 if self.port != '':
00031
00032 rospy.loginfo("Using port %s", self.port)
00033 self.horizon = Horizon(transport = transports.Serial,
00034 transport_args = { 'port': self.port })
00035 else:
00036
00037 rospy.loginfo("Using port autodetection")
00038 self.horizon = Horizon(transport = transports.Serial.autodetect)
00039
00040 first_error = True
00041 while True:
00042 if rospy.is_shutdown():
00043
00044
00045 self.horizon = None
00046 return
00047
00048 try:
00049 self.horizon.open()
00050 rospy.loginfo("Connection successful on %s", self.horizon)
00051 break
00052
00053 except utils.TransportError:
00054 if first_error:
00055 rospy.logerr("Unable to connect on %s. Will retry every second.", self.port if self.port != '' else '/dev/ttyUSB* or /dev/ttyS*')
00056 first_error = False
00057 rospy.sleep(1.0)
00058
00059
00060 rospy.on_shutdown(self.shutdown_handler)
00061 announce_pub = rospy.Publisher('/clearpath/announce/robots', String, latch=True)
00062 announce_pub.publish(rospy.get_namespace());
00063
00064
00065
00066
00067 platform_name = self.horizon.request_platform_name(subscription=0)
00068 platform_info = self.horizon.request_platform_info(subscription=0)
00069 firmware_info = self.horizon.request_firmware_info(subscription=0)
00070 robot_msg = data.pkg_robot_info(platform_name, platform_info, firmware_info)
00071 robot_pub = rospy.Publisher('robot', ClearpathRobot, latch=True)
00072 robot_pub.publish(robot_msg);
00073
00074 self.tx = True
00075 self.hz = 10
00076 self.do_subscriptions()
00077 self.comm_error = False
00078
00079 self.last_cmd = rospy.Time.now()
00080 self.freq_pub = rospy.Publisher('cmd_freq', Float32, latch=True)
00081 self.horizon.acks(False);
00082
00083
00084
00085 def cmd_vel_handler(self, data):
00086 if self.tx:
00087 try:
00088 self.cmd_vel(data.linear.x, data.angular.z)
00089 self.freq_pub.publish((rospy.Time.now() - self.last_cmd).to_sec())
00090 self.last_cmd = rospy.Time.now()
00091 self.tx = False
00092 self.comm_error = False
00093 except IOError as ex:
00094 if not self.comm_error:
00095 rospy.logerr('Problem communicating with platform: %s', ex)
00096 self.comm_error = True
00097 except ValueError as ex:
00098 rospy.logerr('Platform said Bad Values: %f %f', data.linear.x, data.angular.z)
00099
00100
00101 def shutdown_handler(self):
00102 self.horizon.close()
00103 rospy.loginfo("clearpath_base shutdown")
00104
00105
00106 def cmd_vel(self, linear_velocity, angular_velocity):
00107 rospy.logerror('Base class contains no command model. Use kinematic.py or raw.py.')
00108 raise NotImplementedError()
00109
00110
00111 def run(self):
00112 if self.horizon == None: return
00113
00114 rospy.Subscriber("cmd_vel", Twist, self.cmd_vel_handler)
00115 rate = rospy.Rate(self.hz)
00116
00117 while not rospy.is_shutdown():
00118 rate.sleep()
00119 self.tx = True
00120
00121 self.horizon.close()
00122
00123
00124 def do_subscriptions(self):
00125 if hasattr(self, 'publishers'): return
00126
00127 self.publishers = {}
00128 for topic, frequency in rospy.get_param('~data', {}).items():
00129 self.publishers[topic] = rospy.Publisher('data/' + topic,
00130 data.msgs[topic],
00131 latch=True)
00132 subscribe_func = getattr(self.horizon, 'request_' + topic)
00133 subscribe_func(frequency)
00134 rospy.loginfo("Successfully returning data: request_%s", topic)
00135 self.horizon.add_handler(self._receive)
00136
00137
00138 def _receive(self, name, payload, timestamp):
00139 try:
00140 pkg_func = getattr(data, 'pkg_' + name)
00141 except AttributeError:
00142 rospy.loginfo("Unhandled Clearpath message of type: %s", name)
00143 else:
00144 msg = pkg_func(payload)
00145 msg.header.stamp = rospy.Time.now()
00146 if name in self.publishers:
00147 self.publishers[name].publish(msg)
00148 else:
00149 rospy.loginfo("Unsubscribed Clearpath message of type: %s", name)