How To Control 3 Servos With One Pin In Fpga
Previous Tutorial | Tutorial 31 | Next Tutorial | |||||
STM32 Servo Motor Control With PWM Tutorial – ECUAL Servo Commuter | |||||||
STM32 Course Home Page 🏠 |
In this tutorial, we'll talk over how to control servo motors using STM32 PWM to generate the control signal for servo motors. Nosotros'll also develop all the necessary equations and code that is required to build the ECUAL servo motor commuter library. I'll show yous how I congenital it, the bachelor APIs, and how to use it. And finally, we'll do a couple of LABs or examples using the ECUAL STM32 servo motor commuter. While highlighting all the possible options and configurations in the driver's config files. Without farther ado, let's get started!
Required Components For LABs
All the instance code/LABs/projects in the class are going to exist done using those boards below.
- Nucleo32-L432KC (ARM Cortex-M4 @ 80MHz) or (eBay)
- Blue Pill STM32-F103 (ARM Cortex-M3 @ 72MHz) or (eBay)
- ST-Link v2 Debugger or (eBay)
QTY | Component Proper name | 🛒 Amazon.com | 🛒 eBay.com |
2 | BreadBoard | Amazon | eBay |
1 | LEDs Kit | Amazon Amazon | eBay |
1 | Resistors Kit | Amazon Amazon | eBay |
ane | Capacitors Kit | Amazon Amazon | eBay & eBay |
2 | Jumper Wires Pack | Amazon Amazon | eBay & eBay |
1 | 9v Battery or DC Ability Supply | Amazon Amazon Amazon | eBay |
1 | Micro USB Cable | Amazon | eBay |
i | Push Buttons | Amazon Amazon | eBay |
1 | Potentiometers | Amazon Amazon | eBay |
4 | Micro Servo Motor (Metal Gear) | Amazon | eBay |
★ Check The Total Course Complete Kit List
Some Extremely Useful Test Equipment For Troubleshooting:
- My Digital Storage Oscilloscope (DSO): Siglent SDS1104 (on Amazon.com) (on eBay)
- FeelTech DDS Office Generator: KKMoon FY6900 (on Amazon.com) (on eBay)
- Logic Analyzer (on Amazon.com) (on eBay)
Chapter Disclosure: When you click on links in this section and make a purchase, this can result in this site earning a commission. Chapter programs and affiliations include, just are not limited to, the eBay Partner Network (EPN) and Amazon.com.
Introduction To Servo Motor Control
Servo Motor Control Signal
To control the position (angle) of the servo motor's shaft, you'll have to generate a PWM bespeak that has a frequency of 50Hz and a variable duty bike. The datasheet for each stepper motor has to have the verbal specifications for the pulse width that corresponds to the minimum athwart position (0°) it's chosen Minimum_Pulse and the pulse width that corresponds to the maximum angular position (180°) and we'll call this the Maximum_Pulse.
The servo motor that I've in my LAB and volition be using today is the micro servo with metallic gear MG90s. Unfortunately, "The datasheet" constitute on the cyberspace seems to be a poorly translated Chinese paper with a little bit of information near the control signal specs and it turned out to be false. Other than that, the practical experimentation didn't only give me the exact pulse widths that correspond to (0° & 180°) but it besides proved that those little motors are not the same. A quick characterization with a DDS part generator and a DSO has given me the exact figures for the min & max pulse widths (in ms).
This picture shows what are the values I've got for a different 4 servo motors, and I preferred to label them. In a previous tutorial, I've assumed that the minimum pulse is 0.6ms and the maximum pulse is ii.4ms in order to brand everything as simple as possible and not trying to be a perfectionist about information technology. But in today'southward tutorial, I'll make a room for these values to be in our lawmaking configurations and to take effect. All in all, it's non a much of a big bargain anyway!
Servo Motor Resolution
The angular position of the servo motor is determined by the pulse width of the PWM bespeak. Therefore, the PWM point'southward resolution dictates how fine can we accommodate the servo motor's angle. The range of the bending in degrees is from 0 up to 180 will go mapped to whatever values that produces a pulse width between Minimum_pulse and Maximum_Pulse and all depends on the PWM resolution itself.
Maximizing the resolution will be one of our targets while deriving the equations to exist used in code afterwards. The STM32 PWM hardware can go upwards to sixteen-Flake @ 50Hz and we shall seek to get as shut every bit possible to that limit.
And hither is an example for clarification of the concept of resolution.
Merely imagine that we've set up the PWM hardware to generate a PWM signal with a frequency of 50Hz and the duty cycle has a 16-Bit of resolution. This means that the duty wheel has 65536 detached levels between (0% to 100%). That'due south a besides accurate control signal for such an awarding and seems more like overkill but permit's continue.
Suppose that our servo motor has a minimum pulse width of 0.6ms (for the 0° position) and a maximum pulse of 2.4ms (for the 180° position). This ways that the entire range of PWM point that covers the total motor rotation is from 3% upward to 12%. Why these numbers? well, the minimum pulse is 0.6ms in width as fourth dimension, in lodge to get the duty bicycle percentage that represents this pulse width fourth dimension, we'll divide 0.6 past 20ms which is the period of the PWM signal (which has a frequency of 50Hz, so information technology's menstruation is 20ms). This division volition requite you that the minimum pulse 0.6/xx is a iii% duty bicycle. And the maximum pulse is 12% duty bike be the same procedure.
Now, given that the unabridged PWM range (0% upwards to 100%) has a 16-Bit of resolution or 65536 discrete levels. The motor rotation range is (12% – 3% = 9%) and this 9% has (nine/100)*65536 discrete levels of control. This means that the servo motor'southward angular range (0° upwards to 180°) is mapped to nearly 5900 discrete levels. This means nosotros can control the servo motor's bending with a precision of 0.03°. Which is non true! This is obviously ridiculous due to mechanical limitations and the mode this motor is built. However, math is always true simply this tin can't be practically accomplished at least with those cheap little motors.
Power Requirements & Precautions
First of all, if y'all're using a decent servo motor that has well-documented specs, practice cheque them out and encounter the current draw and its ability consumption. By and large no servo motor tin work direct from the USB programmer circuit as it is. Except for a few tiny servo motors that may exist an exception but in most cases, you'll be in need to connect your servo motors to an external power source.
The same power source used by the microcontroller can exist used of class only delight brand sure you're decoupling the power supply correctly and majority bypass capacitors are placed virtually the ability supply and pocket-sized caps near the MCU power input pins.
There are servo motors that can rotate 360 degrees freely and there are other types similar mine that are limited to 180 degrees. There is a mechanical buffer that blocks motion at this angle. Reaching this angle tin crusade the servo motor to release some wining audio and depict a significant amount of current compared to its normal conditions. This situation needs to exist considered likewise and if it can't exist well-handled, and so it should be avoided in the showtime place. Peradventure by capping the maximum pulse width (if it's 2.4ms, merely use 2.iii or something like that).
STM32 ECUAL / Servo Motor Driver
The ECUAL Servo motor driver is congenital for STM32 microcontrollers using the hardware PWM channels in various timers. Yous'll take to configure an instance of it and used the APIs to control your motor and that'southward all. The code should be easily ported to any other STM32 microcontroller or reconfigured to utilise any timer or channel you want just equally nosotros'll see in this section. And here is a link for the course's repo, and you'll find the Servo commuter in the ECUAL directory equally usual.
Servo Motor Commuter Code Files
The servo motor driver consists of the post-obit files:
|
You'll need only to alter the configuration files. The source code for this driver is found in (SERVO.c) and to use it you'll include the header (SERVO.h). Nada in the source code needs to be changed at all unless you need to add any extra features or customize the commuter for your awarding'south needs. For today's labs, we'll simply be changing the configuration files to build some test applications.
Therefore, I'll write here the code listing for the SERVO.h & SERVO_cfg.c files.
SERVO.h File
1 2 3 4 5 6 7 8 9 10 xi 12 13 14 xv 16 17 xviii 19 20 21 22 23 24 25 26 27 28 | // The Number OF Servo Motors To Be Used In The Projection #define SERVO_NUM i typedef struct { GPIO_TypeDef * SERVO_GPIO ; uint16_t SERVO_PIN ; TIM_TypeDef * TIM_Instance ; uint32_t * TIM_CCRx ; uint32_t PWM_TIM_CH ; uint32_t TIM_CLK ; float MinPulse ; float MaxPulse ; } SERVO_CfgType ; /*-----[ Prototypes For All Functions ]-----*/ void SERVO_Init ( uint16_t au16_SERVO_Instance ) ; void SERVO_MoveTo ( uint16_t au16_SERVO_Instance , float af_Angle ) ; void SERVO_RawMove ( uint16_t au16_SERVO_Instance , uint16_t au16_Pulse ) ; uint16_t SERVO_Get_MaxPulse ( uint16_t au16_SERVO_Instance ) ; uint16_t SERVO_Get_MinPulse ( uint16_t au16_SERVO_Instance ) ; void SERVO_Sweep ( uint16_t au16_SERVO_Instance ) ; |
If you're willing to use many servo motors, just adjust that number definition (SERVO_NUM). And we'll do this in the LABs of today's tutorial.
SERVO_cfg.c File
#include "SERVO.h" const SERVO_CfgType SERVO_CfgParam [ SERVO_NUM ] = { // Servo Motor 1 Configurations { GPIOA , GPIO_PIN_0 , TIM2 , & TIM2 -> CCR1 , TIM_CHANNEL_1 , 72000000 , 0.65 , two.3 } } |
I'll discuss those configuration parameters in the next section down beneath.
Servo Motor Driver APIs
Equally you've seen in the SERVO.h file, the provided APIs does all the bones functionalities that you may need from a servo driver library. It initialized the PWM hardware & GPIO pin required to generate the 50Hz control signal.
The servo_moveto function takes the number of the servo motor you wanna control and an bending to move its shaft to that angle (from 0 up to 180).
The structure of the files and the provided APIs look very intuitive and self-explanatory to me. And the example LABs will put everything under test and you lot'll encounter how and when to use each of these functions.
Available Configurations For ECUAL Servo Driver
From the code higher up in SERVO.h & SERVO_cfg.c you lot tin encounter that at that place are a handful of parameters that you (the application programmer) can really configure for each servo motor individually. They are equally follows:
- The timer module y'all wanna use for this servo motor PWM generation (TIM1, 2, or any)
- The timer PWM Channel number that you wanna use for this servo motor (CH1, ii, three, ..)
- The GPIO port for the PWM CHx pin
- The PIN number for the PWM CHx pin
- The clock frequency (in Hz) for the timer y'all're going to use
- A pointer to the Timer CCRx register for the chosen PWM Channel
- The minimum pulse width for the servo motor as in its datasheet (0.6ms, 0.5ms, or whatever)
- The maximum pulse width for the servo motor (2.4ms, two.5ms, or whatever value)
Typical Usage Awarding Example
Here is a typical usage application for this driver in club to initialize a servo motor and move information technology from 0 to 180 back and along. It'll be something similar this shown below.
#include "ECUAL/SERVO/SERVO.h" #define SERVO_Motor1 0 void main ( ) { SERVO_Init ( SERVO_Motor1 ) ; while ( 1 ) { SERVO_MoveTo ( SERVO_Motor1 , 0 ) ; Delay_ms ( 2000 ) ; SERVO_MoveTo ( SERVO_Motor1 , 180 ) ; Delay_ms ( 2000 ) ; } } |
STM32 PWM For Servo Motor Command
In this section, I'll show you the derivation for most of the equations used in the driver's code that may seem ambiguous and highlight the goals and intentions I had while writing it in this manner.
Prescaler Value
And let'due south start with the timer Prescaler (PSC) and Automobile-Reload Register (ARR). We demand to calculate those parameters and write the number in the respective register. In gild to control the PWM's frequency and make information technology equal to 50Hz which is required by the servo motor, nosotros'll use the following equation.
Recall the word of the PWM resolution at the get-go of this tutorial. I said that it'south one of my goals to maximize the PWM resolution, hence in this equation, I've assumed the ARR value to be 65535 which is the maximum possible value that achieves the maximum possible resolution. Then plug in the timer clock frequency FCLK, the 50Hz for FPWM and solve for PSC. This calculation is done in lawmaking in the initialization office.
PSC = FCLK / 3276800
PSC_Value = ( uint32_t ) ( SERVO_CfgParam [ au16_SERVO_Instance ] . TIM_CLK / 3276800.0 ) ; |
And pay attention to the fact that it'll maximize the ARR but won't make it 65535 exactly! it'll but option for us the all-time Prescaler value that maximizes the PWM range (resolution). Therefore, we still need to observe out the value of ARR that should exist written in its respective annals.
ARR Register Value
To find out the value of ARR you'll use likewise the starting time equation
Given that FCLK is known as well as FPWM, fifty-fifty PSC is now known. After we've found the PSC that maximizes PWM resolution, at present plug all variables in the equation and solve for ARR.
ARR_Value = ( uint32_t ) ( ( SERVO_CfgParam [ au16_SERVO_Instance ] . TIM_CLK / ( l.0 * ( PSC_Value + 1.0 ) ) ) - 1.0 ) ; |
Pulse Width Min & Max Period
Concluding but not least, the servo motor'southward minimum & maximum pulse width specs given in the datasheet. Should also be converted into digital values that represent the number of ticks in the CCRx annals of the respective PWM channel to achieve those pulse widths and hence control the servo motor'due south angle.
Calculating both numbers is also disquisitional for determining the total range to which we'll be mapping the angle given by the user. When a user call the moveTo role and gives me 90 degrees. What should be the value of the CCRx register to achieve this?
Well, information technology should be in the heart of the range, as 90degree is in middle between (0 and 180). But we still don't know the number of ticks that represent information technology. Hence, it'southward so important now to find those values.
Depending on the fact that the PWM indicate's frequency is 50Hz, and so it's period is 20ms. And the min_pulse is given to us in ms likewise past the user. Let's say information technology's 0.6ms, so the PWM duty bicycle is 0.vi/20 = 3%. To achieve 3% duty bicycle, the value of the CCRx register will be calculated from this equation.
Period_Min = ARR x (min_pulse_width / 20ms)
Period_Max = ARR x (max_pulse_width / 20ms)
gs_SERVO_info [ au16_SERVO_Instance ] . Period_Min = ( uint16_t ) ( ARR_Value * ( SERVO_CfgParam [ au16_SERVO_Instance ] . MinPulse / xx.0 ) ) ; gs_SERVO_info [ au16_SERVO_Instance ] . Period_Max = ( uint16_t ) ( ARR_Value * ( SERVO_CfgParam [ au16_SERVO_Instance ] . MaxPulse / xx.0 ) ) ; |
And that's it for most of the parts that I call back would be problematic and ambiguous for the readers. If there still something non articulate enough, drop me a comment I'll be here for assist equally usual.
STM32 PWM Servo Motor Command – Basic LAB
LAB Number | 26 |
LAB Title | STM32 Servo Motor Command – Basic LAB |
- Fix a new projection as usual with organisation clock @ 72MHz
- Add the ECUAL / SERVO commuter files to our project
- configure a servo motor case in SERVO_cfg.c file
- Initialize the servo motor and command it to movement to the following angle positions in the same pattern (0, 45, ninety, 135, 180, 90, 0)
And now, let'south build this arrangement step-by-pace
Step1: Open CubeMX & Create New Project
Step2: Choose The Target MCU & Double-Click Its Name
STM32F103C8
Step3: Go To The Clock Configuration
Step4: Set The Organization Clock To Be 72MHz
Step5: Generate The Initialization Code & Open The Project In Your IDE
Step6: Add the ECUAL SERVO driver files to your project
Follow This Tutorial which shows you How To Add together Whatever ECUAL Driver To An STM32 Projection step-by-stride.
Step7: One Last pace is to open stm32f1xx_hal_conf.h file in the include directory and uncomment the following line of code
#ascertain HAL_TIM_MODULE_ENABLED |
To allow the HAL timer drivers to be included and compiled equally our servo driver is built on height of the HAL driver from STMicroelectronics. Now, we're ready to write a test application lawmaking and compile it.
Here is The Application Code For This LAB (master.c)
ane 2 iii iv five 6 7 viii 9 ten xi 12 thirteen fourteen 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include "primary.h" #include "../ECUAL/SERVO/SERVO.h" // The Servo Instance Alphabetize Must Offset From 0 #define SERVO_Motor1 0 void SystemClock_Config ( void ) ; static void MX_GPIO_Init ( void ) ; int principal ( void ) { HAL_Init ( ) ; SystemClock_Config ( ) ; MX_GPIO_Init ( ) ; SERVO_Init ( SERVO_Motor1 ) ; while ( i ) { SERVO_MoveTo ( SERVO_Motor1 , 0 ) ; HAL_Delay ( 1000 ) ; SERVO_MoveTo ( SERVO_Motor1 , 45 ) ; HAL_Delay ( 500 ) ; SERVO_MoveTo ( SERVO_Motor1 , 90 ) ; HAL_Delay ( 500 ) ; SERVO_MoveTo ( SERVO_Motor1 , 135 ) ; HAL_Delay ( 500 ) ; SERVO_MoveTo ( SERVO_Motor1 , 180 ) ; HAL_Delay ( 500 ) ; SERVO_MoveTo ( SERVO_Motor1 , xc ) ; HAL_Delay ( 1000 ) ; } } |
And here is the SERVO_cfg.c configuration file. As you can see here, I'1000 using Timer2 channel1 with a frequency of 72Mz, the servo's min pulse is 0.65ms, the max is 2.3ms, the GPIO port is A, the pivot is A0.
#include "SERVO.h" const SERVO_CfgType SERVO_CfgParam [ SERVO_NUM ] = { // Servo Motor 1 Configurations { GPIOA , GPIO_PIN_0 , TIM2 , & TIM2 -> CCR1 , TIM_CHANNEL_1 , 72000000 , 0.65 , 2.3 } } ; |
Download The STM32 Servo Motor Control Basic LAB26
The Upshot For This LAB Testing (Video)
Here is a snapshot for the Servo PWM Signal (a clean 50Hz)
Extremely Of import Notes From This LAB
First of all, the GPIO pin choice is not an arbitrary procedure. You lot only tin't configure the commuter to employ Timer2 channel 1 and configure the pivot to whatever you want. One time you've made your decision on which timer and which channel y'all want to assign to your servo motor, yous'll have to check its pivot GPIO port and number and use that in your configuration.
Another note is the servo motor alphabetize (example number) you lot're using while calling the APIs. For an awarding where is merely ane servo motor the SERVO_NUM ascertain is one and the index you lot shall be using is 0. Therefore, yous'll init this servo like this
SERVO_Init(0);
However, if more a servo motor is being used, the indexing will start also from 0 and counts upward. Therefore, in club to initialize multiple motors, you'll exercise this.
SERVO_Init(0);
SERVO_Init(1);
And and then on.
Some other note is the power source. As yous've seen in the video I'thou using a power depository financial institution and the Servo motor is continued to the +5v power pin on the Blue Pill board.
STM32 Analog Servo Motor Control + Knob LAB
LAB Number | 27 |
LAB Championship | STM32 Servo Motor Control With Knob (ADC Input) LAB |
- Set upward a new projection every bit usual with organization clock @ 72MHz
- Gear up up an ADC analog input channel (CH7 on A7 pivot)
- Add the ECUAL / SERVO driver files to our project
- configure a servo motor example in SERVO_cfg.c file
- Initialize the servo motor and read the ADC 12-Bit consequence and map it between the maximum & minimum pulse widths for the servo motor to control its athwart position
And now, let's build this system pace-by-step
Step1: Open up CubeMX & Create New Projection
Step2: Choose The Target MCU & Double-Click Its Name
STM32F103C8
Step3: Become To The Clock Configuration
Step4: Set The System Clock To Be 72MHz
Step5: In ADC1 Tab Enable CH7 Analog Input
Step6: Generate The Initialization Code & Open up The Projection In Your IDE
Step7: Add the ECUAL SERVO driver files to your project
Follow This Tutorial which shows you How To Add Any ECUAL Driver To An STM32 Projection step-by-footstep.
Step8: One Terminal pace is to open stm32f1xx_hal_conf.h file in the include directory and uncomment the following line of lawmaking
#ascertain HAL_TIM_MODULE_ENABLED |
To let the HAL timer drivers to be included and compiled equally our servo driver is built on elevation of the HAL driver from STMicroelectronics. Now, we're ready to write a test application lawmaking and compile it.
Here is The Application Lawmaking For This LAB (main.c)
1 ii 3 4 5 six 7 8 ix 10 11 12 13 fourteen 15 16 17 eighteen 19 20 21 22 23 24 25 26 27 28 29 xxx 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include "main.h" #include "../ECUAL/SERVO/SERVO.h" #define SERVO_Motor1 0 ADC_HandleTypeDef hadc1 ; void SystemClock_Config ( void ) ; static void MX_GPIO_Init ( void ) ; static void MX_ADC1_Init ( void ) ; int main ( void ) { uint16_t AD_RES = 0 ; uint16_t Min_Pulse = 0 , Max_Pulse = 0 ; float temp = 0.0 ; uint16_t Servo_Pulse = 0 ; HAL_Init ( ) ; SystemClock_Config ( ) ; MX_GPIO_Init ( ) ; MX_ADC1_Init ( ) ; SERVO_Init ( SERVO_Motor1 ) ; Min_Pulse = SERVO_Get_MinPulse ( SERVO_Motor1 ) ; Max_Pulse = SERVO_Get_MaxPulse ( SERVO_Motor1 ) ; while ( one ) { // Start ADC Conversion HAL_ADC_Start ( & hadc1 ) ; // Poll ADC1 Perihperal & TimeOut = 1mSec HAL_ADC_PollForConversion ( & hadc1 , 1 ) ; // Read The ADC Conversion Effect & Map It To PWM DutyCycle AD_RES = HAL_ADC_GetValue ( & hadc1 ) ; // Map The ADC Result To Servo Pulse Width temp = ( ( Max_Pulse - Min_Pulse ) / 4096.0 ) ; Servo_Pulse = ( uint16_t ) ( AD_RES * temp ) + Min_Pulse ; // Send The Raw Servo Pulse Width To The PWM Hardware SERVO_RawMove ( SERVO_Motor1 , Servo_Pulse ) ; HAL_Delay ( 10 ) ; } } |
And here is the SERVO_cfg.c configuration file. As you tin run across here, I'm using Timer2 channel1 with a frequency of 72Mz, the servo'southward min pulse is 0.65ms, the max is 2.3ms, the GPIO port is A, the pin is A0.
#include "SERVO.h" const SERVO_CfgType SERVO_CfgParam [ SERVO_NUM ] = { // Servo Motor one Configurations { GPIOA , GPIO_PIN_0 , TIM2 , & TIM2 -> CCR1 , TIM_CHANNEL_1 , 72000000 , 0.65 , 2.three } } ; |
Download The STM32 Servo Motor Control With Knob LAB27
The Result For This LAB Testing (Video)
STM32 Multi Servo Motors Control – LAB
LAB Number | 28 |
LAB Title | STM32 Multi Servo Motors Control LAB |
- Set up a new project as usual with system clock @ 72MHz
- Add the ECUAL / SERVO driver files to our project
- configure 4 servo motor instances in SERVO_cfg.c file
- Initialize the servo motors and command them individually to make whatever pattern you want
And now, let'south build this system step-by-step
Step1: Open CubeMX & Create New Project
Step2: Choose The Target MCU & Double-Click Its Name
STM32F103C8
Step3: Get To The Clock Configuration
Step4: Set The System Clock To Be 72MHz
Step5: Generate The Initialization Code & Open up The Project In Your IDE
Step6: Add the ECUAL SERVO commuter files to your project
Follow This Tutorial which shows you How To Add together Whatever ECUAL Commuter To An STM32 Project step-by-step.
Step7: 1 Last step is to open stm32f1xx_hal_conf.h file in the include directory and uncomment the following line of lawmaking
#define HAL_TIM_MODULE_ENABLED |
To allow the HAL timer drivers to be included and compiled as our servo commuter is congenital on acme of the HAL driver from STMicroelectronics. Now, we're ready to write a test awarding code and compile it.
Here is The Application Code For This LAB (main.c)
i 2 3 iv v 6 seven eight 9 10 xi 12 xiii xiv fifteen 16 17 eighteen 19 xx 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include "master.h" #include "../ECUAL/SERVO/SERVO.h" #ascertain SERVO_Motor1 0 #define SERVO_Motor2 one #define SERVO_Motor3 ii #ascertain SERVO_Motor4 3 void SystemClock_Config ( void ) ; static void MX_GPIO_Init ( void ) ; int main ( void ) { HAL_Init ( ) ; SystemClock_Config ( ) ; MX_GPIO_Init ( ) ; SERVO_Init ( SERVO_Motor1 ) ; SERVO_Init ( SERVO_Motor2 ) ; SERVO_Init ( SERVO_Motor3 ) ; SERVO_Init ( SERVO_Motor4 ) ; while ( one ) { SERVO_MoveTo ( SERVO_Motor1 , 0 ) ; SERVO_MoveTo ( SERVO_Motor2 , 0 ) ; SERVO_MoveTo ( SERVO_Motor3 , 0 ) ; SERVO_MoveTo ( SERVO_Motor4 , 0 ) ; HAL_Delay ( 1500 ) ; SERVO_MoveTo ( SERVO_Motor1 , 180 ) ; HAL_Delay ( 500 ) ; SERVO_MoveTo ( SERVO_Motor2 , 180 ) ; HAL_Delay ( 500 ) ; SERVO_MoveTo ( SERVO_Motor3 , 180 ) ; HAL_Delay ( 500 ) ; SERVO_MoveTo ( SERVO_Motor4 , 180 ) ; HAL_Delay ( 1500 ) ; SERVO_MoveTo ( SERVO_Motor1 , 0 ) ; SERVO_MoveTo ( SERVO_Motor2 , 0 ) ; SERVO_MoveTo ( SERVO_Motor3 , 0 ) ; SERVO_MoveTo ( SERVO_Motor4 , 0 ) ; HAL_Delay ( 1500 ) ; SERVO_MoveTo ( SERVO_Motor1 , 180 ) ; SERVO_MoveTo ( SERVO_Motor2 , 180 ) ; SERVO_MoveTo ( SERVO_Motor3 , 180 ) ; SERVO_MoveTo ( SERVO_Motor4 , 180 ) ; HAL_Delay ( 1500 ) ; } } |
And here is the SERVO_cfg.c configuration file.
one 2 3 4 5 6 7 eight 9 10 xi 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include "SERVO.h" const SERVO_CfgType SERVO_CfgParam [ SERVO_NUM ] = { // Servo Motor ane Configurations { GPIOA , GPIO_PIN_0 , TIM2 , & TIM2 -> CCR1 , TIM_CHANNEL_1 , 72000000 , 0.65 , 2.3 } , // Servo Motor 2 Configurations { GPIOA , GPIO_PIN_3 , TIM2 , & TIM2 -> CCR4 , TIM_CHANNEL_4 , 72000000 , 0.75 , two.5 } , // Servo Motor 3 Configurations { GPIOB , GPIO_PIN_0 , TIM3 , & TIM3 -> CCR3 , TIM_CHANNEL_3 , 72000000 , 0.62 , ii.four } , // Servo Motor 4 Configurations { GPIOB , GPIO_PIN_9 , TIM4 , & TIM4 -> CCR4 , TIM_CHANNEL_4 , 72000000 , 0.55 , 2.35 } } ; |
Every bit you tin can see here:
I'1000 mixing betwixt dissimilar timers and PWM channels in order to requite yous an idea of how can y'all easily configure this commuter code. But I'd recommend using one-timer and its 4 channels if information technology's possible in your application. Using a timer for servo motor control will restrict the timing in such a fashion that's user-friendly for the servo driver but not the other parts of the organisation that wanna share the same resources.
For Servo Motor1: TIM2, Ch1, pin A0
For Servo Motor2: TIM2, Ch4, pin A3
For Servo Motor3: TIM3, Ch3, pivot B0
For Servo Motor4: TIM4, Ch4, pin B9
Don't forget to change this line of lawmaking in the SERVO.h file to brand the number of servo motors iv
// The Number OF Servo Motors To Be Used In The Projection #define SERVO_NUM 4 |
Download The STM32 Multi Servo Motors Control LAB28
The Consequence For This LAB Testing (Video)
I Terminal Testing For STM32 Servo Driver
I've also washed one last test that I'd like to share with you. It was an gauge measurement for the execution fourth dimension for the function SERVO_MoveTo(). Considering we're going to build a very bones operating system in the futurity and also make some RTOS applications, it's important to know the execution time of such routines like the servo motor controller.
I've used a GPIO pivot configured in fast fashion push-pull driver output for fast edges switching on the output pin and driven it high before calling the servo motility function and bring it low when it returns. And it turned out to exist 7μs which is amazing as a offset and needs no more than optimization in the meantime. So, that's dandy! and nosotros'll move on to the next tutorial.
Did you find this helpful? If yes, please consider supporting this work and sharing these tutorials!
Stay tuned for the upcoming tutorials and don't forget to SHARE these tutorials. And consider SUPPORTING this work to go along publishing free content merely like this!
Previous Tutorial | Tutorial 31 | Next Tutorial |
How To Control 3 Servos With One Pin In Fpga,
Source: https://deepbluembedded.com/stm32-servo-motor-control-with-pwm-servo-library-examples-code/
Posted by: macedocoundeplaid1949.blogspot.com
0 Response to "How To Control 3 Servos With One Pin In Fpga"
Post a Comment