rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
supply.h File Reference

A programmable supply: what it can be set to, what it was set to, and what it delivers. More...

#include <stdbool.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  supply_caps_t
 What a supply can be set to. More...
struct  supply_state_t
 A supply's readings, set points, mode and trip state. More...
struct  supply_limits_t
 What the operator allows, on top of what the supply can do: caps on the set points, and trips that switch the output off. More...
struct  supply_trip_t
 How long each reading has been over its threshold, counted from the first reading seen over it, for the trips. More...
struct  supply_sim_t
 The state of the supply model. More...

Macros

#define SUPPLY_CAPS_PPS_DEFAULT   { 3.3f, 21.0f, 0.02f, 0.5f, 5.0f, 0.05f }
 The range a USB-PD PPS (Programmable Power Supply) source offers in its widest profile, 3.3 to 21V in 20mV steps and up to 5A in 50mA steps.
#define SUPPLY_OK_VOLTAGE   0x01u
 Which readings arrived; an absent one is drawn and logged empty.
#define SUPPLY_OK_CURRENT   0x02u
#define SUPPLY_SIM_LOAD_OHMS   6.0f
 A supply feeding a servo-like load, for the panel to run while no driver exists, and for the screenshots.
#define SUPPLY_SIM_PERIOD_S   3.0f
#define SUPPLY_SIM_BURST_S   0.6f
#define SUPPLY_SIM_BURST_A   1.4f
#define SUPPLY_SIM_SOURCE_OHMS   0.05f

Enumerations

enum  supply_mode_t { SUPPLY_MODE_OFF = 0 , SUPPLY_MODE_CV , SUPPLY_MODE_CC }
 The output mode of a supply: off, constant voltage or constant current. More...
enum  supply_trip_kind_t { SUPPLY_TRIP_NONE = 0 , SUPPLY_TRIP_CURRENT , SUPPLY_TRIP_VOLTAGE }
 The trips: none, current and voltage.

Functions

supply_caps_t supply_caps_limited (const supply_caps_t *caps, const supply_limits_t *lim)
 The supply's caps narrowed by lim: the set points a screen offers.
void supply_trip_reset (supply_trip_t *t)
 Clears the trip state.
supply_trip_kind_t supply_trip_step (supply_trip_t *t, const supply_limits_t *lim, const supply_state_t *s, float dt_s)
 Take dt_s of the readings in s into the trips.
float supply_snap (float value, float min, float max, float step)
 value clamped to min .. max and rounded to a multiple of step above min.
void supply_reset_peaks (supply_state_t *s)
 The run's extremes start again from what is shown, and only from readings that arrived; a maximum with nothing under it starts at 0.
void supply_track_peaks (supply_state_t *s)
 Take this sample into the run's extremes, only what arrived.
void supply_reset_totals (supply_state_t *s)
 The run begins: nothing delivered, nothing counted.
void supply_count_totals (supply_state_t *s, float dt_s)
 Add dt_s of the readings to the run's charge and energy while the output is on, only what arrived; dt_s is clamped to 0 ..
void supply_sim_init (supply_sim_t *m)
 Sets up the supply model.
void supply_sim_set (supply_sim_t *m, float v, float i)
 Set points are snapped to the caps; the output's state is kept.
void supply_sim_output (supply_sim_t *m, bool on)
 Switches the model's output on or off.
void supply_sim_step (supply_sim_t *m, float dt_s, supply_state_t *out)
 Advance dt_s and write the readings, mode and set points into out, counting one more sample; peaks, totals and taken_ms are the caller's.

Detailed Description

A programmable supply: what it can be set to, what it was set to, and what it delivers.

The SUPPLY screen and the log see this and never the supply's own wire, so the same screen runs on the panel's model now and on a driver for the PD mini (a USB Power Delivery trigger controlled over a UART) once its protocol is known. Which board talks to it is not decided either; this struct is what crosses whichever boundary that turns out to be.

Pure C, no ESP-IDF (Espressif Internet-of-Things Development Framework).

SPDX-License-Identifier: MIT

Macro Definition Documentation

◆ SUPPLY_CAPS_PPS_DEFAULT

#define SUPPLY_CAPS_PPS_DEFAULT   { 3.3f, 21.0f, 0.02f, 0.5f, 5.0f, 0.05f }

The range a USB-PD PPS (Programmable Power Supply) source offers in its widest profile, 3.3 to 21V in 20mV steps and up to 5A in 50mA steps.

The panel's model uses it until a PD mini driver reports what that board and the source behind it can do; a fixed-voltage PD source offers far less.

