Tải bản đầy đủ (.pdf) (17 trang)

Using stm32f4 generate pwm to control dc motor with pid algorithm

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (4.21 MB, 17 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY - VIETNAM NATIONAL UNIVERSITY

---FACULTY OF ELECTRICAL AND ELECTRONICS ENGINEERING

<i>EE4447: EMBEDDED CONTROL SYSTEM</i>

<b>EXERCISE REPORT #3</b>

<b>Using STM32F4 Generate PWM to Control DC Motorwith PID Algorithm</b>

<i><b>Instructor: PhD Nguyen Vinh Hao Class: TT01</b></i>

<i><b>Group: 9Group’s Member: </b></i>

</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">

<b>II.SOFTWARE INTRODUCTION FOR MCU STM32 and PC...7</b>

<b>III.SETUP FIRMWARE FOR MCU STM32F407...8</b>

3.1. Setup Timer 1 (PWM)...8

3.2. Setup Timer 2 (Encoder)...8

3.3. Setup UART...9

3.4. PID Algorithm...10

<b>3.5. Push PWM pules for motor...11</b>

3.6. Function control motor from PID parameter...11

<b>IV.DESIGN GUI IN VISUAL STUDIO...12</b>

4.1. Interface...12

4.2. Code for controlling the received data...13

4.3. GUI when received data...15

<b>V.EVALUATION PROJECT...15</b>

1

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

General about PWM

Pulse-width modulation (PWM) is a modulation process or technique used in most communication systems for encoding the amplitude of a signal right into a pulse width or duration of another signal, usually a carrier signal, for transmission. Although PWM is also used in communications, its main purpose is actually to control the power that is supplied to various types of electrical devices, most especially to inertial loads such as AC/DC motors.

<b>Duty Cycle of the PWM</b>

When the signal is high, we call this “on time”. To describe the amount of “on time”, we use the concept of the duty cycle. Duty cycle is measured in percentage. The percentage duty cycle specifically describes the percentage of time a digital signal is on over an interval or period of time. This period is the inverse of the frequency of the waveform.If a digital signal spends half of the time on and the other half off, we would say the digital signal has a duty cycle of 50% and resembles an ideal square wave. If the percentage is higher than 50%, the digital signal spends more time in the high state than the low state and vice versa if the duty cycle is less than 50%. See the figure below for full understanding:

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

I.HARDWARE INTRODUCTION

1.1. STM32F407VGT

The STM32F4DISCOVERY Discovery kit allows users to easily develop applications with the STM32F407VG high-performance microcontroller with the Arm® Cortex®-M4 32-bit core. It includes everything required either for beginners or experienced users to get started quickly. Based on STM32F407VG, it includes an ST-LINK/V2-A embedded debug tool, one STMEMS digital accelerometer, one digital microphone, one audio DAC with integrated class D speaker driver, LEDs, push-buttons and a USB OTG Micro-AB connector. Specialized addon boards can be connected by means of the extension header connectors. The STM32F4DISCOVERY Discovery kit comes with the STM32 comprehensive free software libraries and examples available with the STM32CubeF4 MCU Package.

3

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

1.2. L298N Driver

The L298N is a dual H-Bridge motor driver which allows speed and direction control of two DC motors at the same time. The module can drive DC motors that have voltages between 5 and 35V, with a peak current up to 2A.

This depends on the voltage used at the motors VCC. The module have an onboard 5V regulator which is either enabled or disabled using a jumper. If the motor supply voltage is up to 12V we can enable the 5V regulator and the 5V pin can be used as output, for example for powering our Arduino board. But if the motor voltage is greater than 12V we must disconnect the jumper because those voltages will cause damage to the onboard 5V regulator. In this case the 5V pin will be used as input as we need connect it to a 5V power supply in order the IC to work properly.

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

1.3. Encoder DC Servo JGB37-520 333RPM

JGB37-520 DC Geared Motor DC Servo Motor is integrated with a two-channel A, B Encoder to help read and accurately adjust the position and rotation of the motor in applications requiring high accuracy: PID control, Automatic robot action,....

JGB37-520 DC Geared Motor DC Servo Motor has a metal structure for durability and high stability, used in robot models, vehicles, boats,..., the motor's reducer has many The gear ratio makes it easy to choose between traction and speed (the greater the traction, the slower the speed and vice versa), the motor uses high-quality materials (pure copper wire core, 407 steel foil, strong magnets,...) for strength and durability superior to cheap ones on the market today (using wire cores, weak magnetic magnets).

Gear ratio 30:1 (motor turns 30 revolutions main shaft, reducer rotates 1 revolution). No-load current: 120mA

Maximum withstand current under load: 1A No-load speed: 333RPM (333 rpm)

Maximum endurance speed under load: 250RPM (250 rpm) Traction Moment Rated: 3.5KG. CM

Torque Maximum Moment: 5KG.CM Gearbox length L: 22mm

Number of Encoder pulses per channel per spindle revolution: 11 x 30 = 330 pulses. Pinout of JBG37-520 motor:

M1 - Red: Power cord for the motor.

GND - Black: Encoder power supply wire, 0VDC. C1/A - Gold: A . pulse return channel

C2/B - Green: Pulse return channel OFF

5

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

M2 - White: Motor power cord

1.4. Synchronization Hardware

Method of triggering the PWM signal and directing

Pulse-width modulation (PWM) is used for controlling the amplitude of digital signals in order to control devices and applications requiring power or electricity. This is an advanced tutorial on PWM generation. We will learn how to generate a variable PWM signal with STM32F407 Series. Variable PWM signal is used for controlling the speed of DC motors/Fans. It is also used in AC Light Dimmer. PWM solar chargers also work on variable PWM signal. Servo Motors angle and direction is also controlled with the PWM signal.

Hardware setup

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

II.SOFTWARE INTRODUCTION FOR MCU STM32 and PC

Software used for MCU STM32F4 NoIDE + CompilerNote

1<b>STM2CubeMX</b><i><b>Version 6.7.0</b></i>

Package for STM32F4<i><b>Verion 1.24.0</b></i>

2<b>MDK-ARM Professional</b><i><b>Version 5.27.1.0</b></i>

Package STM32F4xx_DFP<i><b>Version 2.14.0</b></i>

Software used for GUI in PC NoIDE + CompilerNote

1<i><b>Microsoft Visual Studio EnterpriseVersion 2022</b></i>

7

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

III. SETUP FIRMWARE FOR MCU STM32F407

III.1. Setup Timer 1 (PWM)

//khai bao ham su dung

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

III.4. PID Algorithm

Proportional-Integral-Derivative (PID) control is the most common control algorithm used in industry and has been universally accepted in industrial control. The popularity of PID controllers can be attributed partly to their robust performance in a wide range of operating conditions and partly to their functional simplicity, which allows engineers to operate them in a simple, straightforward manner. As the name suggests, PID algorithm consists of three basic coefficients; proportional, integral and derivative which are varied to get

Kp_part = Kp * ( error - pre_error ); Ki_part = 0.5 * Ki * Ts *( error + pre_error ); Kd_part = Kd / Ts * ( error - 2*pre_error + pre2_error); udk = pre_udk + Kp_part + Ki_part + Kd_part ;

</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">

III.5. Push PWM pules for motor

void chay(float udk)

III.6. Function control motor from PID parameter

// Dieu khien dong co

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

IV. DESIGN GUI IN VISUAL STUDIO 2

GraphPane myPane = zed.GraphPane;

myPane.Title.Text = "PID Control Chart Current Value

3

RollingPointPairList list = new RollingPointPairList(100000); RollingPointPairList list1 = new

//Initially, a curve is added with no data points (list is empty)

// Scale for x axis

5

<sup>double intcurrent;</sup><sub> </sub><sub>double</sub><sub> intudk;</sub>

double.TryParse(current, out intcurrent);

// Design 1 curve

13

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

Scale xScale = zed.GraphPane.XAxis.Scale; (time > xScale.Max - xScale.MajorStep)if

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

Thread.Sleep(20);

serialPort1.Write("gt_Kd" + Kd.Text + "|");

IV.3. GUI when received data

V. EVALUATION PROJECT

</div>

×