yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
L-Type Motor📖
1.About motor
  • About motor
2.ROS robot control board drive
  • 0. Notes (Must read!!)
  • 1. Control motor
  • 2. Timer captures encoder data
  • 3. PID control robot car
  • 4. Kinematic Analysis-Mecanum wheel structure
  • 5. Kinematic Analysis-Ackerman structure
  • 6. Kinematic Analysis-4WD structure
3.STM32 board car tutorials
0. Notes (Must read!!)
  • Notes
1. Get started quickly
  • 0. Installation Instructions
  • 1. Version configuration
  • 2. Smart car installation video
  • 3. Precautions for use
2. Introduction to STM32 development board
  • 0. Notes (must read)
  • 1. Learn about STM32
  • 2. Introduction to STM32 development board
  • 3. Development board pin assignment
  • 4. Introduction to HAL library and LL library
3. Setting up and using the development environment
  • 1. Common development tools
  • 2. STM32CubeMX Install - Use
  • 3. STM32CubeIDE Install - Use
  • 4. Common Driver Installation
  • 5. Program download and simulation
  • 6. Engineering experience and migration
4. Car expansion tutorial
  • 1. Servo control
  • 2. Basic use of ultrasonic steering gear gimbal
  • 3. Motor control
  • 4. OLED display light sensitivity value
  • 5. Infrared sensor
  • 6. Four-way tracking module
  • 7. Infrared remote control
  • 8. Ultrasonic ranging
  • 9. PS2 controller test
  • 10. Car line patrol
  • 11. Ultrasonic follow
  • 12. Ultrasonic obstacle avoidance
  • 13. Ultrasonic obstacle avoidance (servo gimbal version)
  • 14. Infrared sensor obstacle avoidance
  • 15. Infrared remote control
  • 16. PS2 handle remote control
4. 2-channel motor drive module
For MSPM0G3507
  • 1.AT8236 drive tutorials
  • 2.TB6612 drive tutorials
For MSPM0G3519
  • 1.AT8236 drive tutorialsnew
  • 2.TB6612 drive tutorialsnew
5. 4-channel motor drive module
Introduction and Debug
  • 1.Introduction to 4-channel motor drive board
  • 2. Control command
  • 3. 520 motor introduction and usage
  • 4. PC host
IIC communication
  • For MSPM0G3507
  • For MSPM0G3519new
  • For STM32
  • For ESP32
  • For Arduino
  • For PICO2
  • For Jetson NANO
  • For RPi5
  • For RDK X5
USART communication
  • For MSPM0G3507
  • For MSPM0G3519new
  • For STM32
  • For ESP32
  • For Arduino
  • For PICO2
  • For Jetson NANO
  • For RPi5
  • For RDK X5
Download
  • 3D-Model-File
  • Code
Download ZIP

Welcome to L-Type Motor repository

1. Servo control

Servo control

Servo control1. Software-Hardware2. Brief principle1. Hardware schematic diagram2. Physical connection diagram3. Control principle3. Engineering experience1. Open the project4. Project configuration1. New project2、chip selection3、Project settings4、Pin configuration5、Clock configuration6、Project configuration7、Generate code5. Main functions1. User function2. HAL library function analysis6. Program download1. Serial port download2、ST-Link Download7. Experimental Phenomenon

This tutorial demonstrates: using the basic timer (TIM7) interrupt to simulate PWM control of the external S1 interface servo on the development board.

The first tutorial in this chapter will be more detailed than the following tutorials. The purpose is to demonstrate from the new project to the complete effect, and guide users how to use STM32CubeIDE to develop

1. Software-Hardware

  • STM32F103CubeIDE

  • STM32 Robot Development Board

  • 180° servo

  • Type-C data cable or ST-Link

    Download programs or simulate the development board

2. Brief principle

1. Hardware schematic diagram

image-20231014113119959

2. Physical connection diagram

image-20231111153337864

Note: Refer to the color of the servo wire for servo wiring. This experiment requires connecting 5V and GND jumper caps

Steering gearDevelopment board
VCC(red)5V
SIG(yellow)S1/S2/S3/S4
GND(brown)GND

3. Control principle

By changing the duty cycle of the PWM signal, the angle of rotation of the servo is controlled

  • PWM(Pulse Width Modulation)

PWM is the abbreviation of pulse width modulation, which is a technology that controls the level by adjusting the pulse width of the signal.

Period: The duration of a complete PWM waveform;

Duty cycle: the ratio of high level duration to cycle time;

Frequency: The reciprocal of the period is called frequency, which is the number of PWM cycles generated per second;

  • PWM servo

Set the period of the PWM control signal to 20ms, which is a frequency of 50Hz; the high level time of the pulse determines the angle of rotation of the servo.

High level pulse width corresponding to common angles

Steering gear (180°)High level pulse width (us)
0°500
45°1000
90°1500
135°2000
180°2500
  • Basic timer

Using the TIM7 timer interrupt function on the STM32F103ZET6 development board

PWM servo (schematic name)Control pinFunction
S1PB0Analog PWM output control S1 servo
S2PB1Analog PWM output control S2 servo
S3PD14Analog PWM output control S3 servo
S4PD15Analog PWM output control S4 servo
xxxxxxxxxx
For basic knowledge of basic timers, please refer to [3. Basic Development Board Tutorial: Basic Timers]

Timing formula

T(s)=(ARR+1)∗(PSC+1)TIM_CLK(Hz)

Timing time of this project: 10us

T(s)=(ARR+1)∗(PSC+1)TIM_CLK(Hz)=(9+1)∗(71+1)72000000(Hz)=0.00001s=10us

3. Engineering experience

Use the project files we provide to directly experience the corresponding functions of the development board.

Later tutorials do not provide this content to avoid duplication of content. You can go to [2. Development environment construction and use: engineering experience and transplantation] to view the operation

1. Open the project

  • Project file location

    Project file path: Under the [Project Source Code] folder of the Chapter 4 tutorial

image-20231128164212903

  • Open project file

Copy the project file to the directory of English path, use STM32CubeIDE to open the project file, open the project file and select the .project file

image-20231019112028635

xxxxxxxxxx
Tip: You can create a new STM32 folder on the desktop and select the STM32 folder as the workspace when opening the project file.

4. Project configuration

This tutorial will completely demonstrate the configuration process. Later, the content of new project, chip selection, project settings, pin settings of SYS, RCC configuration, clock configuration and project configuration will be omitted. Any changes will be stated in the tutorial. .

1. New project

  • Select workspace

    The path of a new project or the path of an existing project file: the path cannot contain Chinese characters

image-20231019113256941

  • New Project

image-20230925203837627

2、chip selection

image-20230925204025253

3、Project settings

  • project name

image-20231019113232378

  • Firmware version

image-20230925205412520

image-20230925205504890

4、Pin configuration

  • SYS

image-20231019114035388

  • RCC

image-20231019114107414

  • TIM

image-20231019111012841

image-20231019114356700

  • NVIC

image-20231019114451469

  • GPIO

image-20231019111448715

xxxxxxxxxx
Please refer to the figure above for specific configuration options. It is recommended to add tags. STM32CubeIDE will generate corresponding macro definitions.

5、Clock configuration

image-20230926101233831

xxxxxxxxxx
Refer to the options covered by the red arrows

6、Project configuration

  • Project

    do not need to change

image-20231019121526743

  • Code Generator

image-20231019121820781

  • Advanced Settings

image-20231019121843188

7、Generate code

  • Click on the "pinion" icon

image-20231011102854602

xxxxxxxxxx
Click here to save or the Ctrl+C shortcut key to generate code.
  • Edit code

    User code must be located between USER CODE BEGIN and USER CODE END.

image-20231019122700707

The above is the peripheral configuration and initialization code generation.

5. Main functions

It mainly introduces the functional code written by the user. For detailed code, you can open the project file provided by us yourself and enter the Bsp folder to view the source code.

1. User function

Function: PwmServo_Handle

Function prototypevoid PwmServo_Handle(void)
Function descriptionCalled in timer interrupt, simulate output PWM signal
Input parametersNone
Output parametersNone
NotesThis function is called by the timer update interrupt callback function HAL_TIM_PeriodElapsedCallback

Function: PwmServo_Angle_To_Pulse

Function prototypeuint16_t PwmServo_Angle_To_Pulse(uint8_t angle)
Function descriptionConvert the specified angle into pulse width value
Input parametersangle: rotation angle
Output parametersPulse width value

Function: PwmServo_Init

