1 3. Controlling Robot Car by Wireless Sensor The ultimate objective of the project is to control the car motion using the steering wheel The only connection.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Chapter 3 Process Description and Control
1/1/ / faculty of Electrical Engineering eindhoven university of technology Architectures of Digital Information Systems Part 1: Interrupts and DMA dr.ir.
Chapter 5 Processes and Threads Copyright © 2008.
ISP – 3 rd Recitation “The joy of Windows API” Processes Threads Handles Relevant functions A simple code example.
1 2. Controlling Robot Car in Ogre References: 1. LEGO.com MINDSTORMS NXT Home, 2. OGRE 3D, 3. MSDN,
CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Informationsteknologi Friday, November 16, 2007Computer Architecture I - Class 121 Today’s class Operating System Machine Level.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Building Secure Software Chapter 9 Race Conditions.
ISP – 4 th Recitation Times System Errors Threads Waits Code examples.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1.
CSE 380 – Computer Game Programming Render Threading Portal, by Valve,
Win32 Programming Lesson 9: Jobs & Thread Basics.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Processes and Threads.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
The Functions of Operating Systems Interrupts. Learning Objectives Explain how interrupts are used to obtain processor time. Explain how processing of.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1-1 © 2004 JMH Associates. All rights reserved. Windows Application Development Chapter 7 Windows Thread Management.
1 Confidential Enterprise Solutions Group Process and Threads.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Games Development 2 Concurrent Programming CO3301 Week 9.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
Multithreading GSP-420.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
Practical Sockets and Threads Derek Weitzel. Windows Threads Concurrent processing Concurrent processing Windows Create Thread Windows Create Thread HANDLE.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Electronic Analog Computer Dr. Amin Danial Asham by.
Department of Computer Science and Software Engineering
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Lecture on Central Process Unit (CPU)
Notes on Processes, Context, Context Switching The following slides contain partially quoted statements from various Wikipedia pages.
Windows Threading Colin Roby Jaewook Kim.
Processes and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
Assembly Language Co-Routines
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Threads and Thread Synchronization. Introduction In windows the basic unit of execution is the thread. It is the smallest schedulable unit of execution.
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Window Threads Chapter 7 Windows Thread Management.
Tutorial 2: Homework 1 and Project 1
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Processes and threads.
Operating Systems (CS 340 D)
Threads & multithreading
Processes in Unix, Linux, and Windows
An Embedded Software Primer
Processes in Unix, Linux, and Windows
Process Description and Control
Chapter 05. Multithread.
Threads Chapter 4.
Dr. Mustafa Cem Kasapbaşı
Processes in Unix, Linux, and Windows
CSE 153 Design of Operating Systems Winter 2019
COMP755 Advanced Operating Systems
Presentation transcript:

1 3. Controlling Robot Car by Wireless Sensor The ultimate objective of the project is to control the car motion using the steering wheel The only connection between the steering wheel and car racing system is the wireless marker attached to it We have to make use of the marker data to control the car motion Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun Reference: 1. J.M. Hart, Windows System Programming, 3rd Ed., Addison-Wesley, 2005, Ch.7, 12

2 Problem of the Previous Program The markers’ data are obtained by calling the function Latus::UpdatePO() In the previous program, Latus::UpdatePO() are called inside the functions EIE330ProjectApp::renderInfo and EIE330ProjectApp::processCalculation() The above 2 functions will be called by Ogre only when it updates the screen If the graphics is complex, the update rate can be very slow, e.g. 20 frames/sec or slower It means that the markers’ data can only be obtained in such a slow rate Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

3 Ogre main program processCalculation() { : mLatus::UpdatePO() : } Update screen renderInfo() { : mLatus::UpdatePO() : } Finish update screen : If the screen is updated once per 100ms, markers’ data can only be obtained in the same rate, i.e. once per 100ms Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

4 Solution - Multithreading Ogre main program processCalculation() { : } Update screen Finish update screen : renderInfo() { : } Routine A UpdatePO() { : } Get Markers’ data Finish : Routine B Routine A and B run at the same time (virtually)

