Skip to content
Ashish Jaiswal edited this page Jul 21, 2026 · 2 revisions

Motor.h

Bidirectional motor control interface for direct, user-driven motor management.

Caution: Driving motors directly bypasses the flight controller's stabilization. Test with props off.

Enumerations

motor_direction_e

Rotation direction, used by Motor_SetDir().

typedef enum motor_direction {
    CLOCK_WISE = 0,     // Clockwise rotation
    ANTICLOCK_WISE      // Counter-clockwise rotation
} motor_direction_e;

bidirectional_motor_e

Selects the motor for the Motor_* functions.

typedef enum bidirectional_motor {
    M1,    // Motor 1
    M2,    // Motor 2
    M5,    // Motor 5
    M6,    // Motor 6
    M7,    // Motor 7
    M8     // Motor 8
} bidirectional_motor_e;

Two motor groups behave differently:

  • M1, M2 - bidirectional motors. Require Motor_Init(), support Motor_SetDir(), and are driven directly by Motor_Set().
  • M5 - M8 - the regular flight motors. Motor_Init() / Motor_SetDir() have no effect on them; Motor_Set() only takes effect while the drone is disarmed (it writes the disarmed motor value), which sets usingMotorAPI.

Global Variables

  • bool usingMotorAPI - Set when the motor API is in use (the firmware checks this to release motor control to user code).

Functions

void Motor_Init(bidirectional_motor_e _motor)

Initializes a motor for direct control. Do this once at startup.

void plutoInit ( void ) {
    Motor_Init ( M1 );
    Motor_Init ( M2 );
}

void Motor_SetDir(bidirectional_motor_e motor, motor_direction_e direction)

Sets a motor's rotation direction. Set it once before driving the motor.

void onLoopStart ( void ) {
    Motor_SetDir ( M1, CLOCK_WISE );
    Motor_SetDir ( M2, ANTICLOCK_WISE );
}

void Motor_Set(bidirectional_motor_e _motor, int16_t pwmValue)

Sets a motor's speed. pwmValue is constrained to 1000 - 2000 (1000 = stopped, 2000 = full speed). Direction is controlled separately by Motor_SetDir(), not by the sign of this value. Drive motors from plutoLoop().

void plutoLoop ( void ) {
    Motor_Set ( M1, 1500 );   // ~half speed
    Motor_Set ( M2, 2000 );   // full speed
}

Usage Examples

Bidirectional motors (M1, M2)

Initialize at startup, set directions on entry, drive in the loop, stop on exit.

#include "PlutoPilot.h"

// Power-up: initialize the bidirectional motors.
void plutoInit ( void ) {
    Motor_Init ( M1 );
    Motor_Init ( M2 );
}

// Dev button pressed: set rotation directions once.
void onLoopStart ( void ) {
    Motor_SetDir ( M1, CLOCK_WISE );
    Motor_SetDir ( M2, ANTICLOCK_WISE );
}

// While active: drive the motors (1000 = stop, 2000 = full).
void plutoLoop ( void ) {
    Motor_Set ( M1, 1500 );
    Motor_Set ( M2, 1500 );
}

// Dev button released: stop the motors.
void onLoopFinish ( void ) {
    Motor_Set ( M1, 1000 );
    Motor_Set ( M2, 1000 );
}

Regular flight motors (M5 - M8)

No Motor_Init() or Motor_SetDir() is needed for these. Motor_Set() only takes effect while the drone is disarmed, so this is for props-off bench testing of the four flight motors.

#include "PlutoPilot.h"

// While active (and disarmed): spin each flight motor at low speed.
void plutoLoop ( void ) {
    Motor_Set ( M5, 1200 );
    Motor_Set ( M6, 1200 );
    Motor_Set ( M7, 1200 );
    Motor_Set ( M8, 1200 );
}

// Dev button released: stop all flight motors.
void onLoopFinish ( void ) {
    Motor_Set ( M5, 1000 );
    Motor_Set ( M6, 1000 );
    Motor_Set ( M7, 1000 );
    Motor_Set ( M8, 1000 );
}

Clone this wiki locally