robotc software introduction. robotc software robotc developed specifically for classrooms and...

35
ROBOTC Software Introduction

Upload: sarah-davis

Post on 25-Dec-2015

244 views

Category:

Documents


11 download

TRANSCRIPT

Page 1: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

ROBOTC Software

Introduction

Page 2: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

ROBOTC Software

• ROBOTC developed specifically for classrooms and competitions

• Complete programming solution for VEX Cortex and several other popular robot platforms

• Real-time debugger• Similar to industry-standard C programming

Page 3: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Industry Standard Coding

• ROBOTC programming is a key components of industry standard programming languages

Page 4: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Sample Program

// sample comments to describe behaviortask main(){ startMotor(rightMotor, 63); wait(); stopMotor(rightMotor);}

Page 5: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Statements and Expressions

• Statements are the smallest complete unit of a working program.

• Statements primarily consist of expressions, keywords and operators

• Expressions may consist of keywords, operators, values, variables, etc.

• Example:int length = 2 * 12; // convert feet to inches

Page 6: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Comments

• Comments are used to make notes in code for the human programmers

• Every sample program contains comments pertaining to robot configuration, ROBOTC commands, robot behavior, etc.

• // Single line comment – All material after “//” is ignored by the ROBOTC compiler

• /* Multi-line comment*/ – All material between the “/*” and “*/” symbols is ignored by the ROBOTC compiler

Page 7: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Keywords

• Keywords are specific directives that mean something special to the ROBOTC compiler

• They are sometimes called reserved words because the compiler “reserves” those words and they can not be used for any other purpose.

• Some keywords you will see: #pragma, task,

Page 8: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Operators

• Similar in behavior to a mathematical function: Examples: 1 + 2 or ADD(1,2)

• They commonly take one or more operands and produce a result

• Foundational to building expressions and statements

Page 9: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Simplified Order of OperationsOrder Operator(s) Notes

1 () [] -> . :: Grouping, Scope, Array

2 ++ -- increment/decrement*

3 * / % Multiply, divide, modulo

4 + - Addition, subtraction

5 < <= > >= Comparison

6 == != Comparison

7 && Logical AND

8 || Logical OR

9 = += -= *= /= %= Assignment

More info: http://en.wikipedia.org/wiki/Order_of_operations*Note: this also depends on pre versus post behavior

Page 10: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Variables

• A variable is a special form of a label that allows you to reference a value in memory by a name instead of just a location

• Variables are “variable” by nature – their contents can usually be changed

• Variables can improve the readability and expandability of your programs

• To change the value of variable:int distance = 500; // declare and set

Page 11: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Creating a Variable• Declare the variable (stating its type and its

name) once at the beginning of task main:

Type of data:• int• float Name of variable:

• Starts with letter• Letters, numbers, and

underscores are ok• Not a reserved word

Page 12: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Variable Types

Data 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. Not useful with POE kits.

‘L’, ‘f’, ‘8’ char

String Strings of characters, such as words and sentences placed in double quotes. Not useful with POE kits.

“Hello World!”, “asdf”

string

Page 13: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Assigning a Value to a Variable• The assignment operator is the single

equal sign• The right-hand side of the equal sign is

evaluated, and then the value is assigned to variable on the left-hand side

• This is not the equality from algebra!Declaration

Initialization

Assignment

Assignment

Page 14: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Variable Applications

• Variables are needed for most programs. Here are some examples:• Example #1: Repeat code 5 times• Example #2: Count user’s button presses• Example #3: Remember if the user EVER

pushed a button• Example #4: Remember a maximum value• Example #5: Debug a program by

remembering which branch of code has been executed.

Page 15: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Global vs. Local Variables

• Variables can have either a “global” or a “local” scope.– Global variable

• Can be read or changed from any task or function in your code.

• Its value can be seen/read globally.

– Local variable• Belongs only to the task or function in which it was created• Value can only be read or changed from within that task or

