banner



How To Control 3 Servos With One Pin In Fpga

Previous Tutorial Previous Tutorial Tutorial 31 Next Tutorial Next Tutorial
STM32 Servo Motor Control With PWM Tutorial – ECUAL Servo Commuter
STM32 Course Home Page 🏠

STM32 Servo Motor Control Example Code Library Tutorial With PWM

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 Tutorial MG90S

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.

How To Control Servo Motor With Microcontroller

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!

STM32 Servo Motor Control

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.

STM32 Servo Motor Control And Angle Resolution Control

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:

  • SERVO.h
  • SERVO.c
  • SERVO_cfg.h
  • SERVO_cfg.c
STM32 Servo Motor Library Code Examples

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

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

I'll discuss those configuration parameters in the next section down beneath.

Servo Motor Driver APIs

PCBgogo Ad

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.


   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.

STM32 PWM Frequency Formula - STM32 PWM Frequency 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

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

STM32 PWM Frequency Formula - STM32 PWM Frequency 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.

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.

STM32 PWM Duty Cycle Formula - STM32 PWM Duty Cycle Equation

Period_Min = ARR x (min_pulse_width / 20ms)

Period_Max = ARR x (max_pulse_width / 20ms)

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.

Adding ECUAL Driver To STM32 Project

Step7: One Last pace is to open stm32f1xx_hal_conf.h file in the include directory and uncomment the following line of code

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)

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.

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)

Servo Motor Control PWM Signal

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.

Adding ECUAL Driver To STM32 Project

Step8: One Terminal pace is to open stm32f1xx_hal_conf.h file in the include directory and uncomment the following line of lawmaking

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)

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.

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.

Adding ECUAL Driver To STM32 Project

Step7: 1 Last step is to open stm32f1xx_hal_conf.h file in the include directory and uncomment the following line of lawmaking

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)

And here is the SERVO_cfg.c configuration file.

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

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.

Servo Motor Library Code Example 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 Previous Tutorial Tutorial 31 Next Tutorial 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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel