当前位置:网站首页>Sg90 actuator experiment based on STM32, including code (HAL Library)
Sg90 actuator experiment based on STM32, including code (HAL Library)
2022-07-19 04:24:00 【Mixed monster dragon XX】
Preface : This article is HAL library Under the SG90 The steering gear experiment , The use of steering gear is very common . How it works (PWM Adjust the ) And programming is also very simple . As the front wheel control of smart car , Direction control of unmanned ship, etc , Can't leave the figure of the steering gear , I hope you can master this simple module .( The experimental code is at the end of the article )
Experimental hardware :STM32F103C8T6;SG90 The steering gear
Hardware physical drawing :

design sketch :

Pin connection :
VCC --> 5V
GND --> GND
PWM( The signal line ) --> PA0
One 、SG90 Introduction to steering gear module
Steering gear is a kind of position ( angle ) servo Of Driver , It is suitable for those control systems that need to change and maintain the angle . In high-end remote control toys , Such as The plane 、 Submarine model , Remote control robot Has been widely used .
“ servo ”— The word comes from Greek “ slave ” It means . People want to “ Servo mechanism ” Be a handy taming tool , Act in compliance with control signals . Before the signal arrives , The rotor is stationary ; When the signal comes , The rotor turns immediately ; When the signal disappears , The rotor can stop immediately . Because of its “ servo ” performance , Hence the name —— servo system .
Classification of steering gear :
Simulate the steering gear : need Ceaselessly send out Destination PWM The signal , To rotate to the specified position . for example : I now let it spin 90 degree , I need to keep sending 90 Degree PWM The signal cannot stop until it reaches the specified position .( What we use SG90 It's a simulated steering gear )
Digital steering gear : Just give A destination PWM The signal , You can rotate to the specified position . for example : I now let it spin 90 degree , I only need to send it once 90 Degree PWM The signal , It can be rotated to 90 degree .
PWM The signal :PWM, English name Pulse Width Modulation, Short for pulse width modulation , It is by modulating the width of a series of pulses , Equivalent to the required waveform ( Including shape and amplitude ), Digitally code the analog signal level , That is to say, adjust the signal by adjusting the change of duty cycle 、 Changes in energy, etc , Duty cycle means in a cycle , The time when the signal is at high level accounts for the percentage of the whole signal period , For example, the duty cycle of square wave is 50%.
appearance :

Physical wiring :

Two 、 How the steering gear works
The control of steering gear generally requires One 20ms about (50Hz) Time base pulse , The high level part of the pulse is generally 0.5ms-2.5ms Angle control pulse part in range , The total interval is 2ms. With 180 For example, angle servo , So the corresponding control relationship is :

Relationship between angle and pulse time
##############################################################
0.5MS 1.0MS 1.5MS 2.0MS 2.5MS
0° 45° 90° 135° 180°
##############################################################Particular attention : On the market 180° The steering gear and 360° The steering gear , There's a difference between the two , Readers need to pay attention when buying .
180° Steering gear version : You can control the rotation angle 、 Angular positioning . After power on, the steering gear will automatically reset to 0°, Its angle is controlled by pulse signal with certain parameters .
360° Steering gear version : Uncontrollable angle , Only rotate clockwise 、 Counter clockwise rotation 、 stop it 、 Adjust speed . No angle positioning , Power on will not reset to 0°. Because this is 360° Arbitrarily rotating , No, 0. Its selection is controlled by pulse signals with certain parameters .
Programming ideas : When readers control the steering gear , Just use a timer to generate PWM Adjust the . use PWM Adjust the corresponding ms The number of pulses can realize the fixed angle control of the steering gear .
3、 ... and 、CubexMX To configure
1、RCC Configure external high-speed crystal oscillator ( Higher accuracy )——HSE;

2、SYS To configure :Debug Set to Serial Wire( Otherwise, the chip may lock itself );

3、TIM2 To configure : Use TIM2 Of Channel1 produce PWM The signal ( control SG90);

Meaning of data parameters :
At this point PWM Waveform frequency :72M / (719 +1)/ (1999+1) = 50Hz
Timer cycle :1/50 = 20ms
4、 Clock tree configuration :

5、 Engineering configuration

Four 、 Code
main function :
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);//*** Timer 2 initialization
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
int i=1000; // Cycle interval 1s
//* The steering gear points 0°
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,50); // Equivalent to one cycle (20ms) Yes 0.5ms High pulse
HAL_Delay(i);
//* The steering gear points 180°
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,250); // Equivalent to one cycle (20ms) Yes 2.5ms High pulse
HAL_Delay(i);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}matters needing attention :
Simulate the steering gear SG90 The use of is to make use of PWM To control the angle pointed by the steering gear .
By modifying the handle __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,value) In the parameters of the value It can control the angle of the steering gear .
Here we use SG90 Take the steering gear as an example to explain :
It is known that 0.5ms Point to 0° Location ,2.5ms Point to 180° Location .
For example, you need to point to m° It's about :
2.5-0.5=2ms --> Corresponding to 180°
value/(1999+1)*20=0.5+(m/180)× 2
5、 ... and 、 Experimental results
SG90 Steering gear experiment
6、 ... and 、 Code
link :https://pan.baidu.com/s/1lphofKe9yepXJ5XPwHkdUQ Extraction code :3doi
边栏推荐
- C # use this keyword to concatenate constructor to call method
- 图形验证码验证
- 小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(2)小程序功能
- Unity shader - "fast SSS: fast subsurface scattering"
- leetcode209. 长度最小的子数组
- Some problems after xcode11 new project
- ASP. Net1==visual studio create asp net demo
- Common methods of C string
- 牛客2021训练联盟热身训练赛Interstellar Love(并查集)
- 寻找数组的中心下标
猜你喜欢

06 Maui, WPF uses MVVM toolkit framework to build MVVM program

MAUI 框架入门学习05 MVVM数据模型理解

PAC Decade: witness HPC from CPU era to XPU Era

小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(4)开题报告

百度地图技术概述,及基本API与WebApi的应用开发

【微信小程序】超易懂的条件渲染和列表渲染

小程序毕设作品之微信电子书阅读小程序毕业设计(6)开题答辩PPT

Wechat e-book reading applet graduation design of applet completion works (2) applet function

Insert the laptop into the headset and still play it out (the personal test is valid)

High performance and economy: aoteng persistent memory helps mobile cloud cope with severe memory challenges
随机推荐
Structure gets the address of the main structure (struct) through member variables
PAC Decade: witness HPC from CPU era to XPU Era
V4L2学习资料收集
小程序毕设作品之微信电子书阅读小程序毕业设计(3)后台功能
Wechat e-book reading of applet completion works (7) Interim inspection report
Smart fan system based on STM32F103
OSPF basic optimization
Wechat e-book reading applet graduation project (8) graduation project thesis template
Cabasicanimation pause \ enterprise action
Welcome to Hensen_ Blog directory of (full site navigation)
Technical writing guide for programmers to leave work early
机器学习11:代价敏感学习
OSPF的优化和配置
06 MAUI,WPF使用 MVVM Toolkit 框架 构建 MVVM 程序
Simple explanation of C constructors
Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture
牛客2021训练联盟热身训练赛Interstellar Love(并查集)
Eas (energy aware scheduling) green energy-saving scheduler
Skillfully use enterprise network disk to collect reports or summaries
项目套价是什么意思?