CSCI 1600 Embedded and Real Time Software Lecture
CSCI 1600: Embedded and Real Time Software Lecture 14: Modeling V: Control Systems and Feedback Steven Reiss, Fall 2019
Decision Trees � A formula can be written as a decision tree � Each node is labeled with a Boolean variable T � Each node has a true and a false branch out � Try a AND (b OR (c XOR d)) � What is the size of the tree? � Does it have to be this size? 2 Lecture 33: Verification III F B � Leaves are labeled either TRUE or FALSE � Example: A and (B or C) A F T C F T T F 10/31/2020
Simplifying Decision Trees � Most of the decision trees look the same � Extra variables just yield identical subtrees � Trees can be simplified � Simplify the tree into a DAG � There are only two real leaves (merge these) � If two nodes have the same children and the same labels, they can be merged � If both branches of a node go to the same node, the original node can be eliminated � This can be iterated � Worst case size is still exponential, actual size is generally much smaller � The results is a BDD (Binary Decision Diagram) 3 Lecture 33: Verification III 10/31/2020
Ordered BDDs � Place an a priori order on the variables as labels � Note that order affects the size � Optimum ordering is NP complete � Logical operations are easy to do on ordered BDDs � Complement: flip TRUE and FALSE leaves � AND, OR: Merge the two trees level by level � Ordering makes this easy � Setting result to be AND/OR or merged result � EG and EU � Defined as iterative procedures on BDDs � Stopping at appropriate fixed points (no changes to BDD on next pass) � 4 All computations are O(1) or O(nlogn) or O(n) on the tree Lecture 33: Verification III 10/31/2020
Model Checking BDDs � Compute the BDD for R(s, t) for the program � Inductively compute F(s) for the formula f � F(S) is true iff f is true in the program � F(S) is true iff it is the singleton node True � Otherwise there is a path that shows false � Can use the BDD path to FALSE to find counterexample � BDDs have been used to verify programs with 2^80 states 5 Lecture 33: Verification III 10/31/2020
Where are we � We can produce application models � Finite state models from programs or design � We can define properties to prove over those models � Either as formula or automata � What do we do about the outside world? � Allow any action at any time � This implies discrete actions � But this isn’t enough 6 Lecture 33: Verification III 10/31/2020
The Real World � Embedded systems exist in context � That context is the real world � Is the real world continuous or discrete? 7 Lecture 7: Continuous Modeling 10/31/2020
Physical Systems � If we embed into a physical system � What is the effect � What do we want to happen � Will we achieve the effect � What can go wrong � Need to model the physical system 8 Lecture 7: Continuous Modeling 10/31/2020
Consider a RC Car � What are you concerned with � Position of the car (dimension? ) � Location, wheel positions, velocity � Relative or absolute � What are your controls � Accelerate or not � Binary or can you control � How does this affect the position � Turn left/right � Binary or can you control � How does this affect the position 9 Lecture 7: Continuous Modeling 10/31/2020
RC Car � What accelerate means � How much acceleration � Maximum speed � Slowdown when not accelerating � What does turn mean � How fast does the wheel turn � Maximum amount of turn � Does car follow wheel? 10 Lecture 7: Continuous Modeling 10/31/2020
RC Car � Suppose you want to track a line � Can tell left/right by amount � What should you do? 11 Lecture 7: Continuous Modeling 10/31/2020
Understanding Physical Systems � Suppose our RC car were breakable � E. g. helicopter, drone, … � Suppose it had real effects � E. g. real car, X-ray machine � Pin ball machine � Suppose it were expensive � Or not yet built � How would you create the embedded program 12 Lecture 7: Continuous Modeling 10/31/2020
Simulation Environment � Create a simulation environment � Simulate the physical world � Let your program run as part of the simulation � This requires modeling the real world � Physical details � Gravity, motion, etc. � Can be chemical, heat, … � How many have written a simulation? 13 Lecture 7: Continuous Modeling 10/31/2020
Simulation � Small time step � Fixed or variable � Track position, velocity, orientation, state � Of each object � Or subobject (e. g. wheels of RC car) � Compute next state � Next position, velocity, orientation � Consider a bouncing ball 14 Lecture 7: Continuous Modeling 10/31/2020
Simulation � Effectively doing calculus � Small step = delta � Smaller step => more accurate � This is why you develop the formulas as in the text � Different ways of computing next � Runge-Kutta, … � Different amount of current and next deltas � For our purposes, simplest should work 15 Lecture 7: Continuous Modeling 10/31/2020
Time Step � Larger time step is faster � Smaller time step is more accurate � For simple motion, not that important � Complex motions, collisions � Need a time step that ensure events � Pinball machine � Pool table � RC Car 16 Lecture 7: Continuous Modeling 10/31/2020
Simulation � Can be expressed in modeling language � Matlab (Simulink), Lab. View provide a graphical interface � Various systems exist � Special-purpose systems (e. g. flight simulators) 17 Lecture 7: Continuous Modeling 10/31/2020
Simulation Concerns � Fidelity � How accurately does it model the real world � How important is this accuracy � Speed � Does it operate in real time � Does it operate on the time scale of your system � Debugging � Can you debug your program using it � Can you simulate real-world faults 18 Lecture 7: Continuous Modeling 10/31/2020
Modeling and the Real World � We might want to model both the system and the world � The system is a finite automata � The real world is continuous � Continuously changing variables � Problems � How to combine the two � What can we do with the combination � Understanding � Proving properties 19 Lecture 9: Modeling Hybrid Systems 10/31/2020
Modeling in the Real World 20 Lecture 9: Modeling Hybrid Systems 10/31/2020
Hybrid Automata � State variables reflect the real world � Updates done over time automatically � Updates can use continuous modeling � Can specify velocity, acceleration rather than position � Can specify rules for doing the update 21 Lecture 9: Modeling Hybrid Systems 10/31/2020
Bouncing Ball Example 22 Lecture 9: Modeling Hybrid Systems 10/31/2020
RC Car 23 Lecture 9: Modeling Hybrid Systems 10/31/2020
Hybrid Timed Automata � The external (timed) portion is set outside the automata � Rules for continuous update � Model the physics of the real world 24 Lecture 9: Modeling Hybrid Systems 10/31/2020
Why Model the World? � Does the code need to know the state of the real world � Either precisely or approximately � Does it need to compute that state � Or retrieve it via sensors � How accurate can you model the world? � Consider speed control on a car � Proving properties of the program wrt the model � Does this prove the system correct 25 Lecture 9: Modeling Hybrid Systems 10/31/2020
Implementation Approach � Get information from the real world � What the actual speed is, change of speed � Actual temperature (and change rate) � Compute the difference between actual and desired � This is called the error � Use this to determine the action � This is considered feedback 26 Lecture 9: Modeling Hybrid Systems 10/31/2020
Control Theory � Why are computers called “digital computers” � What is the alternative � What is an analog computer � It turns out that analog computers are easy to build � Either mechanically or electronically � Predate digital computers 27 Lecture 9: Modeling Hybrid Systems 10/31/2020
Analog Computers 28 Lecture 9: Modeling Hybrid Systems 10/31/2020
Analog Computer � Can do simple math operations � Some complex math operations are easy � Integration, differentiation � LM 107 is an op amp 29 Lecture 9: Modeling Hybrid Systems 10/31/2020
Controlling Physical Systems � Analog computers are designed for this � If you want a car to go in a straight line � Use feedback from left/right to update steering � Use intergrator to determine error to control system � Analog control is all about feedback � Hybrid digital-analog systems � We can do the same in digital systems 30 Lecture 9: Modeling Hybrid Systems 10/31/2020
Embedded Systems � You can model the outside world � Within your program � But you probably don’t want to simulate it � WHY? � What do you do instead � Get outside state, compare to expected � Compensate 31 Lecture 7: Continuous Modeling 10/31/2020
Feedback � In general, your system doesn’t try to compute the next step based purely on the model � Instead it uses feedback to control the system � Positive versus negative feedback 32 Lecture 7: Continuous Modeling 10/31/2020
Control Systems � Desired output value: target value � Actual output value: measured value � Actuator input: controls the plant’s behavior � Error: desired - actual 33 Lecture 14: Feedback 10/31/2020
Control Variables � The actuator input can be binary or continuous � Amount of heat, turn, gas, … � Turn left/right, turn on heat, accelerate � The outputs (and error) can be a vector or a scalar � Optimize for a single factor (speed, temperature, …) � Optimize for multiple factors (temp + humidity, …) 34 Lecture 14: Feedback 10/31/2020
On-Off Control � Suppose we do the simple thing for a heater � If actual temp < target then turn on heater, else off � What is going to happen to the temperature � Overshoot � Time to heat up (undershoot) � Oscillation 35 Lecture 14: Feedback 10/31/2020
Smarter On-Off Control � A little more sophisticated � temp < target – delta 1 : HEAT ON � temp >= target – delta 2 : HEAT OFF � temp > target + delta 3 : COOL ON � temp <= target + delta 4 : COOL OFF � What’s going to happen here � What if it is very cold (hot) outside 36 Lecture 14: Feedback 10/31/2020
Proportional Control � Suppose we have control over the actuator � Can give it a range of values (low/high, continuous, …) � Acceleration in a car, heater with low/high flame (emergency mode), variable speed fan � What would we want to do in that case 37 Lecture 14: Feedback 10/31/2020
Proportional Control � Make the actuator input proportional to the error � Large error -> large input (accelerate fast) � Small error -> small input (accelerate slow) � No error -> do nothing � Assume doing nothing drives system the other way � Or that there is a corresponding input on the other side � Actuator = Kp * Error 38 Lecture 14: Feedback 10/31/2020
Demo � http: //sites. google. com/site/fpgaandco/pid 39 Lecture 14: Feedback 10/31/2020
Problem: What should Kp be � Should be > 0 � Actual value depends on the system � How could you determine the value? � Modeling � Mathematics � Experimentation 40 Lecture 14: Feedback 10/31/2020
Is This Sufficient � Will it eliminate overshoot, oscillation, slow rise time � Depends on the actual system � If the system is not perfectly linear or the actuator is not immediate, then probably not � We can do better 41 Lecture 14: Feedback 10/31/2020
Proportional-Derivative Control � A and B are two situations leading to point T � What should the output be for each? 42 Lecture 14: Feedback 10/31/2020
Proportional-Derivative Control � Want to take the rate of change into account � Fast rate – slow down the response � Slow rate – speed up the response � Actuator = Kp * error - Kd * deriv � deriv = the derivative of the error � deriv = change in error over time � deriv = change in error from last time to this 43 Lecture 14: Feedback 10/31/2020
Choosing Kp and Kd � Now we have two parameters to determine � How could you do this � Generally Kd is > Kp � Note the Kd is subtracted, but stated as positive 44 Lecture 14: Feedback 10/31/2020
Is This Sufficient � Steady state error � How could this occur 45 Lecture 14: Feedback 10/31/2020
Determining Steady State Error � Look at the sum of the error � In the past � Not necessarily full past � Or constrain in bounds � This is the integral of the error � How might you compute this 46 Lecture 14: Feedback 10/31/2020
Computing Integral of Error � Approximate with sum � integ = integ + error; � if (integ > MAX) integ = MAX; � else if (integ < MIN) integ = MIN � Actuator = Kp*error – Kd*deriv + Ki*integ � Ki now needs to be chosen � Typically much smaller than Kp 47 Lecture 14: Feedback 10/31/2020
Issues in Controllers � Actual input might have a limit range/set of values � Set the actuator to the nearest value � Off/on based on threshold � Sampling rate affects the computation � Might want to average the derivative � Computations are typically non-integer 48 Lecture 14: Feedback 10/31/2020
PID Tuning � Set Ki=0, Kd=0, Kp=1 � Increase Kp until the actual oscillates with a constant amplitude � Let U = this Kp � Let P = oscillation period (in seconds) � Set Kp = U/1. 7, � Ki = (Kp*2), � Kd = (Kp*P)/8 49 Lecture 14: Feedback 10/31/2020
PID Tuning � Requires sophistication � Control theory � Control system design � Control engineers 50 Lecture 14: Feedback 10/31/2020
For More Information � Wikipedia : PID � http: //www. embedded. com/design/embedded/4211211/ PID-without-a-Ph. D � Lab next week 51 Lecture 14: Feedback 10/31/2020
- Slides: 51