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

ĐIỀU KHIỂN VÀ GIÁM SÁT TỐC ĐỘ ĐỘNG CƠ DC SỬ DỤNG MCU STM32 VỚI C#

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 (1.83 MB, 23 trang )

TRƯỜNG ĐẠI HỌC BÁCH KHOA TP. HỒ CHÍ MINH

KHOA ĐIỆN – ĐIỆN TỬ
BỘ MÔN ĐIỀU KHIỂN TỰ ĐỘNG

TIỂU LUẬN
ĐIỀU KHIỂN VÀ GIÁM SÁT TỐC ĐỘ ĐỘNG CƠ DC SỬ
DỤNG MCU STM32 VỚI C#
MÔN HỌC
EE3017 - ĐO LƯỜNG VÀ ĐIỀU KHIỂN BẰNG MÁY TÍNH

NHĨM SINH VIÊN:
NGƠ ĐĂNG HIỀN – 1670782
NGUYỄN THẾ NGỌC - 1770548
NGÀNH KỸ THUẬT ĐIỀU KHIỂN & TỰ ĐỘNG HĨA

GIẢNG VIÊN HƯỚNG DẪN
TS. NGUYỄN TRỌNG TÀI

TP. HỒ CHÍ MINH, 2019


TRƯỜNG ĐẠI HỌC BÁCH KHOA TP. HỒ CHÍ MINH

CỘNG HỊA XÃ HỘI CHỦ NGHĨA VIỆT NAM

KHOA ĐIỆN – ĐIỆN TỬ

Độc lập - Tự do - Hạnh phúc

BỘ MÔN: ĐIỀU KHIỂN TỰ ĐỘNG



TP. HCM, ngày tháng 09 năm 2019

NHẬN XÉT ĐÁNH GIÁ TIỂU LUẬN
Tên tiểu luận:
ĐIỀU KHIỂN VÀ GIÁM SÁT TỐC ĐỘ ĐỘNG CƠ DC SỬ DỤNG MCU STM32 VỚI C#
Nhóm Sinh viên thực hiện:

Cán bộ hướng dẫn:

NGÔ ĐĂNG HIỀN

1670782

NGUYỄN THẾ NGỌC

1770548

Đánh giá tiểu luận:
Về nội dung tiểu luận:

Về tính ứng dụng:

Về thái độ làm việc của sinh viên:

Cho điểm từng sinh viên:
Ngô Đăng Hiền:
Nguyễn Thế Ngọc

TS. NGUYỄN TRỌNG TÀI



MỤC LỤC

Contents
MỤC LỤC .......................................................................................................................................................................... 3
DANH MỤC HÌNH VẼ .................................................................................................................................................. 4
DANH MỤC BẢNG ........................................................................................................................................................ 4
DANH MỤC TỪ VIẾT TẮT ......................................................................................................................................... 4
Chương 1. GIỚI TIỆU TỔNG QUAN VỀ TIỂU LUẬN ........................................................................................ 1
1.1.

Giới thiệu về phần cứng ......................................................................................................................... 1

1.2.

Giới thiệu về phần mềm lập trình cho MCU STM32 và PC....................................................... 3

Chương 2. LẬP TRÌNH FIRMWARE TRÊN MCU STM32F407 .................................................................... 4
2.1. Lập trình thuật toán PID điều khiển động cơ..................................................................................... 4
2.1.1. Bộ PWM...................................................................................................................................................... 4
2.1.2. Đọc tốc độ qua bộ Encoder ................................................................................................................ 5
2.1.3. Giải thuật PID rời rạc ............................................................................................................................ 7
2.2. Lập trình giao tiếp với máy tính .............................................................................................................. 8
2.2.1. Giao tiếp nối tiếp RS232 ...................................................................................................................... 8
2.2.2. Giao tiếp qua chuẩn USB 2.0 .............................................................................................................. 9
2.2.3. giao tiếp qua chuẩn TCP/IP ............................................................................................................ 11
Chương 3. LẬP TRÌNH GUI TRÊN C# ................................................................................................................ 14
3.1.


