ROBOTC Basic Programming - Culpeper Robotics
Transcript of ROBOTC Basic Programming - Culpeper Robotics
ROBOTC Basic Programming
Open ROBOTC and create a new file
Check Compiler Target
• If you plan to download code to a robot, select the “Physical Robot” opBon.
• If you plan to download code to a virtual robot, select the “Virtual Worlds” opBon.
Check PlaGorm Type
• Choose which type of roboBcs you will be programming for.
Vex 2.0 Cortex Vex IQ
UpdaBng Firmware • If this is the first Bme
using a new Cortex, you may want to make sure the firmware is up to date (this can also solve random connecBvity issues).
• To do this, connect the Cortex to the computer using a USB A-‐A cable then select the appropriate opBon from the robot menu (see right).
• This will need to be done for the Cortex and primary joysBck.
Programming Rules/Syntax
• “Code” is the text wriTen for the program. It is instrucBons for what the robot will do.
• The programming language we will be using is RobotC.
• Syntax is an important part of wriBng code—punctua'on and capitaliza'on ma/er!
• “White space” (spaces in-‐between lines) can be added for organizaBonal purposes without affecBng the code.
Sample RobotC Code Block
Let’s take a closer look at capitalizaBon.
NoBce that “task main” is in lowercase. If it were capitalized, RobotC wouldn’t recognize it!
By convenBon, motors, variables, etc. are wriTen in “camel case” –the first word is lowercase and the first leTer of all words following are uppercase, with no spaces. It is important to be consistent with capitalizaBon. Now we will take a closer look at the syntax.
Control structure: task main directs to main body—contains the code that runs.
Parentheses () Follow calling a funcBon. Can be empty or contain an input for the funcBon. Also contain parameters for condiBonals and loops (more on this later).
Semicolon ; Marks the end of a statement.
Curly braces {} Surround commands in a control structure (task main, condiBonals, loops).
Double slash // Marks a line as a comment, or text that is not part of the code.
Square brackets [] indicate which motor is used in a motor command.
Functions
• A funcBon is a block of statements that are run together when the funcBon is called in the task main().
• Can be used for movements and behaviors that are used frequently, like moving forward or turning.
What are some funcBons you would want a robot to be able to
do?
• FuncBons are created or “declared” outside of the task main() and then can be called in the task main whenever they are needed.
• Some funcBons use parameters which pass informaBon into the funcBon to alter the command.
Declaring and Calling Functions Declaring a func'on
• Most funcBons we use will start with “void” since they do not have a return type.
• FuncBon name followed by open and closed parentheses
• FuncBon is contained in curly braces-‐-‐says what the robot has to do to complete the acBon (Which motors to turn on/off, etc.)
Calling a Func'on • Inside task main() • FuncBon name followed by parentheses
Variables
• Variables are essenBally a porBon of memory dedicated to holding a piece of data. A variable’s type describes what kind of data the variable holds.
• A variable can be declared with a statement following the format type varName;
• Variables can also be iniBalized on declaraBon using type varName = ini/alVal; • Boolean types (bool) store a true or a false value. • Integer types (int) store a non-‐decimal number.
Function Example: Moving Forward Without a parameter With a parameter
The funcBon is called with t = 3000. What is inside the parentheses is called the “argument”.
The funcBon has one parameter, “t”. When the funcBon is called, it must have an “int” (integer) in the parentheses following it. “t” will be set to equal this value.
“t” is used in the funcBon wherever the value of the variable will be used.
The parenthesis are empty, so this funcBon does not use a parameter.
Wait Bme can’t be adjusted within the funcBon, so it is separate in the task main().
We will now shic our focus to how the robot moves: motors. We have already seen motors being used in the funcBons we
examined.
• The “Motors and Sensors Setup” page is where you enter which motors are connected to which ports on the robot’s brain.
• Name the motors logical names so that you will know which part of the robot it controls.
• These are the motor names you will use when wriBng code.
• Remember, capitalizaBon maTers!
• It is important to communicate with the person building/wiring.
Motors and Movement
• Which motors are on and the power at which they are running determine how the robot moves.
• To move forward, the lec and right motors should be on at the same posiBve power.
• To move backwards, the lec and right motors should be on at the same negaBve power.
• To turn, one side should be on a posiBve power and the other negaBve.
• Arm lics, claws, and other features act similary.
Let’s revisit the moveForward funcBon, this Bme focusing on how the motors are used and how to call them in the code.
The word “motor” in all lowercase.
The name of the motor being called in [square brackets].
A single equal sign sets the motor, variable, etc. on the lec side of the equal sign equal to the value on the right side of the equal sign.
In this case, the power of rightMotor is being set to equal 127. The highest power is 127 for VEX and 100 for VEX IQ.
Since this funcBon is to make the robot move forward, lecMotor is set to the same power as rightMotor.
wait1Msec() makes the robot keep doing what ever command came before it for the number of milliseconds in parentheses.
Activity: Labyrinth Goal: Write code that will guide a robot through a maze. You will have to: 1) Use the motor set up tool (for this virtual robot, motor1 is lecMotor and
motor6 is rightMotor. Both are VexIQ motors. 2) Write three funcBons: moveForward(int t), turnLec(int t), turnRight(int t) 3) Use these funcBons and appropriate wait Bmes to navigate a robot through the labyrinth.
How to Run Your Code in a Virtual World 1) Choose Virtual Worlds as your Compiler Target (Robot>Compiler Target>Virtual Worlds).
2) Select Curriculum Companion as the Virtual World (Window> Select Virtual Word to Use> Curriculum Companion 4.2.7).
3) Compile code and download to robot.
4) The Curriculum Companion window will appear. Click log in locally then log in as guest. Then from the top bar click movement, and click Labryrinth Challenge from the lec column. Click start acBvity.
Autonomous Robot Virtual World Labyrinth
This code makes use of the functions to the left to autonomously move the virtual robot from the red dashed area below to the black dashed area.
A wait time is used to make this function run for the specified amount of time t.
SomeBmes you don’t want to run all of the code you’ve wriTen in the order
you’ve wriTen it every Bme you run the program. SomeBmes you only want the
robot to do certain tasks if certain condi'ons are true. In these cases we
use condi'onal statements.
Conditional Statements
• If Statements– executes code in curly braces only if the condiBon in parentheses is true.
Important symbols:
• == (double equal signs) checks for equality (doesn’t set one thing equal to the other)
• != does not equal • || or (only one of the condiBons has to be true) • && and (both/all condiBons have to be true)
Conditional Statements Continued
• If, Else if statements: will first check if the condiBons are true for the first statement. If so it will execute that code, then stop. If the condiBons are not met, it will move on and repeat this process for each else if unBl the condiBons for one of them is met and execute that block of code
• Else: will execute this code block if the condiBons are not met for any of the preceding if/else if’s. Does not have any condiBons to check.
While Loops
• SomeBmes you want to execute a block of code over and over again as long as a certain condiBon is met. In these cases you use while loops
• If the condiBon in parentheses is true, it will execute the code block then check the original condiBon again. If the condiBon is sBll true, it will execute the code block again. This process repeats unBl the condiBon is no longer true.
This is the code used for driving a robot with a remote control. It uses both while loops and if/ else if/ else statements.
The condiBon for the while loop is always true, so this block of code will run indefinitely.
These two lines of code simply set the power of the right and lec motors to the value of the right and lec joysBcks, respecBvely.
If the value of the upper right buTon is equal to 1 (the buTon is pressed) then the braced line of code will run. This line of code sets the power of the arm motor to 127 (makes the arm raise up).
If the condiBon for the previous if statement isn’t met, it will move onto this code block. If the lower right buTon is pressed, then the braced line of code will run and the arm will lower.
If neither of the above condiBons are saBsfied, it will run the braced line of code which sets the power of the armMotor to 0.
Once it reaches the end of the while loop, it goes back up to the top to check if the condiBon is sBll true (it is)
Single Controller Driver Control (Vex IQ)
This section of the program will make the rightMotor and leftMotor move based on the value received from the joystick vertical channels A and D. Values range from -127 to 127.
This section of the program will control the armMotor of the robot and will move it up and down based on what button is pressed. If a button is not pressed, the armMotor speed is set to 0.
Dual Controller Driver Control (Vex EDR)
This section of the program will make the rightMotor and leftMotor move based on the value received from the joystick vertical channels 2 and 3 on Controller 1. Values range from -127 to 127.
This section of the program will control the armMotor of the robot with controller 2 and will move it up and down based on what button is pressed. If a button is not pressed, the armMotor speed is set to 0.
Adding Xmtr2 to the end of a button name will allow you to only use it on your 2nd controller.
Controller 1
Controller 2
Summer Camp Program Template This template will allow for timing-based competition control.
It starts by doing nothing until Btn6U is pressed (called an idle loop).
Next, it clears a timer and runs through the autonomous code until 15 seconds are up, then does nothing again until the button is pressed.
Finally, it clears the timer again and repeats the driver code until time runs out.
Additional Resources
• Robotc.net • EducaBon>Vex IQ Curriculum> View Curriculum
• Go through the lessons or look at the teacher’s guide for more informaBon and pracBce.