46045-syslab/util.py

43 lines
1.2 KiB
Python

import numpy as np
from syslab.core.datatypes import CompositeMeasurement
from typing import Union
from time import time
def pos(x):
"""
:param x: input
:return: x if x > 0 else 0
"""
return max(x, 0)
def clamp(a, x, b):
"""
Restrict x to lie in the range [a, b]
"""
return max(a, min(x, b))
def cast_to_cm(m: Union[CompositeMeasurement, float]):
if type(m) == float:
request = CompositeMeasurement(m, timestampMicros=time()*1e6, timePrecision=1000)
elif type(m) == CompositeMeasurement:
request = m
else:
raise TypeError(f"Unknown request type: {type(m)}")
return request
def soc_scaler(soc, min_soc=0.0, max_soc=1.0):
"""
Scale an soc to emulate having a smaller battery.
If the actual battery has a capacity of 14 kWh, the rescaled battery will have a capacity
of 14*(max_soc - min_soc) kWh.
Does not check for negative soc.
:param soc: current actual soc
:param min_soc: actual soc that should correspond to an soc of 0.0
:param max_soc: actual soc that should correspond to an soc of 1.0
:return: rescaled soc
"""
return (soc - min_soc)/(max_soc - min_soc)