Giao diện phần mềm thiết kế ............................................................................................................ 14

3.2. Khung truyền quy định giữa MCU và PC ............................................................................................ 14
3.3. Các giao thức truyền đã triển khai trên C# ........................................................................................ 15
Chương 4. KẾ HOẠCH VÀ KẾT QUẢ THỰC HIỆN ......................................................................................... 18
4.1.

Kế hoạch thực hiện ............................................................................................................................... 18

TÀI LIỆU THAM KHẢO............................................................................................................................................ 19


Môn: Đo lường và điều khiển bằng máy Chương 1. Giới thiệu tổng quan về tiểu
tính
luận

DANH MỤC HÌNH VẼ
DANH MỤC BẢNG
Table 1.1 Tổng quan về Hệ thống.......................................................................................................................... 1
Table 1. 2 Các phần mềm lập trình cho MCU STM32F4 ............................................................................... 3
Table 1. 3 Phần mềm lập trình GUI trên PC ...................................................................................................... 3
Table 3. 1 Software và Library sử dụng trong Project............................................................................... 14
Table 3. 2 Các Function truyền và nhận trên C# .......................................................................................... 14
Table 3. 3 Ý nghĩa của các vị trí trong mảng được truyền đi -txbuff[] ................................................ 15
Table 3. 4 Phần code xử lý data nhận được ................................................................................................... 15
Table 3. 5 Các công việc của Timer 2 – 5ms ................................................................................................... 15
Table 3. 6 Source code RS232, USB, TCP/IP .................................................................................................. 15

DANH MỤC TỪ VIẾT TẮT


4


Chương 1. Giới thiệu tổng quan về tiểu luận

Chương 1. GIỚI TIỆU TỔNG QUAN VỀ TIỂU LUẬN
1.1.

Giới thiệu về phần cứng

STT
Name
1
STM32F4-DIS1
2
H-Bridge HI216
3
MINERTIA MOTOR
27.0V 50W
Encoder: 400 pulse
Speed Max: 2500 rpm
STT
1
INPUT
2
OUTPUT

Encoder Lead
RED
BACK

WHITE
GREEN

STM32F407VGT6
MINERTIA MOTOR, F SERIES
UGFMED-03SRI21
AX070025A

Note
+5V
0V
CHANEL A
CHANEL B

Table 1.1 Tổng quan về Hệ thống

Sơ đồ bố trí các thiết bị

1

Note
Proe.vn
Nhatbon.vn
YASKAWA


Chương 1. Giới thiệu tổng quan về tiểu luận

Mạch cầu H sử dụng


Phương pháp kích tín hiệu PWM và điều hướng

Mơ hình thực tế sử dụng

2


Chương 1. Giới thiệu tổng quan về tiểu luận

1.2.

Giới thiệu về phần mềm lập trình cho MCU STM32 và PC

Table 1. 2 Các phần mềm lập trình cho MCU STM32F4
STT
1
2

IDE + Compiler
STM32CubeMX
Package for STM32F4
MDK-ARM Professional
Package STM32F4xx_DFP

Note
Version 5.3.0
Verion 1.24.0
Version 5.27.1.0
Version 2.14.0


Note: Toàn bộ source code lưu trong Folder: DCMotorController; với
STM32CubeMX.ioc project của STM32CubeMX và STM32CubeMX.uvprojx là
của MDK-ARM

Table 1. 3 Phần mềm lập trình GUI trên PC
STT
1

IDE + Compiler
Microsoft Visual Studio Enterprise
Visual C#

Note
Version 2015
Version 2015

Note: Toàn bộ source code lưu trong Folder WindowsFormsApplication1 với
project là: WindowsFormsApplication1.sln

3


Chương 2. Lập trình Firmware trên MCU STM32F407

Chương 2. LẬP TRÌNH FIRMWARE TRÊN MCU STM32F407
2.1. Lập trình thuật tốn PID điều khiển động cơ
2.1.1. Bộ PWM
Cấu hình PWM sử dụng Timer 12 ở tần số băm là 20Khz
Sử dụng Internal clock (84Mhz), các thơng số đặt như hình bên dưới, chân
ngõ ra PB14 (TIM12_CH1)


