voltmeter.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (C) 2015, Jack O'Quin
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of the author nor of other contributors may be
00018 #    used to endorse or promote products derived from this software
00019 #    without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 
00034 """
00035 This module handles voltage messages from the Arduino Mega 2560
00036 mounted on BWI segbots, publishing them as ROS
00037 smart_battery_msgs/SmartBatteryStatus messages.
00038 """
00039 
00040 # enable some python3 compatibility options:
00041 from __future__ import absolute_import, print_function, unicode_literals
00042 
00043 import re
00044 
00045 import rospy
00046 from smart_battery_msgs.msg import SmartBatteryStatus
00047 from geometry_msgs.msg import Vector3
00048 
00049 
00050 class VoltmeterMessages(object):
00051     """ ROS message translation for UTexas BWI segbot voltage obtained
00052     from Arduino sensor. """
00053     def __init__(self):
00054         self.parser = re.compile(
00055             r'V(\d+(\.\d*)?|\.\d+)')
00056         """ Extracts voltage reading from the Arduino serial message.
00057         :returns: voltage data string reported, may be empty.
00058         """
00059         self.pub = rospy.Publisher('battery0', SmartBatteryStatus)
00060         # initialize constant fields in battery message
00061         self.battery = SmartBatteryStatus(
00062             rate=float('nan'),
00063             charge=float('nan'),
00064             capacity=float('nan'),
00065             design_capacity=float('nan'),
00066             charge_state=SmartBatteryStatus.DISCHARGING,
00067             present=1)
00068 
00069     def publish(self, serial_msg):
00070         """ Publish a ROS Range message for each reading.
00071 
00072         :param serial_msg: voltage message from Arduino.
00073         :type serial_msg: str
00074 
00075         TODO: publish /diagnostic message, too
00076         """
00077         # Parse serial message line into a list of voltage data strings
00078         # containing integers.
00079         readings = self.parser.match(serial_msg)
00080         if not readings:                # no data available?
00081             rospy.logwarn('Invalid voltage message: ' + serial_msg)
00082             return
00083 
00084         # Format ROS battery status message
00085         self.battery.header.stamp = rospy.Time.now()
00086         self.battery.voltage = float(readings.group(1))
00087         rospy.logdebug('voltage: ' + str(self.battery.voltage))
00088         self.pub.publish(self.battery)
00089 
00090 
00091 voltmeter = VoltmeterMessages()   # make an VoltmeterMessages instance
00092 handler = voltmeter.publish       # declare message handler interface
00093 """ This interface is called once for each voltage message received. """


segbot_sensors
Author(s): Piyush Khandelwal
autogenerated on Fri Aug 28 2015 13:03:00