●●● 3. Controlling Robot Car by ●●●● ●●●●● ●●●● ●●●●● Wireless sensor ●●●● o The ultimate objective of the project is to control the car motion using the steering wheel o The only connection between the steering wheel and car racing system is the wireless marker attached to it o We have to make use of the marker data to control the car motion Reference: 1.J.M. Hart, Windows System Programming, 3rd Ed, Addison-Wesley, 2005,Ch.7,12 Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun
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
●●● Problem of the previous ●●●● ●●●●● ●●●● ●●●●● Program ●●●● e The markers data are obtained by calling the function Latus: UpdatePO ( o In the previous program, Latus:: UpdatepO ( are called inside the functions EIE330ProjectApp: renderInfo and EIE330ProjectApp: process Calculation) The above 2 functions will be called by ogre only when it updates the le 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
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
●●● ●●●● ●●●●● Update ●●●● Ogre main ●●●●● screen ●●●● program process Calculation If the screen is mLatus:: UpdatePO () updated once per 100ms. markers data can only be renderInfo o) obtained in the same rate, i. e mLatus:: UpdatePO( once per 100ms Finish update Department of ELECTRONIC AND INFORMATION ENGINEERING screen 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
●●● ●●●● ●●●●● Solution -Multithreading ●●●● ●●●●● ●●●● Get Markers Ogre main Update data program screen UpdatePOO proces s calculation o renderInfoo Routine and b ru at the Routine B same time Routine a (virtually) Finish update screen
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)
●●● ●●●● ●●●●● ●●●● ●●●●● 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
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
●●● ●●●● ●●●●● ●●●● ●●●●● A Process And its threads ●●●● Windows Process 1 Winword Process 5 Process 2 Lab3 Threads Notepad Process 3 Process 4 Winword+ Excel 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
●●● ●●●● ●●●●● ●●●● ●●●●● 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 Ato start its execution as long as program B is not waiting for the result from programA 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
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
●●● ●●●● ●●●●● How Threads are executed? ●●●● ●●●●● ●●●● o 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 thi g ree states Start Finish execution e RI unning execution Sleeping Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun
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: Ready Running 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?
●●● ●●●● ●●●●● ● 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. /0 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
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
●●● ●●●● ●●●●● ●●●● ●●●●● ●●●● 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 o 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
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