Timer Clock = 84Mhz
Prescaler (CNT): 4
Period (ARR): 839
Repetition: 0

➔ UE = 20 KHz
NOTE: Việc cấu hình PWM đã được CubeMX làm sẵn, User chỉ việc tạo hàm Set Duty
như bên dưới.
/* Output pwm frequency - unit: 0.1% */
void PWM0_Set_Duty(int16_t d)
{
if (d < -1000)
d = -1000;
else if (d > 1000)
d = 1000;

4


Chương 2. Lập trình Firmware trên MCU STM32F407

if (d == 0)
{
//GPIO_SetBits(GPIOD,GPIO_Pin_10);
// disable pwm
}
else if (d > 0)
{
//

HAL_GPIO_WritePin(DIR_GPIO_Port, DIR_Pin, GPIO_PIN_RESET); // dir = 1
}
else
{
HAL_GPIO_WritePin(DIR_GPIO_Port, DIR_Pin, GPIO_PIN_SET); // dir = 0
d = -d;
}
d = (TIM12->ARR + 1) * d / 1000;
TIM12->CCR1 = (uint32_t)d;

2.1.2. Đọc tốc độ qua bộ Encoder
Encoder chọn như hình bên dưới, với ARR = 65535 (0xFFFF), Encoder Mode
= TI1 and TI2 (chế độ x4)

5


Chương 2. Lập trình Firmware trên MCU STM32F407

//Note: Function để đọc (lấy mẫu): số xung đếm được trên 1 thời gian (sampe)
/* Get encoder counter 32 bit */
int32_t ENC0_GetSpeed(void)
{
static int32_t p = 0, p_pre = 0, dp = 0;
p = (int32_t)TIM1->CNT;
dp = p - p_pre; //+/- (speed)
if (dp > 32768)
dp -= 65536;
else if (dp < -32768)
dp += 65536;

p_pre = p;
return dp;
}
//Tốc độ (RPM) được tính như sau:
Speed = ENC0_GetSpeed()*60000.0/Ts/1600;
Trong đó:
+ ENC0_GetSpeed(): Số xung Encoder đọc được trong 1 khoản thời gian Ts (ms)
+ Ts: Thời gian lấy mẫu (ms)
+ 1600: Độ phân giải của Encoder (x4): 400x4 = 1600

6


Chương 2. Lập trình Firmware trên MCU STM32F407

2.1.3. Giải thuật PID rời rạc

void PID_step(void)
{
speed = (double)ENC0_GetSpeed() * 37.5/cPID.Ts;
Speed = ENC0_GetSpeed()*60000.0/Ts/1600;
e = setpoint - speed;
pPart = Kp * e;
iPart += 0.5 * Ts * Ki * e;
dPart = Kd / Ts * e - predPart;
predPart = Kd / Ts * e;
udk = pPart + iPart + dPart;
/* Saturate */
if (udk > 1000.0)
pwm = 1000.0;

else if (udk < 0.0)
pwm = 0.0;
else pwm = udk;
PWM0_Set_Duty((int)pwm);
}

7


Chương 2. Lập trình Firmware trên MCU STM32F407

2.2. Lập trình giao tiếp với máy tính
2.2.1. Giao tiếp nối tiếp RS232

char txRs232Buffer[64] = { 0 }; //tx
__IO uint8_t rxUsbBuffer[64] = {0}; //rx
/* Initialize all configured peripherals */

8


Chương 2. Lập trình Firmware trên MCU STM32F407

MX_UART4_Init();
//
HAL_UART_Receive_DMA(&huart4, (uint8_t*)rxUsbBuffer, RXBUFFERSIZE);
//
len =
sprintf((char*)txUsbBuffer,"%ld,%ld,%ld\r\n",(long)setpoint,(long)speed,(long)pwm
);

txRs232Buffer[0] = 'A';
txRs232Buffer[1] = (char)',';
for(int i = 0; i<=len;i++)
{
txRs232Buffer[i + 2] = txUsbBuffer[i];
}
HAL_UART_Transmit_DMA(&huart4,(uint8_t*)txRs232Buffer,len+2);
//

2.2.2. Giao tiếp qua chuẩn USB 2.0

9


Chương 2. Lập trình Firmware trên MCU STM32F407

main.c
/* USER CODE BEGIN PV */
__IO uint8_t rxUsbBuffer[64] = {0}; //rx
char txUsbBuffer[64] = { 0 }; //tx
/* Initialize all configured peripherals */
MX_USB_DEVICE_Init();
usb_device.c
/* USER CODE BEGIN Includes */
#include "usbd_bulk.h"
/* USER CODE END Includes */
void MX_USB_DEVICE_Init(void)
{
//if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC) != USBD_OK)
if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_BULK) != USBD_OK)

