Post on 04-Jan-2016
ROBOTC for VEXOnline Professional Development
Warm-up Activity
• Review/Watch videos from VCVT– Especially the ones from the Fundamentals and
Movement sections
• Work on:– Labyrinth Challenge with obstacles with real robot– Movement > Sentry Challenge or Utility > Robotics
Academy Challenge with virtual robot
Warm-up Questions
• What is Firmware?• What are valid motor power levels on the VEX?• All commands that we want the robot to follow
must be within _________?• Each command must end with a(n) _________?• What VEX sensors are Analog? Digital? What
distinguishes them?
Standard ROBOTC: Basic Movement
Platform Type
1. Choose ROBOTC for VEX Robotics 4.X (not graphical)
2. Choose VEX 2.0 Cortex3. Deselect Natural Language if
needed
Get Your Motors Running
• Motor Check– Right Motor is plugged into MOTORS 2– Left Motor is plugged into MOTORS 3
• Open Sample Program– File > Open Sample Program > Training Samples >
Motor Port 3 Forward
Motor Port 3 ForwardDefines the “main task” of the robot
All commands belonging to task main must be in-between these curly braces
Motor Port 3 Forward
Turns the port3 motor on at full power forward
Motor Port 3 Forward
Robot stays here in the program for 3000 milliseconds
End Result: robot spins one motor for 3 seconds
Try it out!
Basic Movements
• Forward / Reverse
Basic Movements
• Point Turns
Basic Movements
• Swing Turns
Basic Movements
• Manual Adjustments
Movement Challenges
• VCVT Videos (recommended)– Watch videos in the Movement > Moving Forward
Section– Watch videos in the Movement > Speed and Direction
Section
• The Labyrinth– Remember to Pseudocode– Remember to Journal– The two are not mutually exclusive!
Troubleshooting
Troubleshooting Method1. Identify the problem. Don't tell me how to fix the
problem. Just tell me what it is.
2. What can we tell about the student because of the problem? What do they know? What don't they know?
3. What can we tell the student to fix their understanding AND the problem?
• Just giving the answer to the student teaches dependence!
• This method teaches!
Troubleshooting• Student: I want my robot to move forward, then turn. I
had it moving forward, and added the turn. Now it’s turning, then moving forward.
Troubleshooting• Student:
My code won’t compile.
Troubleshooting• Student: I want my robot to move forward, then reverse. I
had it moving forward, and added the reverse, but it never actually backs up.
Troubleshooting• Student: My code compiles, but I get an error
when I try to download it to the robot.• Check:
– Is the robot turned on and sufficiently powered? (blinking green lights)
– Is the robot connected to the computer?– Is the correct platform type selected in ROBOTC?– Is the correct port selected in ROBOTC?
• Has the driver for the programming cable been installed?
– Has the ROBOTC firmware been loaded on the VEX?– Does the Master Firmware need re-downloaded?– Is another ROBOTC window open, using the debugger
windows?
The Problem with Wait States
• Motor Speed is affected by battery power– If the battery is fully charged, the robot moves quickly– If the battery is running low, the robot moves slowly– Consequently, the robot will not move consistently as
the battery power drains– Makes completing the Labyrinth tricky
• Anyone experience these effects?
• Wouldn’t it be better if we could control the distance the robot moves, regardless of how long it took to complete?
Better Movement:Integrated Motor
Encoders
Integrated Motor Encoders• How they work
– “I2C” Smart Sensor
– Replaces the back plate of 269 and 393 Motors
– As inner striped wheel spins, internal light sensor detects and counts
• Capabilities and Resolution– 627 Counts Per Revolution in High Torque Configuration
– 392 Counts Per Revolution in High Speed Configuration
– Up to 8 encoders “daisy chained” in a row
– Counts Up and down
– Allows you to control the distance a robot moves, by monitoring how much the wheels spin
– Due to “closed-loop” configuration, “PID” control is possible
Motors and Sensors Setup
• Enabled and configured on the Motors tab
• Motor “Type” is important, as their resolution is not uniform
Testing your Encoder Setup
• You can try to follow the wires from the sensors to the I2C and Motor ports…
• …or use the Motor Debug Window!
Better Movement with Encoders
• Forward and Backward for Distance
Better Movement with Encoders
• Turning Right and Left
PID Control • Proportional-Integral-Derivative• With the integrated motor encoders, we have
what is called a “closed-loop” system– The Cortex knows exactly how fast the motors should be
spinning versus how fast they are actually spinning and can make rapid adjustments to compensate for the difference
Variables• A variable is a space in your robots memory
where data can be stored, including whole numbers, decimal numbers, and words
• Variable names follow the same rules as custom motor and sensor names: capitalization, spelling, availability
• Variables can improve the readability and expandability of your programs
Variable TypesData Type Description Example Code
Integer Positive and negative whole numbers, as well as zero
-35, -1, 0, 33, 100
int
Floating Point Number
Numeric values with decimal points (even if the decimal part is zero)
-.123, 0.56, 3.0, 1000.07
float
Boolean True or false – Useful for expressing the outcomes of comparisons
true, false bool
Character Individual characters, placed in single quotes
‘L’, ‘f’, ‘8’ char
String Strings of characters, such as words and sentences placed in double quotes
“Hello World!”, “asdf”
string
Creating a Variable• To create a variable you must give it a:
1. Type for type of data it will hold
2. Name by which variable can be referenced
• Variables can be set to different values throughout program
• Naming variables follows the same rules as naming your Motors and Sensors
• Giving a variable an initial value is called “initializing the variable”
Common Variable Uses• Variables are useful for keeping track of loop iterations
• The following code lets the loop run 10x
Note that the side on the right of the equal sign is evaluated first, then set to the variable on the left. This is always the case.
Common Variable Uses
• Variables are useful for keeping track of sensor or timer values at different parts of the program
Common Variable Uses
• Variables are useful for keeping track of results from complicated equations
Variable Exercises
• Create and initialize the following variables in your Labyrinth Program:– int motorSpeed– int encoderCount1– int encoderCount2– int encoderCount3– int encoderCount4
• Replace the corresponding values in your program with variables.– Download and run. What happens?– Try increasing and decreasing the speed of your robot only by
adjusting the variable.
Variables Continued
More 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 -32768 to +32767
– Long Integer (long) – Memory Usage: 32 bits / 4 bytes• Integer Numbers Only• Ranges in value from -2147483648 to +2147483647
– Floating Point (float) – Memory Usage: 32 bits / 4 bytes• Integer or Decimal Numbers• Variable precision, maximum of 4 digits after decimal
More 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
• Integer Numbers Only• Ranges in value from 0 to +255
– Boolean Value (bool) – Memory Usage 4 bits / .5 bytes• True (1) or False (0) values only.
More 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 (NXT Screen limit)
Constants• 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
The M in STEM
The M in STEM• Current method for moving the robot:
– Guess-and-check - true for wait states and encoder rotations
• Do it smarter!– We can measure the distance for the robot to travel– We can measure the circumference of the wheel– We know 627 encoder counts = 1 wheel rotation– Spend 10 - 15 minutes working out a solution
• Use your idea to move forward and reverse– Discuss how well your ideas worked
• Now Consider Turning– Is having the robot turn 90 degrees the same as having the
encoder turn 90 counts? Why or why not?
The M in STEM
• How did you solve the math problem?• Some common methods:
– Scale Factor (“Scaling” multiples of a known quantity)– Rate: Unit Ratio (# of X in a single Y, times the number of Y’s)
• “Rate” relationship• Find #degrees/1in, then multiply that rate by the total• What about #degrees per floor-line?
– Rate: Raw Ratio (# of X per # of Y, times the number of Y’s)• Find 360degrees/8.6in, then multiply that rate by the total
– Direct Proportion (Traditional “ratio” equation)• 8.6 in = 24 in
627 deg X deg• Solved mechanically using cross-multiplication• Solved algebraically as a Linear Equation of One Variable
Everyone is a Math Teacher
• How did you solve the math problem?• Many STEM teachers will not be teaching in Math
class, yet Math is clearly what they are teaching– Make the math explicit; don’t waste the opportunity!– Embrace multiple methods of solving the same
problem; students need more tools at their disposal, not conflicting information about which ones are “better” than others (none are, use what works!)
Exercise: Precise Turning
• Now that you know how to calculate the encoder counts for any forward movement, can you apply that to point turns and swing turns?
• Figure it out! • Keep in mind what “distance” the wheels are
traversing to accomplish the turn.• Be prepared to share your findings!
Programming Discussion• The behaviors we are using work, but imagine
using them to solve the Labyrinth.– Moving Forward or Turning with Encoders = ~8 lines– 7 movements needed for the Labyrinth– 7 x ~8 = ~56 Lines of code!
• Notice that the behaviors are mostly comprised of the same exact code, over and over.– What if there were a way to reuse the same set of code?– There is: Functions!
Functions
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 Labyrinth– Moving Forward– Turning 90 Degrees Left– Turning 90 Degrees Right
• Creating Functions– Example: Moving Forward– Function Header (Part1 – The name of the function) – Function Definition (Part 2 – The code that goes with the function)
• Using Functions– Function Call (Part 3 – Where you want the function code to run)
Function Definition
Function Call
Function Practice
• Create Functions to:– Point Turn 90 Degrees Right– Point Turn 90 Degrees Left– Reverse for 2 Rotations
• Review VCVT Videos– Note: The code is different,
but the process is the same
Advanced Functions• What about the very similar lines of code that go
with the automatically moving straight code? Is there a way to include those lines in the function?– What’s different each time we use the code?
• The number of degrees for the distance of the straight movement!
• 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.
Advanced Function Definition
Advanced Function Call
Advanced Function Practice
• Expand your previous functions :– Point Turn x Degrees Right– Point Turn x Degrees Left– Reverse for x Rotations
• Review VCVT Videos
Homework!• Homework assignments can always be found
and turned-in on CS2NLearn
• You’ll also be expected to watch a series of videos for review• Optional practice quizzes will prepare you for the certification
exam at the end