06/2012tanya mishra1 robotc for vex cortex llano estacado roboraiders frc team 1817

109
06/2012 Tanya Mishra 1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

Upload: marjorie-stafford

Post on 28-Dec-2015

222 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 1

ROBOTC for VEX CortexLlano Estacado RoboRaiders

FRC Team 1817

Page 2: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 2

Humans and Machines Machines are built to perform useful tasks. The way a Machine works depends entirely on the way

the Human build it. Since Machines and Robots need a medium of

communication, a language called “Programming Language” is used.

EASYC, ROBOTC, C++, C all are programming languages.

The instructions in the language are called “Programs” and the human that writes the instructions is called the “Programmer”.

Page 3: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 3

Task of Programmer: Understand Problem, Find a solution, Write a program to solve the problem in the required Programming language.

Task of Machine: Follow the program provided. A ROBOT is a Machine.

Page 4: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 4

Planning and Behavior

Behavior: Action the Robot has to take. Big Behavior: Solving a maze Small Behavior: Turn Left, Move Forward, etc. Big Behavior is made up of Smaller Behaviors. Plan a Solution to the problem. Break down the plan into detailed smaller steps. Each step is a behavior the robot needs to follow. Sequence of these steps in English is called “pseudo-

code”.

Page 5: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 5

Page 6: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 6

“Flow charts” are a visual representation of the program flow.

Start and End: “Rounded Rectangles”. They contain the word “Start” or “End”, but can be more specific such as “Power Robot Off” or “Stop All Motors”.

Actions: “Rectangles”. They act as basic commands and process steps.

Decision blocks: “Diamonds”. These typically contain Yes/No questions. Based on the choice, the next step is determined.

Page 7: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 7

Page 8: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 8

Introduction to C Programming

EasyC is based on the the principles of C Programming.

We will cover the following concepts:

1. Basic Components of a C Program

2. Data Types and Variables

2. Conditional operators

3. Control Structures and Loops

4. Methods and Functions

Page 9: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 9

A Basic C Program

#include<stdio.h> // Header File

void main(void) // Main function

{

// Body of the main function

}

Header file: Includes all the required words and instructions needed to write a program

Main function: Execution starts from here Body: stepwise Instructions to the Robot

Page 10: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 10

Each Instruction to the robot is also called a “Statement”.

When the list of instruction is send to the VEX cortex, it read them from top to bottom and left to right.

Different Commands use different paired Punctuations such as “[]” “{}” “()”

“{..}” defines a body of one or more instructions. Also called a “Compound Statement”.

Every instruction in the body ends with a “;”. It shows the end of a instruction.

Page 11: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 11

Comments are for the programmer to understand what a particular statement does.

Two kinds of comments:

1. // This is a one line comment

2. /* This is a more than one line

Comment.*/ C language is case sensitive: Upper and lower

cases are considered different.

Page 12: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 12

Data types and Variables A “Variable” is a place to store a value. A variable has a “Type” and a “Name” “Type” deals with the type of Data the variable

will hold.

Type of Data:

Int: Whole Numbers. Positive, Negative, Zero

float(Floating Point): Decimal Point Numbers. Positive and Negative.

String: Text. Letters, Spaces and characters.

Page 13: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 13

char(Characters): Single characters

bool(Boolean): True and False values

Declare a variable: int Age;

float Score;

string Name;

char Grade;

bool Pass;

Page 14: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 14

Assign a variable: Age = 18;

Score = 90.5;

Name = “William”;

Grade = 'A';

Pass = True;

Declare and assign: int Age = 18;

float Score = 90.5;

string Name = “William”;

char Grade = 'A';

bool Pass = True;

Page 15: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 15

Variable Naming Rules: A variable name can not have spaces in it. A variable name can not have symbols in it. A variable name can not start with a number. A variable name can not be the same as an

existing reserved word.

Scope of a Variable: Local variables: Within a certain block of code

or function. Cannot be accessed outside. Global variables: Can be accessed anywhere

in the code.

Page 16: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 16

Using Variables in a print to screen function:

When printing a variable on the screen, the following syntax is used:

Print to screen function(“%type”,Variable);

Signed : + and - , unsigned: - , short: less range , long : more range

Page 17: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 17

Conditional Operators Comparison operators

Relational Operator Example

> (Greater Than) 7 > 5

>= (Greater Than or Equal To) 7 >= 5 , 7 >= 7

< (Less Than) 5 < 7

<= (Less Than or Equal To) 5 < 7 , 7<=7