5 Processes and Threads In Windows, usually a process will be generated when an application is executed When an application is executed m times, m processes will be generated, each with a different process ID A Windows process contains its own independent virtual address space with both code and data Each process contains one or more independently execution unit, called threads The Windows thread is the basic executable unit A process can Create new threads within the processes Create new, independent processes Manage communication and synchronization between these objects Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

6 A Process And Its Threads Windows Winword Process 1 Notepad Process 2 Winword Process 3 Excel Process 4 Process 5 Threads Lab3 Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

7 Why Threads? In normal execution, a program always needs to wait Wait for user input, wait for screen display, wait for file access, etc. It is unwise to require programs to execute one after the finish of another Program B can make use of the waiting time of program A to start its execution as long as program B is not waiting for the result from program A It was first proposed to achieve this by having multiple processes However, it was soon found that the overhead (e.g. the time required) for switching between processes is very high Besides, processes are not tightly coupled to one another, it is difficult to share resources, such as open files They motivate the idea of further dividing a process into smaller units, i.e. threads

8 A computer has only one CPU, which can execute one program at a time Hence, in reality, threads are not executing at the same time, but alternatively one after the other For a multithreading system, a thread has at least the following three states: ReadyRunning Sleeping Start execution Finish execution Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun How Threads Are Executed?

9 Ready state – All threads that are ready to execute but without the CPU are at the ready state – If there is only 1 CPU in the system, all threads except one are at the ready state Running state – The thread that actually possesses the CPU is at the running state – If there is only 1 CPU in the system, at most there is only one thread is at the running state Sleeping state – The process that is waiting for other resources, e.g. I/O or a preset time, is at the sleeping state Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

10 Threads will alternatively get into the CPU one after the other (called the round robin scheme) At the time that a thread is selected to be “in” the CPU – It goes from ready state to running state After that, it will be swapped out – It goes from running state back to ready state Or it may due to the waiting of a preset time – It goes from running state to sleeping state When time-up – It goes from sleeping state to ready state Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

11 Implementation - CreateThread HANDLE CreateThread ( LPSECURITY_ATTRIBUTES lpsa, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddr, LPVOID lpvThreadParm, DWORD dwCreationFlag, LPDWORD lpThreadId ); HANDLE mThread;// Handle to thread DWORD mThreadId;// Use to store the thread id UpdateParaStruct *pUPS = new UpdateParaStruct; // Define a structure to store passing parameters mThread = CreateThread(NULL, 0, RunningUpdateThread, (LPVOID)pUPS, 0, &mThreadId); Example Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

12 CreateThread Parameters Input parameters lpsa – A pointer to a security attribute structure. It determines whether the returned handle can be inherited by child processes. If lpsa is NULL, the handle cannot be inherited dwStackSize – Indicate thread’s stack size. Use 0 for default size lpStartAddr – A pointer to the function to be executed. For our example, the function RunningUpdateThread will be executed. RunningUpdateThread () should be defined as follows: DWORD WINAPI RunningUpdateThread (LPVOID parameter) { : } Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

13 CreateThread Parameters lpvThreadParm – A pointer passed as the thread argument. For our example, the pUPS pointer, i.e. the pointer of the structure UpdateThreadStruct, is passed to the thread function. dwCreationFlag – indicate the readiness of the thread. If 0, means that the thread is ready to run immediately lpThreadId – A pointer to a DWORD that receives the new thread’s identifier. The system will fill in it with the thread ID CreateThread will return the handle to the thread created. A NULL handle value indicates a failure Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

14 Passing Parameters to Thread A thread function only accepts one input parameter, i.e. lpvThreadParm If more than one parameters are to be passed to a thread function, the most convenient way is to put them in a struct and pass its pointer, e.g. struct UpdateParaStruct // For passing 2 data to the Update thread { Latus *pL; bool *pExit; }; Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

15 Receive the Passed Parameters When the thread function receives this pointer, it should first tell the function what kind of pointer it is Then retrieve the parameters stored in the structure DWORD WINAPI RunningUpdateThread (LPVOID parameter) {UpdateParaStruct * pUPS = (UpdateParaStruct *) parameter; Latus *pLatus = pUPS->pL; bool *pExit = pUPS->pExit; : }

16 To Control the Execution Rate of a Thread To control the execution rate, the simplest way is to force the thread to “sleep” for some time DWORD WINAPI RunningUpdateThread (LPVOID parameter) {UpdateParaStruct * pUPS = (UpdateParaStruct *) parameter; Latus *pLatus = pUPS->pL; bool *pExit = pUPS->pExit; while (true) {pLatus->UpdatePO(); Sleep(10); // Force to sleep for 10msec } return 0; } For the about thread, assume the other part of the program uses negligible amount of time, the execution rate of this thread is approximately once per 10msec

17 How a Thread Terminates Most common way: A thread terminates itself by returning from the thread function using the exit code as the return value DWORD WINAPI RunningUpdateThread (LPVOID parameter) {: : return 0;//exit code = 0 //Never return 259 since it is equal to the // constant STILL_ACTIVE, which is used to // indicate the thread is still active } Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

18 How to Know if a Thread is Still Alive We can check if a thread is still alive by calling GetExitCodeThread(), e.g. HANDLE hThread;// Handle to thread DWORD ThreadId;// Use to store the thread id UpdateParaStruct *pUPS = new UpdateParaStruct; hThread = CreateThread(NULL, 0, StartPlayer, (LPVOID)pUPS, 0, &ThreadId); : DWORD exitCode; if(GetExitCodeThread(hThread, (LPDWORD)&exitCode)) std::cout << exitCode << std::endl; //GetExitCodeThread will return nonzero if successful //The exit code is stored in exitCode Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

19 Force a Thread to Terminate The simplest way is to call the function TerminateThread(), but many side effects Another way is to let the thread function to check frequently a shared variable which can be modified by the thread outside DWORD WINAPI RunningUpdateThread (LPVOID parameter) {UpdateParaStruct * pUPS = (UpdateParaStruct *) parameter; Latus *pLatus = pUPS->pL; bool *pExit = pUPS->pExit; while (!(*pExit)) {: } return 0; } Assume pExit is a pointer of a shared variable which will be updated by a thread outside. If that thread wants this thread to terminate, it just needs to change such variable to TRUE

20 Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun Thread A Thread B CreateThread(… &Exit … ) bool Exit = false; &Exit if Exit == false if Exit == true return 0; // terminate bool Exit = true;

21 Software Architecture for Lab 3 Thread 2 Thread 1 Screen Update Thread 3 EIE330ProjectApp RunningUpdateThread RunningActionThread Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun Latus Exit 1 Exit 2 Latus Exit 1 Exit 2 &UpdateParaStruct &ActionParaStruct mLatus->UpdatePO() mLatus->Analysis() mLatus->Action() Game mLatus mGame

22 Analysis() After receiving the markers’ data, we should analyze the data to determine the kind of action we should perform The analysis is supposed to be done in a function Latus::Analysis() which should be called after each call to UpdatePO() The analysis result should be put in an array of ActionStruct structs, each struct has 2 fields Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun

23 Array of ActionStruct ActionStruct mActionTable[3] mActionTable[0] mActionTable[1] mActionTable[2] typedata Based on the analysis result, a number should be put by the function Latus::Analysis() to the type of an ActionStruct to indicate the kind of action to be performed. E.g. type = 1 go forward type = 2go backward type = 3turn right type = 4turn left For some actions, you may want to provide additional data Put it in the data field In some cases, you may want to define a sequence of operations for each analysis result Do it by filling in all 3 structs For some actions, you may want to provide additional data Put it in the data field In some cases, you may want to define a sequence of operations for each analysis result Do it by filling in all 3 structs

24 Action() After the action table is filled, the function Latus::Action() should be called to carry out the operations defined in the table one by one Since the car motion is much slower than the data received from the markers, the function Action() can be called in a much slower rate A separated thread is used to called Action() – called in thread function RunningActionThread() The rate of execution of this thread should match with the command execution rate of Mindstorms Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun