tdk_robokit.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2019-2020 TDK Corporation
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 #
17 # 3. Neither the name of the copyright holder nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 import rospy, sys, time, math
35 from sensor_msgs.msg import Range, Imu
36 from redswallow.control import DeviceControl
37 import redswallow.boards as boards
38 from redswallow.chsensors import Ch101Sensor
39 
41 
42  def __init__(self):
43 
44  # Publishers
45  self.pub_range = rospy.Publisher('/tdk_robokit/range', Range, queue_size=10)
46  self.pub_imu = rospy.Publisher('/tdk_robokit/imu', Imu, queue_size=10)
47 
48  # ROS Node
49  rospy.init_node('tdk_robokit', anonymous=True)
50 
51  # Register shutdown callback
52  rospy.on_shutdown(self.close_device)
53 
54  # Clear Imu raw data buffers
55  self.acc_raw_x = 0
56  self.acc_raw_y = 0
57  self.acc_raw_z = 0
58  self.gyr_raw_x = 0
59  self.gyr_raw_y = 0
60  self.gyr_raw_z = 0
61 
62  # Get parameter, COM port for Robokit
63  com_port = rospy.get_param('~robokit_com_port', '/dev/ttyUSB0')
64 
65  # Connect to Robokit
66  self.device = boards.SmartSonic(com_port=com_port, event_handler=self)
67  endtime = time.time() + 5 # 5sec timeout
68  while time.time() < endtime:
69  try:
70  self.device.open()
71  break
72  except Exception as e:
73  time.sleep(0.5)
74  if not self.device.openned:
75  print("Board not found")
76  exit(1)
77 
78  # Setup Robokit
79  rx_length = 115 # do not change
80  self.device.setSRange(min(rx_length, Ch101Sensor()['max_samples']))
81  sample_rate_hz = 16.6 # do not change
82  self.device.setODR(round(1000 / sample_rate_hz))
83  self.device.enableAsicData()
84  self.device.enableIQStream()
85  self.device.enableImu()
86  self.device.startDeviceProcessing()
87  print('Board setup successful')
88 
89  def ch_data_handler(self, rx_sensor_id, tx_sensor_id, timestamp, idata, qdata, target_detected, range_cm, amplitude):
90  # publish Range
91  if rx_sensor_id == tx_sensor_id:
92  msg = Range()
93  msg.header.stamp = rospy.get_rostime()
94  msg.header.frame_id = "ch-%d" % tx_sensor_id
95  msg.radiation_type = Range.ULTRASOUND
96  msg.field_of_view = math.pi / 2.0
97  msg.min_range = 0.14
98  msg.max_range = 1.0
99  msg.range = range_cm / 100.0
100  self.send(self.pub_range, msg)
101 
102  def imu_data_handler(self, timestamp, acc_x, acc_y, acc_z, gyr_x, gyr_y, gyr_z):
103  # Will be published with Orientation when GRV is available
104  # Note: Imu raw and GRV are being updated at the same rate
105  self.acc_raw_x = acc_x
106  self.acc_raw_y = acc_y
107  self.acc_raw_z = acc_z
108  self.gyr_raw_x = gyr_x
109  self.gyr_raw_y = gyr_y
110  self.gyr_raw_z = gyr_z
111 
112  def grv_data_handler(self, timestamp, grv_w, grv_x, grv_y, grv_z):
113  # publish Imu
114  gyr_scale = (500.0 / 32768.0) / 180.0 * math.pi # 500dps FSR
115  acc_scale = (4.0 / 32768.0) * 9.81 # 4g FSR
116  msg = Imu()
117  msg.header.stamp = rospy.get_rostime()
118  #msg.header.frame_id = 'base_link' # for test
119  msg.angular_velocity.x = self.gyr_raw_x * gyr_scale
120  msg.angular_velocity.y = self.gyr_raw_y * gyr_scale
121  msg.angular_velocity.z = self.gyr_raw_z * gyr_scale
122  msg.angular_velocity_covariance = [0.01, 0.0, 0.0, 0.0, 0.01, 0.0, 0.0, 0.0, 0.01]
123  msg.linear_acceleration.x = self.acc_raw_x * acc_scale
124  msg.linear_acceleration.y = self.acc_raw_y * acc_scale
125  msg.linear_acceleration.z = self.acc_raw_z * acc_scale
126  msg.linear_acceleration_covariance = [0.01, 0.0, 0.0, 0.0, 0.01, 0.0, 0.0, 0.0, 0.01]
127  msg.orientation.w = float(grv_w) / 2**30
128  msg.orientation.x = float(grv_x) / 2**30
129  msg.orientation.y = float(grv_y) / 2**30
130  msg.orientation.z = float(grv_z) / 2**30
131  msg.orientation_covariance = [0.01, 0.0, 0.0, 0.0, 0.01, 0.0, 0.0, 0.0, 0.01]
132  self.send(self.pub_imu, msg)
133 
134  def exception_handler(self, exception):
135  # Error message from lower layer
136  print('ROS publisher Exception', str(exception))
137 
138  def send(self, pub, msg):
139  try:
140  if not rospy.is_shutdown():
141  pub.publish(msg)
142  except rospy.ROSInterruptException:
143  print(''.join((pub.name, 'interrupted')))
144 
145  def close_device(self):
146  # close device safely
147  if self.device.openned:
148  print("Closing device ...")
149  self.device.disableImu()
150  time.sleep(0.1)
151  self.device.stopDeviceProcessing()
152  time.sleep(0.1)
153  self.device.close()
154  print("Done")
155 
156 if __name__ == '__main__':
157 
158  ros_publisher = ROSPublisher()
159  rospy.spin()
def grv_data_handler(self, timestamp, grv_w, grv_x, grv_y, grv_z)
Definition: tdk_robokit.py:112
def ch_data_handler(self, rx_sensor_id, tx_sensor_id, timestamp, idata, qdata, target_detected, range_cm, amplitude)
Definition: tdk_robokit.py:89
def imu_data_handler(self, timestamp, acc_x, acc_y, acc_z, gyr_x, gyr_y, gyr_z)
Definition: tdk_robokit.py:102
def exception_handler(self, exception)
Definition: tdk_robokit.py:134
def send(self, pub, msg)
Definition: tdk_robokit.py:138


tdk_robokit
Author(s): TDK-OpenSource
autogenerated on Sat Jan 11 2020 03:18:39