当前位置:网站首页>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
边栏推荐
- 小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(3)后台功能
- Brief introduction to cmtime
- [super cloud terminal to create a leading opportunity] local computing cloud management, Intel helps digitalize Education
- leetcode7-dfs+动态规划+双指针
- Common methods of C string
- 【微信小程序】超易懂的条件渲染和列表渲染
- Cocos creator 3.0 foundation - event system
- Wechat e-book reading applet graduation project (6) opening defense ppt
- [database] must know and be able at the end of the term ----- Chapter 10 database programming
- Wechat online education video on demand learning applet graduation project (4) opening report
猜你喜欢

PLC OPC 信息模型(DI,PLCopen NodeSets)简介

51单片机一究到底输入模式

Small program completion work wechat online education video on demand learning small program graduation design (2) small program function

Wechat e-book reading applet graduation design of applet completion works (1) development outline

Intel experts share: how to program efficiently on XPU architecture? Zhiqiang Research Institute

巧用企业网盘收取报告或总结

High performance and economy: aoteng persistent memory helps mobile cloud cope with severe memory challenges

机器学习09:无监督学习

Leetcode7 DFS + dynamic programming + double pointer
![[ruoyi Vue plus] learning notes 30 - redisson (VI) bounded blocking queue (redisson source code + Lua script)](/img/56/f52d13f86764768eee190e22ba136a.png)
[ruoyi Vue plus] learning notes 30 - redisson (VI) bounded blocking queue (redisson source code + Lua script)
随机推荐
机器学习09:无监督学习
小程序毕设作品之微信电子书阅读小程序毕业设计(4)开题报告
[ruoyi Vue plus] learning notes 30 - redisson (VI) bounded blocking queue (redisson source code + Lua script)
机器学习10:集成学习
High performance and economy: aoteng persistent memory helps mobile cloud cope with severe memory challenges
By voting for the destruction of STI by Dao, seektiger is truly community driven
Academic sharing | design and development of multi staining pathological image information evaluation system based on openvino
小程序毕设作品之微信电子书阅读小程序毕业设计(7)中期检查报告
Eas (energy aware scheduling) green energy-saving scheduler
结构体通过成员变量获取主结构体地址(struct)
微信附近的人小程序怎么开(开通附近小程序的方法)
Chapter 6 performance platform godeye source code analysis - Custom expansion module
C# 构造函数(Constructors)简单讲解
Niuke 2021 training League warm-up training match interstellar love (and search Collection)
Thesis research NLP
Chapter 3 performance platform godeye source code analysis - memory module
Find the central subscript of the array
寻找数组的中心下标
Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture
Machine learning 10: Integrated Learning