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

Single-servo and two-servo models of a control surface. More...

#include "servo_sim.h"
#include <string.h>

Functions

void servo_sim_defaults (servo_sim_cfg_t *cfg)
 A standard servo on a well-set-up surface, binding at 1150 and 1880 us.
void servo_sim_init (servo_sim_t *s, const servo_sim_cfg_t *cfg)
 Start the single-servo model at the middle of its stops.
static float noise (servo_sim_t *s)
 Deterministic, so a failing case reproduces.
float servo_sim_step (servo_sim_t *s, uint16_t cmd_us, uint32_t now_ms)
 Advance the model to now_ms with the servo commanded to cmd_us, and return the current its sensor would report.
void servo_pair_defaults (servo_pair_cfg_t *cfg, uint16_t centre_us)
 Defaults for two servos on one surface.
void servo_pair_init (servo_pair_t *p, const servo_pair_cfg_t *cfg)
 Start the two-servo model with both servos at the centre.
float servo_pair_disagreement (const servo_pair_t *p, uint16_t cmd_a_us, uint16_t cmd_b_us)
 How far apart the two hold the surface, in microseconds. Hardware cannot report this; the search is blind to it and a test may read it.
static bool approach (float *pos, float target, float reach)
 Move pos toward target by at most reach; true if it arrived.
float servo_pair_step (servo_pair_t *p, uint16_t cmd_a_us, uint16_t cmd_b_us, uint32_t now_ms)
 Advance both servos to now_ms with the commands given, and return the total current the pair draws.

Detailed Description

Single-servo and two-servo models of a control surface.

SPDX-License-Identifier: MIT

Function Documentation

◆ approach()

bool approach ( float * pos,
float target,
float reach )
static

Move pos toward target by at most reach; true if it arrived.

Parameters
posPosition to move, us. Updated in place.
targetTarget position, us.
reachLargest move allowed, us.
Returns
true when the position equals the target after the move.

◆ noise()

float noise ( servo_sim_t * s)
static

Deterministic, so a failing case reproduces.

Parameters
sModel state. Advances the noise generator.
Returns
noise, A, within plus or minus half of noise_a.

◆ servo_pair_defaults()

void servo_pair_defaults ( servo_pair_cfg_t * cfg,
uint16_t centre_us )

Defaults for two servos on one surface.

Parameters
cfgConfiguration to fill.
centre_usCentre pulse width, us.

◆ servo_pair_disagreement()

float servo_pair_disagreement ( const servo_pair_t * p,
uint16_t cmd_a_us,
uint16_t cmd_b_us )

How far apart the two hold the surface, in microseconds. Hardware cannot report this; the search is blind to it and a test may read it.

Parameters
pModel state.
cmd_a_usCommanded pulse width of servo A, us.
cmd_b_usCommanded pulse width of servo B, us.
Returns
distance between the two servos on the surface, us; 0 when p is NULL.

◆ servo_pair_init()

void servo_pair_init ( servo_pair_t * p,
const servo_pair_cfg_t * cfg )

Start the two-servo model with both servos at the centre.

Parameters
pModel state to initialise.
cfgConfiguration to copy.

◆ servo_pair_step()

float servo_pair_step ( servo_pair_t * p,
uint16_t cmd_a_us,
uint16_t cmd_b_us,
uint32_t now_ms )

Advance both servos to now_ms with the commands given, and return the total current the pair draws.

Both servos take time to arrive, which is why this takes a clock: a reading taken before they have arrived is a reading of two servos in transit, not of two servos disagreeing. The settle time separates the two.

Parameters
pModel state.
cmd_a_usCommanded pulse width of servo A, us.
cmd_b_usCommanded pulse width of servo B, us.
now_msCurrent time, ms.
Returns
total current the pair draws, A; 0 when p is NULL.

◆ servo_sim_defaults()

void servo_sim_defaults ( servo_sim_cfg_t * cfg)

A standard servo on a well-set-up surface, binding at 1150 and 1880 us.

Parameters
cfgConfiguration to fill. Does nothing when NULL.

◆ servo_sim_init()

void servo_sim_init ( servo_sim_t * s,
const servo_sim_cfg_t * cfg )

Start the single-servo model at the middle of its stops.

Parameters
sModel state to initialise. Does nothing when NULL.
cfgConfiguration to copy. Does nothing when NULL.

◆ servo_sim_step()

float servo_sim_step ( servo_sim_t * s,
uint16_t cmd_us,
uint32_t now_ms )

Advance the model to now_ms with the servo commanded to cmd_us, and return the current its sensor would report.

Parameters
sModel state.
cmd_usCommanded pulse width, us.
now_msCurrent time, ms.
Returns
current the sensor reports, A; 0 when s is NULL.