making your own daq
DESCRIPTION
This projeact deals with maki g yoyr own data acquisition card and then use it to implement digital control algo's.TRANSCRIPT
DESIGN OF USB BASED DATA ACQUISITION
SYSTEM AND IMPLEMENTATION OF DIGITAL
CONTROL ALGORITHMS
A PROJECT REPORT
Submitted by
DHARSHANA ,S. (20083213)
GOURAV SAHA (20083218)
JANAARTHANAN ,M. (20083223)
in partial fulfillment for the award of the degree
of
BACHELOR OF ENGINEERING
IN
ELECTRONICS AND INSTRUMENTATION ENGINEERING
MADRAS INSTITUTE OF TECHNOLOGY CAMPUS
ANNA UNIVERSITY : CHENNAI 600 044
APRIL 2012
DESIGN OF USB BASED DATA ACQUISITION
SYSTEM AND IMPLEMENTATION OF DIGITAL
CONTROL ALGORITHMS
A PROJECT REPORT
Submitted by
DHARSHANA ,S. (20083213)
GOURAV SAHA (20083218)
JANAARTHANAN ,M. (20083223)
in partial fulfillment for the award of the degree
of
BACHELOR OF ENGINEERING
IN
ELECTRONICS AND INSTRUMENTATION ENGINEERING
MADRAS INSTITUTE OF TECHNOLOGY CAMPUS
ANNA UNIVERSITY : CHENNAI 600 044
APRIL 2012
ANNA UNIVERSITY : CHENNAI 600 044
BONAFIDE CERTIFICATE
Certified that this project titled ‘DESIGN OF USB BASED DATA
ACQUISITION SYSTEM AND IMPLEMENTATION OF DIGITAL
CONTROL ALGORITHMS’, is the bonafide work of
DHARSHANA ,S. (20083213)
GOURAV SAHA (20083218)
JANAARTHANAN ,M. (20083223)
who carried out the project work under my supervision. Certified further, that to
the best of my knowledge the work reported herein does not form part of any
project on the basis of which a degree or award was conferred on an earlier
occasion on this or any other candidate.
Dr.K.BHOOPATHY BAGAN Dr.J.PRAKASH
Professor and Head, Associate Professor,
Department of Instrumentation Engg, Department of Instrumentation Engg,
MIT campus, MIT Campus,
Anna University, Anna University,
Chennai – 600 044 Chennai – 600 044
i
ACKNOWLEDGEMENT
We would take immense pleasure in thanking Dr. S. Thamarai Selvi,
Dean, Madras Institute of Technology, for providing us all the facilities.
We would like to express our grateful thanks to Dr. K. Bhoopathy
Bagan, Professor and Head, Department of Instrumentation Engineering,
Madras Institute of Technology, for being an inspiration to us and for his
encouragement during the course of the project.
We are indebted to Dr.J.Prakash, Associate Professor, for his excellent
guidance and continuous encouragement throughout this project work. His
friendly guidance and expert advice were invaluable throughout this work. We
wish to express our deep sense of gratitude for him.
We would also like to thank Mrs. Sabitha Ramakrishnan, Dr. N.
Pappa, and Mr.Raman for giving us valuable suggestions.
Also, we would like to thank all the staff members of the Department of
Instrumentation Engineering, who had helped us during the course of this
project work.
ii
ABSTRACT
In this project, our main objective is to design an USB data
acquisition module and program it to receive inputs from a system which is then
controlled by the conventional and advanced control algorithms implemented in
a personal computer. This project is an integration of the following modules:
Analog to digital convertor
Digital to analog convertor
Power delivering circuitry
Interfacing algorithms
Control algorithms coded in Matlab
Online control of a process using the algorithms in PC
The process that is selected for control is a first order RC process which is
realized using analog circuit components. The following are the algorithms that
have been implemented:
System identification using Linear Least Square Algorithm
Proportional- Integral control
Auto-tuning Proportional- Integral control
Model Reference Adaptive Control
Synthesis method of controller design
iii
LIST OF FIGURES
FIG.
NO
NAME PAGE
NO.
1.1 BASIC BLOCK DIAGRAM OF THE PROJECT 5
2.1 PIN CONFIGURATION OF LM79XX SERIES 6
2.2 PIN CONFIGURATION OF LM78XX SERIES 6
2.3 TYPICAL CONNECTION FOR POWER SUPPLY CIRCUIT 7
2.4 PIN CONFIGURATION OF ADC 0804 8
2.5 TYPICAL CONNECTIONS BETWEEN ADC 0804 AND
MICROCONTROLLER
9
2.6 PIN CONFIGURATION OF DAC 0808 9
2.7 TYPICAL CONNECTION FOR DAC 0808 10
2.8 PIN CONFIGURATION OF ATMega328
MICROCONTROLLER
11
2.9 PIN CONFIGURATION OF CD4051 ANALOG
MULTIPLEXER
14
2.10 PIN CONFIGURATION OF MAX 232 15
2.11 TYPICAL CONNECTION FOR SERIAL LEVEL
CONVERTOR CIRCUIT
16
2.12 PROLIFIC USB TO SERIAL CONVERTER 17
2.13 ARDUINO UNO 18
2.14 INTEGRATED HARDWARE 19
4.1 BLOCK DIAGRAM OF MRAC SYSTEM 46
5.1 GRAPH FOR SYSTEM IDENTIFICATION ALGORITHM 49
5.2 SERVO RESPONSE OF FIRST ORDER SYSTEM WITH PI
CONTROLLER
50
5.3 SERVO RESPONSE OF FIRST ORDER SYSTEM WITH
AUTOTUNED PI CONTROLLER
51
iv
5.4 SERVO RESPONSE OF FIRST ORDER SYSTEM WITH
MODEL REFERNCE ADAPTIVE CONTROLLER
52
5.5 SERVO RESPONSE OF FIRST ORDER SYSTEM WITH
SYNTHESISED CONTROLLER
53
1
TABLE OF CONTENTS
CHAPTER NO. TITLE PAGE NO.
ABSTRACT ii
LIST OF FIGURES iii
1. INTRODUCTION
1.1 DATA ACQUISITION SYSTEM 3
1.2 WHY USB? 4
1.3 DIGITAL/DISCRETE CONTROL SYSTEMS 5
2. DAQ HARDWARE
2.1 HARDWARE USED
2.1.1 VOLTAGE REGULATOR 6
(LM79xx & LM78xx series)
2.1.2 ADC0804 8
2.1.3 DAC0808 9
2.1.4 ATMega328 MICROCONTROLLER 11
2.1.5 CD4051 ANALOG MULTIPLEXER 14
2.1.6 MAX232 15
2.1.7 PROLIFIC USB TO SERIAL CONVERTER 17
2.1.8 ARDUINO UNO 18
2.1.9 INTEGRATED HARDWARE 19
3. SOFTWARE
3.1 INTERFACING WITH C
3.1.1 MICROCONTROLLER CODE 20
3.1.2 PC CODE (WRITTEN IN C) 26
2
3.2 INTERFACING WITH MATLAB
3.2.1 MICRCONTROLLER CODE 29
3.2.2 PC CODE (WRITTEN IN MATLAB) 35
4. DIGITAL CONTROL ALGORITHMS
4.1 SYSTEM IDENTIFICATION: LINEAR LEAST 38
SQUARE ALGORITHM
4.2 PI CONTROLLER 41
4.3 AUTOTUNED PI CONTROLLER 42
4.4 CONTROLLER DESIGNED USING SYNTHESIS 44
METHOD
4.5 MODEL REFRENCE ADAPTIVE CONTROLLER 46
5. RESULTS AND DISCUSSION
5.1 SERVO RESPONSE OF FIRST ORDER SYSTEM FOR:
5.1.1 SYSTEM IDENTIFICATION: LINEAR 48
LEAST SQUARE ALGORITHM
5.1.2 PI CONTROLLER 49
5.1.3 AUTOTUNED PI CONTROLLER 49
5.1.4 CONTROLLER DESIGNED USING 50
SYNTHESIS METHOD
5.1.5 MODEL REFRENCE ADAPTIVE 51
CONTROLLER
6. CONCLUSION 52
7. REFERENCES 53
3
CHAPTER 1
INTRODUCTION
1.1 DATA ACQUISITION SYSTEM
Data acquisition is the process of sampling signals that measure real world
physical conditions and converting the resulting samples into digital numeric values
that can be manipulated by a computer. Data acquisition systems (abbreviated with the
acronym DAS or DAQ) typically convert analog waveforms into digital values for
processing. DAQ systems also employ various signal conditioning techniques to
adequately modify acquired electrical signals into voltage that can then be digitized
using an Analog-to-digital converter (ADC).
DAQ hardware is what usually interfaces between the signal and a PC. It could
be in the form of modules that can be connected to the computer's ports
(parallel, serial, USB, etc.) or cards connected to slots (S-100 bus, AppleBus,
ISA, MCA, PCI, PCI-E, etc.) in the motherboard. The fixed connection with the PC
allows for comfortable compilation and debugging. Not all DAQ hardware has to run
permanently connected to a PC, for example intelligent stand-alone loggers and
oscilloscopes, which can be operated from a PC, yet they can operate completely
independent of the PC. In this project, USB port is used for communication with the
PC.
DAQ software is needed in order for the DAQ hardware to work with a PC. The
device driver performs low-level register writes and reads on the hardware. Data
acquisition applications are controlled by software programs developed using various
general purpose programming languages such as BASIC , C, Fortran , Java , Lisp,
Pascal.
4
1.2 WHY USB?
Universal Serial Bus (USB) is an industry standard developed in the mid-1990s
that defines the cables, connectors and communications protocols used in a bus for
connection, communication and power supply between computers and electronic
devices.
USB was designed to standardize the connection of computer peripherals, such
as keyboards, pointing devices, digital cameras, printers, portable media players, disk
drives and network adapters to personal computers, both, to communicate and to
supply electric power. USB has effectively replaced a variety of earlier interfaces,
such as serial and parallel ports, as well as separate power chargers for portable
devices. As of 2008, approximately 6 billion USB ports and interfaces are currently in
the global marketplace, and about 2 billion were being sold each year.
The popularity of USB is mainly due to its ease of device detection that occurs
automatically once the device is plugged-in. It is fondly referred to as a “ Hot spot
port ” since it enables easy plug-in and plug- out of portable computer accessories.
Unlike the other device ports, the USB port does not require explicit and intensive
programming to detect each device. Finally, owing to the growing speed technology
of the USB port, it tops the other methods of interfacing a portable device with PC.
Hence USB port is chosen as the communication means for the Data Acquisition
System designed as a part of this project.
1.3. DIGITAL / DISCRETE CONTROL SYSTEMS:
Computer-controlled digital systems can be controlled by software, allowing
new functions to be added without changing hardware. For instance, changing the
proportional gain for digital system can be done just by changing the program in the
computer, whereas in an analog system it would require changes done in the design
5
from the scratch. Also, running the controller algorithms on a computer facilitate
much faster execution rather than embedding it in a microcontroller. The memory in
microcontroller will not be sufficient to run complex programs like neural control
programs. Hence, in this project we have chosen to interface the microcontroller with
the computer where the control programs will actually be run.
Further more, signals represented digitally can be transmitted without
degradation due to noise. For example, a continuous audio signal, transmitted as a
sequence of 1s and 0s, can be reconstructed without error provided the noise picked
up in transmission is not enough to prevent identification of the 1s and 0s.
Information storage can be easier in digital systems than in analog ones. The
noise-immunity of digital systems permits data to be stored and retrieved without
degradation. In an analog system, noise from aging and wear degrade the information
stored. In a digital system, as long as the total noise is below a certain level, the
information can be recovered perfectly.
1.1. BASIC BLOCK DIAGRAM OF THE PROJECT
6
CHAPTER 2
DAQ HARDWARE
2.1 HARDWARES USED
2.1.1 VOLTAGE REGULATOR (LM79xx & LM78xx series)
2.1 PIN CONFIGURATION OF LM79XX SERIES
2.2 PIN CONFIGURATION OF LM78XX SERIES
The 78xx (sometimes LM78xx) is a family of self-contained fixed linear
voltage regulator integrated circuits. The 78xx family is commonly used in electronic
circuits requiring a regulated power supply due to their ease-of-use and low cost. For
ICs within the family, the xx is replaced with two digits, indicating the
outputvoltage (for example, the 7805 has a 5 volt output, while the 7812 produces
7
12 volts). The 78xx line are positive voltage regulators: they produce a voltage that is
positive relative to a common ground. There is a related line of 79xx devices which
are complementary negative voltage regulators. 78xx and 79xx ICs can be used in
combination to provide positive and negative supply voltages in the same circuit.
Typical connections for the power supply circuit are shown in Fig.2.3.
2.3 TYPICAL CONNECTION FOR POWER SUPPLY CIRCUIT
COMPONENTS USED IN THE POWER SUPPLY CIRCUIT
• 18-0-18 centre tap transformer
• Rectifier bridge with 1N4007 diodes
• IC 7805, IC 7812, IC 7815
• IC 7905, IC 7912, IC 7915
• 1N4007 diodes used as current limiting diode across each of the 79XX ICs
• 2200 µF 50V capacitors
• 100 µF 63 V capacitors
The above circuit provides +15 V,-15 V, +12 V, -12 V, +5 V, -5 V.
8
2.1.2 ADC 0804
2.4 PIN CONFIGURATION OF ADC 0804
Features
• Compatible with 8080 Microprocessors and commonly used Microcontrollers.
• Easy interface to all microprocessors, or operates 'stand alone'
• Differential analog voltage inputs
• Logic inputs and outputs meet both MOS and TTL voltage level specifications
• Works with 2.5V (LM336) Voltage Reference
• On-chip clock generator
• 0V to 5V analog input voltage range with single 5V supply
• No zero adjust required
• Operates ratiometrically or with 5 Vdc, 2.5Vdc, or analog span adjusted voltage
Reference
Typical connections between microcontroller and ADC are portrayed in Fig.2.5.
9
2.5 TYPICAL CONNECTIONS BETWEEN ADC 0804 AND
MICROCONTROLLER
2.1.3 DAC 0808
2.6 PIN CONFIGURATION OF DAC 0808
10
Features
• Relative Accuracy: ±0.19% error maximum
• Full scale current match: ±1 LSB typical
• Fast Settling Time: 150 ns typical
• Non inverting digital inputs are TTL and CMOS compatible
• High speed multiplying input slew rate: 8 mA/µs
• Power Supply Voltage Range: ±4.5V to ±18V
• Low power consumption: 33 mW @ ±5V
Typical connections for the operation of DAC 0808 are shown in Fig.2.7.
2.7 TYPICAL CONNECTION FOR DAC 0808
11
2.1. 4 ATMega328 MICROCONTROLLER
2.8 PIN CONFIGURATION OF ATMega328 MICROCONTROLLER
Features
• Peripheral Features
– Two 8-bit Timer/Counters with Separate Prescaler and Compare Mode
– One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture
Mode
– Real Time Counter with Separate Oscillator
– Six PWM Channels
– 8-channel 10-bit ADC in TQFP and QFN/MLF package
Temperature Measurement
– 6-channel 10-bit ADC in PDIP Package
Temperature Measurement
12
– Programmable Serial USART
– Master/Slave SPI Serial Interface
– Byte-oriented 2-wire Serial Interface
– Programmable Watchdog Timer with Separate On-chip Oscillator
– On-chip Analog Comparator
– Interrupt and Wake-up on Pin Change
• Special Microcontroller Features
– Power-on Reset and Programmable Brown-out Detection
– Internal Calibrated Oscillator
– External and Internal Interrupt Sources
– Six Sleep Modes: Idle, ADC Noise Reduction, Power-save, Power-down, Standby,
and Extended Standby
• I/O and Packages
– 23 Programmable I/O Lines
– 28-pin PDIP, 32-lead TQFP, 28-pad QFN/MLF and 32-pad QFN/MLF
PIN DESCRIPTION
1- VCC
Digital supply voltage.
2- GND
Ground.
3- Port B (PB7:0) XTAL1/XTAL2/TOSC1/TOSC2
Port B is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for
each bit). The Port B output buffers have symmetrical drive characteristics with both
high sink and source capability. As inputs, Port B pins that are externally pulled low
will source current if the pull-up resistors are activated. The Port B pins are tri-stated
when a reset condition becomes active, even if the clock is not running.
13
Depending on the clock selection fuse settings, PB6 can be used as input to the
inverting Oscillator amplifier and input to the internal clock operating circuit.
Depending on the clock selection fuse settings, PB7 can be used as output from the
inverting Oscillator amplifier.
4- Port C (PC5:0)
Port C is a 7-bit bi-directional I/O port with internal pull-up resistors (selected for
each bit). The PC5...0 output buffers have symmetrical drive characteristics with both
high sink and source capability. As inputs, Port C pins that are externally pulled low
will source current if the pull-up resistors are activated. The Port C pins are tri-stated
when a reset condition becomes active, even if the clock is not running.
5- PC6/RESET
If the RSTDISBL Fuse is programmed, PC6 is used as an I/O pin. Note that the
electrical characteristics of PC6 differ from those of the other pins of Port C.
If the RSTDISBL Fuse is unprogrammed, PC6 is used as a Reset input. A low level
on this pin for longer than the minimum pulse length will generate a Reset, even if the
clock is not running. Shorter pulses are not guaranteed to generate a Reset.
6- Port D (PD7:0)
Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for
each bit). The Port D output buffers have symmetrical drive characteristics with both
high sink and source capability. As inputs, Port D pins that are externally pulled low
will source current if the pull-up resistors are activated. The Port D pins are tri-stated
when a reset condition becomes active, even if the clock is not running.
7- AVCC
AVCC is the supply voltage pin for the A/D Converter, PC3:0, and ADC7:6. It should
be externally connected to VCC, even if the ADC is not used. If the ADC is used, it
should be connected to VCC through a low-pass filter. Note that PC6...4 use digital
supply voltage, VCC
14
8- AREF
AREF is the analog reference pin for the A/D Converter.
9- ADC7:6 (TQFP and QFN/MLF Package Only)
In the TQFP and QFN/MLF package, ADC7:6 serve as analog inputs to the A/D
converter. These pins are powered from the analog supply and serve as 10-bit ADC
channels.
2.1.5 CD4051 ANALOG MULTIPLEXER
2.9 PIN CONFIGURATION OF CD4051 ANALOG MULTIPLEXER
These analog multiplexers are digitally controlled analog switches having low
``ON'' impedance and very low ``OFF'' leakage currents. Control of analog signals up
to 15Vp-p can be achieved by digital signal amplitudes of 3±15V. For example, if
VDD 5V, VSS 0V and VEE 5V, analog signals from 5V to 5V can be controlled by
digital inputs of 0±5V. The multiplexer circuits dissipate extremely low quiescent
power over the full VDD VSS and VDD VEE supply voltage ranges, independent of
the logic state of the control signals. When a logical ``1'' is present at the inhibit input
terminal all channels are ``OFF''. CD4051BM/CD4051BC is a single 8-channel
15
multiplexer having three binary control inputs. A, B, and C, and an inhibit input. The
three binary signals select 1 of 8 channels to be turned ``ON'' and connect the input to
the output.
2.1.6 MAX232
2.10 PIN CONFIGURATION OF MAX 232
The MAX232 IC is used to convert the TTL/CMOS logic levels to RS232
logic levels during serial communication of microcontrollers with PC. The controller
operates at TTL logic level (0-5V) whereas the serial communication in PC works on
RS232 standards (-25 V to + 25V). This makes it difficult to establish a direct link
between them to communicate with each other.
The intermediate link is provided through MAX232. It is a dual driver/receiver
that includes a capacitive voltage generator to supply RS232 voltage levels from a
single 5V supply. Each receiver converts RS232 inputs to 5V TTL/CMOS levels.
These receivers (R1 & R2) can accept ±30V inputs. The drivers (T1 & T2), also called
transmitters, convert the TTL/CMOS input level into RS232 level.
16
The transmitters take input from controller’s serial transmission pin and send
the output to RS232’s receiver. The receivers, on the other hand, take input from
transmission pin of RS232 serial port and give serial output to microcontroller’s
receiver pin. MAX232 needs four external capacitors whose value ranges from 1µF to
22µF. Typical connections for constructing the serial level convertor circuit are given
in Fig.2.11.
2.11 TYPICAL CONNECTION FOR SERIAL LEVEL CONVERTOR
CIRCUIT
In computing, a serial port is a serial communication physical interface through
which information transfers in or out one bit at a time. Throughout most of the history
of personal computers, data transfer through serial ports connected the computer to
devices such as terminals and various peripherals.
MAX232: As we know for serial port we require TIA/EIA-232-F voltage level i.e.
it can accept ±30-V. MAX232 has a capacitive voltage generator to supply TIA/EIA-
232-F voltage levels from a single 5-V supply. Each receiver converts TIA/EIA-232-F
inputs to 5-V TTL/CMOS levels.
17
So, to interface the atmega32 to the computer we will be connecting atmega32 (pin
no 14 & 15) to serial port via max232. We are using max232 because atmega32 works
at TTL voltage level and for serial port we require other voltage level, max232
converts the TTL voltage level to the required voltage level.
Atmega32 will receive the signal from MAX232 and transmit to max232 which
will be received by serial port. The following connections were done:
• pin no 2 (Tx) of serial port to Tx out of the MAX 232 IC (pin no 14)
• pin no 3 (Rx) of serial port to Rx in of the MAX 232 IC (pin no 13)
• pin no 14 (RxD) of Atmega to Rx out of MAX 232 IC (pin no 12)
• pin no 15 (TxD) of Atmega to Tx in of MAX 232 IC (pin no 11)
2.1.7 PROLIFIC USB TO SERIAL CONVERTER
2.12 PROLIFIC USB TO SERIAL CONVERTER
The Beagle Software USB to serial adapter allows you to use serial hardware with
your USB-equipped laptop or desktop computer.The USB to serial adapter connects
RS-232 serial devices to your USB-equipped Windows computer, providing a bridge
18
connection with a standard DB 9-pin male serial port connector on one end and a
standard USB plug connector on the other. You simply attach your serial device to the
DB 9-pin end of the adapter cable and plug the USB connector into your PC's USB
port.
Features
• Adds a COM port to USB PC allowing usage of legacy serial devices on the
PC
• Can be used with modems, PDAs (including Palm), cellular phones, GPSs,
digital cameras, card readers, and more
• Supports Beagle Software's serial devices including DocuClock printers, Star
Sync and Cell Sync receivers
• Easy plug-and-play installation
• USB-powered—no separate power supply or battery required
• Easily fits into laptop cases for on-the-go applications
2.1.8 ARDUINO UNO
2.13 ARDUINO UNO
19
The Arduino Uno is a microcontroller board based on
the ATmega328 (datasheet). It has 14 digital input/output pins (of which 6 can be used
as PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a
power jack, an ICSP header, and a reset button. It contains everything needed to
support the microcontroller; simply connect it to a computer with a USB cable or
power it with a AC-to-DC adapter or battery to get started.
2.1.9 INTEGRATED HARDWARE
2.14 INTEGRATED HARDWARE
20
CHAPTER 3
SOFTWARE
3.1 INTERFACING WITH C
3.1.1 MICROCONTROLLER CODE
The baud rate of communication is set as 9600 bps in the function setup(). The
function loop() polls itself continously. Inside the function loop() the value of choice
can be either -1, 0 or 1. If the value of choice is -1 then it listens to the serial port for
the command "RD" or "WT". If the command received is "RD" choice is set to 0 and
1 if command received is "WT". If choice is set to 0 then the micrcontroller reads the
input analog volatge using ADC0804 and saves the result in the variable value. The
value is then serially transmitted to the computer. After this, the serial buffer is
flushed and choice is again set to -1. Likewise, if the choice is set as 1 then the
microcontroller has to perform a write operation. So, it first listens to the serial port
for the analog voltage which it has to set using the DAC0808. It saves this voltage as
value. The value is then converted into its digital equivalent and sent to the DAC0808.
The serial buffer is then flushed and choice is set to -1.
Given below is the code burnt into the microcontroller.
int i, k, choice, ch;
char cmnd[2];
void setup()
{
Serial.begin(9600);
Serial.flush();
for (i=2; i<=13; i=i+1)
{
pinMode(i, OUTPUT);
21
}
k=0;
choice=-1;
}
void loop()
{
if (choice==0)
{
int value=0, b;
digitalWrite(2, LOW);
delay(2);
digitalWrite(2, HIGH);
delay(2);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(13, LOW);
delay(1);
b=analogRead(5);
if (b>200)
{
value=value+1;
}
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(13, HIGH);
delay(1);
b=analogRead(5);
22
if (b>200)
{
value=value+2;
}
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
digitalWrite(13, LOW);
delay(1);
b=analogRead(5);
if (b>200)
{
value=value+4;
}
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
digitalWrite(13, HIGH);
delay(1);
b=analogRead(5);
if (b>200)
{
value=value+8;
}
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
digitalWrite(13, LOW);
delay(1);
b=analogRead(5);
23
if (b>200)
{
value=value+16;
}
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
digitalWrite(13, HIGH);
delay(1);
b=analogRead(5);
if (b>200)
{
value=value+32;
}
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
digitalWrite(13, LOW);
delay(1);
b=analogRead(5);
if (b>200)
{
value=value+64;
}
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
digitalWrite(13, HIGH);
delay(1);
b=analogRead(5);
24
if (b>200)
{
value=value+128;
}
Serial.write(value);
choice=-1;
Serial.flush();
}
else
if (choice==1)
{
int value=0, f=1;
do
{
if (Serial.available()>0)
{
ch=Serial.read();
value=ch;
f=0;
}
}
while (f==1);
for (i=0; i<=7; i=i+1)
{
if (i>=0 && i<=3)
{
digitalWrite((12-i), bitRead(value, i));
25
}
else
if (i>=4 && i<=7)
{
digitalWrite((1+i), bitRead(value, i));
}
}
choice=-1;
Serial.flush();
}
else
if (choice==-1)
{
if (Serial.available()>0)
{
ch=Serial.read();
if (ch>=int('A') && ch<=int('Z'))
{
cmnd[k]=char(ch);
k=k+1;
}
if (k==2)
{
if (cmnd[0]=='R' && cmnd[1]=='D')
{
choice=0;
}
26
else
if (cmnd[0]=='W' && cmnd[1]=='T')
{
choice=1;
}
k=0;
}
}
}
}
3.1.2 PC CODE (WRITTEN IN C)
In the function setport() the parameter of the serial communication is set, i.e.-
baud rate, parity bits, stop bits and the data word length. For or program baud rate is
9600 bps, no parity bits, one stop bit and data word length of 8.COM PORT 2 (0x2F8
is the base address of the COM PORT 2) is used for this purpose .When the function
read() is called, it sends the command to the "RD" to COM PORT 2 using the
function outportb(). This will instruct the microcontroller to perform read operation.
After that it waits for the microcontroller to send the analog input voltage. When the
microcontroller send the data, it is read using the function inportb(). When the
function write() is called, it sends the command to the "WT" to COM PORT 2 using
the function outportb(). This will instruct the microcontroller to perform write
operation. After this it sends the analog value the microcontroller has to output using
the outportb() function.
Given below is the code running on the PC.
#include<dos.h>
#include<stdio.h>
27
#include<conio.h>
#define PORT 0x2F8
double REF_ADC=5.0, REF_DAC=5.0;
double read()
{
int value, flag, c, ch, k;
double val;
char cmnd[2];
cmnd[0]='R';
cmnd[1]='D';
for (k=0; k<2; k++)
{
delay(1);
outportb(PORT, int(cmnd[k]));
}
value=0;
flag=1;
while (flag)
{
c=inportb(PORT + 5);
if (c & 1)
{
ch=inportb(PORT);
value=ch;
flag=0;
}
}
28
val=(REF_ADC*value)/255.0;
return val;
}
void write(double val)
{
int k, value;
int arr[4], cmnd[4];
cmnd[0]=int('W');
cmnd[1]=int('T');
for (k=0; k<2; k++)
{
delay(1);
outportb(PORT, cmnd[k]);
}
value=int((255.0*val)/REF_DAC);
if (value>255)
value=255;
else
if (value<0)
value=0;
outportb(PORT, value);
}
void setport()
{
outportb(PORT + 1 , 0);
outportb(PORT + 3 , 0x80);
outportb(PORT + 0 , 0x0C);
29
outportb(PORT + 1 , 0x00);
outportb(PORT + 3 , 0x03);
outportb(PORT + 2 , 0xC7);
outportb(PORT + 4 , 0x0B);
}
3.2 INTERFACING WITH MATLAB
3.2.1 MICRCONTROLLER CODE
This code basically implements a state machine that reads the serial port
and makes just one transition to a new state, depending on both the previous state and
the command that is read from the serial port. Some commands need additional inputs
from the serial port, so they need 2 transitions (each one happening as soon as
anything new is available from the serial port) to be fully executed. After a command
is fully executed the state returns to its initial value s=-1. When state s=-1 it listens to
the serial port for any available data. If the incoming data from serial port is 51 then
the state s is set as 30, if it is 52 then s=40 and if it is 57 then s=90. If state s=30 read
operation has to be performed and microcontroller request the ADC0804 for the
analog input. After receiving the analog input from ADC0804 it is sent serially to the
computer. If s=40 then the write operartion has to be performed. The microcontroller
will listen to the serial port for the analog value it has to output and accordingly set the
DAC0808. State s=90 is basically used by MATLAB to know if the DAQ is actually
connected to the computer. So when state s=90 is invoked the microcontroller send
the number 1 to MATLAB to acknowledge its presence.
Given below is the code burnt in microcontroller for interfacing with
MATLAB.
30
void setup()
{
int i;
for (i=2;i<13;i++)
{
pinMode(i,OUTPUT);
}
Serial.begin(9600);
}
void loop()
{
int s = -1;
int pin = 3;
int val = 0;
int agv = 0;
if (Serial.available()>0)
{
val = Serial.read();
switch (s)
{
case -1:
if (val>47 && val<90)
{
s=10*(val-48);
}
if ((s>40 && s<90) || (s>90 && s!=340))
{
31
s=-1;
}
break;
case 30:
if (val>96 && val<103)
{
int value=0, b;
digitalWrite(2, LOW);
delay(2);
digitalWrite(2, HIGH);
delay(2);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(13, LOW);
delay(1);
b=analogRead(5);
if (b>200)
{
value=value+1;
}
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(13, HIGH);
delay(1);
b=analogRead(5);
if (b>200)
{
32
value=value+2;
}
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
digitalWrite(13, LOW);
delay(1);
b=analogRead(5);
if (b>200)
{
value=value+4;
}
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
digitalWrite(13, HIGH);
delay(1);
b=analogRead(5);
if (b>200)
{
value=value+8;
}
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
digitalWrite(13, LOW);
delay(1);
b=analogRead(5);
if (b>200)
{
33
value=value+16;
}
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
digitalWrite(13, HIGH);
delay(1);
b=analogRead(5);
if (b>200)
{
value=value+32;
}
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
digitalWrite(13, LOW);
delay(1);
b=analogRead(5);
if (b>200)
{
value=value+64;
}
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
digitalWrite(13, HIGH);
delay(1);
b=analogRead(5);
if (b>200)
{
34
value=value+128;
}
Serial.println(value);
}
s=-1;
break;
case 40:
if (val>98 && val<117)
{
pin=val-97;
s=41;
}
else
{
s=-1;
}
break;
case 41:
{
for (i=0; i<=7; i=i+1)
{
if (i>=0 && i<=3)
{
digitalWrite((12-i), bitRead(val, i));
}
else
if (i>=4 && i<=7)
35
{
digitalWrite((1+i), bitRead(val, i));
}
}
s=-1;
break;
case 90:
if (val==57)
{
Serial.println(1);
}
s=-1;
break;
default:
s=-1;
}
}
}
3.2.2 PC CODE (WRITTEN IN MATLAB)
This code is implemented in MATLAB. It basically creates a class mydaq
which has all the function to operate on the DAQ, for e.g. initialization (constructor
function), deletion (destructor function), reading (read function) and writing(write
function). When the constructor function is called it first sets the baud rate of the
desired COM PORT as 9600 bps. After that it tries to communicate with the DAQ.
Only if the DAQ communicates back will it create an object of its type. The funcion
read() and write() is to perform reading and writting operation. The function fwrite() is
36
used to send data serially while the function fscanf() is used to read data which is
coming from the serial port.
Given below is the code written in PC for interfacing the microcontroller with
MATLAB.
classdef mydaq < handle
properties (SetAccess=private,GetAccess=private)
aser
end
methods
function a=mydaq(comPort)
a.aser=serial(comPort,'BaudRate',9600);
try
fopen(a.aser);
catch ME,
delete(a);
error(['Could not open port: ' comPort]);
end
fprintf(1,'Attempting connection .');
for i=1:7,
fprintf(1,'.');
pause(1);
end
fprintf(1,'\n');
fwrite(a.aser,[57 57],'uchar');
chk=fscanf(a.aser,'%d');
if isempty(chk)
delete(a);
37
error('Connection unsuccessful, please make sure that the Daq is powered
on, running the server program, and that the board is connected to the indicated
serial port.');
end
if chk==1
disp('DAQ READY');
end
end
function delete(a)
if isa(a.aser,'serial') && isvalid(a.aser) &&
strcmpi(get(a.aser,'Status'),'open'),
fclose(a.aser);
end
if isobject(a.aser),
delete(a.aser);
end
end
function val=read(a)
fwrite(a.aser,[51 97],'uchar');
val=fscanf(a.aser,'%d');
end
function write(a,val)
fwrite(a.aser,[52 100 val],'uchar');
end
end
end
38
CHAPTER 4
DIGITAL CONTROL ALGORITHMS
4.1 SYSTEM IDENTIFICATION: LINEAR LEAST SQUARE ALGORITHM
The system identification code employs an offline linear least square algorithm.
If the process is given by,
����� � �������� �
�� ���� ������ ������ ����� ������� ��
(1)
The formula for offline least square algorithm is:
� � �����-���� where,
� � ����������������������� !�"
#
�
$%%%%& ��'���' � ����' � ��
((
��' � )�
���
��' � ����'�
��' � ��((
��' � ) � ��
���
��' � ����' � ����'�((
��' � ) � ��
���
(((
(
���
(((
(
���
��' � ����' � � �����' � � ���(
(��' � ) ���
���
���' � �����'�
���' � ��((
���' � ) � ��
���
���' � �����' � �����'�((
���' � ) � ��
���
(((
(
���
(((
(
�����
�
* � ���'�����' � �������' � �����' � )�!
N is the number of samples.
The code first creates the matrix A and B. Then it performs the operation
described by offline least square algorithm.
The myident() function has the provision for getting both the discrete and
continous model of the process.
CODE:
function [model]=myident(u, y, n, m, del, type)
data=numel(u);
for i=1:1:data
39
for j=1:1:(n+1)
degree=n+1-j;
A(i,j)=0;
if ((i-degree)>=1)
A(i,j)=u(i-degree);
end
end
for j=(n+2):1:(n+m+1)
degree=n+m+2-j;
A(i,j)=0;
if ((i-degree)>=1)
A(i,j)=-1*y(i-degree);
end
end
B(i,1)=y(i);
end
C=transpose(A);
coeff=inv(C*A)*(C*B);
for k=1:1:(n+1)
NUM(k,1)=coeff((n+2-k),1);
end
for k=1:1:m
DENO(k,1)=coeff(n+m+2-k,1);
end
if (type=='D')
num=NUM;
deno=DENO;
40
model=tf(num, [1 deno], del, 'variable','z^-1');
elseif (type=='C')
for i=0:1:n
num(1+i)=0;
for j=i:1:n
num(1+i)=num(1+i)+NUM(1+j)*((j*del)^(i))/factorial(i);
end
if ((i-floor(i/2.0)*2.0)==1)
num(1+i)=num(1+i)*-1;
end
end
for i=1:1:m
deno(i)=0;
for j=i:1:m
deno(i)=deno(i)+DENO(j)*((j*del)^(i))/factorial(i);
end
if ((i-floor(i/2.0)*2.0)==1)
deno(i)=deno(i)*-1;
end
end
factor=1;
for j=1:1:m
factor=factor+DENO(j);
end
num=num/factor;
deno=deno/factor;
len=numel(num);
41
mid=floor(len/2);
for m=1:mid
copy=num(m);
num(m)=num(len+1-m);
num(len+1-m)=copy;
end
len=numel(deno);
mid=floor(len/2);
for m=1:mid
copy=deno(m);
deno(m)=deno(len+1-m);
deno(len+1-m)=copy;
end
model=tf(num,[deno 1]);
end
4.2 PI CONTROLLER
The algorithm implements a simple PI controller described by the following
formula:
��+� � *,�� � -./�+� � -0 1 /�2�324� (2)
The variable sumerror stores the value of the integral of past errors.
It has the provision for both direct and reverse action. For our process we have
set Kp=0.9 and Ki=0.4.
42
CODE:
function [OP sumerror]=myPI(Kp, Ki, Bias, SP, Y, del, sumerror, action)
y=5.0*Y/256.0;5
if (action=='D')
error=y-Sp;
elseif (action=='R')
error=SP-y;
end
op=Bias+Kp*error+Ki*sumerror;
OP=max(0,min(255,floor(255.0*op/5.0)));
sumerror=sumerror+error*del;
end
4.3 AUTOTUNED PI CONTROLLER
The autotuning PI code aims to tune the PI controller for a particular process in
such a way so as to reduce integral square error (ISE). The code first uses the
myident() function to make a model of the process. The model is then used to obtain
the unit step response of the process. The genetic algorithm function (ga()) of
MATLAB is then used to reduce the integral square error of the step response.
CODE:
function [Kp Ki]=myAutoPI(obj, Tf, del, nProcess, mProcess)
t=0:del:Tf;
N=Tf/del+1;
for i=1:1:floor(N/4);
u(i)=4;
43
end
for i=(floor(N/4)+1):1:floor(2*N/4);
u(i)=1;
end
for i=(floor(2*N/4)+1):1:floor(3*N/4);
u(i)=3;
end
for i=(floor(3*N/4)+1):1:N;
u(i)=2;
end
for i=1:1:N
Y=obj.read();
y(i)=5.0*Y/256.0;
U=floor(255.0*u(i)/5.0);
U=max(0,min(255,U));
obj.write(U);
pause(del);
end
obj.write(0);
Gp=myident(u,y,nProcess,mProcess,del,'C');
k=ga(@myise,2);
function [ise]=myise(Const)
Gc=tf(Const, [1 0]);
Gcl=Gc*Gp/(1+Gc*Gp);
y=step(Gcl,t);
ISE=0;
for j=1:1:N
44
ISE=ISE+(1-y(j))*(1-y(j))*del;
end
ise=ISE;
end
Kp=k(1);
Ki=k(2);
end
4.4 CONTROLLER DESIGNED USING SYNTHESIS METHOD
Designing a synthetic controller consist of designing a controller for a closed
feedback loop so as to get the desired closed loop transfer function. The formula used
is:
�5��� � ������
6 �57������57���
(3)
Considering a first order process whose transfer function is:
����� � ���8��
(4)
We can design controller such that the final closed loop transfer function is:
�57��� � ���857�
(5)
The transfer function of the desired controller is:
�5��� � ����/��� �
��8��89�
(6)
The discrete version of the above controller is:
��,� � ��, � �� � :";"<=">
/�,� � "<">/�, � �� (7)
where, T is the sampling time.
CODE:
i=1;
45
ebef=0;
mbef=0;
while i<=201
Y=obj.read();
y(i)=(5.0*Y)/(256.0);
e(i)=u(i)-y(i);
m(i)=mbef+(T+Tp)/Tr*e(i)-Tp/Tr*ebef;
mbef=m(i);
ebef=e(i);
M=floor(255.0*m(i)/5.0);
M=max(0,min(255,M));
obj.write(M);
i=i+1;
pause(del);
end
46
4.5 MODEL REFRENCE ADAPTIVE CONTROLLER
4.1 BLOCK DIAGRAM OF MRAC SYSTEM
CODE:
ardOBJ=arduino('COM23');
ardOBJ.pinMode(5,'output');
ardOBJ.analogWrite(5,0);
while ardOBJ.analogRead(0)>1
end
T=0.1;
t=0:T:20;
i=1;
while i<=201
Vin(i)=2;
47
i=i+1;
end
K=2;
Tm=4;
r=1.5;
i=1;
while i<=201
Vout(i)=ardOBJ.analogRead(0)*5.0/1024;
u(i)=K*Vin(i);
if (i==1)
Vm(i)=Vin(i)/(1+Tm/T);
end
if (i>1)
Vm(i)=(Tm/T*Vm(i-1)+Vin(i))/(1+Tm/T);
end
K=K-r*(Vout(i)-Vm(i))*Vin(i)*T;
op=max(min(floor(255.0/5.0*u(i)),255),0);
ardOBJ.analogWrite(5,op);
pause(T);
i=i+1;
end
delete(ardOBJ);
RESU
In this section we will present a
function is:
1
Gp(s)= ----------
(1+10*s)
5.1 OUTPUT FOR SYSTEM
SQUARE METHOD
Obtained Model:
0.968
---------------
8.955 s + 1
5.1 GRAPH FOR SYS
48
CHAPTER 5
RESULTS AND DISCUSSION
sent a case study of a RC Process whose theor
TEM IDENTIFICATION DONE BY LINE
R SYSTEM IDENTIFICATION ALGORIT
theoretical transfer
LINEAR LEAST
ORITHM
49
5.2 SERVO RESPONSE OF FIRST ORDER SYSTEM WITH PI
CONTROLLER
5.2 SERVO RESPONSE OF FIRST ORDER SYSTEM WITH PI
CONTROLLER
5.3 AUTOTUNED PI CONTROLLER
Autotuned Value:
Kp=19.0;
Ki=2.3;
50
5.4 SERVO RESPONSE OF FIRST ORDER SYSTEM WITH
MODEL REFERENCE ADAPTIVE CONTROLLER
5.4 SERVO RESPONSE OF FIRST ORDER SYSTEM WITH
MODEL REFERENCE ADAPTIVE CONTROLLER
51
5.5 SERVO RESPONSE OF FIRST ORDER SYSTEM WITH
CONTROLER DESIGNED USING SYNTHESIS METHOD
5.5 SERVO RESPONSE OF FIRST ORDER SYSTEM WITH
CONTROLER DESIGNED USING SYNTHESIS METHOD
From the graph, it is clearly visible that the dynamic response of the closed loop
process is much better than the dynamic response of the open loop process.
52
CHAPTER 6
CONCLUSION
We have acquired the data from a first order RC process through the data
acquisition card we designed and controlled it online through control algorithms
implemented in PC. The following modules were developed.
• Analog to digital convertor
• Digital to analog convertor
• Power delivering circuitry
• Interfacing algorithms
• Control algorithms coded in Matlab
• Online control of a process using the algorithms in PC
The following algorithms were implemented and the results were as desired.
• System identification using Linear Least Square Algorithm
• Proportional- Integral control
• Auto-tuning Proportional- Integral control
• Model Reference Adaptive Control
• Synthesis method of controller design
53
CHAPTER 7
REFERENCES
1) Jan Axelson, "Serial Port Complete", Lakeview Research, 2nd
Edition, 1998
2) Jan Axelson, "USB Complete-The Developer's Guide", Lakeview Research, 4th
Edition, 2009
3) Willis J. Tompkins, John G. Webster, "Interfacing sensors to the IBM PC", Prentice
Hal, 2nd
Edition, 1998
4) http://retired.beyondlogic.org/serial
5) http://www.engineersgarage.com/microcontroller/8051projects/interface-
ADC0804-serial-port-AT89C51-circuit
6) John Hyde, "USB Design by Example-A practical guide to building I/O Devices",
Intel University Press, 2nd
Edition, 2001
7) http://arduino.cc/playground/Interfacing/Matlab
8) David Trumper, "Analysis and Design of Digital Control Systems",
MITOpenCourseWare
9) Yoan D. Landau, Gianluca Zito, "Digital Control Systems: Design, Identification
And Implementation", Springer, 1st Edition, 2006