fake_bms.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 import rospy
19 from std_msgs.msg import Bool
20 from std_msgs.msg import Float64
21 from cob_srvs.srv import SetFloat, SetFloatResponse
22 from cob_srvs.srv import SetInt, SetIntResponse
23 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus
24 from diagnostic_updater import Updater
25 
26 class FakeBMS(object):
27  def __init__(self):
28  self._srv_current = rospy.Service('~set_current', SetFloat, self.current_cb)
29  self._srv_relative_remaining_capacity = rospy.Service('~set_relative_remaining_capacity', SetInt, self.relative_remaining_capacity_cb)
30  self._poll_frequency = rospy.get_param('~poll_frequency_hz', 20.0)
31  self._pub_voltage = rospy.Publisher('~voltage', Float64, queue_size = 1)
32  self._pub_current = rospy.Publisher('~current', Float64, queue_size = 1)
33  self._pub_remaining_capacity = rospy.Publisher('~remaining_capacity', Float64, queue_size = 1)
34  self._pub_full_charge_capacity = rospy.Publisher('~full_charge_capacity', Float64, queue_size = 1)
35  self._pub_temparature = rospy.Publisher('~temperature', Float64, queue_size = 1)
36 
37  self._updater = Updater()
38  self._updater.setHardwareID("bms")
39  self._updater.add("cob_bms_dagnostics_updater", self.produce_diagnostics)
40 
41  self._voltage = rospy.get_param('~voltage', 48.0) # V
42  self._current = rospy.get_param('~current', -8.0) # A
43  self._remaining_capacity = rospy.get_param('~remaining_capacity', 35.0) # Ah
44  self._full_charge_capacity = rospy.get_param('~full_charge_capacity', 35.0) # Ah
45  self._temperature = rospy.get_param('~temperature', 25.0) # °C
46 
47  rospy.Timer(rospy.Duration(1.0), self.publish_diagnostics)
48  rospy.Timer(rospy.Duration(1.0/self._poll_frequency), self.timer_cb)
49  rospy.Timer(rospy.Duration(1.0/self._poll_frequency), self.timer_consume_power_cb)
50 
51  def current_cb(self, req):
52  self._current = round(req.data,2)
53  res_current = SetFloatResponse(True, "Set current to {}".format(self._current))
54  return res_current
55 
57  self._remaining_capacity = round(((req.data * self._full_charge_capacity)/100.0), 3)
58  res_capacity = SetIntResponse(True, "Set remaining capacity to {}".format(self._remaining_capacity))
59  return res_capacity
60 
61  def publish_diagnostics(self, event):
62  self._updater.update()
63 
64  def produce_diagnostics(self, stat):
65  stat.summary(DiagnosticStatus.OK, "Fake Driver: Ready")
66  stat.add("current[A]", self._current)
67  stat.add("voltage[V]", self._voltage)
68  stat.add("temperature[Celsius]", self._temperature)
69  stat.add("remaining_capacity[Ah]", round(self._remaining_capacity, 3))
70  stat.add("full_charge_capacity[Ah]", self._full_charge_capacity)
71  return stat
72 
73  def timer_cb(self, event):
74  self._pub_voltage.publish(self._voltage)
75  self._pub_current.publish(self._current)
76  self._pub_remaining_capacity.publish(round(self._remaining_capacity, 3))
78  self._pub_temparature.publish(self._temperature)
79 
80  def timer_consume_power_cb(self, event):
81  # emulate the battery usage based on the current values
82  self._remaining_capacity += (self._current/self._poll_frequency)/3600.0
83  if self._remaining_capacity <= 0.0:
84  self._remaining_capacity = 0.0
86  self._remaining_capacity = round(self._full_charge_capacity,3)
87 
88 if __name__ == '__main__':
89  rospy.init_node('fake_bms')
90  FakeBMS()
91  rospy.spin()
def timer_consume_power_cb(self, event)
Definition: fake_bms.py:80
def publish_diagnostics(self, event)
Definition: fake_bms.py:61
def produce_diagnostics(self, stat)
Definition: fake_bms.py:64
def relative_remaining_capacity_cb(self, req)
Definition: fake_bms.py:56
_srv_relative_remaining_capacity
Definition: fake_bms.py:29
def current_cb(self, req)
Definition: fake_bms.py:51
def __init__(self)
Definition: fake_bms.py:27
def timer_cb(self, event)
Definition: fake_bms.py:73


cob_bms_driver
Author(s): mig-mc , Mathias Lüdtke
autogenerated on Thu Nov 17 2022 03:17:26