== (Equal To) 7 == 7

!= (Not Equal To) 7 != 5

= (Assignment Operator) number = 7

Page 18: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 18

Logical operators:

Boolean Truth TableJoining two or more statements using “And” and “Or”

A: Its a Sunny Day

B: My Car is working

Can I go out?(A and B)

Can I go out?(A or B)

Yes Yes Yes Yes

Yes No No Yes

No Yes No Yes

No No No No

Page 19: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 19

And : &&

Or : ||

Not : !

A B !A A && B A || B

True True False True True

True False False False True

False True True False True

False False True False False

Page 20: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 20

Control Structures and LoopsControl Structure

IF statements:if(condition)

{

Instructions, if “condition” is True

}

Page 21: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 21

IF-ELSE Statements:if(condition)

{

// Instructions, if “condition” is true

}

else

{

// Instructions, if “condition” is false

}

Page 22: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 22

ELSE-IF Statements:if(condition1)

{

// Instructions, if “condition 1” is true

}

else if(condition 2)

{

// Instructions, if “condition 2” is true

}

else

{

// Instructions, if “condition 1” and “condition 2” are false

}

Page 23: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 23

Switch Statements:switch(expression) //expression can only be an “int” or “char”

{

case Value-1:

// Instructions, if expression = Value-1

break;

case Value-2:

// Instructions, if expression = Value-2

break;

default:

// Instructions, if expression does not match any Value

}

Page 24: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 24

Example:

char Grade = 'A';

switch(Grade)

{

case A:

Your Grade is A;

break;

case B:

Your Grade is B;

break;

default:

This is the default choice

}

Page 25: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 25

Loops While loops:

while(condition)

{

// Instructions, if “condition” is true

}Note: Control Structures execute only once. On the other hand, while

loops execute continuously until the condition is false.

If the condition in the loop is always true, the loop never ends. Such a loop is called an “Infinite Loop”.

A loop will end only when the condition is false or there is a “break” statement.

Page 26: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 26

Example:int count = 0; //Initialization

while(count <= 2) //Condition

{

PrintToScreen(“ I have %d apple \n”, count);

count = count + 1; //Increment

}

Output: I have 0 apple

I have 1 apple

I have 2 apple

Page 27: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 27

For Loop:

for(initialization; condition; increment)

{

// Instructions, If “condition” is true

}

Similar to a while loop except that the initialization and increment are all together.

Note: Initialization is done only when the loop first starts. After that it is skipped.

Page 28: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 28

Example:int count;

for(count =0; count <= 2; count = count +1)

{

PrintToScreen(“ I have %d apple \n”, count);

}

Output: I have 0 apple

I have 1 apple

I have 2 apple

Note: \n is called new lines. It prints the next sentence in a new line.

Page 29: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 29

Methods and function

Functions are named sections of code that can be called from other sections of code.

Also called subroutines. Every executable statement in C must live in a

function. Functions have a Data type, name and input

values. Input values are called Parameters. They are

the values you want the function to work with.

Page 30: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 30

The function definition specifies the “return value” and the “parameters” of the function:

<data type> FunctionName(<param1>, <param2>, ...)

{

<function body>

<return type>

}

Return type and Data type should be of the same kind.

Return type is “void” if nothing is to be returned.

Parameters is “void” if nothing is to be passed in.

Page 31: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 31

Example:

int addition(int x, int y)

{

int z;

z = x+ y;

return z;

}

void main(void)

{

addition(2, 3);

}

Page 32: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

08/2012 Tanya Mishra 32

Some Useful termsCompiler: Turns C program into the machine

language for the controller.

Loader: Loads the machine language output of the compiler (along with other stuff) into the robot controller.

Machine Language: What the robot controller actually understands. Found in .HEX files.

10110100

11100101

00001011

Page 33: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 33

Download ROBOTC

Download Link: ROBOTC for CORTEX and PIC

www.robotc.net->Download->ROBOTC 3.0 for CORTEX and PIC

or

http://www.robotc.net/download/cortex/

Page 34: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 34

ROBOTC Rulestask main()

{

motor[port3] = 127;

wait1Msec(3000);

}

Important words are highlighted. Uppercase and Lowercase matters. White spaces and tabs are for programmer convenience Sentences or Instructions are separated by semicolon. Instructions are read from T – B and L-R.

Page 35: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 35

Error Messages

Compiler analyzes your programs to identify syntax errors, capitalization and spelling mistakes, and code inefficiency (such as unused variables).

