Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI1600: Embedded and Real Time Software

Similar presentations


Presentation on theme: "CSCI1600: Embedded and Real Time Software"— Presentation transcript:

1 CSCI1600: Embedded and Real Time Software
Lecture 17: Advanced Programming Concepts Steven Reiss, Fall 2017 Get lots of feedback from the class. Make them think. Should have 2-4 different implementations of the task loop on slides to show alternatives. (along with corresponding task prefix) Code for timer transition?

2 Programming Is about knowing the tricks of the trade
Libraries Patterns Techniques This week I’d like to show you some of these For real time and embedded programming Lecture 17: Advanced Programming 6/28/2019

3 Task Loop Tasks are modeled as automata
Code consists of executing each task periodically What is the main loop void loop() { task1(); task2(); taskn(); } What are the problems / issues Lecture 17: Advanced Programming 6/28/2019

4 Task Loop Coding Issues
Some tasks are periodic Should be run every k milliseconds Some tasks are sporadic Run when needed, not other times Tasks don’t know about other tasks Timing demands for example Tasks need to communicate Tasks have different priorities Lecture 17: Advanced Programming 6/28/2019

5 Example Suppose we have 3 tasks How would you code this?
One should be run every 6 ms One should be run every 10 ms One should be run when a flag is set How would you code this? What would the main loop look like What would the task prefix code look like Lecture 17: Advanced Programming 6/28/2019

6 Timed Task Loop Where do the numbers (2000,15,5,3) come from?
void loop() { long time = micros(); if ((time – last_time) >= PERIOD) { long last_time = 0; last_time = time; int cycle_counter = 0; cycle_counter = (cycle_counter+1)%15; boolean run_task = false; if (cycle_counter%3 == 0) taskA(); const int PERIOD = 2000; if (cycle_counter%5 == 0) taskB(); } if (run_task) taskC(); Where do the numbers (2000,15,5,3) come from? This is one way of doing it. What are other ways. Each task could maintain its own last_time and time. This is a bit more complex, but allows easy changes to task. Similarly taskC could always run and could check the run_task flag itself. Lecture 17: Advanced Programming 6/28/2019

7 Other Alternatives Wait at the end for next cycle to begin
Use a (priority) queue of tasks to run Tasks can be added/removed from queue Main loop Pop top task off queue, execute it Task can add itself back to queue if periodic Priority can be when the task needs to be run This looks a lot like scheduling Lecture 17: Advanced Programming 6/28/2019

8 Coding a Task Given a FSA model, what should the code do
The code should be designed to be quick State actions should never wait The code should take the next step, no more Any actions it does should be fast Setting output, reading input, setting flags It shouldn’t do any waiting Long computations should be broken up Lecture 17: Advanced Programming 6/28/2019

9 Coding a Task enum State { S1, S2, … Sn } State cur_state = S1; void taskA() { switch (cur_state) { case S1 : // check for transitions, set cur_state if so // also, do any action associated with transition case S2 : … } // Do any action associated with state Can simplify this if there are common actions or transitions by state (e.g. error flag for all states goes to error state) Lecture 17: Advanced Programming 6/28/2019

10 Coding Timing Information
Suppose a state has a timeout transition How should this be coded Example: beep for 5 seconds What’s wrong here: Turn on tone() wait(5000) Turn off tone() go to next state Lecture 17: Advanced Programming 6/28/2019

11 Coding a Timer Transition
Keep track of time of state entry Static variable (global or static for function) Set to current time when entering a state with a timeout Or to timeout time Code for the state checks if current time>last_time+TO If so, it transitions to the next state Implies that check is made often enough Need to know tolerance Lecture 17: Advanced Programming 6/28/2019

12 Coding a Timer Transition
If task is periodic with fixed period Runs every 10 ms for example Use the task timing in place of the real clock Can model the delay as a set of interim states Or just have a counter of the number of runs since you entered the current state Lecture 17: Advanced Programming 6/28/2019

13 Light Array Task What should this task look like?
What is the appropriate model? Lecture 17: Advanced Programming 6/28/2019

14 Floating Point Problem: computations sometimes need real numbers
Floating point is slow (not built into hardware) Integers aren’t accurate enough Solution Use fixed point arithmetic Essentially scaled integers Lecture 17: Advanced Programming 6/28/2019

15 Fixed Point Suppose we want to add 10.234 + 5.1
We could use integers scaled by 10^3 Add = === Can use different scalings (more complex) Multiplying is slightly more complex Mutliply * 5100 = Divide by 10^3 = = === Actually want to round ( ) / 1000 What do you do for division? Lecture 17: Advanced Programming 6/28/2019

16 Fixed Point Arithmetic
Suppose we want to divide / 5.1 Multiple * 10^3 = Divide by 5100 = 2006 with remainder 3400 3400 > 5100/2 so set result to 2007 === 2.007 Can also multiply by 10^4 to get more accuracy and scale back Can also take (10234*10^ /2)/5100 Computers don’t do this with powers of 10 Why not? Do it with powers of two (eg 8 bit fraction, 24 bit int) Lecture 17: Advanced Programming 6/28/2019

17 Simplifying Computations
Embedded systems deal with limited domain We can use this to simplify coding Suppose we know angle to a degree Want to compute next position in x,y coordinates This requires sin/cos computation How to compute sin and cos These require multiple floating point computations Taylor series approximation Lecture 17: Advanced Programming 6/28/2019

18 Look Up Tables Instead of computing it as needed, precompute it
We only need it for 90 values (0..90) Actually 45 will do with some extra computation Set up a table sin_data[90] with the result for each degree sin and cos then can be done with table lookup cos(X) = sin(X+90) sin(X+90) = 1-sin(X-90) sin(X+180) = -sin(X) … This can often be used for complex computations How could you use lookup tables in tic-tac-toe? Lecture 17: Advanced Programming 6/28/2019

19 Look up Tables How could you use look up tables in nim?
What about tic-tac-toe? Lecture 17: Advanced Programming 6/28/2019

20 Homework Read Chapter 10 NIM is due Wednesday
Exercise 10.4 NIM is due Wednesday Project Status Report due Next Wednesday Suitable for posting on the web site (HTML preferred) Lecture 17: Advanced Programming 6/28/2019


Download ppt "CSCI1600: Embedded and Real Time Software"

Similar presentations


Ads by Google