main.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 from geometry_msgs.msg import Twist
5 from sensor_msgs.msg import Range
6 from rodi_robot.transport import Transport
7 
8 
9 class RodiRobot(object):
10 
11  ''' Robot class to add ROS support to RoDI'''
12 
13  def __init__(self):
14  hostname = rospy.get_param('~hostname', '192.168.4.1')
15  port = rospy.get_param('~port', '1234')
16  self.timeout = rospy.get_param('~timeout', 2.0)
17 
18  self.transport = Transport(hostname, port)
19 
20  rospy.Subscriber('cmd_vel',
21  Twist,
22  self._cmd_vel_cb)
23  self.pub = rospy.Publisher('ultrasound',
24  Range,
25  queue_size=1)
26 
27  self.rate = rospy.Rate(1) # 1 Hz
28 
29  self.us_sensor = Range()
30  self.us_sensor.radiation_type = 0 # ULTRASOUND
31  self.us_sensor.header.frame_id = "/ultrasound"
32  self.us_sensor.field_of_view = 0.52
33  self.us_sensor.min_range = 0.2
34  self.us_sensor.max_range = 1.0
35 
36  self.last_cmd_vel = rospy.Time.now()
37 
38  def _cmd_vel_cb(self, msg):
39  self.last_cmd_vel = rospy.Time.now()
40  if msg.angular.z == 0 and msg.linear.x == 0:
41  self.transport.stop()
42  elif msg.linear.x > 0:
43  self.transport.move_forward()
44  elif msg.linear.x < 0:
45  self.transport.move_reverse()
46  elif msg.angular.z > 0:
47  self.transport.move_left()
48  elif msg.angular.z < 0:
49  self.transport.move_right()
50 
51  def run(self):
52  while not rospy.is_shutdown():
53  if (rospy.Time.now() - self.last_cmd_vel).secs >= self.timeout:
54  self.transport.stop()
55  self.last_cmd_vel = rospy.Time.now()
56  try:
57  range = self.transport.see()
58  if range is not None:
59  self.us_sensor.range = float(range) / 100.0
60  self.us_sensor.header.stamp = rospy.Time.now()
61  self.pub.publish(self.us_sensor)
62  except Exception as exception:
63  rospy.logerr("getting sonar data failed: " + str(exception))
64 
65  self.rate.sleep()
def __init__(self)
Definition: main.py:13
def _cmd_vel_cb(self, msg)
Definition: main.py:38


rodi_robot
Author(s): Javier Martinez Canillas
autogenerated on Mon Jun 10 2019 14:36:49