Errors: Major issues. misspelled words, missing semicolons, and improper syntax. Errors are denoted with a Red X.

Warnings: Minor issues. These are usually incorrect capitalization or empty, infinite loops. Warnings are denoted with a Yellow X.

Page 36: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 36

Information: ROBOTC will generate information messages when it thinks you have declared functions or variables that are not used in your program. These messages inform you about inefficient programming. Information messages are denoted with a “White X”.

Page 37: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 37

System Configurations “Firmware” is a piece of software that accessing

the operating system in the processor enabling it to perform its task.

Update firmware to make sure it is compatible with the ROBOTC and latest Vex Hardware

Update Firmware on Cortex: Cortex Micro controller is the “Brain”

Page 38: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 38

It has two separate processors inside:

1. User Processor : Handles all ROBOTC programming instructions.

2. Master Processor: Handles all lower level operations like Motor Control and VexNet Communication.

Make sure you have the following first:

1. A USB A-A cable.

2. A charged robot battery

3. The cortex Powered Off

4. Latest version of ROBOTC installed on PC

Page 39: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 39

Step 1: Connect Cortex and PC

using A-A cable. Step 2: Turn Cortex ON. Step 3: Go to ROBOTCRobot->Platform Type->Innovation First(IFI)->Vex 2.0 Cortex

Page 40: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 40

Step 4: View->Select Communication Port->USB wired Cable or Vex Robotics COMM Port (if Cortex recognized) otherwise Automatic

Step 5: Robot->Download Firmware->Automatically update Vex Cortex

Page 41: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 41

Update Firmware on Joystick: Make sure you have the following first:

1. A USB A-A cable.

2. Latest version of ROBOTC installed on PC Step 1: Connect Joystick and PC

using A-A cable. Step 2: Go to ROBOTCRobot->Platform Type->Innovation First(IFI)->Vex 2.0 Cortex

Page 42: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 42

Step 4: View->Select Communication Port->USB wired Cable or Vex Robotics COMM Port (if Cortex recognized) otherwise Automatic

Step 5: Robot->Download Firmware->Automatically update VexNET Joystick

Page 43: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 43

Download Firmware when: You start using a particular Vex Cortex or

VexNET Joystick You update a newer version of ROBOTC.

Page 44: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 44

Download Sample Program

Step 1: Go to ROBOTCFile->Open Sample Program->Training Samples->Motor port 3

forward.c

Page 45: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 45

Before Downloading, make sure: Your cortex and VexNet remote control are

paired and equipped with VexNet USB keys. Batteries connected to remote control and

cortex. Robot propped up. Orange programming kit connecting PC and

remote control. Turn on Remote Control and Robot. Robot and VexNet status lights should blink

green on both remote control and Robot.

Page 46: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 46

Step 2: View->preferences->Detailed Preferences->Platform: Vex 2.0 Cortex and Communication Port: Prolific USB-Serial Comm port.

Step 3: Robot->Vex Cortex Communication Mode->VexNet or USB

Page 47: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 47

Step 4: Robot->Compile and Download Program

Step 5:Debugger Window->Start

Page 48: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 48

Motorstask main()

{

motor[port3] = 127;

motor[port2] = 127;

wait1Msec(3000);

}

Running this program makes the Robot spin because the motors on the robot are mirrored. So making the motor on port2 move forward in the code, makes it move in reverse in real time.

Page 49: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 49

Reverse Motor Polarity: Robot->Motors and Sensors Setup->Motors

tab-> Port 2 reversed check box is checked.

Page 50: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 50

#pragma config(Motor, port2, , tmotorNormal, openLoop, reversed)

The “pragma” statement contains configuration information from the motors and sensors settings and should only be changed from the window.

Page 51: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 51

The Motors and Sensor Window can also be opened by double clicking on the pragma statement.

Motors and Sensor Window: Name: Name of the Motor eg. leftMotor, rightMotor

Type : Motor Equipped or Type of Motor

Reversed: Checked if Motor needs to be reversed.

task main()

{

motor[leftMotor] = 127;

motor[rightMotor] = 127;

wait1Msec(3000);

}

Page 52: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 52

Page 53: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 53

Assigning Positive power Values(127) on both motors makes the robot go forward.

Lower speed of robot by assigning values less than full power(127).

Assigning negative power values(-127) on both motors makes the robot go reverse.

Assigning zero power values(0) on both motors makes the robot stay in place.

Turning of Motors

Page 54: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 54

Point Turn: Turn in place (-63 and 63)

Swing Turn: Making one motor on and the other off makes the robot swing around the stationary wheels. (0 and 63)

Page 55: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 55

Sometimes the power assigned in the code to the motors does not reach the motors. Reasons: friction or construction. Manual adjustments to the power levels.

task main()

{

//Makes the robot go forward at half speed for 2 seconds.

motor[leftMotor] = 63;

motor[rightMotor] = 63;

wait1Msec(2000);

//Makes the Robot turn left in place for 0.7 seconds.

motor[leftMotor] = -63;

motor[rightMotor] = 63;

wait1Msec(700);

}

Page 56: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 56

Shaft Encoders/Rotation Sensor

Manual adjustments of power are not same for all robots and as the battery power drains, the robot is move shorter distances.

Shaft Encoders are used to control how far the Robot Moves.

Number of counts per Revolution on a axle mounted through the encoder center.

Max 360 up for forward movements and 360 down for reverse movements.

Page 57: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 57

ROBOTC->File->Open Sample Program->Shaft Encoder->Forward for Distance.c

Page 58: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 58

Page 59: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 59

Encoders need to be set to zero before you use them.

On the Cortex Micro controller, the Analog and Digital Ports are used to connect sensors.

In Motors and Sensors setup: Digital Port, Sensor Name and Sensor Type Naming Conventions: No special Characters,

No spaces and Not a reserved word in ROBOTC.

Both Encoder wired on the Right and Left side of the robot must be in neighboring ports.

Page 60: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 60

Page 61: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 61

Clear the encoders for better consistency and precision on your robots movements.

Page 62: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 62

Sensor Debug Window: In order to see the values in the encoders as

the robot runs, we can use the debugger window.

Compile and Download the program. Press “continuous” on the debugger window

instead of “start”. Robot->Debugger Windows->Sensors Scroll down and find your encoders in the list. Press “start” on the debugger window to start

your robot and watch the values change.

Page 63: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 63

Page 64: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 64

Forward movement and Turning: Not controlled by power or time but rotation

counts.

Page 65: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 65

Joystick Mapping

Vex Remote control provides you with two joysticks (each having a x- axis and y-axis), eight buttons on the front and four additional buttons on the top.

Page 66: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 66

The remote control sends streams of data to the Robot over the VexNet.

The Vertical motion of the joysticks on the remote control range from +127 to -127, where +127 is full power forward, 0 is stop and -127 is full power reverse.

To access the values in the joystick in ROBOTC, we use the following command:

vexRT[channel Number]

VexRT is short for Vex Remote Transmitter. The channel number for each input is shown on

the remote.

Page 67: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 67

For example, the right joystick's vertical motion is on channel 2.

vexRT[ch2]

Pairing each motor with each joystick we can make the robot move forward, turn or go in reverse.

motor[port number] = vexRT[channel number]

Page 68: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 68

Sample Program:ROBOTC->file->Open Sample Program->Remote Control->Dual

Joystick Control.c

Page 69: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 69

Page 70: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 70

Limiting the range of power from [127, -127] to half power [63,-63] to achieve accurate movements.

Similarly, many other possible mapping combinations are possible.

Page 71: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 71

Timers

Timers are like “Stopwatches”. Used in competition to know how long the robot

has been working. Four Timers provided in ROBOTC: T1 , T2, T3

and T4.ClearTimer(TimerName)

This command resets the Timer to 0 and immediately starts counting again.

Page 72: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 72

To check the elapsed time on the Timer T1, we use the following command:

time1[T1]

It returns the elapsed time in milliseconds. To check elapsed time in 10 milliseconds or

100 milliseconds, the commands are:

time1[T1], time10[T1], or time100[T1]

Page 73: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 73

Thus, to make your robot unresponsive for 90 seconds:

Page 74: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 74

Buttons Mapping

The 8 buttons on front of the remote control are divided into two groups and the four buttons on top are also divided into two groups. The group number is written next to the numbers.

Page 75: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 75

Unlike joysticks, the buttons have two values: 1 when pressed and 0 when released.

VexRT[Btn Group# Direction],

where Direction = U(Up), D(Down), L(Left), R(Right)

For example, to access the Down button on Group 7 (Left side buttons on the remote):

VexRT[Btn 7 D]

Page 76: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 76

To make your robot respond to user control on a button press:

Page 77: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 77

Sensor Configuration

Vex Cortex allows us to incorporate various sensors on the robot which provides valuable sensor feedback for intelligent decision making.

There are 8 Analog Sensor ports for sensors like Line Follower, Accelerometer and Potentiometer.

There are 12 Digital Sensor ports for sensors like the limit switch, Shaft Encoder and Ultrasonic Rangefinder.

Page 78: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 78

Page 79: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 79

These sensors are configured in the same way we have configured motors and shaft encoders before.

The ROBOTC Motors and Sensors setup allows you to setup these sensors depending on if it is a Digital or Analog Sensor.

If you are unsure of the type of sensor being Analog or Digital, you can see it in the drop down menus in the Motor and Sensor Setup.

Page 80: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 80

Limit Switch: Limit Switch is a Digital Sensor(0 and 1) and is

plugged into the Digital port 6 on the Cortex. When pressed, it provides a sensor value 1,

when released, it provides a sensor value 0.SensorValue[Limit switch name]

Page 81: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 81

Potentiometers: A potentiometer is a Analog Sensor and is

plugged into port 6 of the Analog board on the cortex.

It measures rotation between 0 to 250 (not 360 due to internal mechanical stops) degrees and return value ranging from 0 to approximately 4095.

Page 82: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 82

The values returned by the Potentiometers can be seen using the Sensor Debug Window that we used with the Shaft encoders.

SensorValue[Potentiometer name]

Page 83: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 83

Ultrasonic Range Finder Ultrasonic Range Finder allows us to sense

obstacles in the path of the robot and avoid bumping into them.

Page 84: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 84

It measures distance to the obstacle using sound waves. It calculates the Distance depending on how long it takes for the sound wave to bounce back.

Distance = Speed * Time / 2

Digital Sensor and connections on the Cortex are as follows:

Page 85: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 85

Motors and Sensor Setup: Choose type depending on what distance scale

you want: cm or mm or inch

Page 86: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 86

According to the program, till the distance to the obstacle is greater than 25 cm, the robot will move forward. As soon as it is 25 cm away from the obstacle, to robot will stop.

Page 87: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 87

Sometimes the echo of the sound wave does not reach the range finder on soft surfaces like a sweater or certain surfaces such as a slope deflect the wave in another direction.

In the debugger window, we can see that when the sonar does not detect any sound, the sensor value if set to -1.

Page 88: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 88

If the sonar value will be -1, the condition in the code will always be false and the robot will not move when no obstacle is detected.

To make the robot move forward when no obstacle is detected, the condition needs to be changed.

Page 89: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 89

Line Tracking Sensors

Below is shown a set of three Line tracking Sensors. Line tracking sensors work on the bases of Infrared Light. Each sensor has an Infrared LED and an Infrared Light sensor.

Page 90: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 90

The LED emits light and the sensor detects the amount the light reflected back.

Light Surfaces = Low Sensor Reading Dark Surfaces = High Sensor Reading

Page 91: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 91

The cortex gives a Sensor reading from 0 to 4095. The value does not correspond to any unit of measurement.

Thus it is important to take care of lighting conditions around the robot and the height at which the sensors are placed in order to determine the threshold of reading.

Page 92: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 92

Motors and Sensor Setup:

Page 93: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 93

Calculate Threshold Values (Example): Leaving the task main() empty, if we run the

robot on a light surface, we get the the following values in the Sensor Debugger Window:

For Dark Surfaces we get the following values:

Page 94: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 94

Light Surface Average Value:

Average = 165 + 167 + 160 = 492 /3 = 164

Dark Surface Average Value:

Average = 2795 + 2821 + 2837 = 8453 / 3 = 2817.66 ~ 2818

Threshold Value:

164 + 2818 = 2982 / 2 = 1491

Page 95: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 95

Using all three sensors allows us to detect the line as well as border of the line, corners and intersections, which is not possible if you use just one line tracking sensor.

Page 96: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 96

Sample Program:File->Open Sample Program->VEX2->Training Samples->Simple

Line Tracking.c

Page 97: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 97

Page 98: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 98

We can see that the motor value pairs of 0 and 40 cause the robot to make sharp swings as it steers itself on to the line. This causes waste of energy. To simplify, use pairs of motor values close enough lie 20 and 40.

For sharp turns and curves on the lines, we can optimize the motor values, shaft encoder rotation counts and threshold values to work together in order to perfect the different sections of the path.

Page 99: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 99

Tips on Line Tracking: Break the entire path into sections depending

on how the line curves. Instead of programming the entire path at once,

implement and test each section one after another.

The behaviour in each section will almost eb the same. The only difference will be the values of the motors, encoders or threshold. This can be achieved by implementing a LineTracking() function with different parameter values!

Page 100: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 100

Reserved words

Motors

Motor control and some fine-tuning commands. motor[output] = power;

The VEX has 8 motor outputs: port1, port2... up to port8. The VEX supports power levels from -127 (full reverse) to 127 (full forward). A

power level of 0 will cause the motors to stop.

bMotorReflected[output] = 1; (or 0;)

Page 101: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 101

Timing

The VEX allows you to use Wait commands to insert delays into your program. It also supports Timers, which work like stopwatches; they count time, and can be reset when you want to start or restart tracking time elapsed.

wait1Msec(wait_time);

Maximum wait_time is 32768Msec, or 32.768 seconds.

Page 102: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 102

wait10Msec(wait_time);

Maximum wait_time is 32768, or 327.68 seconds.

time1[timer]

It returns the current value of the referenced timer as an integer. The maximum amount of

time that can be referenced is 32.768 seconds The VEX has 4 internal timers: T1, T2, T3, and T4.

time10[timer]

The maximum amount of time that can be referenced is 327.68 seconds.

Page 103: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 103

time100[timer]

The maximum amount of time that can be referenced is 3276.8 seconds.

ClearTimer(timer);

This resets the referenced timer back to zero seconds.

SensorValue(sensor_input)

SensorValue is used to reference the integer value of the specified sensor port. Values will correspond to the type of sensor set for that port. The VEX has 16 analog/digital inputs: in1, in2... to in16

Page 104: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 104

Type of Sensor Digital/Analog? Range of Values

Touch Digital 0 or 1

Reflection (Ambient) Analog 0 to 1023

Rotation (Older Encoder) Digital 0 to 32676

Potentiometer Analog 0 to 1023

Line Follower (Infrared) Analog 0 to 1023

Sonar Digital -2, -1, and 1 to 253

Quadrature Encoder Digital -32678 to 32768

Digital In Digital 0 or 1

Digital Out Digital 0 or 1

Page 105: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 105

Sounds

The VEX can play sounds and tones using an external piezoelectric speaker attached to a motor port.

PlayTone(frequency, duration);

This plays a sound from the VEX internal speaker at a specific frequency (1 = 1 hertz) for a specific length (1 = 1/100th of a second).

Page 106: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 106

Radio Control

ROBOTC allows you to control your robot using input from the Radio Control Transmitter.

BvexAutonomousMode = <value>;

Set the value to either 0 for radio enabled or 1 for radio disabled (autonomous mode). You can also use “true” for 1 and “false” for 0.

vexRT[joystick_channel]

This command retrieves the value of the specified channel being transmitted.

Page 107: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 107

If the RF receiver is plugged into Rx 1, the following values apply:

Control Port Joystick Channel Possible Values

Right Joystick, X-axis

Ch1 -127 to 127

Right Joystick, Y-axis

Ch2 -127 to 127

Left Joystick, Y-axis

Ch3 -127 to 127

Left Joystick, X-axis

Ch4 -127 to 127

Left Rear Buttons Ch5 -127, 0, or 127

Right Rear Buttons

Ch6 -127, 0, or 127

If the RF receiver is plugged into Rx 1, the following values apply:

If the RF receiver is plugged into Rx 2, the following values apply:

Page 108: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 108

Control Port Joystick Channel Possible Values

Right Joystick, X-axis

Ch1Xmtr2 -127 to 127

Right Joystick, Y-axis

Ch2Xmtr2 -127 to 127

Left Joystick, Y-axis

Ch3Xmtr2 -127 to 127

Left Joystick, X-axis

Ch4Xmtr2 -127 to 127

Left Rear Buttons Ch5Xmtr2 -127, 0, or 127

Right Rear Buttons

Ch6Xmtr2 -127, 0, or 127

bVexAutonomousMode = false; //enable radio control while(true) {

motor[port3] = vexRT[Ch3]; //right joystick, y-axis //controls the motor on port 3 motor[port2] = vexRT[Ch2]; //left joystick, y-axis //controls the motor on port 2

}

Page 109: 06/2012Tanya Mishra1 ROBOTC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817

06/2012 Tanya Mishra 109

Reference

VEX Cortex Video Trainer using ROBOTC[http://www.education.rec.ri.cmu.edu/products/teaching_robotc_cortex/index.html]

ROBOTC : A C Programming language for Robotics

[http://www.robotc.net/]