How to charge battery using engine power

Discussion in 'Content Creation' started by ArkFz, Mar 1, 2024.

  1. ArkFz

    ArkFz
    Expand Collapse

    Joined:
    Dec 24, 2023
    Messages:
    19
    I'm new to BeamNG lua programing.
    I am trying to program a powertrain Lua based on
    Code:
    /lua/vehicle/powertrain/combustionEngine.lua
    I create a variable "device.chargeEnergy" which equals to "device.spentEnergy" to calculate how much energy will be charged to the battery. Then I edited local function "updateFuelUsage" to update energy consumption.
    Code:
    local function updateBatteryStorageRatios(device)
      for _, s in pairs(device.registeredBatteryStorages) do
        local storage = energyStorage.getStorage(s)
        if storage and storage.energyType == "electricEnergy" then
          if storage.storedEnergy > 0 then
            device.batteryStorageRatios[storage.name] = 1 / device.storageWithBatteryCounter
          else
            device.batteryStorageRatios[storage.name] = 0
          end
        end
      end
    end
    
    local function updateFuelUsage(device)
      if not device.energyStorage then
        return
      end
    
      local hasFuel = false
      local previousTankCount = device.storageWithEnergyCounter
      local previousBatteryCount = device.storageWithBatteryCounter
      local remainingFuelRatio = 0
      local remainingBatteryRatio = 0
      for _, s in pairs(device.registeredEnergyStorages) do
        local storage = energyStorage.getStorage(s)
        if storage and storage.energyType == device.requiredEnergyType then
          --guihooks.message({txt = "Debug1", context = {ttl = 3, color = "info"}})
          --guihooks.message({txt = "Required energy type:" .. string.format("%s", device.requiredEnergyType), context = {ttl = 3, color = "info"}})
          local previous = device.previousEnergyLevels[storage.name]
          storage.storedEnergy = max(storage.storedEnergy - (device.spentEnergy * device.energyStorageRatios[storage.name]), 0)
          if previous > 0 and storage.storedEnergy <= 0 then
            device.storageWithEnergyCounter = device.storageWithEnergyCounter - 1
          elseif previous <= 0 and storage.storedEnergy > 0 then
            device.storageWithEnergyCounter = device.storageWithEnergyCounter + 1
          end
          device.previousEnergyLevels[storage.name] = storage.storedEnergy
          hasFuel = hasFuel or storage.storedEnergy > 0
          remainingFuelRatio = remainingFuelRatio + storage.remainingRatio
        end
      end
     
      for _, s in pairs(device.registeredBatteryStorages) do
        guihooks.message({txt = "Debug", context = {ttl = 3, color = "info"}})
        local storage = energyStorage.getStorage(s)
        if storage and storage.energyType == electricEnergy then
          local previous = device.previousEnergyLevels[storage.name]
          storage.storedEnergy = max(storage.storedEnergy + (device.chargeEnergy * device.batteryStorageRatios[storage.name]), 0)
          guihooks.message({txt = "Stored Energy" .. string.format("%.2f", storage.storedEnergy), context = {ttl = 3, color = "info"}})
          if previous > 0 and storage.storedEnergy <= 0 then
            device.storageWithBatteryCounter = device.storageWithBatteryCounter - 1
          elseif previous <= 0 and storage.storedEnergy > 0 then
            device.storageWithBatteryCounter = device.storageWithBatteryCounter + 1
          end
          device.previousBatteryLevels[storage.name] = storage.storedEnergy
          remainingBatteryRatio = remainingBatteryRatio + storage.remainingRatio
        end
      end
    
      if previousTankCount ~= device.storageWithEnergyCounter then
        device:updateEnergyStorageRatios()
      end
    
      if previousBatteryCount ~= device.storageWithBatteryCounter then
        device:updateBatteryStorageRatios()
      end
    
      device.chargeEnergy = 0
    
      if not hasFuel and device.hasFuel then
        device:disable()
      elseif hasFuel and not device.hasFuel then
        device:enable()
      end
    
      device.hasFuel = hasFuel
      device.remainingFuelRatio = remainingFuelRatio / device.storageWithEnergyCounter
      device.remainingBatteryRatio = remainingBatteryRatio / device.storageWithBatteryCounter
    end
    However, the battery cannot be charged. I am wondering how I can charge energy produced by engine into the battery.
     

    Attached Files:

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice