Download presentation
Presentation is loading. Please wait.
Published byAngela Watkins Modified over 6 years ago
1
ROBOTC for VEX Robotics (VEX IQ) On-Line Session 2016 Friday - Day 5
Take the first few minutes of class and watch the videos on Direct Control in the curriculum. Heath Lauster: Instructor Zach Deloe: Tech Assistant
2
Remote Control
3
Remote Control
4
Remote Control
5
Remote Control
6
Remote Control
7
Remote Control - Names ROBOTC can access all of the data from the VEX IQ Remote Control by referencing the buttons and axes by their described names. Joystick buttons return values of… 1 – Pressed 0 – Not Pressed/Released Joystick Axis return values of… -100 to (0 when centered) ChA ChD ChB ChC BtnEUp BtnEDown BtnFUp BtnFDown BtnRUp BtnRDown BtnLUp BtnLDown
8
Switching Controller Mode
When using the VEX IQ Remote Control, make sure to switch your “Controller Mode” Tele-Op: Requires a Remote Control to be powered on and linked before program will run. Autonomous: No Remote Control required, but program will not read data from the Remote Control Switch this mode from: “Robot Menu - >VEX IQ Controller Mode”
9
Joystick Control – Tank Control
ChA ChD
10
Joystick Control – Arcade Control
ChA ChB
11
Joystick Control – Arm Control
BtnEUp BtnEDown BtnFUp BtnFDown BtnRUp BtnRDown BtnLUp BtnLDown
12
Create a Joystick Program
Using Robot C Graphical create a new Direct Drive program with arm control. Download it to your Clawbot Before you download make sure the controller mode is set for TeleOp.
13
Joystick Control – Get Data
getJoystickValue(joystickChannel) This command will return the current value of a joystick axis (-100 to +100) or joystick button (0 or 1) Parameter Description Default Value joystickChannel The name of the specified joystick component to be accessed. Valid names include: Joystick Axis: ChA, ChB, ChC, ChD Joystick Button: BtnEUp, BtnEDown, BtnFUp, BtnFDown, BtnLUp, BtnLDown, BtnRUp , BtnRDown N/A Returned Value Value returned by the specific Axis or Button. Joystick Axis: -100 to +100 (0 when centered) Joystick Button: 0 (not pressed) or 1 (pressed)
14
Joystick Control - Examples
Simple Joystick Drive Notice how ChA + ChB and ChA – ChB allows the user to turn. For example: If I move my Joystick like this with Arcade Control: ChA = 0 ChB = 50 Thus, the leftMotor = 50 and the rightMotor = -50 As a result, the robot turns.
15
Joystick Control - Examples
Continuous Loop for “Tank Drive”
16
Joystick Control--Examples
Adding Buttons—Control the Arm
17
Joystick Control--Examples
Adding Buttons—Control the Claw
18
Joystick Control--Advanced
What can we do to utilize all of the buttons on the Remote Control? What are the things that we are going to do repeatedly? How can we address those things to make our use of the Remote Control more efficient?
19
Review From Thursday
20
Playing Sounds VEX IQ Speaker
The VEX IQ system has a built in speaker that allows you to play tones and built in sounds. You will need to make sure sound is enabled on your VEX IQ: At the main menu, go to “Settings” (press the “X” button) The top option in the settings should say “Sound On” If it doesn’t press the “Check” button to toggle the sound “on” VEX IQ Speaker
21
Playing a Sound Effect playSound(nameOfSound); This command will play a built-in sound effect once. Parameter Description Default Value nameOfSound Specify the name of the sound effect you would like the VEX IQ system to play. The name of the sound files are below: soundSiren2 / soundWrongWay / soundWrongWays soundGasFillup / soundHeadlightsOn / soundHeadlightsOff soundTollBooth / soundCarAlarm2 / soundTada soundGarageDoorClose / soundRatchet / soundAirWrench soundSiren4 / soundRatchet4 / soundCarAlarm4 soundPowerOff2 N/A
22
Sound Examples Play the sound effect “Air Wrench”
playSound(soundAirWrench); sleep(500); Play the sound effect “Tada” playSound(soundTada); sleep(500); Note: Sounds typically need a “Sleep” time to give them time to actually be played.
23
Playing a Sound Effect Repeatedly
playRepetitiveSound(nameOfSound, duration); This command will play a built-in sound effect multiple times for a specified amount of time. Parameter Description Default Value nameOfSound Specify the name of the sound effect you would like the VEX IQ system to play. The name of the sound files are listed previously. N/A duration This is how long to play the sound effect for. This amount of time is in 10s of milliseconds (100 = 1 second)
24
Sound Examples Play the sound effect “Air Wrench” repeatedly for 1 second. playRepetitiveSound(soundAirWrench, 100); sleep(1000); Play the sound effect “Tada” repeatedly for 3 second. playRepetitiveSound(soundTada, 300); sleep(3000); Note: Sounds typically need a “Sleep” time to give them time to actually be played.
25
Sound Examples
26
VEX IQ LCD Screen The VEX IQ has a LCD Screen that can be used to display text, number, or shapes/drawings to assist with programming. The VEX IQ LCD has a specific area of the screen that is for the user to draw/write text into. Users can choose to either write to line numbers or directly to x/y coordinates on the LCD screen. Text Line #0 Text Line #1 Text Line #2 Text Line #3 Text Line #4 (0, 47) (127, 47) 48 Vertical Pixels (Y) 128 Horizontal Pixels (X) (0, 0) (127, 0)
27
Display Text to the LCD displayTextLine(lineNumber, “Text”); This command will display a line of text on the VEX IQ LCD Screen. Parameter Description Default Value lineNumber The line number to write to on the VEX IQ LCD. Any previous text on the specified line number will be deleted. Line numbers range from 0-4. N/A “Text” This is the text to be displayed. Because the text displayed is considered to be a ‘string’, it must be surrounded by double quotes. You can display up to 21 characters on a single line.
28
Display Text Example Display the words “Hello World!” to the middle of the VEX IQ LCD Screen. displayTextLine(2, "Hello World!"); sleep(1000); Note: Display commands will need “sleep” time to allow the text to be displayed if the program is not being controlled by a loop.
29
Display Numbers to the LCD
displayTextLine(lineNumber, “Text”, variable); This command will display a line of text and the value of a variable to the VEX IQ LCD Screen. Parameter Description Default Value lineNumber The line number to write to on the VEX IQ LCD. Any previous text on the specified line number will be deleted. Line numbers range from 0-4. N/A “Text” This is the text to be displayed. Because the text displayed is considered to be a ‘string’, it must be surrounded by double quotes. The text can also display “formatting codes” to display values and variables. variable The name of a variable or value – this could be anything that returns a numeric value (sensors, encoders, etc.)
30
Display Text with Numbers Example
A program to constantly update the VEX IQ LCD Screen with the current value from the Gyro Sensor
31
Other LCD Commands Erases everything in the user’s area of the VEX IQ’s LCD screen. eraseDisplay(); Turn on and off a specific pixel at a specific x/y coordinate. SetPixel(x, y); ClearPixel(x, y);
32
More Display Commands ROBOTC has over 50 different text and drawing commands available for the VEX IQ Take a look at the descriptions/examples of more at the ROBOTC.net Wiki
33
Variables in Graphical
In Robot C Graphical the primary type of variable is the float variable. Float variable will have digits on both sides of a decimal for high accuracy. This is in contrast to a int or integer data type, which will only house a whole number.
34
Variables in Graphical
Counter Game Physical Robot Only We will build together Use of touch LED to Start Introduction to display on LCD Introduction to sound Get Distance Continuous Show Program Get Distance Reset
35
Text Based ROBOTC
36
Convert Graphical to Texted Based Robot C
At this point you have used Robot C Graphical and know how to program your robot in the areas of Movement, Sensors, Program Flow, and Direct Drive to complete various challenges. However there are limits to Robot C Graphical and a transition to Text Robot C will allow you to expand your horizons when programming robot behaviors. To easily see the differences between try “Convert Graphical File to Text” under the view menu.
37
Graphical File Vs. Text File
38
ROBOTC Rules/Syntax What does every program have to have?
task main() Set of "Curly Braces“ {} In what order does ROBOTC run a program? Sequentially – Top to Bottom What is whitespace? Space in a program to make it easier for a human to read. Simple Robot Commands (statements) always end with a…? Semicolon ;
39
ROBOTC Rules/Syntax Paired Punctuation Control Structures Comments
Some functions have "square brackets“ [] , other have "parenthesis“ () How do I keep them straight? Memorization Functions Library Control Structures Just like task main, they have a set of curly braces defining their beginning and end. {} Example? Comments //Necessary Evil Get into the habit now, or you won't do it ever Your students learn their habits from you…
40
ROBOTC Rules/Syntax Syntax is the cause of 80% of programming errors…
Most important thing to do when troubleshooting: Check Syntax Missing Semicolons Incorrect Braces on Structures Misspelled words Improper ‘case’ – motor vs. MoToR ROBOTC is friendly about most of these errors... but can’t catch everything.
41
New Commands moveMotorTarget(motor, distance, speed);
This command will set a specific motor to move a number of degrees at a specific speed. After the number of rotations have completed, the motor will automatically come to a stop. waitUntilMotorStop(motor); Because the “moveMotorTarget” is not a “blocking” command (otherwise you could only move one motor at a time), we can use the “waitUntilMotorStop” command to create a block that will wait until a specific “target movement” is complete.
42
Other Motor Commands setMotorSpeed(motor, speed);
Sets the speed of the motor (-100 to +100) Motors will run until told otherwise or the end of the program. Set the speed to zero (0) to turn the motor off.
43
Other Motor Commands setMotorTarget(motor, distance, speed);
motor – specifies which motor to move distance – the distance the motor should travel in motor degrees (based on it’s total distanced moved) Note if this value is negative, the motor will move backwards. 50 – the speed (0 to 100) the motor will travel.
44
Other Motor Commands getMotorEncoder(motor); resetMotorEncoder(motor);
Retrieves the value of a motor’s encoder (in degrees traveled) for use in a conditional statement or storing to a variable. resetMotorEncoder(motor); Resets the motor’s encoder back to a value of zero (0).
45
Sentry Challenge Program the robot to drive one full lap around a box.
Use any combination of the new motor commands in your solution.
46
Challenge: Labyrinth Complete the ”Labyrinth Challenge”
First “flowchart” or plan your program Then program your robot If finished early… Make sure your code is commented
47
Variables What is a variable? How do they work?
A variable is a facility for storing data in a program. How do they work? When a variable is “declared”, the compiler sets aside a piece of memory to store the variable’s numeric value. When the variable is called, the processor “retrieves” the numeric value of the variable and “returns” the value to be used in your program in that specific location.
48
Variables How do I create a variable?
To create (“declare”) a variable you need 3 things. Decide the data type of the variable (more on this soon) Give the variable a name Assign the variable a value (optional, but recommended) Example: int myVariable = 1000; Type: Integer Name: myVariable Value: 1000
49
Data Types There are 8 different types of variables in ROBOTC
Integer (int) – Memory Usage: 16 bits / 2 bytes Integer Numbers Only Ranges in value from to Long Integer (long) – Memory Usage: 32 bits / 4 bytes Ranges in value from to Floating Point (float) – Memory Usage: 32 bits / 4 bytes Integer or Decimal Numbers Variable precision, maximum of 4 digits after decimal
50
Data Types There are 8 different types of variables in ROBOTC
Single Byte Integer (byte) – Memory Usage: 8 bits/1 byte Integer Numbers Only Ranges in value from -128 to +127 Unsigned Single Byte Integer (ubyte) – Memory Usage: 8 bits / 1 byte Ranges in value from 0 to +255 Boolean Value (bool) – Memory Usage 4 bits / .5 bytes True (1) or False (0) values only.
51
Data Types There are 8 different types of variables in ROBOTC
Single Character (char) - Memory Usage: 8 bits / 1 byte Single ASCII Character only Declared with apostrophe – ‘A’ String of Character (string) - Memory Usage: 160 bits / 20 bytes Multiple ASCII Characters Declared with quotations – “ROBOTC” 19 characters maximum per string)
52
Variables Some additional notes
Adding “const” in front of a variable will make that variable a constant. This will prevent the variable from being changed by the program Constants do not take up any memory on the VEX IQ The VEX IQ has room for 7500 bytes of variables
53
Variables Some additional notes
Variable’s names must follow a specific rules:
54
Using Variables Variables can be used in your program anywhere!
Motor Speeds, If/Else Loops, Conditional Statements Commands you know act just like variables SensorValue – Returns the value of a sensor value Variables are just numbers You can perform math operations on variables newVariable = oldVariable + 15;
55
Using Variables VS.
56
Formatting Strings Formatting Characters:
ROBOTC supports a number of formatting characters for created a formatted string.
57
Functions Functions are used to group together several lines of code, which can then be referenced many times in task main, or even other functions. Convenient uses in the Sentry Challenge Moving Forward for Distance Turning 90 Degrees Left (or Right) Creating Functions Example: Moving Forward for Distance Function Definition (Part 1 – The code that goes with the function) Using Functions Function Call (Part 2 – Where you want the function code to run)
58
Function Definition The Function Definition “defines” what the function is and does To create a Function Definition: 1. Set the “type” of function by declaring the “data type” of the function. Void is a special data type which means no value will be returned Commonly used when the robot is carrying out instructions 2. Give the function a name. Following the same rules as variables, motors, and sensors 3. Add a set of parenthesis and curly braces Parenthesis are used for “parameters” Curly braces define the beginning and end of the function.
59
Function Call Once you have created a Function, you can “call” it inside of task main (or another function) by referencing its name, along with a set of parenthesis and a semicolon. Note: Keep functions above task main, or else you will have to have a “Function Prototype”
60
Functions: Program Flow
Even with functions defined, the robot starts executing code at task main When the robot reaches a function call, the Program Flow jumps to the Function Definition and running there When the robot finishes running the code in the function, Program Flow returns to task main
61
Function Practice Create Functions for the Sentry Challenge:
Move Forward for a Number of Rotations Point Turn 90 Degrees Right Point Turn 90 Degrees Left
62
Functions and Variables
Key Concept: Variable “Scope” Variables are “local” to where they are declared. Just because “distanceInDegrees” exists in “task main” doesn’t mean it can be used in a function. “distanceInDegrees” is currently localized to only “task main”
63
Functions and Variables
Solution #1 – “Globalization” Setting the variable outside any function will cause it to become a “global” variable. This is not an ideal solution because multiple functions can use and modify this variable! Use sparingly, but don’t be afraid to use it…
64
Functions Part 2 (Solution # 2)
65
Functions with Parameters
When working on the Sentry Challenge (or many other challenges) going forward the same distance in the function isn’t very helpful. What is controlling the distance that the function allows the robot to move forward? What programming tool do we have that lets us store numbers in our code? Variables! Functions allow use for a special kind of variable, called a Parameter. Parameters allow you to pass values into functions to account for small differences, like the number of degrees to move forward.
66
Function Definition with Parameters
A parameter is simply a variable that you are using to pass data (numbers, strings, etc.) into your function Creating a parameter is similar to creating a variable You must give it a “type” and a “name” Multiple parameters can be separated by commas Once you create a parameter, don’t forget to use it in the code of your function
67
Function Call with Parameters
Once you create a function with parameters, you must provide a value to fill those parameters in your function call To assign a value to the parameter, simply place that value between the parenthesis of the function call If you are using multiple parameters, you must specify their values in order!
68
Functions: Program Flow 2
Program Flow when working with Functions with Parameters is very similar to Functions without Parameters This time, when the robot encounters the Function Call, the value (960) is passed into the “distanceInDegrees” Parameter, and used as part of the moveMotorTarget commands
69
Functions and Variables
Solution #1 – “Globalization” Setting the variable outside any function will cause it to become a “global” variable. This is not an ideal solution because multiple functions can use and modify this variable! Use sparingly, but don’t be afraid to use it…
70
Functions and Variables
Solution #2 – “Passing Values” Instead of using the variable, we can just pass the value as a parameter to our function instead. This method is ideal because it gives you flexibility in your functions. This requires us to edit our existing functions, however.
71
Functions and Variables
Variables are now declared inside of the parameter field of the function. Multiple variables are separated by a comma. Variables are used inside of the function, but are localized to this function only. Each time the function is called a different value can be passed. This promotes code flexibility and reuse!
72
Variable Locations It is important where you declare your variables:
Variables declare outside of any structure (function or task) are considered to be “global” Variables declared inside of any structure (function to task) are considered to be “localized” to that structure. Variables declared in a loop/conditional statement are localized to that statement block It is always best to declare your variables in the correct location (usually at the top of your program)
73
Functions with Parameters Practice
Expand your previous functions : Move Forward for x Degrees at Y Speed Point Turn x Degrees Right Point Turn x Degrees Left Reverse for x Degrees at Y Speed
74
Sentry Challenge (Last Time)
Convert your solution to use Variables and Functions to solve
75
Other Function Types Functions don’t always have to be a “void” function. You can use any data type that you would assign to a variable! – int, float, bool, etc. A special command “return” is required to return a value back to the parent function.
76
Other Function Types Instead of “void” this function uses “int”. This tells us that this function will return an integer result. After we add 10 to the parameter value that was passed to use by task main, we send the new value back by using the “return” command. We assign the returned value to a variable so we can use the Debugger to verify the correct value was returned.
77
Other Function Types
78
Math with Functions
79
Final Challenge Complete the ”Minefield Challenge” If finished early…
First “flowchart” or plan your program Then program your robot Use Robot C Texted base with Natural Language Use a function in the program. If finished early… Make sure your code is commented
80
Creating a Function Library
Make sure you save in the same place Suppress warnings for unused functions #pragma systemFile
81
Global Variables Debugger
82
Constant Variables It’s best to intelligently use variables to preserve as much of this space as possible Constant variables and #define statements can help: Adding “const” to any variable will make it a compile-time constant and not use any variable space. A #define statement will create an alias to a value or statement in a single variable name.
83
Constant Variables Examples:
84
Variable Locations It is important where you declare your variables:
Variables declare outside of any structure (function or task) are considered to be “global” Variables declared inside of any structure (function to task) are considered to be “localized” to that structure. Variables declared in a loop/conditional statement are localized to that statement block It is always best to declare your variables in the correct location (usually at the top of your structure)
85
Variable Locations In this example, “test2” is not known outside of the “if” statement block.
86
Arithmetic Operators ROBOTC accepts a number of arithmetic operators:
Assignment: a = b Addition: a + b Subtraction: a – b Multiplication: a * b Division: a / b Modulo (remainder): a % b Increment: ++x, x++ Decrement: −−x, x−− Unary Positive: +x Unary Negative (inverse): −x
87
Arithmetic Operators Differences between ++i and i++
++i will increment the value of i, and then return the incremented value i++ will increment the value of i, but return the pre-incremented value.
88
Comparison Operators ROBOTC supports 6 different comparison/relational operators between two values: Equal to: a == b Not Equal to: a != b Greater than: a > b Less than: a < b Greater than or equal to: a >= b Less than or equal to: a <= b
89
Logical Operators ROBOTC has 3 logical operators to assist when making complex decisions: Logical Negation (NOT): !a Logical AND: a && b Logical OR: a || b
90
Logical Truth Tables AND (&&) OR (||) NOT (!)
91
If/Else Statements If the “while(true)” loop was missing, this code would only execute the “if” or “else” section once.
92
If/Else Statements If statements do not require an “else” statement.
If the “if” statement is false, it will just be skipped over. You can also make a multiple decision “if/else” statements. Example on the right
93
Make your code more readable by using the “else if(condition)” command
“else if” Shorthand Make your code more readable by using the “else if(condition)” command
94
If/else Shorthand You can also have a single line “if” statement with out the need for curly braces “{“
95
For Loops “For Loops” are important for repeating a block of a code a certain number of times. For loops require a specific structure:
96
For Loops For Loop Syntax:
Initial – The variable that will be used to count the number of iterations through the loop Example: int i = 0; Condition – The conditional statement to decide how many iterations to loop through Example: i < 10; Increment – The statement to modify the counter variable through each iteration of the loop. Example: i++ No semicolon at the end of this increment portion
97
For Loops Drive and Turn – Loops 10 Times
98
For Loop Walkthough Create the for loop initial variable
Check the condition of the for loop If true, continue onward If false, skip the “for” loop Run code inside of “for” loop Run the iteration code and increment “i” by one. Loop steps 2-4 until 2 returns false.
99
Loop Control Two control statements are available to you when using “while” and “for” loops: continue; - skips any remaining code below the continue statement and proceeds with the next iteration in the loop. break; - breaks out of the current looping structure and proceeds execution of the rest of the program.
100
Loop Control The continue statement will cause the robot to keep moving forward until the touch sensor it pressed. The break statement will end the infinite while loop if the touch sensor is pressed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.