361 likes | 1.06k Views
Multitasking allows us to run multiple tasks (or processes) at the same time. Ideal Example : Getting your robot to walk and chew gum at the same time Multitasking does NOT allow us to run two lines of code as the same time – your robot only has one processor!
E N D
Multitasking allows us to run multiple tasks (or processes) at the same time. Ideal Example: Getting your robot to walk and chew gum at the same time Multitasking does NOT allow us to run two lines of code as the same time – your robot only has one processor! Real Example: The robot has to be walking, then chewing gum, then walking again, then chewing gum (loop over and over – really quickly!) What is Multitasking?
Why Multitasking? • You can have different parts of your program running together • Think about how a “wait1Msec” holds up your program? How about executing other code while one of your tasks is busy doing something else. • Best example: Having a “e-stop switch” in your program that works instantaneously.
Multitasking Example • Our Task: A robot that will detect a wall and then back away from the wall and turn away to avoid it. • Enhancement: Adding in a “e-stop switch” so if the robot misbehaves, we can stop it manually.
Multitasking Example Start Task Main Go Forward Is the Sonar < 20? Reverse for 5000 milliseconds Turn left for 2000 milliseconds Yes No Is the EStop-Switch == 1 Will this program end when the “EStop-Switch” is pressed? No Yes Stop Task Main
Multitasking Example • Because the “E-Stop Switch” check is apart of our main task, it will only be check as fast as the loop can run. • If stuck in the “backup and turn” portion, we can’t stop our robot until this completes! • Using multitasking, we can solve this issue! • Task 1 – Run Forward and Detect/Avoid Wall • Task 2 – Watch the E-Stop-Switch to see if it’s pressed.
Creating a New Task • Step 1: Create a task, just like a function: • Place this above task main() • Note instead of “void” we’ll use “task” as our identifier.
Creating a New Task • Step 2: Write the code for the “emergency stop” • The StopAllTasks() is a new function that will abort every task running at the same time (including task main)
Creating a New Task • Step 3: Add a StartTask(emergencyStop); call in the “task main” section:
Multitasking Solution Start Task Main Start Task emergencyStop Start Task: emergencyStop Go Forward Is the EStopSwitch == 0 No Is the Sonar < 20? Reverse for 5000 milliseconds Yes Yes Stop All Tasks No Turn left for 2000 milliseconds
Multitasking Under the Hood • So how does this work? • The two tasks are running in “Cooperative Multitasking” style. • The two tasks are actually switching back and forth so quickly that it appears as if they are running simultaneously. • The tasks each have the ability to give up their processing time and allow other tasks to run. • What command do you think provides this functionality?
Multitasking and Waiting • wait1Msec(); • By having a wait statement in a task, it free up the CPU to allow other tasks to execute.
“Task Status” Debugger • We can view the status of any tasks using the “Task Status” debugger window. • You will have to change your menu level to “Super User” to use this window.
Multitasking Woes • So multitasking seems pretty easy, right? • It can be VERY dangerous. • Improper use of multitasking can cause your program to behave incorrectly. • “Starving” a task of CPU time can cause it to stop at places you did not expect. • Multitasking should only be used sparingly • ROBOTC can execute a few lines of code per millisecond, so multitasking should not be use trivially.
Multitasking Do’s and Don’ts • Good times to use Multitasking • Displaying Diagnostics (encoder values) to your LCD screen while running your program • Emergency Stop functionality • Two person remote control of different motors • User 1 drivers the robot • User 2 control the gripper arm movement • Bad times to use Multitasking • Moving Forward commands • Automated pick-up and move actions • Single driver remote control • Use a function instead!
Multitasking In-Depth • So how exactly does it work? • Under the hood there is a “Task Scheduler” that controls when each task runs • This scheduler is built into the ROBOTC firmware • This task scheduler runs using a “Round Robin” style of cooperative multitasking to choose when each task runs
Multitasking In-Depth • ROBOTC Tasks • Up to twenty tasks (19 user, 1 main) can execute concurrently within a user program. • The ‘main’ or ‘primary’ task is automatically started when a user program is run. • Execution of other tasks can be started and stopped with the ‘StartTask’ and ‘StopTask’ functions.
Multitasking In-Depth • Time Slicing • ROBOTC will share CPU execution time among various tasks by giving each task a “time slice” where it will execute a group of instructions. • A task that is waiting (wait1Msec) for a time period to expire is not ready to run. Once the time period has expired it will again become ready to run. • Waiting tasks do not consume any CPU cycles. Read SensorValue Play Sound Do Math Operation Time Slices Evaluate While Loop Turn Motor On Task A Task B
Multitasking In-Depth • Priority • Each task can be assigned a priority from 0 to 255. • All tasks in ROBOTC (including main) have a default priority of 7. • The ROBOTC scheduler gives execution time to the highest priority task that is waiting to run. • A round robin scheduling scheme is used when there are multiple tasks ready to run all with the highest priority. • This prevents any task from being “starved” • Lower priority tasks will not execute until there are no tasks of higher priority that are ready to run. Read Sensor Evaluate While Loop Wait 1 Sec. Do Math Operation Time Slices Read Sensor Play Sound Evaluate While Loop Task A Priority - 10 Task B Priority - 7
Multitasking Commands • StartTask(TaskID, nPriority); • A functional call to start or restart the execution of a specific task. • StopTask(TaskID); • Stops execution of a single task. • StopAllTasks(); • A function call to stop every task running, including the main program task. This function is similar to pressing the gray "exit" button on your NXT.
Critical Sections • Critical section - a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution. • This shared resource is typically a global variable or global resource – resources owned to a single task typically do not require critical sections.
Critical Sections • The task schedule can give exclusive access to the CPU if a task requires it. • A single task can request to “lock up” the CPU time to perform a crucial calculation and ensure no other task can interrupt it. • Use the hogCPU(); and releaseCPU(); functions to create a critical section