wheeled_robin_sensor_handler.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2011, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of the Willow Garage nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 import roslib.message
00034 import struct
00035 import logging
00036 import time
00037 import rospy
00038 
00039 
00040 from math import radians
00041 from wheeled_robin_driver import SENSOR_GROUP_PACKET_LENGTHS
00042 
00043 #_struct_I = roslib.message.struct_I
00044 _struct_Group0 = struct.Struct(">8hB5h2B")
00045 _struct_Group1 = struct.Struct(">8hB")
00046 _struct_Group7 = struct.Struct(">h")
00047 
00048 def deserialize(msg, buff, timestamp, packet_id):
00049     """
00050     unpack serialized message in str into this message instance
00051     @param buff: byte array of serialized message
00052     @type  buff: str
00053     """
00054     try:
00055         _x = msg
00056         
00057         msg.header.stamp = rospy.Time.from_seconds(timestamp)        
00058                 
00059         # set everything to default values
00060         msg.linear_velocity = 0
00061         msg.angular_velocity = 0
00062         msg.pitch = 0
00063         msg.pitch_rate = 0
00064         msg.left_wheel = 0
00065         msg.right_wheel = 0
00066         msg.left_wheel_rate = 0
00067         msg.right_wheel_rate = 0
00068         msg.mode = 0;
00069         msg.left_current = 0
00070         msg.right_current = 0
00071         msg.voltage = 0
00072         msg.imu_acc_x = 0
00073         msg.imu_acc_y = 0
00074 
00075         
00076         if packet_id == 0:
00077                 (_x.linear_velocity, _x.angular_velocity, _x.pitch, _x.pitch_rate, _x.left_wheel, _x.right_wheel, _x.left_wheel_rate, _x.right_wheel_rate, _x.mode, _x.left_current, _x.right_current, _x.voltage, _x.imu_acc_x, _x.imu_acc_y, _x.digital_outputs, _x.digital_inputs,) = _struct_Group0.unpack(buff[0:SENSOR_GROUP_PACKET_LENGTHS[packet_id]])
00078                 
00079                 # do unit conversions
00080                 msg.linear_velocity = float(msg.linear_velocity) /1000.
00081                 msg.angular_velocity = float(msg.angular_velocity) /1000.
00082                 msg.pitch = float(msg.pitch) /1000.
00083                 msg.pitch_rate = float(msg.pitch_rate) /1000.
00084                 msg.left_wheel = float(msg.left_wheel) /1000.
00085                 msg.right_wheel = float(msg.right_wheel) /1000.
00086                 msg.left_wheel_rate = float(msg.left_wheel_rate) /1000.
00087                 msg.right_wheel_rate = float(msg.right_wheel_rate) /1000.
00088                 msg.left_current = float(msg.left_current) /1000.
00089                 msg.right_current = float(msg.right_current) /1000.
00090                 msg.voltage = float(msg.voltage) /1000.
00091                 msg.imu_acc_x = float(msg.imu_acc_x) /1000.
00092                 msg.imu_acc_y = float(msg.linear_velocity) /1000.
00093         elif packet_id == 1:
00094                 (_x.linear_velocity, _x.angular_velocity, _x.pitch, _x.pitch_rate, _x.left_wheel, _x.right_wheel, _x.left_wheel_rate, _x.right_wheel_rate, _x.mode,) = _struct_Group1.unpack(buff[0:SENSOR_GROUP_PACKET_LENGTHS[packet_id]])
00095                 
00096                 # do unit conversions
00097                 msg.linear_velocity = float(msg.linear_velocity) /1000.
00098                 msg.angular_velocity = float(msg.angular_velocity) /1000.
00099                 msg.pitch = float(msg.pitch) /1000.
00100                 msg.pitch_rate = float(msg.pitch_rate) /1000.
00101                 msg.left_wheel = float(msg.left_wheel) /1000.
00102                 msg.right_wheel = float(msg.right_wheel) /1000.
00103                 msg.left_wheel_rate = float(msg.left_wheel_rate) /1000.
00104                 msg.right_wheel_rate = float(msg.right_wheel_rate) /1000.
00105 
00106         elif packet_id == 7:
00107                 (_x.linear_velocity) = _struct_Group .unpack(buff[0:SENSOR_GROUP_PACKET_LENGTHS[packet_id]])
00108                 
00109                 # do unit conversions
00110                 msg.linear_velocity = float(msg.linear_velocity[0]) /1000.
00111                 msg.mode = 3 #this is just a workaround so that the robot works even if mode is not updated
00112 
00113         return msg
00114     except struct.error, e:
00115         raise roslib.message.DeserializationError(e)
00116 
00117 class WheeledRobinSensorHandler(object):
00118     def __init__(self, robot):
00119         self.robot = robot
00120 
00121     def request_packet(self, packet_id):
00122         """Request a sensor packet."""
00123         with self.robot.sci.lock:
00124             self.robot.sci.flush_input()
00125             self.robot.sci.sensors(packet_id)
00126             #kwc: there appears to be a 10-20ms latency between sending the
00127             #sensor request and fully reading the packet.  Based on
00128             #observation, we are preferring the 'before' stamp rather than
00129             #after.
00130             stamp = time.time()
00131             length = SENSOR_GROUP_PACKET_LENGTHS[packet_id]
00132             return self.robot.sci.read(length), stamp
00133 
00134     def get_all(self, sensor_state):
00135         buff, timestamp = self.request_packet(1)
00136         if buff is not None:
00137             deserialize(sensor_state, buff, timestamp,1)


wheeled_robin_node
Author(s): Johannes Mayr , Klemens Springer
autogenerated on Fri Aug 28 2015 13:39:00