function• Value can only be seen/read locally• Generally the type of variable you’ll want to use, local to

“main”

Page 16: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Functions

• Functions– Group together several lines of code– Referenced many times in task main or in other

functions

• Creating Functions

Example: LED on if bumper is pressed, off if released

1. Function header (name of function)

2. Function definition (code in the function)

3. Function call (where function code will run)

Page 17: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Function Definition

• Function definitions define the code that belongs to the function

Page 18: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Function Call

• Function calls– Call and run code from function– Placed in task main or other functions

Page 19: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

While Loops

• While loop is a structure within ROBOTC• Allows a section of code to be repeated as long

as a certain condition remains true

• Three main parts to every while loop1. The word “while”

2. The condition

3. Commands to be repeated

Page 20: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

2. The Condition

• Condition is an expression that controls how many times a while loop repeats– When condition is true, the while loop repeats– When condition is false, the while loop ends

and the remainder of the program executes

• Condition is checked once every time loop repeats before commands between curly braces are run

Page 21: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Boolean Logic

• Program decisions are always based on questions

• Only two possible answers– yes or no– true or false

• Statements that can be only true or false are called Boolean statements

• Their true-or-false value is called a truth value.

Page 22: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Boolean Logic

Page 23: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Writing a condition: Example• While the bump switch is not pressed:

wait until it’s dark, then turn on light;

wait until it’s light, then turn off light

Page 24: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Timers

• Loop control– Where would the wait statement go if we

wanted the loop to repeat for a controlled amount of time?

– Nowhere! We need something else.• Solution: Timers

– Internal stopwatches (4 available)– Like encoders, timers should be cleared

before they are used– Be careful: don’t clear a timer in a timed loop

Page 25: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

TimersTimer T1 is used as the condition for the while loop, which will run for 30 seconds

Page 26: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

If Statements

• If statement in the program is evaluated by condition contained in parentheses– If condition is true, commands between

braces are run– If condition is false, those commands are

ignored• Very similar to how a while loop works, but

does not repeat the code

Page 27: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

If-Else Statements

• If-else statement is an expansion of if statement– If checks condition and runs appropriate

commands when it evaluates to true– Else allows code to run when condition is

false– Either if or else branch is always run once

Page 28: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Multiple If-Else Statements

• Be careful when using two separate if-else statements, particularly if both are used to control the same mechanism

• One branch of each if-else statement is always run so that you may create a scenario where the two statements ‘fight’ one another

Page 29: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Behavior-Based Programming• A behavior is anything your robot does

– Turning on a single motor or servo

• Three main types of behaviors 1. Complex behaviors – Robot performs a complex

task (automated fan control)

2. Simple behaviors – Simple task performed by the robot (fan stops when sensor activated)

3. Basic behaviors – Single commands to the robot (turn on a motor)

• Complex behaviors can always be broken down into simple behaviors, which are then broken down into basic behaviors

Page 30: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Program Design

• Many basic behaviors generally come together to create a complex behavior.

• Troubleshoot basic behaviors as they come together to form a complex behavior.

Page 31: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Complex Behaviors

• Describe the task or overall goal that your program will accomplish.– A fan will run until someone

needs it to stop. A safety device warning light will come on before the fan turns on. Another light will indicate that the fan has stopped.

• This may be described as one or more complex behaviors.

Page 32: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Simple Behaviors

• Break each complex behavior down into simple behaviors.

• List the behaviors line by line in the order that each should occur.

• Describe actions and what prompts each action to continue.

Page 33: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Basic Behaviors

• Break each simple behavior down further into basic behaviors.

• Think in terms of what each input and output component will be on your device.

Page 34: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Program Design

• Code and test small behaviors or sets of behaviors individually.

• Edit or add comments as you build code.

Page 35: ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex

Program Design

• Continue programming while testing one behavior at a time.– Temporarily turn sections of code into

comments using /* followed by */.