Presentation is loading. Please wait.

Presentation is loading. Please wait.

ROBOTC for VEX Professional Development

Similar presentations


Presentation on theme: "ROBOTC for VEX Professional Development"— Presentation transcript:

1 ROBOTC for VEX Professional Development

2 for(;;) Loops

3 for(;;) Loops Like a counter-based while() loop

4 Initial Value The equivalent of creating an integer counter variable and initializing it to 0

5 Condition The condition of the loop to control how many times it iterates

6 Increment Specifies how many to increment the value by each iteration of the loop

7 Comparison vs.

8 for(;;) Loop Exercise

9 Return Type Functions

10 Convert Distance to CM Inline Math Operations
Remember that all variables are simply representations of numbers. You can perform any math operation on a value. All operations are performed on the “right” side of the equation – then they’re assigned to the left side.

11 Calculate Distance Function
Code Reuse Remember that functions are useful when trying to utilize the same function multiple times. Parameters Allow the user to pass data into a functions to promote code reuse.

12 VEX LCD – Part 2

13 VEX LCD 2 x 16 Character Array
Connects to the UART 2 Port on the Cortex Display Sensor feedback or messages 3 Buttons can be detected and used

14 Ambient Light Sensor

15 Ambient Light Sensor Plugged into Analog Port 4 “Passive” Light Sensor
Measures the amount of ambient light in its environment Returns values between 0 and 4095 Range is 0 – 6 feet from the sensor Using them The SensorValue[] command

16 Different Types of Threshold
No Threshold Touch Sensors (Only 1 or 0) Chosen Threshold Ultrasonic Rangefinder (You choose the distance) Shaft Encoders (When guessing and checking) Measured Threshold Potentiometer (Viewed in the Sensor Debug Window) Calculated Threshold Shaft Encoders (When using a mathematical method) Measured and Calculated Threshold Line Following Sensor (Measurements taken, mean calculated) Light Sensor (Measurements taken, mean calculated)

17 Light Sensor Activities
Lights On / Lights Off Graphing Data to Debug Stream

18 Accelerometers

19 Accelerometers Electromechanical device that measures acceleration forces in three axis simultaneously. Connects to the Cortex using up to 3 Analog Ports ANALOG Ports 6, 7, and 8 Selectable sensitivity via jumper: ±2g and ±6g. 0-5V analog output (one for each axis). LED indicated power and proper connection. Mounts directly to VEX.

20 Accelerometers How they work: Additional Information:
“Piezoelectric Effect” – accelerometers contain microscopic crystal structures that get stressed by accelerative forces, which causes a voltage (and consequently an Analog sensor value) to be generated. Additional Information: Gravity is indistinguishable from upward acceleration, so the sensor will detect a constant 1.0G while at rest. If the board is mounted horizontally, gravity will effect only the Z axis. If the sensor is tilted away from the horizontal, the gravity reading on the Z axis will diminish, and the readings on the other axis will change depending on which way you are tilting it. Accelerometers are sensitive to heat, fluctuations in voltage, ect., so some filtering on the Sensor Data is necessary.

21 Accelerometer Activities
Wait for Acceleration Robot waits for you to tap it before performing its task Other Uses: Detecting when your autonomous robot crashes Keeping an arm level, going up a ramp

22 Accelerometer Control
A built-in Accelerometer provides X-Y tilt values Allows you to control an arm or drive system by changing the orientation of the controller

23 Accelerometer Control
Open the Accelerometer Control Sample Program in the Remote Control folder

24 Q & A

25 Libraries

26 Building an External Library
Now that we have our functions, we want to move onto being able to use them with copy/pasting all week. We’ll build a library file that we can use a #include “LibraryName.h” to store our functions.

27 Building an External Library
Step 1 – Open a new file

28 Building an External Library
Step 2 – Save the file with a “.h” extension “.h” is used for “header” files, but ROBOTC uses them for external libraries. Save this in the folder where you’ll be developing most of your code.

29 Building an External Library
Step 3 – Cut and Paste your functions from your program to your library file.

30 Building an External Library
Step 3 – Cut and Paste your functions from your program to your library file.

31 Building an External Library
Step 4 – Add: #include “CodeLibrary.h” to your program with “task main()” Your library is now complete!

32 Library Notes If every file uses the same library, be aware when making changes to the library. These changes will affect every other program you have written. Also avoid making routines that rely on sensors to be connected to specific ports unless your Motors and Sensor Setup matches exactly, you’ll get errors

33 Multi-Tasking

34 What is Multitasking? Multitasking allows us to run multiple tasks (or processes) at the same time. Ideal Example: Getting your VEX to walk and chew gum at the same time Multitasking does NOT allow us to run two lines of code as the same time – the VEX only has one processor, so this is not the same as on a computer. Real Example: The VEX has to be walking, then chewing gum, then walking again, then chewing gum (loop over and over)

35 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 “kill switch” in your program that works instantaneously.

36 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 “kill-switch” so if the robot misbehaves, we can stop it manually.

37 Will this program end when the “Kill-Switch” is pressed?
Multitasking Example Will this program end when the “Kill-Switch” is pressed?

38 Multitasking Example Because the “Kill-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 Kill-Switch to see if it’s pressed.

39 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.

40 Creating a New Task Step 2: Write the code for the “kill-switch”
The “StopAllTasks()” is a new function that will abort every task running at the same time (including task main)

41 Creating a New Task Step 3: Add a “StartTask(emergencyStop);” call in the “task main” section:

42 Multitasking Solution
1

43 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?

44 Multitasking and Waiting
wait1Msec(); By having a wait statement in a task, it frees up the CPU to allow other tasks to execute.

45 “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.

46 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.

47 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!

48 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

49 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.

50 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.

51 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.

52 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.

53 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.

54 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


Download ppt "ROBOTC for VEX Professional Development"

Similar presentations


Ads by Google