Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 """
00035 This module handles voltage messages from the Arduino Mega 2560
00036 mounted on BWI segbots, publishing them as ROS
00037 sensor_msgs/BatteryState messages.
00038 """
00039
00040
00041 from __future__ import absolute_import, print_function, unicode_literals
00042
00043 import re
00044
00045 import rospy
00046 from sensor_msgs.msg import BatteryState
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', BatteryState,
00060 queue_size=1)
00061
00062 self.battery = BatteryState(
00063 current=float('nan'),
00064 charge=float('nan'),
00065 capacity=float('nan'),
00066 design_capacity=float('nan'),
00067 percentage=float('nan'),
00068 power_supply_status=BatteryState.POWER_SUPPLY_STATUS_DISCHARGING,
00069 present=1)
00070
00071 def publish(self, serial_msg):
00072 """ Publish a ROS Range message for each reading.
00073
00074 :param serial_msg: voltage message from Arduino.
00075 :type serial_msg: str
00076
00077 TODO: publish /diagnostic message, too
00078 """
00079
00080
00081 readings = self.parser.match(serial_msg)
00082 if not readings:
00083 rospy.logwarn('Invalid voltage message: ' + serial_msg)
00084 return
00085
00086
00087 self.battery.header.stamp = rospy.Time.now()
00088 self.battery.voltage = float(readings.group(1))
00089 rospy.logdebug('voltage: ' + str(self.battery.voltage))
00090 self.pub.publish(self.battery)
00091
00092
00093 voltmeter = VoltmeterMessages()
00094 handler = voltmeter.publish
00095 """ This interface is called once for each voltage message received. """