Skip to content

File syn_dc_motor.c

FileList > motor > syn_dc_motor.c

Go to the source code of this file

DC motor controller implementation.

  • #include "../util/syn_assert.h"
  • #include "../util/syn_ramp.h"
  • #include "syn_dc_motor.h"
  • #include <string.h>

Public Functions

Type Name
void syn_dc_motor_brake (SYN_DCMotor * motor)
Brake the motor (both pins high, if driver supports it).
void syn_dc_motor_coast (SYN_DCMotor * motor)
Stop the motor (coast — both pins low).
void syn_dc_motor_init (SYN_DCMotor * motor, SYN_GPIO_Pin pin_a, SYN_GPIO_Pin pin_b, SYN_DCMotorMode mode)
Initialize DC motor controller.
SYN_MotorOutput syn_dc_motor_output (SYN_DCMotor * motor)
Create a SYN_MotorOutput interface for this DC motor.
void syn_dc_motor_ramp_to (SYN_DCMotor * motor, int32_t speed, uint16_t duration)
Ramp to a target speed over a duration.
void syn_dc_motor_set_duty_callback (SYN_DCMotor * motor, void(*)(SYN_GPIO_Pin, uint16_t, void *) cb, void * ctx)
Set the PWM duty callback.
void syn_dc_motor_set_duty_max (SYN_DCMotor * motor, int32_t duty_max)
Set the maximum duty cycle value.
void syn_dc_motor_set_speed (SYN_DCMotor * motor, int32_t speed)
Set motor speed immediately.
void syn_dc_motor_update (SYN_DCMotor * motor)
Update motor ramp. Call periodically.

Public Static Functions

Type Name
void apply_speed (SYN_DCMotor * m)
Apply the current speed to the motor outputs.
int32_t clamp_speed (const SYN_DCMotor * motor, int32_t speed)
Clamp speed to the valid range [-duty_max, +duty_max].
void dc_output_brake (void * ctx)
Brake adapter for SYN_MotorOutput .
void dc_output_coast (void * ctx)
Coast adapter for SYN_MotorOutput .
void dc_output_set (void * ctx, int32_t output)
Set output adapter for SYN_MotorOutput .

Public Functions Documentation

function syn_dc_motor_brake

Brake the motor (both pins high, if driver supports it).

void syn_dc_motor_brake (
    SYN_DCMotor * motor
) 

Parameters:

  • motor Motor instance.

function syn_dc_motor_coast

Stop the motor (coast — both pins low).

void syn_dc_motor_coast (
    SYN_DCMotor * motor
) 

Parameters:

  • motor Motor instance.

function syn_dc_motor_init

Initialize DC motor controller.

void syn_dc_motor_init (
    SYN_DCMotor * motor,
    SYN_GPIO_Pin pin_a,
    SYN_GPIO_Pin pin_b,
    SYN_DCMotorMode mode
) 

Sets duty_max to SYN_DC_MOTOR_DUTY_MAX_DEFAULT (1000).

Parameters:

  • motor Motor instance.
  • pin_a PWM pin (or IN_A).
  • pin_b Direction pin (or IN_B).
  • mode Control mode.

function syn_dc_motor_output

Create a SYN_MotorOutput interface for this DC motor.

SYN_MotorOutput syn_dc_motor_output (
    SYN_DCMotor * motor
) 

Returns a motor output vtable that maps set_output() to syn_dc_motor_set_speed(), coast() to syn_dc_motor_coast(), etc. The output range maps directly to [-duty_max, +duty_max].

Parameters:

  • motor DC motor instance (must outlive the returned output).

Returns:

Motor output interface.


function syn_dc_motor_ramp_to

Ramp to a target speed over a duration.

void syn_dc_motor_ramp_to (
    SYN_DCMotor * motor,
    int32_t speed,
    uint16_t duration
) 

Parameters:

  • motor Motor.
  • speed Target speed [-duty_max, +duty_max].
  • duration Ramp duration in milliseconds.

function syn_dc_motor_set_duty_callback

Set the PWM duty callback.

void syn_dc_motor_set_duty_callback (
    SYN_DCMotor * motor,
    void(*)( SYN_GPIO_Pin , uint16_t, void *) cb,
    void * ctx
) 

The callback is called with the pin and a duty value in [0, duty_max] whenever the motor speed changes.

Parameters:

  • motor Motor instance.
  • cb Duty callback.
  • ctx User context.

function syn_dc_motor_set_duty_max

Set the maximum duty cycle value.

void syn_dc_motor_set_duty_max (
    SYN_DCMotor * motor,
    int32_t duty_max
) 

Call after init to change from the default (1000). Resets speed to 0.

Parameters:

  • motor Motor instance.
  • duty_max Maximum duty value (e.g., 255, 1000, 4095, 65535).

function syn_dc_motor_set_speed

Set motor speed immediately.

void syn_dc_motor_set_speed (
    SYN_DCMotor * motor,
    int32_t speed
) 

Parameters:

  • motor DC motor handle.
  • speed Speed in range [-duty_max, +duty_max]. Positive = forward, negative = reverse, 0 = stop. Clamped to ±duty_max.

function syn_dc_motor_update

Update motor ramp. Call periodically.

void syn_dc_motor_update (
    SYN_DCMotor * motor
) 

Parameters:

  • motor Motor instance.

Public Static Functions Documentation

function apply_speed

Apply the current speed to the motor outputs.

static void apply_speed (
    SYN_DCMotor * m
) 

Parameters:

  • m DC motor instance.

function clamp_speed

Clamp speed to the valid range [-duty_max, +duty_max].

static int32_t clamp_speed (
    const SYN_DCMotor * motor,
    int32_t speed
) 

Parameters:

  • motor Motor instance (for duty_max).
  • speed Raw speed value.

Returns:

Clamped speed.


function dc_output_brake

Brake adapter for SYN_MotorOutput .

static void dc_output_brake (
    void * ctx
) 

Parameters:

  • ctx DC motor instance (SYN_DCMotor*).

function dc_output_coast

Coast adapter for SYN_MotorOutput .

static void dc_output_coast (
    void * ctx
) 

Parameters:

  • ctx DC motor instance (SYN_DCMotor*).

function dc_output_set

Set output adapter for SYN_MotorOutput .

static void dc_output_set (
    void * ctx,
    int32_t output
) 

Parameters:

  • ctx DC motor instance (SYN_DCMotor*).
  • output Signed output level.


The documentation for this class was generated from the following file src/syntropic/motor/syn_dc_motor.c