{
Error_Handler();
}
// if (USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) !=
USBD_OK)
// {
//
Error_Handler();
// }
}
usbd_bulk.c Phần truyền nhận dữ liệu từ USB
static uint8_t USBD_BULK_DataOut(USBD_HandleTypeDef* pdev, uint8_t epnum)
{
// Ham nhan giu Lieu .../
USBD_LL_PrepareReceive(pdev, BULK_EPOUT_ADDR, rxUsbBuffer, 64);
cComStatus.ready = 1;
// Nhan dc thi gui luon.
USBD_BULK_SendData(pdev, (uint8_t*)txUsbBuffer, 64);
//
return USBD_OK;

10


Chương 2. Lập trình Firmware trên MCU STM32F407

}
Thêm File usbd_bulk.c vào Project

2.2.3. giao tiếp qua chuẩn TCP/IP

Chọn RMII; Chọn Ext PHY: DP83848
Để mọi thứ mặc định (Rx Mode: Polling Mode; TX IP …. By Hardware)

Chọn địa chỉ IP Address, còn lại để mặc định

11


Chương 2. Lập trình Firmware trên MCU STM32F407

Thêm File tcp_echoserver.c vào Project

Trong File: main.c
/* Initialize all configured peripherals */
MX_LWIP_Init();
/* USER CODE BEGIN 2 */
tcp_echoserver_init();
/* USER CODE END 2 */
Trong File: tcp_echoserver.c
// Day se lam ham gui, nhan du lieu./
extern char txUsbBuffer[64];
extern uint8_t rxUsbBuffer[64];
extern Status cComStatus;
static void tcp_echoserver_send(struct tcp_pcb * tpcb, struct
tcp_echoserver_struct * es)
{
struct pbuf * ptr;
err_t wr_err = ERR_OK;
while ((wr_err == ERR_OK) &&
(es->p != NULL) &&


12


Chương 2. Lập trình Firmware trên MCU STM32F407

(es->p->len <= tcp_sndbuf(tpcb)))
{
/* get pointer on pbuf from es structure */
ptr = es->p;
//add
for (int i = 0; ilen; i++)
rxUsbBuffer[i] = *((uint8_t*)(ptr- >payload)+i);
/* enqueue data for transmission */
wr_err = tcp_write(tpcb, txUsbBuffer, 64, 1);

}
}

13


Chương 3. Lập trình GUI trên C#

Chương 3. LẬP TRÌNH GUI TRÊN C#
3.1.

Giao diện phần mềm thiết kế

Hình 3. 1 Giao diện phần mềm thiết kế

Trong đó:

Table 3. 1 Software và Library sử dụng trong Project
STT
1
2
3
4

Visual Studio C#
LibUsbDotNet Library
ZedGraph Library
Measurement Studio Shared
.NET

Name
WindowsFormsApplication1.sln
LibUsbDotNet.dll
ZedGraph.dll
NationalInstruments.UI

Version
2015
2.2.8.104
5.1.7.430
19.0.45.491
54

3.2. Khung truyền quy định giữa MCU và PC


Table 3. 2 Các Function truyền và nhận trên C#

RS232
USB

