ev

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ev models the EV on a connector: just enough of a battery to make the energy flow of a transaction look real.

Index

Constants

View Source
const (
	CurveStandard = "standard"
	CurveDCFast   = "dc-fast"
)

Names of the built-in curves.

Variables

This section is empty.

Functions

func BuiltinCurveNames

func BuiltinCurveNames() []string

BuiltinCurveNames returns the names of the built-in curves, sorted.

func ParseEnergy

func ParseEnergy(s string) (float64, error)

ParseEnergy reads an energy value such as "60kWh" or "500Wh" into Wh.

func ParsePercent

func ParsePercent(s string) (float64, error)

ParsePercent reads a percentage such as "20%" or "20" into a fraction from 0 to 1.

Types

type Curve

type Curve struct {
	// Name is the name of a built-in curve, or empty for a custom one.
	Name   string  `json:"name,omitempty"`
	Points []Point `json:"points,omitempty"`
}

Curve is a charge curve: how much of its maximum power an EV accepts at each state of charge. Between points the value changes in a straight line. The zero Curve is the standard curve.

func BuiltinCurve

func BuiltinCurve(name string) (Curve, error)

BuiltinCurve returns the built-in curve with the given name.

func NewCurve

func NewCurve(points []Point) (Curve, error)

NewCurve returns a custom curve through the given points.

func (Curve) At

func (c Curve) At(soc float64) float64

At returns the fraction of its maximum power the EV accepts at the given state of charge.

func (Curve) Validate

func (c Curve) Validate() error

Validate reports the first problem with the curve, if any. A curve must cover 0% to 100% and may never reach zero, or an EV could stop gaining charge before its target and stay there forever.

type EV

type EV struct {
	// contains filtered or unexported fields
}

EV is a plugged-in EV.

func New

func New(p Params) *EV

New returns an EV with the given parameters.

func Restore

func Restore(s State) *EV

Restore returns the EV described by a saved state.

func (*EV) Charge

func (e *EV) Charge(offeredW float64, d time.Duration) float64

Charge advances the EV by d of charging with up to offeredW on offer, and returns the energy taken in Wh. There are no losses: all of it goes into the battery.

func (*EV) ChargeWithin

func (e *EV) ChargeWithin(connectorW, limitW float64, d time.Duration) float64

ChargeWithin is Charge for a connector that can deliver connectorW and offers only limitW of it for now.

func (*EV) Full

func (e *EV) Full() bool

Full reports whether the EV has reached its target and wants no more energy.

func (*EV) Params

func (e *EV) Params() Params

Params returns the parameters the EV was created with.

func (*EV) Power

func (e *EV) Power(offeredW float64) float64

Power returns the power in W the EV draws right now when the connector offers up to offeredW.

The charge curve belongs to the EV: it scales the EV's own maximum power, and the connector then caps the result. An EV whose maximum is far above the connector's therefore charges at the connector's full power until late, as a fast-charging car does on a slow charger. An EV with no maximum of its own takes the connector's as its maximum.

func (*EV) PowerWithin

func (e *EV) PowerWithin(connectorW, limitW float64) float64

PowerWithin is Power for a connector that can deliver connectorW and offers only limitW of it for now, as under a charging profile. The limit caps the power and leaves the EV's own maximum as it is.

func (*EV) SoC

func (e *EV) SoC() float64

SoC returns the current state of charge, from 0 to 1.

func (*EV) State

func (e *EV) State() State

State returns the EV as it is now, for saving.

type Overrides

type Overrides struct {
	BatteryWh *float64 `json:"batteryWh,omitempty"`
	SoC       *float64 `json:"soc,omitempty"`
	MaxPowerW *float64 `json:"maxPowerW,omitempty"`
	TargetSoC *float64 `json:"targetSoc,omitempty"`
	Curve     *Curve   `json:"curve,omitempty"`
}

Overrides are EV parameters that may or may not have been given. They are what an EV preset holds, and what the flags of a plug carry.

type Params

type Params struct {
	// BatteryWh is the battery capacity in Wh.
	BatteryWh float64
	// SoC is the state of charge at plug-in, from 0 to 1.
	SoC float64
	// MaxPowerW is the most power the EV accepts, in W. Zero means no limit
	// of its own, so the connector's maximum applies.
	MaxPowerW float64
	// TargetSoC is the state of charge at which the EV stops charging, from
	// 0 to 1.
	TargetSoC float64
	// Curve is the charge curve: the share of its maximum power the EV
	// accepts at each state of charge. The zero value is the standard curve.
	Curve Curve
}

Params are the parameters of an EV.

func Defaults

func Defaults() Params

Defaults returns the built-in EV: 60 kWh, 20% charged, no power limit, charging to 100%.

func (Params) Validate

func (p Params) Validate() error

Validate reports the first problem with p, if any.

func (Params) With

func (p Params) With(o Overrides) Params

With returns p with every parameter that o gives replaced.

type Point

type Point struct {
	SoC      float64 `json:"soc"`
	Fraction float64 `json:"fraction"`
}

Point is one point of a charge curve: at state of charge SoC (0 to 1) the EV accepts Fraction (above 0, at most 1) of its maximum power.

type State

type State struct {
	Params Params  `json:"params"`
	SoC    float64 `json:"soc"`
}

State is a plugged-in EV as it is saved: its parameters and how far it has charged.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL