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

A simulated ESC in its stick menu. More...

#include "esc_sim.h"
#include <string.h>
#include "esc_stick.h"

Enumerations

enum  seg_t {
  SEG_NONE = 0 , SEG_BEEP , SEG_GAP , SEG_PAUSE ,
  SEG_WAIT
}
 Segment of the sound the simulated ESC is making. More...

Functions

void esc_sim_defaults (esc_sim_cfg_t *c)
 Fill a simulator config with its default settings.
void esc_sim_init (esc_sim_t *s, const esc_profile_t *p, const esc_sim_cfg_t *c)
 Start a simulated ESC on a profile.
uint8_t esc_sim_stored (const esc_sim_t *s, uint8_t item)
 Value stored for an item.
static esc_throttle_t classify (float pct)
 Classify a throttle percentage as a position.
static esc_throttle_t listen_pos (const esc_sim_t *s)
 Where the stick rests while the menu sounds: the profile's listen position, else where this power-up entered.
static bool enters_from (const esc_profile_t *p, esc_throttle_t pos)
 Whether a power-up at pos enters the menu: the profile's entry, or the position any value is programmed from.
static esc_throttle_t value_entry (const esc_profile_t *p, uint8_t item, uint8_t value)
 The position the value numbered value of item item is programmed from.
static bool two_stage (const esc_profile_t *p)
 Whether a profile has a two-stage menu.
static void add_sorted (esc_sim_t *s, uint8_t v)
 Add a number to the loop, in ascending order.
static void add_extra (esc_sim_t *s)
 Add the extra groups past the highest number.
static void build_loop (esc_sim_t *s)
 The loop a mode sounds: the items, one item's values, or every value.
static uint32_t pulse_ms (const esc_sim_t *s)
 Length of the pulse being sounded.
static void start_beep (esc_sim_t *s, uint32_t at)
 Start one pulse of a group.
static void start_group (esc_sim_t *s, uint32_t at, uint8_t n, bool menu, bool tone)
 A group of n beeps, longs first, or one long tone when tone.
static void sound_next (esc_sim_t *s, uint32_t at)
 Sound the next group of the loop.
static void begin_menu (esc_sim_t *s, uint32_t at)
 Start the menu sounds.
static void segment_over (esc_sim_t *s, uint32_t at)
 A segment ended at at: what follows it.
static bool in_window (const esc_sim_t *s, uint32_t now, uint32_t profile_ms)
 Whether a move now answers the group that ended last: inside the window the configuration or the profile gives, or with neither, before the next group begins.
unsigned esc_sim_store_moves (const esc_profile_t *p, uint8_t item, uint8_t value, esc_throttle_t *out, size_t cap)
 List the moves that store a selected value.
static void store (esc_sim_t *s, uint32_t now, uint8_t item, uint8_t value)
 Store a selected value.
static void moved (esc_sim_t *s, uint32_t now)
 React to a throttle move in the current mode.
int32_t esc_sim_step (esc_sim_t *s, uint32_t now_ms, bool powered, float throttle_pct)
 Advance the simulated ESC to a time and return its supply current.
void esc_sim_hand (esc_sim_t *s, uint32_t now_ms)
 Record the person's action at the ESC.

Detailed Description

A simulated ESC in its stick menu.

See esc_sim.h.

The sound is a list of segments – a beep, the gap after it, the pause after a group, a wait – each with the time it ends, walked forward to the time asked for. A throttle move is judged after the walk, against the group that ended last.

SPDX-License-Identifier: MIT

Enumeration Type Documentation

◆ seg_t

enum seg_t

Segment of the sound the simulated ESC is making.

Enumerator
SEG_NONE 

silent until something happens

SEG_BEEP 

one pulse of a group

SEG_GAP 

silence between two pulses

SEG_PAUSE 

after a group

SEG_WAIT 

the entry's silences

Function Documentation

◆ add_extra()

void add_extra ( esc_sim_t * s)
static

Add the extra groups past the highest number.

Adds the configured number of groups after the highest one in the loop, stopping at 255.

Parameters
sThe simulator.

◆ add_sorted()

void add_sorted ( esc_sim_t * s,
uint8_t v )
static

Add a number to the loop, in ascending order.

Adds nothing when the number is already in the loop or the loop is full.

Parameters
sThe simulator.
vThe group number to add.

◆ begin_menu()

void begin_menu ( esc_sim_t * s,
uint32_t at )
static

Start the menu sounds.

Parameters
sThe simulator.
atTime the menu starts, ms.

◆ build_loop()

void build_loop ( esc_sim_t * s)
static

The loop a mode sounds: the items, one item's values, or every value.

Parameters
sThe simulator.

◆ classify()

esc_throttle_t classify ( float pct)
static

Classify a throttle percentage as a position.

Below 15 percent is MIN, 35 to 65 percent is MID and above 85 percent is MAX. Anything else, or a negative value, is ESC_THR_NONE.

Parameters
pctThe throttle, percent of travel; negative for no signal.
Returns
The position.

◆ enters_from()

bool enters_from ( const esc_profile_t * p,
esc_throttle_t pos )
static

Whether a power-up at pos enters the menu: the profile's entry, or the position any value is programmed from.

