You are on page 1of 6

Control and Automation Laboratory-Ho Chi Minh City University of Technology

S DNG TH VIN ARDUINO SIMULINK


TRONG IU KHIN

Edited by Quoc Chi Nguyen, Ha Quang Thinh Ngo, and Phuong Tung Pham-1st rev

Control and Automation Laboratory-Ho Chi Minh City University of Technology

1. CI T TH VIN ARDUINO
Chn mc Add-Ons ti phn cng v.

Ci t gi Arduino v Arduino Uno/Nano/Mega2560. Sau khi ci t hon tt, trong giao


din workspace ca Matlab chy 3 file arduino.m , contents.m v install_arduino.m trong
th mc ArduinoIOSimulink.

Sau khi ci t Simulink Library s xut hin hai th vin Arduino IO v Simulink
support package of Arduino Hardware.

Edited by Quoc Chi Nguyen, Ha Quang Thinh Ngo, and Phuong Tung Pham-1st rev

Control and Automation Laboratory-Ho Chi Minh City University of Technology

Trong th mc ArduinoIOsimulink \pde , cn upload 1 trong 5 sketches vo Arduino


mega 2560

adio.pde : analog and digital IO & basic serial


adioe.pde : adio.pde + encoders support
adioes.pde : adioe.pde + servo support
motor_v1.pde : adioes.pde + Adafruit Motor Shield v1
motor_v2.pde : adioes.pde + Adafruit Motor Shield v2

Vi 2 sketches motor_v1.pde v motor_v2.pde cn chp 2 th vin AdafruitMotorShield


v AFMotor vo th vin ca Arduino.
Vi vic s dng cc chn IO iu khin ng c, ch cn upload file adioe.pde l
c.
2. Cc KHI TRONG TH VIN ARDUINO IO
Arduino Digital Read: c gi tr digital t pin c chn, tr v
gi tr 0 v 1.

Arduino Analog Read:


tr t 0 n 1023.

c gi tr analog t pin c chn, tr v gi

Arduino Digital Read: Xut gi tr digital (0 v 1) vo pin c


chn

Arduino Analog Read: Xut gi tr analog (0-255) vo pin c


chn

Arduino IO Setup: Dng thit lp Serial (COM) port ca


Arduino

Edited by Quoc Chi Nguyen, Ha Quang Thinh Ngo, and Phuong Tung Pham-1st rev

Control and Automation Laboratory-Ho Chi Minh City University of Technology

Real-time Pacer: Thit lp t l gia thi gian m phng


(Simulation time) v thi gian thc (Real time).

Encoder Read: Dng


n 32767.

c v tr ca encoder, tr v gi tr -32768

Encoder Reset: Khi kim tra tnh hiu u vo, nu ln hn


khng Encoder c xc lp s Reset v gi tr 0.
Ngoi ra cn cc khi ng dng iu khin cc loi ng c nh DC motor, RC
servo, Stepper motor.

3. NG DNG TH VIN ARDUINOIO TRONG IU KHIN NG C


Mc tiu: S dng th vin ArduinoIO iu khin v tr ng c bng b iu khin PID
3.1. Phn cng h thng
-

Board Arduino Mega 2560


L298 DFRobot
ng c DC
Encoder

3.2. iu khin ng c s dng b iu khin PID


3.2.1. Thit lp h thng
thit lp h thng m phng, c th s dng 2 khi Arduino IO Setup v Real-time
pacer.
3.2.2. iu khin ng c
Ta a tnh hiu vo PIN4 (M1) chn chiu quay cho ng c v a xung PWM (0255) vo PIN5 (E1) cho vic iu khin tc ng c.

Edited by Quoc Chi Nguyen, Ha Quang Thinh Ngo, and Phuong Tung Pham-1st rev

Control and Automation Laboratory-Ho Chi Minh City University of Technology

Hnh 2.2.1: S iu khin tc ng c


3.2.3. c Encoder v tnh vn tc
c Encoder th vin ArduinoIO h tr 2 khi Encoder Read
c Encoder

Hnh 2.2.2: S c v tr encoder


Gi tr ly trc tip t khi Encoder Read ch c th s dng vi thi gian m
phng ngn. Vi thi gian m phng ln, gi tr vt qu khong 32767, encoder s t
ng reset li gi tr -32768.
3.2.4. Xy dng b iu khin PID

Edited by Quoc Chi Nguyen, Ha Quang Thinh Ngo, and Phuong Tung Pham-1st rev

Control and Automation Laboratory-Ho Chi Minh City University of Technology

Edited by Quoc Chi Nguyen, Ha Quang Thinh Ngo, and Phuong Tung Pham-1st rev