◆ SUPPLY_SIM_LOAD_OHMS

#define SUPPLY_SIM_LOAD_OHMS   6.0f

A supply feeding a servo-like load, for the panel to run while no driver exists, and for the screenshots.

The load is a resistance with a burst on top: LOAD_OHMS at rest, and every SUPPLY_SIM_PERIOD_S a burst of SUPPLY_SIM_BURST_A for SUPPLY_SIM_BURST_S, which is what a servo moving under load draws. When the load would draw more than the limit the model holds the limit and the voltage falls with it (CC); otherwise it holds the set voltage less a 0.05ohm source resistance (CV). Deterministic, so a screenshot is the same every time.

Enumeration Type Documentation

◆ supply_mode_t

The output mode of a supply: off, constant voltage or constant current.

Enumerator
SUPPLY_MODE_OFF 

output switched off

SUPPLY_MODE_CV 

regulating its voltage

SUPPLY_MODE_CC 

holding its current limit

Function Documentation

◆ supply_caps_limited()

supply_caps_t supply_caps_limited ( const supply_caps_t * caps,
const supply_limits_t * lim )

The supply's caps narrowed by lim: the set points a screen offers.

A cap below the supply's own minimum leaves the range at that minimum.

Parameters
capsThe supply's caps.
limThe limits to narrow by.
Returns
The caps narrowed by lim.

◆ supply_count_totals()

void supply_count_totals ( supply_state_t * s,
float dt_s )

Add dt_s of the readings to the run's charge and energy while the output is on, only what arrived; dt_s is clamped to 0 ..

1s, as the bench's totals are.

Parameters
sThe supply state.
dt_sThe time since the last call, in s.

◆ supply_reset_peaks()

void supply_reset_peaks ( supply_state_t * s)

The run's extremes start again from what is shown, and only from readings that arrived; a maximum with nothing under it starts at 0.

The minimum voltage is a reading only while the output is on: an output switched off reads 0V, and a floor of 0V reads as a collapsed rail.

Parameters
sThe supply state.

◆ supply_reset_totals()

void supply_reset_totals ( supply_state_t * s)

The run begins: nothing delivered, nothing counted.

Parameters
sThe supply state.

◆ supply_sim_init()

void supply_sim_init ( supply_sim_t * m)

Sets up the supply model.

Parameters
mThe model to set up.

◆ supply_sim_output()

void supply_sim_output ( supply_sim_t * m,
bool on )

Switches the model's output on or off.

Parameters
mThe model.
ontrue to switch the output on.

◆ supply_sim_set()

void supply_sim_set ( supply_sim_t * m,
float v,
float i )

Set points are snapped to the caps; the output's state is kept.

Parameters
mThe model.
vThe set point voltage, in V.
iThe set point current, in A.

◆ supply_sim_step()

void supply_sim_step ( supply_sim_t * m,
float dt_s,
supply_state_t * out )

Advance dt_s and write the readings, mode and set points into out, counting one more sample; peaks, totals and taken_ms are the caller's.

Parameters
mThe model.
dt_sThe time step, in s.
outReceives the readings, mode and set points.

◆ supply_snap()

float supply_snap ( float value,
float min,
float max,
float step )

value clamped to min .. max and rounded to a multiple of step above min.

A step of 0 or less only clamps.

Parameters
valueThe value to snap.
minThe lowest value.
maxThe highest value.
stepThe step above min, in the value's unit; 0 or less only clamps.
Returns
The value clamped to min .. max and snapped to a step.

◆ supply_track_peaks()

void supply_track_peaks ( supply_state_t * s)

Take this sample into the run's extremes, only what arrived.

Parameters
sThe supply state.

◆ supply_trip_reset()

void supply_trip_reset ( supply_trip_t * t)

Clears the trip state.

Parameters
tThe trip state.

◆ supply_trip_step()

supply_trip_kind_t supply_trip_step ( supply_trip_t * t,
const supply_limits_t * lim,
const supply_state_t * s,
float dt_s )

Take dt_s of the readings in s into the trips.

The count starts at the first reading seen over a threshold, and each reading over it after that adds the time since the one before; one at or under it starts the count again, and one that did not arrive leaves it where it is. A trip that is off forgets its count, and so does one whose threshold changed. A trip time of 0 fires on the first reading over. Returns which trip fired, once the time over reaches lim->trip_s; nothing while the output is off. dt_s is clamped to 0 .. 1s, as the totals' steps are.

Parameters
tThe trip state.
limThe trip limits.
sThe readings.
dt_sThe time since the last call, in s.
Returns
The trip that fired.