简体   繁体   中英

Simulate battery charging in real time with python simpy

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?

After running and doing a lot of debugging i am able to run your code, i made following changes to make it run

  • Get the value from the charging_capacity resource to solve your actual error
  • added yield self.env.timeout(1) to resolve further error in Charge function

Final Running Code

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.capacity/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
        yield self.env.timeout(1)


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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM