what is robotc?!?! team 2425 hydra. overview what is robotc what is robotc used for what you need to...

34
What is RobotC?!?! Team 2425 Hydra

Upload: prosper-harris

Post on 25-Dec-2015

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

What is RobotC?!?!

Team 2425 Hydra

Page 2: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Overview

• What is RobotC• What is RobotC used for• What you need to program a robot• How a robot program works• Framework• Assigning values• Variables• What is logic?• Sensors

Page 3: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

What is RobotC

• Computer programming language• A way for humans to easily tell machines

what to do• Converts understandable “words” into

machine (or binary) code• A text-based form of C specially crafted

for use with robots

Page 4: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

What is RobotC Used For

• Tele-operated programs• Autonomous programs• Used for the FIRST Tech Challenge

(along with LabView)• Used for the Vex Robotics Competition

(along with EasyC)

Page 5: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

What You Need to Program a Robot in RobotC

• Robot with a microcontroller that can be programmed in RobotC

• A computer• A driver for your robot (allows your

computer to talk to the robot)• A way to connect your microcontroller to

your computer (USB, Bluetooth, etc.) • A RobotC compiler compatible with the

type of microcontroller you are using

Page 6: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

What is a Compiler?

Basic Concepts• You “write” your RobotC as a text

document• Robots can only understand a file written

in binary code• Writing in binary is time consuming and

difficult• Writing in RobotC is easy because it lets

you use words and symbols instead of 1’s and 0’s

Page 7: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

What is a Compiler?

• A compiler is what converts your RobotC text file into a binary executable file

• What you downloaded from RobotC.net is actually called and Integrated Development Environment (IDE)o Allows you to write codeo Checks code for errorso Converts codeo Downloads code

Page 8: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

How a Robot Program Works

• Follows a linear model (like a list of instructions)

• Code is written like a booko Reads left to righto Top to bottomo Chronological order

• Every line of code represents a new instruction. The robot will do EXACTLY what each line of the code says when it reaches that point of the program

Line 1

Line 2

Line 3

Line 4

Line 5

Line 1

Line 2

Line 3

Line 4

Line 5

Page 9: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Framework

1.Pragma statements2.Tasks (task main)

o Initializationso Logico Declarations

– Open “v1”

Pragma statements

Void tasks

Task mainInitializationsLogic Declarations

Void tasks

Page 10: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Starting Your Program

1.Plan out how you want your program to work

2.Open the autonomous template file3.Write your pragma statements using the

“Motor and Sensor Setup” button in the “Robot” tab1.Setup your “TETRIX Controllers”2.Setup your “Motors”3.Setup your “Sensors”

Page 11: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Task Main

• First part of the code you write• After the pragma statements are read, the

microcontroller jumps to the “task main”• First part of the code the microcontroller

“executes”• Usually the main part of your code• Can be as simple as one declaration or as

complex as a having numerous logic statements

Page 12: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Assigning Values

Objective: Make an NXT motor spin

Steps:1.Create a new file called “helloworldv1”– Set up the pragma statements with no Tetrix

controllers and one NXT motor called “motorA”– Create the task main by typing:

task main() { }

Page 13: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Assigning Values

Steps:1.Go inside the “task main”2.Type (will be explained later):

while(true)1. {2. }

3.Assign a value to the motor inside the loop: motor[motorA] = 100;

– Select “Compile Program” from the “Robot” tab– Test the code

Page 14: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Assigning Values

Now that we have a functional program, lets examine what we have written.• Task main

o The phrase “task main” is specific name that tells the microcontroller that what is contained within its brackets is the main part of the code and where the microcontroller should start.

o It is followed by (). This specifies what information is imported into this task, which is not important since no information is imported to the main task

o () is followed by {}. These indicate what is contained in the task main.

Page 15: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Assigning Values

• Assigning the value of the motoro The phrase “motor[]” specifies that you are assigning

a value to a motor.o Since “motorA” is written inside the [], you are

assigning a value to the motor named motorA.o The “=“ tells the microcontroller that the whatever

comes before the = will be set equal to what follows the =.

o Motor values can range between -100 and 100 with 0 meaning the motor is not moving.

o The line ends with a “;”. All lines end with a ; except for logic statements. (If you forget, the compiler will usually catch this error)

Page 16: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Variables

• A variable is something within a program that can be assigned a value, have its value recalled, and have its value changed.

• Think of a variable like a bucket, what you put in stays there until you change it.

• Like in math, they can be single letters• They can also be words• Values for motors act like variables

Page 17: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Types of Variables

• bool – boolean variable: a conditional variable which holds a numerical value of 0 or 1, with 0 for no and 1 for yes (1 byte)

• byte – byte variable: holds numerical values with no decimal place from 0 to 255 (or -127 to 127 if signed) (1 byte)

• short – short variable: holds numerical values (2 bytes)

• char – character variable: holds a sing;e character of text (2 bytes)

Page 18: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Types of Variables

• int – integer variable: holds numerical values with no decimal place (4 bytes)

• double – 2 decimal point variable: holds numerical values with two decimal points (all doubles have two decimal places, even if they are .00)

• float – floating decimal point variables: holds numerical values with almost any number of decimal points

• string – string variable: holds a text value

Page 19: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Using Variables

• Before a variable can be used, it must be initialized, or declared.

• This can be done form within the task main• To declare a variable, write the type of variable,

followed by the name of the variable• Once declared, a variable can be assigned a

value by typing the name of the variable, followed by an =, and then the value.

• You can also assign a value to a variable at the same time that you declare it by writing the type of variable, the variable name, an =, and the variable’s value.

Page 20: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Using Variables

Objective: Make an NXT motor spin based off of the value of a variable

Steps:1. Create a new program called “hellloworldv2”– Set up the pragma statements with no Tetrix controllers

and one NXT motor called “motorA”– Create the task main (everything from here will be done

within the task main)– Create an infinite while loop by typing:

while (true) { }

Page 21: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Using Variables

1.Create an integer variable called “speed” outside the while loop:

int speed;

– Assign “speed” a value of 50 inside the while loop :

50 = speed;

– Set motorA equal to “speed”: motor[motorA] = speed;

– Compile and test the program

Page 22: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Using Variables

• Why didn’t this work?o You wrote “50 = speed;”o You cannot assign “50” a value of “speed”, you can

only assign “speed” a value of “50”• Note that the value of the motor can be easily

assigned to an integer variable• Now lets try using the “Variables” debugging

window.• It can be found under “Debugging Windows”

under the “Robot” tab

Page 23: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

What is Logic?

• Logic is the way that your program makes decisions.

• Expressed in logic statements• Controls the flow of the program• Parts of a logic statement:

Identifier (type of logic statement) Condition (the question asked that determines if

the body of the logic statement is executed) Body code (the code executed if the conditional

statement is found to be true)

Page 24: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Logic Statements

• while – the program will constantly repeat the lines of code contained within this statement until the condition is no longer valid

• if – if the condition is satisfied, the program will execute the code contained within the “if” statement

• else if – if the “if” condition is not satisfied and the “else if” condition is, the program will execute the code contained within the “else if” statement

• else – if the “if” and “else if” conditions are not met, the program will execute the code contained in the “else” statement

Page 25: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Conditions

A condition is a test comparing one thing to another. There are several comparison operators used in conditions• A == B – Is A equal to B?• A > B – Is A greater than B?• A >= B – Is A greater than or equal to B?• A < B – Is A less than B?• A <= B – Is A less than or equal to B?• A != B – Is A not equal to B?

Page 26: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Conditions

You can also put multiple conditions in one logic statement using these operators:• && - And• || - Or

You can use parenthesis just like you do in math to set the order in which things are conducted.

Page 27: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Examples

• if (A == B){hi}• else if (A != B && A != 0)

{good bye}• else

{?}

Page 28: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

What is Logic?

Objective: Make an NXT motor spin if a variable equals 4 or 5.

Steps:1. Create a new program called “hellloworldv3”– Set up the pragma statements with no Tetrix controllers

and one NXT motor called “motorA”– Create the task main (everything from here will be done

within the task main)– Create an infinite while loop by typing (everything from

here is in the while loop: while (true) { }

Page 29: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

What is Logic?

1.Create an integer variable called “x” with a value of 50

2.Create an if statement with a condition that checks if “x” is equal to 4 or 5:

if (x == 4 || x == 5) { }

– Set motorA equal to 100 inside the if statement.– Compile and test the program

Page 30: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

What is Logic?

• Why didn’t the motor stop spinning when the value was changed?

• We forgot the “else” statement• Underneath the if statement, add:

else { motor[motorA] = 0; }

• Try the program again

Page 31: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Sensors

• Sensors find information about the robot’s surroundings

• Their values can be put into variables and used for logic statements

• In FTC, most of the sensors return a numerical value to the microcontroller

• Sensor values are updated many times per second, so remember that they will change quickly when a robot starts to move

Page 32: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Sensors

Objective: Make an NXT motor spin if a touch sensor is pressed.

Touch sensors return two possible values, “0” for depressed and “1” for not depressed

Steps:1. Create a new program called “hellloworldv4”– Set up the pragma statements with no Tetrix controllers, one

NXT motor called “motorA”, and one touch sensor called “touch”.

– Create the task main– Create an infinite while loop by typing:

while (true) { }

Page 33: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

Sensors

1.Create an if statement with a condition that checks if “touch” is equal to 1:

if (SensorValue[touch] == 1) { }

– Set motorA equal to 100 inside the if statement.– Add an “else” statement which sets the motor

value to 0.– Compile and test the program

Page 34: What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework

That is everything you need to know to start writing RobotC

programs!

(I hope you aren’t too confused)