This library provides advanced functionality to control DC motors using PWM, position control, and stall detection. It depends on the MotorDriver library and the hardware encoder from the SEEED Xiao BLE Arduino package. This module is tested on the SEEED Xiao BLE board with the FreeRTOS framework.
- Download the library from the GitHub repository: PDL_Motor_Controller.
- Unzip the library and place the
MotorController
folder in your Arduinolibraries
directory. - Restart the Arduino IDE.
This library depends on the following:
- MotorDriver library. Please ensure it is installed in your Arduino libraries directory.
- Hardware encoder provided by the SEEED Xiao BLE Arduino package.
- Download the MotorDriver library from here.
- Unzip the library and place the
MotorDriver
folder in your Arduinolibraries
directory. - Restart the Arduino IDE.
- Open the Arduino IDE.
- Go to
File
>Preferences
. - In the "Additional Boards Manager URLs" field, add the following URL:
https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json
. - Go to
Tools
>Board
>Boards Manager
. - Search for
Seeed nRF52 Boards
and install the package. - Restart the Arduino IDE.
For more information, refer to the official SEEED Xiao BLE documentation: Seeed Studio XIAO BLE.
#include <PDL_Motor_Controller.h>
MotorDriver motor;
HwRotaryEncoder encoder;
MotorController motorController(motor, encoder);
Different motor drivers have different pin definitions. Ensure you set the appropriate pins for the motor driver you are using. Refer to the example sketches in the examples
folder for specific pin setups.
motorController.setPwm(control_signal); // control_signal value between -1 and 1
MotorController(MotorDriver &motor, HwRotaryEncoder &encoder)
: Constructor for the MotorController class.~MotorController()
: Destructor for the MotorController class.void setPositionLimits(int32_t max_pos, int32_t min_pos)
: Set the position limits using encoder count values.void setTargetPosition(int32_t target_position)
: Set the target position.void setPositionTolerance(uint32_t position_tolerance)
: Set the position tolerance.void setStallThreshold(uint32_t stall_threshold_ms)
: Set the stall threshold in milliseconds.int32_t getCurrentPosition() const
: Get the current position.void setCurrentPosition(int32_t current_position)
: Set the current position.int32_t getCurrentSpeed() const
: Get the current speed.bool isMotorStalled() const
: Check if the motor is stalled.bool isTargetReached() const
: Check if the target position is reached.void setPwm(float control_signal)
: Set the PWM control signal.void setGain(float Kp, float Ki = 0.0, float Kd = 0.0)
: Set the PID gains.void setLoopDelay(uint32_t delay_ms)
: Set the loop delay in milliseconds.void setDebug(uint8_t debug)
: Enable or disable debug mode.void printDebug() const
: Print debug information.void start(uint8_t priority = 1)
: Start the motor controller task with a given priority.void pause()
: Pause the motor controller task.void setOnMotorStall(MotorEventCallback callback)
: Set the callback for motor stall events.void setOnTargetReach(MotorEventCallback callback)
: Set the callback for target reach events.
This library is licensed under the MIT License. See the LICENSE
file for more details.