Introduction: Before writing a motor control program, ask yourself first: for this motor, should I give it a "target" or "effort"? Confusing the two can lead to incorrect code. This article uses common motor types and practical code to help you clarify control semantics.
1. Control Semantics of the Motor Family
1. Servos: Listen to the "Target"
Servos (position servos) receive a PWM pulse width representing a target angle. Internally, they use a potentiometer for feedback to automatically adjust to the target. The external system only provides the target value, not the effort.
(Source: cnblogs.com)
2. Brushed DC Motors: Listen to the "Effort"
Brushed DC motors receive voltage or duty cycle, directly determining speed and direction. This is open-loop control with no automatic feedback mechanism for target position.
(Source: cnblogs.com)
3. Stepper Motors: Listen to the "Step Count"
Stepper motors receive pulse step counts to achieve precise stepping, allowing open-loop positioning. However, they are prone to step loss at high speeds or under heavy loads.
(Source: cnblogs.com)
4. Brushless DC Motors and Servos: Complex Control Loops
Integrated servo joints commonly used in modern humanoid robots integrate motors, reducers, encoders, drivers, controllers, and heat sinks into a compact unit. Brushless motors achieve smoother torque and higher efficiency through vector control (FOC).
(Source: blog.csdn.net)
2. Hands-on: STM32 Driving SG90 Servo
The following code from cnblogs.com shows how to use an STM32 timer to generate 50Hz PWM to control a servo.
c // Timer 2 Channel 4 (PA3) outputs PWM, period 20ms PSC = 72 - 1; // 72MHz / 72 = 1MHz, 1 count ≈ 1us ARR = 20000 - 1; // Count up to 20000 counts, period ≈ 20ms CCR4 = 1166; // High level ≈ 1.166ms, corresponding to -60° CCR4 = 1500; // High level ≈ 1.5ms, corresponding to 0° CCR4 = 1833; // High level ≈ 1.833ms, corresponding to +60°
Relationship formula between pulse width and angle:
pulse = 1000 + (angle + 90) * 1000 / 180
(Source: cnblogs.com)
Wiring Key Points
- Do not power the servo from the STM32's 3.3V pins; use an independent 5V power supply.
- The GND of the voltage reduction module and the STM32's GND must share a common ground.
- Connect the signal line to PA3 (TIM2_CH4).
3. Overview of Advanced Motor Control Modes
For scenarios requiring force control, common control strategies include:
- Point-to-Point (PTP) Control: Only cares about the target point, not the intermediate trajectory.
- Continuous Path (CP) Control: Requires the end effector to move smoothly along a predetermined path.
- Force/Torque Control: Uses force or torque as the feedback variable, requiring force sensors.
- Impedance Control: Takes environmental force as input, and by adjusting virtual mass, damping, and stiffness, makes the gripper exhibit specific compliance.
- Admittance Control: The opposite of impedance control. It takes motion deviation or velocity as input, calculates the desired force or position offset through an admittance function, and then executes it via the underlying position controller.
(Source: robot.m.ofweek.com, dh-robotics.com)
For example, the CubeMars AK45-36 brushless joint motor supports MIT control mode, allowing simultaneous transmission of position, velocity, and torque commands in a single CAN message. An Enable message must be sent to unlock the motor before use.
(Source: cubemars.com)
4. Common Issues and Troubleshooting
| Phenomenon | Possible Cause | Solution |
|---|---|---|
| Servo does not move, but signal is seen on oscilloscope | Insufficient power or no common ground | Check 5V power supply and common ground wire |
| Servo only buzzes to one side | Linkage stuck, stalled and heating up | Power off, loosen the linkage, re-find the center position |
| Motor shakes or lags | Poor CAN communication quality | Check termination resistors, avoid parallel routing of signal and power lines |
| MIT mode motor has no response | Enable command not sent | Send specific CAN message to unlock |
(Source: cnblogs.com, cubemars.com)
5. Summary and Next Steps
Clarifying whether a motor listens to "target" or "effort" is the first step to writing correct code. Next, you can:
- Use an oscilloscope to observe the actual PWM waveform and verify if the pulse width is accurate.
- Download the CubeMars upper computer software to experience motor enabling and control under MIT mode.
- Read the motor datasheet to confirm the control interface and driving requirements of the selected motor.
The content of this article is from public technical materials; please refer to official documents for specific parameters.