Parameters
pThe profile.
posThe stick position at power-up.
Returns
true when the power-up enters the menu.

◆ esc_sim_defaults()

void esc_sim_defaults ( esc_sim_cfg_t * c)

Fill a simulator config with its default settings.

Parameters
cReceives the defaults.

◆ esc_sim_hand()

void esc_sim_hand ( esc_sim_t * s,
uint32_t now_ms )

Record the person's action at the ESC.

The person's action at the ESC – the jumper pulled, the button pressed – at now_ms. With wait_hand, a menu waiting for it starts; one done before the entry ends lets the menu start when it does.

Parameters
sThe simulator.
now_msTime of the action, ms.

◆ esc_sim_init()

void esc_sim_init ( esc_sim_t * s,
const esc_profile_t * p,
const esc_sim_cfg_t * c )

Start a simulated ESC on a profile.

Parameters
sThe simulator to set up.
pThe profile the simulated ESC follows.
cThe timing and fault settings.

◆ esc_sim_step()

int32_t esc_sim_step ( esc_sim_t * s,
uint32_t now_ms,
bool powered,
float throttle_pct )

Advance the simulated ESC to a time and return its supply current.

Advance to now_ms with the supply powered and the signal at throttle_pct, negative for no signal. Returns the current it draws, mA. Call it often enough for its pulses: in the host suite every few milliseconds, on the panel at the supply's 50ms.

Parameters
sThe simulator.
now_msTime of this step, ms.
poweredWhether the supply is on.
throttle_pctThrottle signal, percent; negative for no signal.
Returns
The current drawn, mA.

◆ esc_sim_store_moves()

unsigned esc_sim_store_moves ( const esc_profile_t * p,
uint8_t item,
uint8_t value,
esc_throttle_t * out,
size_t cap )

List the moves that store a selected value.

The moves that store value value of the item numbered item once it is selected, into out: the profile's store move, then the value's own after_select, of the first item and value of those numbers – items that apply to different models may share both. At most cap are written; returns how many.

Parameters
pThe profile.
itemItem number.
valueValue number.
outReceives the moves.
capCapacity of out, in moves.
Returns
The number of moves written.

◆ esc_sim_stored()

uint8_t esc_sim_stored ( const esc_sim_t * s,
uint8_t item )

Value stored for an item.

The value stored for the item numbered item, 0 when none was.

Parameters
sThe simulator.
itemItem number.
Returns
The stored value, or 0 when none was stored.

◆ in_window()

bool in_window ( const esc_sim_t * s,
uint32_t now,
uint32_t profile_ms )
static

Whether a move now answers the group that ended last: inside the window the configuration or the profile gives, or with neither, before the next group begins.

Parameters
sThe simulator.
nowTime of the move, ms.
profile_msThe profile's window, ms; 0 for none.
Returns
true when the move answers the group.

◆ listen_pos()

esc_throttle_t listen_pos ( const esc_sim_t * s)
static

Where the stick rests while the menu sounds: the profile's listen position, else where this power-up entered.

Parameters
sThe simulator.
Returns
The listen position.

◆ moved()

void moved ( esc_sim_t * s,
uint32_t now )
static

React to a throttle move in the current mode.

Parameters
sThe simulator.
nowTime of the move, ms.

◆ pulse_ms()

uint32_t pulse_ms ( const esc_sim_t * s)
static

Length of the pulse being sounded.

Parameters
sThe simulator.
Returns
The long beep length, ms, for a long pulse, else the short beep.

◆ segment_over()

void segment_over ( esc_sim_t * s,
uint32_t at )
static

A segment ended at at: what follows it.

Parameters
sThe simulator.
atTime the segment ended, ms.

◆ sound_next()

void sound_next ( esc_sim_t * s,
uint32_t at )
static

Sound the next group of the loop.

Parameters
sThe simulator.
atTime the group starts, ms.

◆ start_beep()

void start_beep ( esc_sim_t * s,
uint32_t at )
static

Start one pulse of a group.

Parameters
sThe simulator.
atTime the pulse starts, ms.

◆ start_group()

void start_group ( esc_sim_t * s,
uint32_t at,
uint8_t n,
bool menu,
bool tone )
static

A group of n beeps, longs first, or one long tone when tone.

Parameters
sThe simulator.
atTime the group starts, ms.
nNumber the group sounds.
menutrue when the group is a menu entry.
tonetrue for one long tone.

◆ store()

void store ( esc_sim_t * s,
uint32_t now,
uint8_t item,
uint8_t value )
static

Store a selected value.

Starts the store moves when the value has any; otherwise stores the value at once and starts the stored tone.

Parameters
sThe simulator.
nowTime of the store, ms.
itemItem number.
valueValue number.

◆ two_stage()

bool two_stage ( const esc_profile_t * p)
static

Whether a profile has a two-stage menu.

Parameters
pThe profile.
Returns
true when a value select move stores the value.

◆ value_entry()

esc_throttle_t value_entry ( const esc_profile_t * p,
uint8_t item,
uint8_t value )
static

The position the value numbered value of item item is programmed from.

Parameters
pThe profile.
itemItem number.
valueValue number.
Returns
The entry position of the value.