Function prototypevoid PwmServo_Init(void)
Function descriptionSet the initial angle of each servo to 90°
Input parametersNone
Output parametersNone

Function: PwmServo_Set_Angle

Function prototypevoid PwmServo_Set_Angle(uint8_t index, uint8_t angle)
Function descriptionSet the initial angle of a single servo
Input parameter 1index: servo serial number
Input parameter 2angle: rotation angle
Output parametersNone

Function: PwmServo_Set_Angle_All

Function prototypevoid PwmServo_Set_Angle_All
(uint8_t angle_s1, uint8_t angle_s2, uint8_t angle_s3, uint8_t angle_s4)
Function descriptionSet all servo rotation angles
Input parameter 1S1 servo rotation angle
Input parameter 2S2 servo rotation angle
Input parameter 3S3 servo rotation angle
Input parameter 4S4 servo rotation angle
Output parametersNone

2. HAL library function analysis

Function: HAL_GPIO_Init

Function prototypevoid HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
Function descriptionInitialize GPIO pin parameters
Input parameter 1GPIOx: Set the GPIO port, x takes the value A, B, C, D, E, F, G
Input parameter 2GPIO_Init: GPIO initialization structure
Output parametersNone

Function: HAL_GPIO_WritePin

Function prototypevoid HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
Function DescriptionSet/Clear the specified pin
Input parameter 1GPIOx: Set the GPIO port, x takes the value A, B, C, D, E, F, G
Input parameter 2GPIO_Pin: Set the GPIO pin, x value is 0-15
Input parameter 3PinState: Bit_RESET: clear the data port bit (low level); Bit_SET: set the data port bit (high level)
Output parametersNone

Function: HAL_TIM_Base_Init

Function prototypeHAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
Function descriptionInitialize timer time base unit
Input parametershtim: timer handle address
Output parametersHAL status value: HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT
NotesThis function will call the MCU underlying initialization function HAL_TIM_Base_MspInit to complete the settings of pins, clocks and interrupts

Function: HAL_TIM_Base_MspInit

Function prototypevoid HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim);
Function descriptionInitialize the peripheral clock, GPIO and NVIC of the timer
Input parametershtim: timer handle address
Output parametersNone

Function: HAL_TIM_Base_MspDeInit

Function prototypevoid HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
Function descriptionCancel the initialization of timer peripheral clock, GPIO and NVIC
Input parametershtim: timer handle address
Output parametersNone

HAL_TIM_IRQHandler: timer interrupt service function

Function prototypevoid HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
Function descriptionTimer interrupt service function
Input parametershtim: timer handle address
Output parametersNone
NotesInternally, this function needs to first determine the interrupt type and clear the corresponding interrupt flag bit, and finally call the callback function

HAL_TIM_PeriodElapsedCallback: Timer update interrupt callback function

Function prototypevoid HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
Function descriptionTimer update interrupt callback function
Input parametershtim: timer handle address
Output parametersNone
NotesThis function is called by HAL_TIM_IRQHandler, and specific processing tasks can be written internally

6. Program download

1. Serial port download

  • Generate .hex file: Left-click the project → select "Properties"

image-20231019151805108

  • Check the corresponding option

image-20231019151944969

image-20231019152053805

xxxxxxxxxx
hex file location: under the Debug folder of the project file
  • Program Download

    Use the Type-C data cable to connect the development board and the computer. For more detailed burning process, please refer to [2. Development environment construction and use: program download and simulation]

image-20231201121939503

xxxxxxxxxx
Pay attention to the contents selected in the red box, which must be consistent

2、ST-Link Download

  • Debug Settings: ST-Link → SWD

image-20231019153213132

image-20231019154039009

  • Program Download: Click the Debug icon

image-20231019174450537

7. Experimental Phenomenon

After downloading the program successfully, press the RESET button of the development board and observe the development board phenomenon!

xxxxxxxxxx
For program download, please refer to [2. Development environment construction and use: program download and simulation]

Phenomenon:

S1 interface servo: cyclic rotation from 0° → 45° → 90° → 135° → 180° → 135° → 90° → 45° → 0°.

The provided project only controls the S1 interface servo. You can modify the interface function parameters by yourself to control other servos.
For experimental phenomena, please see [Servo Control_Experimental Phenomenon.mp4]
Engineering experience and migration
Basic use of ultrasonic steering gear gimbal

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文