Professional Documents
Culture Documents
(Feedback Control)
MAE490G/EE 442
Mechatronic Systems Design
CSULB
Topics
Feedback control
Open vs. closed-loop
Stability and performance specifications
System response
PID controller
J F
1
KT
K Ai
fwd. kin.
x, x
C ( s )G ( s )
1 C ( s )G ( s ) H ( s )
With Disturbance
C(S)
H(S)
G( s)
1 C ( s )G ( s ) H ( s )
LTI Systems
LTI = Linear time-invariant system.
For y(t) = S[x(t)],
Linearity: For any two input signals x1(t) and x2(t)
and a real constant a,
S[x1(t)+x2(t)] = S[x1(t)] + S[x2(t)] and
S[ax1(t)] = aS[x1(t)]
Time invariance: For any real constant T, y(t-T) =
S(x(t-T)). This includes constant-gain system and
linear combinations of time-shifts of the input
signal, i.e. y(t) = x(t) 2x(t-5).
Stability Criterion
Rouths Method: Developed for obtaining the
information about the pole locations without
solving the roots of the characteristic equation, P.
Two conditions:
A necessary (but not sufficient) condition for stability is
that all the coefficients of P are positive
A necessary and sufficient condition is that all elements
in the first column of the Routh array are positive (see LTI
control references for further details)
Performance specifications
Rise time (tr): The time for output to rise from 0 (or
10%) to 90% of the final value
Overshoot (Mp): Maximum deviation of the output
above its steady-state value. Mp increases with
decreasing . Mp can also be expressed in % of the final
value (Xmax Xss)*100/Xss.
Peak time (tp): Time at which the max overshoot occurs
Settling time (ts): Time required for the oscillation to
stay within specified small percentage (usually 2 or 5%).
For 2% settling time, ts = 4/( n) = 4*time constant.
Delay time (td): Time required for the response to reach
50% of its final value
L-1,
1
+
1
()
+1
= +
1
()
+1
+ [1
Consider a RL circuit:
Rewrite,
+ = , 0 = 0
+ =
Time constant =
1
(1
mx
mx f kx bx
mx bx kx f
fk
fb
Divided by 1/m: x
Natural frequency:
Damping ratio:
Note that
b
x
m
k
x
m
1
f
m
k
rad / s
m
b
2m
b
2 mk
0
17
Total Response
Laplace transform:
Response to
Initial conditions
Response to
Input
18
Step Response
Assume the forcing function, step input
Step Response
= 0.4
0.5
= 1.0
= 2.0
=0
0.4
Amplitude
= 0 (Undamped)
pure oscillation
= 0.1
0.3
0.2
0.1
-0.1
5
Time (sec)
19
10
Topics
Feedback control
Open vs. closed-loop
Stability and performance specifications
System response
PID controller
PID Controller
Proportional-integral-derivative control provides
a widely-used generic feedback controller
The control signal is generated based on the
error, e(t), between the current system state
and the reference and involves 3 gains:
kp = proportional gain (apply to the error)
kD = derivative gain (apply to the derivative of
the error)
kI = integral gain (apply to the integral of the
error)
Transfer fn:
U ( s)
E ( s)
kp
kI
s
kD s
dt
PD Controller Example
Consider
2nd-order
plant:
G( s)
C ( s)
PD controller:
Overall TF:
Y ( s)
R( s )
s2
kp
s2
b
a1s a2
kD s
b(k D s k p )
(a1 bkD ) s a2 bk p
Proportional
Control signal = kp(ref-y) = kpe
Effect: big signal for large error, small signal
for small error
Results in steady-state error
Needs a nonzero error to generate a control
action
Essentially a spring at the desired position
(F=kx)
High gain makes a stiffer spring and
increases tendency to oscillate
Effect of kp
Derivative
Control signal = kDd(e)/dt
Effect: slows control action and damps
oscillations; increases system stability
Can also make response more sluggish
Based on first derivative: either noise or
filtering lag (for velocity estimate from position)
can actually introduce instability again
Effect of kD
Integral
Control signal = ki (e)dt
Effect: integrates error over time
Control signal builds up and eventually brings
error to zero
Eliminates steady-state error and improves system
responsiveness
Unpleasant side effects:
Reduced damping and larger overshoot
(decreased stability)
Integrator wind-up
Effect of kI
Topics
Feedback control
Open vs. closed-loop
Stability and performance specifications
System response
PID controller
Optical encoder
A digital optical encoder is a device that converts
motion into a sequence of digital pulses. The motion
may be either linear or rotational.
We focus on the more common rotary optical
encoder.
Quadrature signals
If Va is a digital signal (high and low only), use pulsewidth modulation (PWM) to change its average
value.
PWM : fix the frequency but vary duty cycle to
change average output voltage:
H-bridge driver
PWM is often use with a H-bridge:
Arduino Microcontroller
Low cost, open source microcontroller.
Based on Atmel Atmega chip.
Different versions: Uno, Duemilanove, Mega,
Nano.
Easy to extend its function using accessory
shield board. Ex: Motor shield, wireless shield.
Arduino architecture
ATmega328 on Uno
Arduino Uno
Arduino Uno
Pins A0-A5 are analog
inputs. Can be used
as digital input or
output as well.
Pins 0-13 are digital
inputs/outputs. Max
current is 40mA per
pin.
Pins 3, 5, 6, 9, 10, and
11 can be configured
as 8-bit PWM output.
Arduino Program
Download and install free
Arduino Integrated
Development Environment
(IDE) software and USB
drivers:
http://arduino.cc/en/Main/Software
Arduino IDE
Verify
syntax
Upload
sketch to
board
new
Open
save
Arduino IDE
Examples:
File > Examples -> ..
Choose board
Tools -> Board -> Arduino Uno
Declaration section
Notes:
Commands are case sensitive
Text after // are comments
Text between /* and */ are comments
Each line of code needs to end with a ;
digitalWrite(pin, value)
Turns a digital pin either on or off.
value: HIGH or LOW
digitalRead(pin)
Reads the value from a specified digital pin, either
HIGH or LOW.
Usually use an int variable to store the value.
analogWrite(pin, value)
Changes the PWM rate on one the digital pins 3, 5, 6,
9, 10, and 11.
Value may be a number between 0 and 255. 0 means
OFF, 255 means 100% duty cycle.
Need to use pinMode to set pin as output before using.
Sketch boilerplate
Any Arduino sketch must have a setup and loop
section.
void setup() {
// code
}
void loop() {
//code
}
Serial monitor
To display information on screen.
Click on
to open the serial monitor window.
Example sketch:
// sketch 3-05
void setup()
{
Serial.begin(9600);
int degC = 20;
int degF;
degF = degC * 9 / 5 + 32;
Serial.println(degF);
}
void loop()
{}
If-else statement
Syntax:
if (condition)
{
// action A
}
else
{
// action B
}
Condition operator:
Note:
count++ is the same as
count=count+1
Compound operators
x++;
x- - ;
x += y;
x -= y;
x *= y;
x /= y;
Boolean operators
&&
-- logical and
True only if both operands are true, e.g.
if (digitalRead(2) == 1 && digitalRead(3) == 1)
{ // code
}
||
--- logical or
True if either operand is true, e.g.
if (x > 0 || y > 0) {
// ...
}
is true if either x or y is greater than 0.
-- not
True if the operand is false, e.g.
if (!x) {
// ...
}
is true if x is false (i.e. if x equals 0).
Math operations
Data types
For statement
Syntax:
Example:
while statement
Syntax:
Example:
while(expression)
{
// statement(s)
}
// sketch 3-08
int ledPin = 13;
int delayPeriod = 100;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
int i=0;
while (i < 20)
{
digitalWrite(ledPin, HIGH);
delay(delayPeriod);
digitalWrite(ledPin, LOW);
delay(delayPeriod);
i ++;
} delay(3000); }
#define directive
Can use #define to associate a value with a
variable provided the value does not change. This
saves memory.
For example, replace
int ledPin = 13;
by
#define ledPin 13
Functions
A function is a like a program within a program.
Syntax: returntype functionName( arguments )
{
//function body
return returntype;
}
Example:
Use a
function call
flash
void flash()
{
digitalWrite(ledPin, HIGH);
delay(delayPeriod);
digitalWrite(ledPin, LOW);
delay(delayPeriod);
}
// sketch 4-02
int ledPin = 13;
int delayPeriod = 250;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
flash(20, delayPeriod);
delay(3000);
}
Example:
float centToFaren(float c)
{
float f = c * 9.0 / 5.0 + 32.0;
return f;
Arrays
An array contains a list of
values, rather than just one
value.
int durations[] = {200, 200, 200, 500, 500, 500,
200, 200, 200};
Arrays
Example:
// sketch Morse code
int ledPin = 13;
int durations[] = {200, 200, 200,
500, 500, 500, 200, 200, 200};
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
for (int i = 0; i < 9; i++)
{
flash(durations[i]);
}
delay(3000);
}
Strings
A string is an array of type char.
The type char is a number between 0 and 127 that
represents one character (ASCII).
Common ASCII code:
Ex:
char name[] = "Hello";
Strings
The following are equivalent:
char name[] = "Hello";
char *name = "Hello";
Array of strings
We can also form an array of strings (not just array of char)
char *letters[] = { abc, def,ghi);
This means:
letters[0] holds abc
letters[1] holds def, etc
Serial communication
Serial.begin(speed) - sets the data rate in bits per second,
where speed is one of these rates: 300, 600, 1200, 2400, 4800,
9600, 14400, 19200, 28800, 38400, 57600, or 115200
Serial.available() -- This function returns the number of bytes
of data in the buffer that are waiting for you to read.
Serial.flush() - Waits for the transmission of outgoing serial
data to complete.
Serial.read() reads in one byte (or -1 if no data available)
Serial.write(val) - write binary data to serial port, where val is a
value to send as a single byte.
Serial.write(str) write binary data to serial port, where str is a
string to send as a series of bytes
Serial.write(buf, len) - write binary data to serial port, where
buf is an array to send as a series of bytes and len is the length
of the buffer
Serial communication
Serial.print(data) - Prints data to the serial port as humanreadable ASCII text.
Ex:
Serial.print(78) gives "78"
Serial.print(1.23456) gives "1.23"
Serial.print('N') gives "N"
Serial.print("Hello world.") gives "Hello world."
Serial.println(data) - prints data to the serial port as humanreadable ASCII text followed by a carriage return character
Serial communication
Example: we can communicate with arduino using serial monitor
//sketch
int outPin = 4;
void setup()
{
pinMode(outPin, OUTPUT);
Serial.begin(9600);
Serial.println("Enter 1 or 0");
}
void loop()
{
if (Serial.available() > 0)
{
char ch = Serial.read();
if (ch == '1')
{ digitalWrite(outPin, HIGH); }
else if (ch == '0')
{ digitalWrite(outPin, LOW); }
}
}
Bitwise operations
& -- bitwise AND.
Example: B11111011 & B00001111 yields B00001011
| - bitwise OR
Example: B11111011 | B00001111 yields B11111111
~ - bitwise NOT
Example: ~ B11111011 yields B00000100
Interrupts
Arduino is single thread. So monitoring the
occurrence of an event uses up precious CPU
time.
Use interrupt instead.
CPU will perform its regular activities until
interrupt occurs. Then it will suspend
whatever it is doing and runs a function to
handle the interrupt.
Arduino Uno has 2 interrupt pins (Pin 2 and 3)
corresponding to interrupt 0 and 1 respectively.
Using interrupts
Attach a signal to an interrupt pin.
Associate a function, interrupt service routine (ISR), with the
interrupt:
attachInterrupt(interrupt, function, mode)
where
interrupt: the number of the interrupt (0 or 1)
function: the name of the ISR
mode:
defines when the interrupt should be triggered.
LOW to trigger the interrupt whenever the pin is low,
CHANGE to trigger the interrupt whenever the pin changes
value
RISING to trigger when the pin goes from low to high,
FALLING when the pin goes from high to low.
When the interrupt occurs, the arduino will run the ISR.
When the ISR is done, the arduino will resume its regular
activities before the interruption.
Using interrupts
Example:
//sketch 7-04
int interruptPin = 2;
int ledPin = 13;
int period = 500;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT);
attachInterrupt(0, goFast, FALLING);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(period);
digitalWrite(ledPin, LOW);
delay(period);
}
void goFast()
{
period = 100;
}
Note that there are 4 reads (one byte at a time). First into def[0],
then def[1], def[2], def[3], where each is one byte.