Truyền (PC to MCU) – Timer2 (5ms)

Nhận (MCU to PC)

byte[] txbuff = new byte[64];

byte[] readBuffer = new byte[64];
serialPort1.ReadLine();

serialPort1.Write(txbuff, 0, 36);
writer.Write(txbuff, 1000, out
bytesWritten);

reader.Read(readBuffer, 1000, out
bytesRead);
stream.Read(readBuffer, 0, 64);

TCP/IP stream.Write(txbuff, 0, 36);

14


Chương 3. Lập trình GUI trên C#

Table 3. 3 Ý nghĩa của các vị trí trong mảng được truyền đi -txbuff[]

STT
1
2
3
4
5
6
7
8
9
10
11
12

[Buffer]
txbuff[0]
txbuff[1]
txbuff[2]
txbuff[3-10]
txbuff[11-18]
txbuff[19-26]
txbuff[27-28]
txbuff[29]
txbuff[30]
txbuff[31-32]
txbuff[33]
txbuff[34-35]

Ý nghĩa
Start/Stop

Com = comuncation:
Ts
Double (Kp)
Double (Ki)
Double (Kd)
Amplitude (rpm)
Period (secs)
Pulse Width (% of period)
Stop time (sec)
Manual/Pulse
slide1

Note
1 = start
0:rs232, 1:usb, 2: LAN
Sample time (ms)
Hệ số Kp (PID)
Hệ số Ki (PID)
Hệ số Kd (PID)
Ampilute
Period
Pulse
TimeStop
M/Auto
Silde (0->2400)

Table 3. 4 Phần code xử lý data nhận được
STT
1
2

3
4
5

Ý nghĩa
Convert String
Split

readBuffer
string data = Encoding.Default.GetString(readBuffer);
string[] subdata = data.Split(',');
if (subdata.Length < 3) return;
string realSpeed, setpoint, udk;
setpoint = subdata[0];
realSpeed = subdata[1];
udk = subdata[2];
Draw(setpoint, realSpeed, udk);

Copy data
Vẽ đồ thị

Note: Phần nhận (from MCU) trên PC tuân theo mảng Byte được gửi lên từ MCU

Table 3. 5 Các công việc của Timer 2 – 5ms
STT
1
2
3
4
5


Note

Các cơng việc của Timer2 (5ms)

Tính tốn khối Performance and Robustness
Liên tục gửi và nhận dữ liệu từ USB
Liên tục gửi và nhận dữ liệu từ TCP/IP
Liên tục nhận dữ liệu từ MCU gửi lên qua RS232
Vẽ đồ thị đáp ứng của Setpoint, realSpeed, Udk

POT,Peak…
USB selected
TCP/IP selected
RS232 selected
Draw()

3.3. Các giao thức truyền đã triển khai trên C#

Table 3. 6 Source code RS232, USB, TCP/IP
STT
1
serialPort1

RS232

Note

string[] port = SerialPort.GetPortNames();


15

Using
System.IO.Ports
Form1_Load()


Chương 3. Lập trình GUI trên C#

cbComPort.Items.AddRange(port);
serialPort1.PortName = cbComPort.Text;
serialPort1.BaudRate = Int32.Parse(cbBaudRate.Text);
serialPort1.Open(); ckConnect = true;
serialPort1.Write(txbuff, 0, 36);
if (serialPort1.IsOpen)
{
if (serialPort1.BytesToRead > 8)
{
string data = serialPort1.ReadLine();
serialPort1.DiscardInBuffer();
string[] subdata = data.Split(',');

btConnect_Click()

timer2_Tick()

if (subdata.Length < 4) return;
if (subdata[0] == "A")
{
string realSpeed, setpoint, udk;

setpoint = subdata[1];
realSpeed = subdata[2];
udk = subdata[3];
Draw(setpoint, realSpeed, udk);
}
}
}

2

USB

Note

