Skip to content

File syn_dc_motor.h

FileList > motor > syn_dc_motor.h

Go to the source code of this file

DC motor controller for H-bridge drivers. More...

  • #include "../common/syn_defs.h"
  • #include "../drivers/syn_gpio.h"
  • #include "../motor/syn_motor_output.h"
  • #include "../port/syn_port_system.h"
  • #include "../util/syn_ramp.h"
  • #include <stdbool.h>

Classes

Type Name
struct SYN_DCMotor
DC motor instance — pins, speed, ramp state.

Public Types

Type Name
enum SYN_DCMotorMode
DC motor control wiring mode.

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
bool syn_dc_motor_at_target (const SYN_DCMotor * motor)
Is the ramp complete?
int32_t syn_dc_motor_get_speed (const SYN_DCMotor * motor)
Get current speed.

Macros

Type Name
define SYN_DC_MOTOR_DUTY_MAX_DEFAULT 1000
Default duty cycle range (0.1% resolution).

Detailed Description

Controls direction + speed via two GPIO pins (or one PWM + one dir pin). Supports soft start/stop ramps to limit inrush current and mechanical stress. Provides a SYN_MotorOutput interface for use with syn_motor_ctrl.

** **

static SYN_DCMotor motor;
syn_dc_motor_init(&motor, PWM_PIN, DIR_PIN, SYN_DC_MODE_PWM_DIR);
syn_dc_motor_set_speed(&motor, 750);   // 75% of duty_max (default 1000)
syn_dc_motor_set_speed(&motor, -500);  // 50% reverse

// With ramp:
syn_dc_motor_ramp_to(&motor, 1000, 500);  // ramp to full over 500ms
while (!syn_dc_motor_at_target(&motor)) {
    syn_dc_motor_update(&motor);
}

// With motor controller:
cfg.motor = syn_dc_motor_output(&motor);

Public Types Documentation

enum SYN_DCMotorMode

DC motor control wiring mode.

enum SYN_DCMotorMode {
    SYN_DC_MODE_PWM_DIR = 0,
    SYN_DC_MODE_DUAL_PWM = 1
};


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 syn_dc_motor_at_target

Is the ramp complete?

static inline bool syn_dc_motor_at_target (
    const SYN_DCMotor * motor
) 

Parameters:

  • motor Motor instance.

Returns:

true if at target speed.


function syn_dc_motor_get_speed

Get current speed.

static inline int32_t syn_dc_motor_get_speed (
    const SYN_DCMotor * motor
) 

Parameters:

  • motor Motor instance.

Returns:

Speed in range [-duty_max, +duty_max].


Macro Definition Documentation

define SYN_DC_MOTOR_DUTY_MAX_DEFAULT

Default duty cycle range (0.1% resolution).

#define SYN_DC_MOTOR_DUTY_MAX_DEFAULT `1000`



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