Skip to content

File sstep_ctrl.c

FileList > src > sstep_ctrl.c

Go to the source code of this file

  • #include "sstep.h"
  • #include <stddef.h>
  • #include "sstep_internal.h"

Classes

Type Name
struct align_ctx
struct align_pub

Public Static Functions

Type Name
int check_config (const struct sstep_config * cfg)
Validate a configuration field by field and as a whole.
int check_stall (const struct sstep_config * cfg)
Validate the stall detection fields (ignored when stall_time_ms is 0).
void control (struct sstep_ctx * m)
Current control, power stage and PWM write of one fast loop period.
int derive (struct sstep_ctx * m)
Derive gains, scaling and limits from the (validated) configuration.
int derive_stall (struct sstep_ctx * m)
Derive the stall detection constants (after the other gains).
int64_t div_round (int64_t num, int64_t den)
round(num / den) for den > 0, half away from zero.
sstep_real_t gain_apply_i32 (const struct sstep_gain * g, int32_t v)
g * v for a 32-bit integer v (slow loop, 64-bit product), as a saturated signal.
bool is_active (enum sstep_state s)
Outputs are on.
bool is_moving (const struct sstep_ctx * m)
A command is executing.
bool is_pow2 (uint32_t v)
v is a power of two (v > 0).
uint8_t log2_u32 (uint32_t v)
log2 of a power of two.
bool mul_ok (uint64_t a, uint64_t b, uint64_t * out)
out = a * b; false when the product reaches 2^62.
int64_t pow2_i64 (uint32_t n)
2^n for n = 0 .
void publish (struct sstep_ctx * m, const struct sstep_setpoint * sp)
Hand a setpoint to the fast loop (slow loop context).
int ratio (uint64_t n1, uint64_t n2, uint64_t d1, uint64_t d2, struct sstep_gain * g)
g = (n1 n2) / (d1 d2) with overflow checks (configuration only).
sstep_angle_t raw_angle (const struct sstep_ctx * m, int64_t pos)
Electrical angle of a position [Q16 microsteps]: microstep = 2^30 / M.
int set_currents (struct sstep_ctx * m, uint32_t run_ma, uint32_t hold_ma)
Validate and store the run and hold currents.
int set_limits (struct sstep_ctx * m, uint32_t v_max, uint32_t accel, uint32_t decel)
Validate and store the motion limits (engineering units -> Q16 per period).
void shutdown (struct sstep_ctx * m, uint16_t faults)
Switch the outputs off and enter the fault state (any context).

Macros

Type Name
define Q16_LIMIT ((int64\_t)0x40000000)
Velocities and accelerations in Q16 per period stay below 2^30.
define RATIO_LIMIT ((uint64\_t)1U &lt;&lt; 62U)
Operand limit of the gain ratios (sstep_gain_from_ratio64).
define TWO_PI_DEN 113U
define TWO_PI_NUM 710U
2 pi = 710 / 113 (error 8.5e-8).
define VALUE_MAX 10000000U
Largest engineering value accepted in the motor and stage configuration.
define VBUS_PU_MAX [**SSTEP\_ACC\_SCALE\_MAX**](group__sstep__math.md#define-sstep_acc_scale_max)
Bus voltage limit of the fast loop: 2 pu - 2^-15.

Public Static Functions Documentation

function check_config

Validate a configuration field by field and as a whole.

static int check_config (
    const struct sstep_config * cfg
) 


function check_stall

Validate the stall detection fields (ignored when stall_time_ms is 0).

static int check_stall (
    const struct sstep_config * cfg
) 


function control

Current control, power stage and PWM write of one fast loop period.

static void control (
    struct sstep_ctx * m
) 


function derive

Derive gains, scaling and limits from the (validated) configuration.

static int derive (
    struct sstep_ctx * m
) 


function derive_stall

Derive the stall detection constants (after the other gains).

static int derive_stall (
    struct sstep_ctx * m
) 


function div_round

round(num / den) for den > 0, half away from zero.

static int64_t div_round (
    int64_t num,
    int64_t den
) 


function gain_apply_i32

g * v for a 32-bit integer v (slow loop, 64-bit product), as a saturated signal.

static sstep_real_t gain_apply_i32 (
    const struct sstep_gain * g,
    int32_t v
) 


function is_active

Outputs are on.

static bool is_active (
    enum sstep_state s
) 


function is_moving

A command is executing.

static bool is_moving (
    const struct sstep_ctx * m
) 

In hold mode the velocity is always zero: a move ends with v = 0 on the target and a stop becomes hold only at v = 0.


function is_pow2

v is a power of two (v > 0).

static bool is_pow2 (
    uint32_t v
) 


function log2_u32

log2 of a power of two.

static uint8_t log2_u32 (
    uint32_t v
) 


function mul_ok

out = a * b; false when the product reaches 2^62.

static bool mul_ok (
    uint64_t a,
    uint64_t b,
    uint64_t * out
) 


function pow2_i64

2^n for n = 0 .

static int64_t pow2_i64 (
    uint32_t n
) 

. 62, built with an unsigned shift (MISRA C:2012 Rule 10.1).


function publish

Hand a setpoint to the fast loop (slow loop context).

static void publish (
    struct sstep_ctx * m,
    const struct sstep_setpoint * sp
) 

The fast loop never reads the buffer being written: sp_pending is cleared first (single core).


function ratio

g = (n1 n2) / (d1 d2) with overflow checks (configuration only).

static int ratio (
    uint64_t n1,
    uint64_t n2,
    uint64_t d1,
    uint64_t d2,
    struct sstep_gain * g
) 

Every factor is a product of at most two validated values (< 10^7 each), so it fits.


function raw_angle

Electrical angle of a position [Q16 microsteps]: microstep = 2^30 / M.

static sstep_angle_t raw_angle (
    const struct sstep_ctx * m,
    int64_t pos
) 


function set_currents

Validate and store the run and hold currents.

static int set_currents (
    struct sstep_ctx * m,
    uint32_t run_ma,
    uint32_t hold_ma
) 


function set_limits

Validate and store the motion limits (engineering units -> Q16 per period).

static int set_limits (
    struct sstep_ctx * m,
    uint32_t v_max,
    uint32_t accel,
    uint32_t decel
) 


function shutdown

Switch the outputs off and enter the fault state (any context).

static void shutdown (
    struct sstep_ctx * m,
    uint16_t faults
) 


Macro Definition Documentation

define Q16_LIMIT

Velocities and accelerations in Q16 per period stay below 2^30.

#define Q16_LIMIT `((int64_t)0x40000000)`


define RATIO_LIMIT

Operand limit of the gain ratios (sstep_gain_from_ratio64).

#define RATIO_LIMIT `((uint64_t)1U << 62U)`


define TWO_PI_DEN

#define TWO_PI_DEN `113U`

define TWO_PI_NUM

2 pi = 710 / 113 (error 8.5e-8).

#define TWO_PI_NUM `710U`


define VALUE_MAX

Largest engineering value accepted in the motor and stage configuration.

#define VALUE_MAX `10000000U`


define VBUS_PU_MAX

Bus voltage limit of the fast loop: 2 pu - 2^-15.

#define VBUS_PU_MAX `SSTEP_ACC_SCALE_MAX`



The documentation for this class was generated from the following file src/sstep_ctrl.c