public static UsbDevice myUsbDevice;
UsbEndpointReader reader;
UsbEndpointWriter writer;
if (myUsbDevice == null)
{
UsbRegDeviceList allDevices = UsbDevice.AllDevices;
foreach (UsbRegistry usbRegistry in allDevices)
{
if (usbRegistry.Open(out myUsbDevice))
{
txtproductname.Text =
myUsbDevice.Info.ProductString;
txtvendorid.Text =
myUsbDevice.Info.Descriptor.VendorID.ToString();
txtproductid.Text =
myUsbDevice.Info.Descriptor.ProductID.ToString();

}
}
}
//USB - Connect
UsbDevice wholeUsbDevice = myUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// Select config #1
wholeUsbDevice.SetConfiguration(1);
// Claim interface #0.
wholeUsbDevice.ClaimInterface(0);
}
//Open usb end point reader and writer
reader =
myUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); //
open read endpoint 1.

Global

16

btConnect_Click()


Chương 3. Lập trình GUI trên C#

writer =
myUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);//
open write endpoint 1.
//Send

try
{
int bytesWritten;
writer.Write(txbuff, 1000, out bytesWritten);
}
catch (Exception err)
{
MessageBox.Show("Can Not Send Data To USB
Device\nDetails: " + err, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//Receive
try
{
int bytesRead = 0;
byte[] readBuffer = new byte[64];
reader.Read(readBuffer, 1000, out bytesRead);
string data = Encoding.Default.GetString(readBuffer);
string[] subdata = data.Split(',');
if (subdata.Length < 3) return;
string realSpeed, setpoint, udk;
setpoint = subdata[0];
realSpeed = subdata[1];
udk = subdata[2];
Draw(setpoint, realSpeed, udk);
}
catch (Exception err)
{
MessageBox.Show("Can Not Receive Data From USB
Device\nDetails: " + err, "Error",

MessageBoxButtons.OK, MessageBoxIcon.Error);
}

3

TCP/IP

timer2_Tick()

Note

TcpClient client = null;
Stream stream = null;
client = new TcpClient();
client.Connect("192.168.0.10", 7);
stream = client.GetStream();
// 2. send
stream.Write(txbuff, 0, 36);
// 3. receive
byte[] readBuffer = new byte[64];
stream.Read(readBuffer, 0, 64);
//process
string data = Encoding.Default.GetString(readBuffer);
string[] subdata = data.Split(',');
if (subdata.Length < 3) return;
string realSpeed, setpoint, udk;
setpoint = subdata[0];
realSpeed = subdata[1];
udk = subdata[2];
Draw(setpoint, realSpeed, udk);


17

Global
btConnect_Click()

timer2_Tick()


Chương 4. Kế hoạch và kết quả thực hiện

Chương 4. KẾ HOẠCH VÀ KẾT QUẢ THỰC HIỆN
4.1.

Kế hoạch thực hiện
Địa điểm thực hiện: Phòng trọ 708 Học viện Quân y, 84 Thành Thái, Q10, Tp. Hồ Chí

Minh
STT
1
2
3
4
5

Cơng việc
Mua các thiết bị cho thí nghiệm (khu vực Chợ Nhật
Tảo)
Ghép các thiết bị thành mơ hình
Lập trình Firmware cho MCU STM32

Lập trình GUI trên C#
Viết báo cáo tiểu luận

18

Phân công
Hiền + Ngọc
Hiền
Hiền
Ngọc
Hiền + Ngọc


TÀI LIỆU THAM KHẢO
[1] Nguyễn Thị Phương Hà, Lý thuyết điều khiển tự động, Nhà xuất bản
ĐHQG HCM, 2005
[2] Bộ mơn TĐH, Bài thí nghiệm 03 – Điều khiển vị trí động cơ DC dùng bộ
tự chỉnh STR – Khoa ĐĐT, HCMUT
[3] Bộ mơn TĐH, Bài thí nghiệm nhận dạng hệ thống, Khoa ĐĐT, HCMUT
[4] Carmine Noviello, Mastering STM32 (Leanpub book – PDF), 2016
[5] UM1718, STM32CubeMX for STM32 configuration and initialization C
code generation, STMicroelectronics, 2019

19



×