使用Python Simpy实时模拟电池充电
我正在尝试使用Simpy进行模拟,以进行电池充电。所需的参数是电池容量(W),电池的充电状态(百分比)和充电器的充电功率(W/H)
我的代码看起来像这样:
import simpy
import random
battery_percentage = []
class ChargingCycle(object):
def __init__(self, env, battery_capacity, SoC, charging_power):
self.env = env
self.battery_capacity = battery_capacity
self.SoC = SoC
self.charging_power = simpy.Resource(env,charging_power)
def charge(self, battery):
print(self.charging_power)
power_per_minute = self.charging_power/60
charge_in_kw = (self.SoC/100)/self.battery_capacity
charge_in_kw += power_per_minute
self.SoC = charge_in_kw/self.battery_capacity*100
def start_charging(env, battery, charging):
with charging.charging_power.request() as request:
yield request
yield env.process(charging.charge(battery))
def run_charging(env, battery_capacity, SoC, charging_power):
charging = ChargingCycle(env, battery_capacity, SoC, charging_power)
battery = 0
while True:
yield env.timeout(1)
battery += 1
env.process(start_charging(env, battery, charging))
def main():
env = simpy.Environment()
env.process(run_charging(env,45000, 10, 7630))
env.run(until=360)
if __name__ == "__main__":
main()
基本上,我想为每一个获得充电能力分钟,每分钟,我将充电器给电池的电源添加到变量chode> Charge_in_kw
,然后更新了电荷状态soc
。
当我运行模拟时,我会收到此错误:
File "c:\Users\sc57978\Desktop\OCPP\sim_charge.py", line 17, in charge
power_per_minute = self.charging_power/60
TypeError: unsupported operand type(s) for /: 'Resource' and 'int'
有人知道如何解决此问题?
I'm trying to do a simulation with simpy for a battery charging. The parameters needed are the battery capacity (W), the state of charge of the battery (in percentage) and the charging power of the charger (W/h)
My code looks like this:
import simpy
import random
battery_percentage = []
class ChargingCycle(object):
def __init__(self, env, battery_capacity, SoC, charging_power):
self.env = env
self.battery_capacity = battery_capacity
self.SoC = SoC
self.charging_power = simpy.Resource(env,charging_power)
def charge(self, battery):
print(self.charging_power)
power_per_minute = self.charging_power/60
charge_in_kw = (self.SoC/100)/self.battery_capacity
charge_in_kw += power_per_minute
self.SoC = charge_in_kw/self.battery_capacity*100
def start_charging(env, battery, charging):
with charging.charging_power.request() as request:
yield request
yield env.process(charging.charge(battery))
def run_charging(env, battery_capacity, SoC, charging_power):
charging = ChargingCycle(env, battery_capacity, SoC, charging_power)
battery = 0
while True:
yield env.timeout(1)
battery += 1
env.process(start_charging(env, battery, charging))
def main():
env = simpy.Environment()
env.process(run_charging(env,45000, 10, 7630))
env.run(until=360)
if __name__ == "__main__":
main()
Basically I want to get the charging power for every minute, and every minute I add the power given from the charger to the battery to a variable charge_in_kw
and I update the state of charge SoC
.
When I run the simulation I get this error:
File "c:\Users\sc57978\Desktop\OCPP\sim_charge.py", line 17, in charge
power_per_minute = self.charging_power/60
TypeError: unsupported operand type(s) for /: 'Resource' and 'int'
Someone knows how can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在运行和进行大量调试后,我能够运行您的代码,我进行了以下更改以使其运行以
charging_capacity
资源中获取值,以求解您suff
suffer self self。 Env.TimeOut(1)
解决电荷功能最终运行代码的更多错误
After running and doing a lot of debugging i am able to run your code, i made following changes to make it run
charging_capacity
resource to solve your actual erroryield self.env.timeout(1)
to resolve further error in Charge functionFinal Running Code