coding for the first tech challenge: robotc presented by: audrey yeoh acknowledgements: team...
TRANSCRIPT
Coding for the FIRST Tech Challenge:
RobotC
Presented by:Audrey Yeoh
Acknowledgements: Team Unlimited FTC 0001
Why RobotC?
SpeedRobotC runs quicker on the NXTDifferences in speed are small until larger code is written
Control In RobotC you have control over almost everythingReal Time Debugger to allow viewing of code line by line while
running Personal Preference
Text base controlAdvanced math functionsTeam Decision
RobotC
Hardware Review
ServoCan only go 180 degrees In code, it is from 0 to 255
DC MotorContinuousControlled by power rather than degree (+100
forward to -100 backward)
RobotC
0 255
180°
Plan the Code
Get a diagram of the field Draw the general routine w/ arrows and numbers Make side notes noting what happens at each time interval Start your first draft of the code
RobotC
Coding Basics
Comments:“//” is used as a line comment“/*” is the beginning of a group comment“*/” ends a group commentComments never affect the function of codeAlways comment code so your team members
understand it!
Example:
RobotC
Dead Reckoning vs. Sensing
Dead ReckoningA prewritten routineCode flows A to B to C If interrupted, will only try to continueEasier to write
Sensing Routine that can adaptCode flows, but can respond If interrupted, can attempt to correctMore complicated
RobotC
Reacting to the Environment
Sense the world and react to itTouch switch is pressed, against somethingUltrasonic rapidly changes, something crossed pathsLight sensor spikes, on a line or off a line
Useful to be able to change course Code “thinks” for itself Only as smart as YOU code it!
RobotC
Flow of the Code
Flow from place to placeDON’T JUMP ALL OVER THE PLACE!Structure the code ahead of time, plan it out
Make things easy to fixMake your code neat and clean
Plan the code out ahead of timeDon’t haphazardly place code in
Order your codeDo you want an interrupt? Is there an importance to the steps?
RobotC
Commenting
Use comments to remind yourselfOften you will forget why something is in the codeExplains to the JUDGES what you were thinkingAllows for easy correctionsWrite complete thoughts
“Checks for a touch sensor being pressed, if not, continue”Use them LIBERALLY
RobotC
RobotC Basics
Make sure you are on version 3.00 If not, uninstall RobotC COMPLETELY before installing the newest
version Use the Sample codes included in RobotC
Access by File > Open Sample Programs Save RobotC files in separate location on Hard Drive Download and watch coding presentations at Robotics
Academy
RobotC
Name Your NXT
Type in the name for your NXT (default is NXT) Recommended name is your team number to four
digits (i.e FTC #0001 is 0001 If you have more than one NXT, place an A, B, etc. after
the number
RobotC
Connecting to Bluetooth
RobotC
2. Make sure that this box is checked to perform a bluetooth search
2. Select here to make a pair between the NXT and your laptop
Bluetooth at the Competition
Take from the Field Preparation Guide (you can find it here)
Remember to always do these steps at the Playing Field
RobotC
RobotC Menu
Compile and DownloadCompiles and downloads
Compile ProgramCompiles locally
DebuggerScans code for errors
Motor & Sensor SetupSet up Motor and Sensors
Download firmwareDownload firmware to NXT
RobotC
RobotC Menu
Link SetupUsed to establish a link
between NXT and Computer
Poll Brick “Online Window”
File ManagementManage files on the NXT
Joystick Control“FTC Controller Station”
RobotC
RobotC Debugger
Allows for program control wirelessly
Allows to view where the code is currently
Allows to view errors in your code while running
RobotC
RobotC Debugger Buttons
Start/Stop – Starts or Aborts Program Suspend – Pauses code Step Into – Pauses code and proceeds to next line Step Over – Pauses code and skips over next
procedure Step Out - Pauses code and exits procedure Clear All – Resets all values to default Refresh Rate
Once – Refreshes values manually Continuous/Stop – Switch between continuous
and stop updating
RobotC
RobotC Debugger Joystick Control
“FTC Controller Station”
Allows control over Teleoperator and Autonomous
Views controller data
RobotC
RobotC “Loops”
While statementPerforms code as long as the statement is true
I.e. while (ServoValue[Servo1] < 255) If statement
Performs entirety of code if statement is trueI.e. if (sensorValue[Touch] == 0)
Else statementPerforms entirety of code if “If Statement” is false
i.e. else
RobotC
RobotC Punctuation
Brackets { }Used to control flow of code
Semicolons ;Used after lines to denote an end
Square Brackets [ ]Used for sensors and motor declarations
Parenthesis ()Used for time and math functions
RobotC
RobotC Operators
RobotC
== (test a condition)i.e. while (sensorValue[Touch] == 0)
= (equals)i.e. motor[motorD] = 0;
< , > (less than, greater than)i.e. if ( -10 < joystick.joy1_y1 < 10 )
|| (or)i.e. if ( joy1Btn(1) || joystick.joy2_TopHat == 0 )
RobotC Commenting
// TEXTComments out a single line of text
/* TEXT */Comments out all text within the /* and */
/*Comments out all text after
RobotC
Sample Code
FTC Template Joystick Control DC Drive Motor sample NXT Motor sample Servo sample Touch Sensor sample Light Sensor sample Ultrasonic Sensor sample Compass Sensor sample
RobotC
RobotC Resources
RobotC RobotC Forums Robotics Academy FTC Function Guide (From Robotics Academy) Setting up Dual Template Mode Field Procedures for Bluetooth Software Information (Links at our website) Code and Software Presentation
RobotC
RobotC Resources
RobotC Sample codeProgram files > Robotics Academy > RobotC for Mindstorms >
Sample Programs > NXTFile > Open Sample Programs (if RobotC is open)
RobotC
Variables
What are variables?They are things you store in the program that
you may want to useHow are they made?They are made according to their ‘types’ eg.
Integer (int)Example Structure:
int apple = 0; If they occur outside { } of any methods, they
are instance variables
RobotC
Variables Continued…
Variables are ‘alive’ only in the highest method they are in
‘Dead’ Variables can be used againEg.
Void appleCount {int apple = 3;
}
Void pearCount{int apple = 4
}
RobotC
Camel Casing
thisIsCamelCasingCapitalizing the first letter of a word in a
sentence starting from the second wordLooks like camel humpsWhy? Not to confuse with other commandsCamel Casing is used for variables you make
RobotC
For Loop
Does the loop for a specific number of timesKnown repeat numberStructure:
for(int i=0; i< (number of times you want to repeat it; i++){
//code
}
RobotC
While Loop
Want to repeat itUnknown number of times of repeatExample Structure:
while ( sensorVal < 100){
// code
}
RobotC
If … Else… Statements
If something is true, do this, or else do something else
Example Structure:
if ( sensorVal == 4){//code for dance happy dance
} else{//code for go forward
}
RobotC