CSCI1600: Embedded and Real Time Software

Slides:



Advertisements
Similar presentations
CS1101: Programming Methodology Aaron Tan.
Advertisements

Virtual Memory.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Peripherals and their Control An overview of industrially available “peripheral devices” that use “pulse-width modulation” for information passing. Review.
ENTC-489 Embedded Real Time Software Development Embedded Real Time Software Development Week 8 Review.
Chapter 8 Problems Prof. Sin-Min Lee Department of Mathematics and Computer Science.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
CS101 Computer Programming I Chapter 4 Extra Examples.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
June 10, 2002© Howard Huang1 Number systems To get started, we’ll discuss one of the fundamental concepts underlying digital computer design:
CSCI1600: Embedded and Real Time Software Lecture 8: Modeling III: Hybrid Systems Steven Reiss, Fall 2015.
CSCI1600: Embedded and Real Time Software Lecture 2: Introduction Steven Reiss, Fall 2015.
CSCI1600: Embedded and Real Time Software Lecture 33: Worst Case Execution Time Steven Reiss, Fall 2015.
CSCI1600: Embedded and Real Time Software Lecture 6: Modeling I: Continuous Systems Steven Reiss, Fall 2015.
CSCI1600: Embedded and Real Time Software Lecture 9: Input Output Concepts Steven Reiss, Fall 2015.
CSCI1600: Embedded and Real Time Software Lecture 28: Verification I Steven Reiss, Fall 2015.
CSCI1600: Embedded and Real Time Software Lecture 14: Input/Output II Steven Reiss, Fall 2015.
CSCI1600: Embedded and Real Time Software Lecture 23: Real Time Scheduling I Steven Reiss, Fall 2015.
CSCI1600: Embedded and Real Time Software Lecture 16: Advanced Programming with I/O Steven Reiss, Fall 2015.
Programming with LabVIEW Intro to programming and.
CSCI1600: Embedded and Real Time Software Lecture 5: Arduino Programming Steven Reiss, Fall 2015.
CSCI1600: Embedded and Real Time Software Lecture 15: Advanced Programming Concepts Steven Reiss, Fall 2015.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
CSCI1600: Embedded and Real Time Software Lecture 10: Modeling IV: Compositions of State Machines Steven Reiss, Fall 2015.
Computer System Structures
Lecture 3: Logic Bryan Burlingame 06 Sept 2017.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CS 6560: Operating Systems Design
Repetition (While-Loop) version]
Stack Data Structure, Reverse Polish Notation, Homework 7
About Instructions Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter PHY 201 (Blum)
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
An Embedded Software Primer
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
EET 2261 Unit 14 INCOMPLETE Analog-to-Digital Conversion (ADC) & Digital-to-Analog Conversion (DAC) Read. Homework #13 and Lab #13 due next week. Quiz.
CSCI1600: Embedded and Real Time Software
Threads and Multithreading
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
Coding Concepts (Basics)
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
SSEA Computer Science: Track A
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Case Study 1 By : Shweta Agarwal Nikhil Walecha Amit Goyal
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
CSCS-200 Data Structure and Algorithms
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
Embedded System Development Lecture 12 4/4/2007
CSCI1600: Embedded and Real Time Software
CSCI1600: Embedded and Real Time Software
Presentation transcript:

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?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Fixed Point Arithmetic Suppose we want to divide 10.234 / 5.1 Multiple 10234 * 10^3 = 10234000 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^3 + 5100/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

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

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

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

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