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 diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus
23 from diagnostic_updater import Updater
24 
25 class FakeBMS(object):
26  def __init__(self):
27  self.srv_current = rospy.Service('~set_current', SetFloat, self.current_cb)
28  self.srv_relative_remaining_capacity = rospy.Service('~set_relative_remaining_capacity', SetFloat, self.relative_remaining_capacity_cb)
29  self.poll_frequency = rospy.get_param('~poll_frequency_hz', 20.0)
30  self.pub_voltage = rospy.Publisher('~voltage', Float64, queue_size = 1)
31  self.pub_current = rospy.Publisher('~current', Float64, queue_size = 1)
32  self.pub_remaining_capacity = rospy.Publisher('~remaining_capacity', Float64, queue_size = 1)
33  self.pub_full_charge_capacity = rospy.Publisher('~full_charge_capacity', Float64, queue_size = 1)
34  self.pub_temparature = rospy.Publisher('~temperature', Float64, queue_size = 1)
35 
36  self.updater = Updater()
37  self.updater.setHardwareID("bms")
38  self.updater.add("cob_bms_dagnostics_updater", self.produce_diagnostics)
39 
40  self.voltage = 48.0
41  self.current = -8.0
42  self.remaining_capacity = 35.0
43  self.full_charge_capacity = 35.0 # Ah
44  self.temperature = 25.0
45 
46  rospy.Timer(rospy.Duration(1.0), self.publish_diagnostics)
47  rospy.Timer(rospy.Duration(1.0/self.poll_frequency), self.timer_cb)
48  rospy.Timer(rospy.Duration(1.0/self.poll_frequency), self.timer_consume_power_cb)
49 
50  def current_cb(self, req):
51  self.current = round(req.data,2)
52  res_current = SetFloatResponse(True, "Set current to {}".format(self.current))
53  return res_current
54 
56  self.remaining_capacity = round(((req.data * self.full_charge_capacity)/100.0), 3)
57  res_capacity = SetFloatResponse(True, "Set relative remaining capacity to {}".format(self.remaining_capacity))
58  return res_capacity
59 
60  def publish_diagnostics(self, event):
61  self.updater.update()
62 
63  def produce_diagnostics(self, stat):
64  stat.summary(DiagnosticStatus.OK, "Fake Driver: Ready")
65  stat.add("current[A]", self.current)
66  stat.add("voltage[V]", self.voltage)
67  stat.add("temperature[Celsius]", self.temperature)
68  stat.add("remaining_capacity[Ah]", self.remaining_capacity)
69  stat.add("full_charge_capacity[Ah]", self.full_charge_capacity)
70  return stat
71 
72  def timer_cb(self, event):
73  self.pub_voltage.publish(self.voltage)
74  self.pub_current.publish(self.current)
75  self.pub_remaining_capacity.publish(self.remaining_capacity)
76  self.pub_full_charge_capacity.publish(self.full_charge_capacity)
77  self.pub_temparature.publish(self.temperature)
78 
79  def timer_consume_power_cb(self, event):
80  # emulate the battery usage based on the current values
81  self.remaining_capacity += (self.current/self.poll_frequency)/3600.0
82  self.remaining_capacity = round(self.remaining_capacity,3)
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:79
srv_relative_remaining_capacity
Definition: fake_bms.py:28
def publish_diagnostics(self, event)
Definition: fake_bms.py:60
def produce_diagnostics(self, stat)
Definition: fake_bms.py:63
def relative_remaining_capacity_cb(self, req)
Definition: fake_bms.py:55
def current_cb(self, req)
Definition: fake_bms.py:50
def __init__(self)
Definition: fake_bms.py:26
def timer_cb(self, event)
Definition: fake_bms.py:72


cob_bms_driver
Author(s): mig-mc , Mathias Lüdtke
autogenerated on Wed Apr 7 2021 02:11:37