fake_bms.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 #   http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
00016 
00017 
00018 import rospy
00019 from std_msgs.msg import Bool
00020 from std_msgs.msg import Float64
00021 from cob_srvs.srv import SetFloat, SetFloatResponse
00022 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus
00023 from diagnostic_updater import Updater
00024 
00025 class FakeBMS(object):
00026     def __init__(self):
00027         self.srv_current              = rospy.Service('~set_current', SetFloat, self.current_cb)
00028         self.srv_relative_remaining_capacity   = rospy.Service('~set_relative_remaining_capacity', SetFloat, self.relative_remaining_capacity_cb)
00029         self.poll_frequency           = rospy.get_param('~poll_frequency_hz', 20.0)
00030         self.pub_voltage              = rospy.Publisher('~voltage', Float64, queue_size = 1)
00031         self.pub_current              = rospy.Publisher('~current', Float64, queue_size = 1)
00032         self.pub_remaining_capacity   = rospy.Publisher('~remaining_capacity', Float64, queue_size = 1)
00033         self.pub_full_charge_capacity = rospy.Publisher('~full_charge_capacity', Float64, queue_size = 1)
00034         self.pub_temparature          = rospy.Publisher('~temperature', Float64, queue_size = 1)
00035 
00036         self.updater = Updater()
00037         self.updater.setHardwareID("bms")
00038         self.updater.add("cob_bms_dagnostics_updater", self.produce_diagnostics)
00039 
00040         self.voltage              = 48.0
00041         self.current              = -8.0
00042         self.remaining_capacity   = 35.0
00043         self.full_charge_capacity = 35.0 # Ah
00044         self.temperature          = 25.0
00045 
00046         rospy.Timer(rospy.Duration(1.0), self.publish_diagnostics)
00047         rospy.Timer(rospy.Duration(1.0/self.poll_frequency), self.timer_cb)
00048         rospy.Timer(rospy.Duration(1.0/self.poll_frequency), self.timer_consume_power_cb)
00049 
00050     def current_cb(self, req):
00051         self.current = round(req.data,2)
00052         res_current = SetFloatResponse(True, "Set current to {}".format(self.current))
00053         return res_current
00054         
00055     def relative_remaining_capacity_cb(self, req):
00056         self.remaining_capacity = round(((req.data * self.full_charge_capacity)/100.0), 3)
00057         res_capacity = SetFloatResponse(True, "Set relative remaining capacity to {}".format(self.remaining_capacity))
00058         return res_capacity
00059 
00060     def publish_diagnostics(self, event):
00061         self.updater.update()
00062 
00063     def produce_diagnostics(self, stat):
00064         stat.summary(DiagnosticStatus.OK, "Fake Driver: Ready")
00065         stat.add("current[A]", self.current)
00066         stat.add("voltage[V]", self.voltage)
00067         stat.add("temperature[Celsius]", self.temperature)
00068         stat.add("remaining_capacity[Ah]", self.remaining_capacity)
00069         stat.add("full_charge_capacity[Ah]", self.full_charge_capacity)
00070         return stat
00071 
00072     def timer_cb(self, event):
00073         self.pub_voltage.publish(self.voltage)
00074         self.pub_current.publish(self.current)
00075         self.pub_remaining_capacity.publish(self.remaining_capacity)
00076         self.pub_full_charge_capacity.publish(self.full_charge_capacity)
00077         self.pub_temparature.publish(self.temperature)
00078 
00079     def timer_consume_power_cb(self, event):
00080         # emulate the battery usage based on the current values
00081         self.remaining_capacity += (self.current/self.poll_frequency)/3600.0
00082         self.remaining_capacity = round(self.remaining_capacity,3)
00083         if self.remaining_capacity <= 0.0:
00084             self.remaining_capacity = 0.0
00085         if self.remaining_capacity >= self.full_charge_capacity:
00086             self.remaining_capacity = round(self.full_charge_capacity,3)
00087         
00088 if __name__ == '__main__':
00089   rospy.init_node('fake_bms')
00090   FakeBMS()
00091   rospy.spin()


cob_bms_driver
Author(s): mig-mc , Mathias Lüdtke
autogenerated on Sat Jun 8 2019 21:01:57