lkmotor_mf4005_driver is an out-of-tree Zephyr RTOS driver for the MF4005 brushless PMSM/BLDC motor from LK Motor, communicating over CAN bus.
It provides a clean and extensible interface to command torque, velocity, and position, using the motorโs internal controller.
This driver is useful for robotics, automation, mechatronics, and any embedded system that requires deterministic motor control within Zephyr.
-
Full CAN protocol support for LK Motor MF4005
-
Closed-loop control modes:
- Torque control
- Velocity control
- Position control
-
Zephyr-native integration
- Kconfig options
- Devicetree bindings
- Standard Zephyr driver API structure
-
Shell-based control interface (optional)
- Send torque/velocity/position commands at runtime
-
Sample applications included
-
Modular design โ easy integration with sensors, feedback loops, and higher-level robotics frameworks
lkmotor_mf4005_driver/
โ
โโโ drivers/ # Source code for the MF4005 driver
โโโ dts/ # Devicetree bindings
โโโ include/zephyr/drivers/ # Public driver headers
โโโ samples/
โ โโโ mf4005_shell/ # Example: control motor via shell commands
โ
โโโ Kconfig # Driver configuration options
โโโ CMakeLists.txt # Build integration (out-of-tree module)
โโโ west.yml # Manifest for west-based integration
โโโ README.md # <โโ You are here
โโโ LICENSE # Apache-2.0
west init -m https://github.com/uLipe/lkmotor_mf4005_driver
west updateremotes:
- name: uLipe
url-base: https://github.com/uLipe
projects:
- name: lkmotor_mf4005_driver
remote: uLipe
revision: main
path: modules/lib/lkmotor_mf4005_driverThen run:
west updateCONFIG_LKMOTOR_MF4005_DRIVER=y
Enable optional shell interface:
CONFIG_MF4005_DRIVER_SHELL=y
Example DTS snippet:
&can0 {
status = "okay";
bus-speed = <1000000>;
mf4005: motor@1 {
compatible = "lkmotor,mf4005";
reg = <1>; // Motor CAN ID
can = <&can0>;
status = "okay";
};
};
This exposes a Zephyr device:
const struct device *motor = DEVICE_DT_GET(DT_NODELABEL(mf4005));west build -b <your_board> path/to/app
west flashIf shell support is enabled, run:
mf4005 help
#include <zephyr/drivers/mf4005.h>
const struct device *motor = DEVICE_DT_GET_ONE(lkmotor_mf4005);
if (!device_is_ready(motor)) {
printk("MF4005 motor not ready!\n");
}mf4005_set_torque(motor, 0.3f); // 30% torquemf4005_set_velocity(motor, 120.0f); // 120 deg/s or RPM-equivalent (depends on motor spec)mf4005_set_position(motor, 90.0f); // Move to 90 degreesstruct mf4005_status st;
mf4005_get_status(motor, &st);
printk("Pos: %d\n", st.position);
printk("Vel: %d\n", st.velocity);
printk("Fault: %d\n", st.error_flags);If CONFIG_MF4005_DRIVER_SHELL=y:
mf4005 torque 0.2
mf4005 velocity 100
mf4005 position 45
mf4005 status
This is extremely useful for:
- tuning
- verifying wiring
- validating CAN bus timing
- debugging motion profiles
Included sample:
samples/mf4005_shell/
Demonstrates:
- Devicetree motor declaration
- Initialization flow
- Shell command handling
- Basic control execution
Use this as a template for real projects.
This driver follows Zephyrโs standard driver architecture:
Defines:
- CAN bus handle
- Motor ID
- Driver instance
Functions exposed in include/zephyr/drivers/mf4005.h
Split into:
struct mf4005_configstruct mf4005_data
Responsible for:
- Formatting and encoding motor commands
- Parsing returned frames
- Tracking state
- Abstraction of torque/velocity/position commands
Most operations are typically done inside:
- main thread
- motion control tasks
- interrupt callbacks (CAN RX)
Allows integration with:
- estimators
- control loops
- ROS 2 nodes
- sensor fusion
- trajectory planners
- MF4005 brushless motor (CAN interface)
- CAN transceiver compatible with your board
- ESP32, STM32, NRF, or any Zephyr-supported MCU with CAN controller
- Stable power supply for the motor
- Proper bus termination (120ฮฉ)
- Multi-motor support on shared CAN bus
- Better telemetry (temperature, torque feedback, faults)
- Trajectory generator integration
- Stronger configuration through Kconfig
- Optional ROS 2 interface layer
- More advanced diagnostics via shell
Pull requests, bug reports, and suggestions are welcome! Feel free to:
- Open issues
- Submit PRs
- Share test findings
- Request new features
Licensed under the Apache-2.0 License.
See LICENSE for full details.