an introduction to vex coding by joseph newman. what languages are available? we will be covering...

34
An Introduction to Vex Coding by Joseph Newman

Upload: nataly-emmitt

Post on 13-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

An Introduction to Vex Coding

by Joseph Newman

Page 2: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

What languages are available?

We will be covering two popular languages, RobotC and EasyC.

EasyC is a drag and drop program, that involves less typing, because of the way it is formatted. RobotC has no prewritten commands, so the user must type everything they wish to happen.

RobotC is first on the agenda.

Page 3: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Transitioning from RobotC to EasyC

When you switch programs, from RobotC to EasyC, no extra software is needed. When you compile and download the EasyC program onto the Vex, it downloads all needed firmware with it each time you use it.

Because of this, when you want to return to RobotC, you must download the RobotC firmware back onto it, or it won’t be able to communicate with the Vex Cortex.

Page 4: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

How to download RobotC firmware:

Page 5: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Syncing Vexnet Joystick with Cortex Attach the orange cable to the Vexnet

Page 6: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Why RobotC?

RobotC is an easy-to-learn language used in microcontrollers to control various equipment. It also has a great user-interface, helping the coder along as they work on a project.

Knowledge of RobotC can be applied to almost any other program that is based off of C.

Page 7: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Syntax

Uncapitalized and capitalized words ARE different. RobotC is case sensitive. e.g. “task” is different from “Task”

RobotC colors words that it recognizes. e.g. When “task” is typed, it turns into “task,” and

“port2” turns into “port2.”

Most lines end with a semicolon. ;

Page 8: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Running the Code Upon startup, after it is finished initializing, the Vex

microcontroller immediately starts going through the code that has been uploaded to it.

The Vex runs the main task, or task main(), once all the way through, and then stops. If you had motors powered up, once the Vex reaches the end of the code, all movement and communication stops.

To have the Vex repeat the code, all of the code you want repeated must be placed inside a loop of some sort, depending on how long you want it to run.

Page 9: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Defining Variables

Boolean – boolbool variable = false;

Integer – intint number = 5280;

Long Integer – longlong large = 63360;

Character – charchar letter = ‘c’;

Floating Decimals – floatfloat decimal = 2.54;

String – stringstring word = “RobotC”;

A Boolean variable can either be true or false.

An integer is just that – any integer from -32,768 to 32,767.

A long integer must be between -2,147,483,648 and 2,147,483,647.

A character can be any letter or symbol on the keyboard.

Floating decimals allow for variables to carry decimals, not just the integer value.

A string can be a long string of characters – (e.g. “Hello” or “abcdef”)

Page 10: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Basic Commands

task main() {

This is where yourcode goes.

}

This is the task that the Vex Cortex runs upon startup. Each time you start a new program, this is the first thing you must write.

The colors you see here are what you should see in RobotC.

The brackets tell the robot when each function starts and stops within the code, as well as different commands and

loops.

Page 11: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Basic Commands

task main(){

motor[port2] = 127;wait1Msec(2000);

}

This task tells the robot to give full power to the motor attached to port 2, for two seconds.

The motor[port2] command writes a power to the motor. Changing the number in the brackets changes which port the Vex is giving power to. (e.g. motor[port3] )

The wait1Msec(2000) command works this way—the “1Msec” tells the Vex to count in 1 millisecond increments. Hence, it counts 2000 milliseconds, or 2 seconds, before ending.

Again, this is what

this code should

look like in

RobotC.

Motor powers range from -127 to 127.

Similar commands are wait10Msec(), which counts in 10 millisecond increments, and wait100Msec(), which counts in 100 millisecond increments.

Page 12: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Communication from the Joystick

This is the Vexnet Joystick:

vexRT[Ch2] – This brings in the value of Channel 2 of the Vexnet Joystick. Channel 2 is the vertical half of the right joystick.

Each channel of the joysticks (Channels 1-4) can input any value from -127 up to 127.

1423

Page 13: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Communication from the Joystick

vexRT[Btn8U] - This brings in the value of Channel 8’s Up button. The four buttons on the right half of the Vexnet Joystick are on Channel 8.

All buttons give values of either 0 or 1. 0 is not pressed. 1 is pressed.

Other examples:vexRT[Btn6D]vexRT[Btn7R]

7

6 58 7

8

Page 14: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

VEXnet Joystick - Accelerometer

The VEXnet Joystick also has a built-in, 3-axis accelerometer. All bring in a value between -127 and 127, just like the joysticks.

vexRT[AccelX] – Brings in the value of the x-axis. (Left/Right acceleration)

vexRT[AccelY] – Brings in the value of the y-axis. (Forward/Back acceleration)

The z-axis doesn’t return any values, because it wasn’t implemented into the hardware.

Y-AxisX-Axis

Z-Axis

Page 15: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Sensors There are two types of

sensory inputs: Analog, and Digital.

Analog inputs can be a wide range of numbers.

Digital inputs, however, only have two options: 0 and 1. Just like the buttons on the Joystick.

int value = SensorValue[in2]; This brings in the value of the

sensor on Analog Port 2, and assigns it to the variable “value.”

int value = SensorValue[dgtl8]; This brings in the value of the

sensor on Digital Port 8, and assigns it to the variable “value.”

Page 16: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Loops

For Loop What does this mean?

task main(){

for(int n = 0; n < 5; n++){

motor[port2] = 127;motor[port3] = 127;wait1Msec(500);

motor[port2] = 63;motor[port3] = -63;wait1Msec[1000];

}}

for(variable; condition; increment) In this example, the variable is

“n,” the condition is that n must be less than 5, and the increment is 1.

Each time this loop is executed, at the end “n” is raised by one. Once n reaches 5, after the fifth time through the loop, the Vex controller will move on to the next bit of code.

“n++” means the

same as “n=n+1.”

You are adding 1 to

n.

Page 17: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Loops

While Loop What does this mean?

task main(){

ClearTimer(T1);while(time1[T1] < 5000){

motor[port2] = 63;motor[port3] = 63;

}}

while(condition) In this case, the while loop is

essentially saying “until 5 seconds have passed, give half power to motor ports 2 and 3.”

Before you can use a while loop with a time increment: You must clear the timer. If you don’t clear the timer,

your loop may be completely skipped.

This clears the timer, T1. RobotC has 4

different prewritten timers, called T1, T2, T3,

and T4.

Like the other timer, “time1[T1]” allots the timer to

count 1 millisecond increments. This counts 5

seconds. If it read “time10[T1],” it would count

50 seconds.

Page 18: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Loops

Do-While Loop

This works just like a while loop?

task main(){

int n = 0;do{

motor[port2] = 63;motor[port3] = 63;wait1Msec(500);n++;

}while(n < 5)

}

do{

Instructions}while(condition)

Yes, but there is one difference. Even if your condition is never met, it still executes what is in the braces once.

If you were change “int n = 0;” to “int n = 8;” the loop would still run once, even though it doesn’t meet the requirement, because 8 is not less than 5.

Page 19: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

If Condition

task main()

{

ClearTimer(T1);

while(time10[T1] < 6000)

{

if(vexRT[Btn6U] != 0)

{

motor[port2] = 127;

motor[port3] = 127; }

}

}

if(condition) If the condition is met, it

executes what is in the braces once, and moves on.

The characters “!=“ mean “is not equal to.” What this is statement is saying is “if the 6-Up button is pushed, move forward at full power.” This while loop runs for 60

seconds. (time10[T1] counts in 10

millisecond increments, and 6000 of those is equal to 60,000 milliseconds, or 60 seconds)

Since there is not a wait statement inside the if statement, the changes are instantaneous.

Page 20: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

This is what RobotC looks like. All new programs start out like this, with nothing pre-written.Errors can be

found by clicking this button down

here, if they aren’t already being

shown.

Page 21: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Compiling

After you have written your code, you compile it to see if any errors are found. You can either go to the Robot Menu, and click Compile Program, or you can hit the F7 key on your keyboard.

Page 22: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Here is some code:Actual Code What does this mean?

task main(){

while(1 == 1){

if (vexRT[Btn8U] == 1){

motor[port2] = 63.5;

motor[port3] = 63.5;

}else if(vexRT[Btn8R] ==

1){ motor[port2] = -

63.5; motor[port3] =

63.5; }

else if(vexRT[Btn8D] == 1)

{ motor[port2] = -

63.5; motor[port3] = -

63.5;}else if(vexRT[Btn8L] ==

1){ motor[port2] =

63.5; motor[port3] = -

63.5;}else{ motor[port2] = 0; motor[port3] = 0;}wait1Msec(20);

}}

A task is a set of commands. If it is not the main task, it must be called inside the main task.

While loop – Does whatever is in the brackets while the initial condition is true.

If the Up Button is pressed, do this.

Since 1 is always equal to 1 , this loop

will run forever.

If the Right button is pressed, the right motor turns backward, and the left motor turns forward.

If the down button is pressed, both motors turn at half power, moving the robot backwards.

If the Left button is pressed, the right motor turns forward, and the left motor turns backward.

This time, it tells both

motors to turn at half

power the opposite

direction, moving the

robot backward.

In this case, it tells

both motors to turn

at half power, moving

the robot forward.

And of course, if nothing is pressed, it tells the motors to do nothing.

Page 23: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is
Page 24: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Now for EasyC! The same concepts from

RobotC are present in EasyC; all those loops you learned about earlier are there, except for the do-while loops.

EasyC is a drag-and-drop format; you look through the list on the left part of the screen, click on it, and drag it into your code.

They must be placed on a vertical line to be added into the code.

Page 25: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Variables in EasyC

To declare a variable, you double click on the “Variables” button, and a box comes up with columns.

You can either type the variable type, or choose from a list of available types.

Page 26: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Compiling and Downloading EasyC compiling is somewhat similar

to RobotC in its hotkeys.

F7 is the Compile key and Download key.

The control key (Ctrl) plus the F7 key just Compiles the program.

Page 27: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is
Page 28: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Two Basic Motor Drive Types

Arcade Drive (Single Joystick)

Uses one joystick.

Leaves one hand free to control other objects on the robot.

Less control on individual wheels.

More multitasking ability.

Tank Drive (Two Joysticks)

Uses both joysticks.

Both hands are occupied with the motors.

More individual control over the wheels.

Less multitasking ability.

Page 29: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Demo Board

Go to “Sensor.c” in the folder on the desktop.

Channel 6 changes the motor output from linear to parabolic. (Low speed, change type)

Page 30: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Drive Demo

Go to “ArcadeTank Drive.c” in the folder on the desktop.

Channel 6 also changes the drive type. (Hold it down for a second or two)

Page 31: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Levers & Gears

This is a balanced lever. The distance on each side is the same, and the weights are the same, so the lever is balanced.

1 kg

1 kg

Fulcrum

3 Meters 3 Meters

1 kg

1 kg

Fulcrum

4 Meters 2 meters

Now this lever is unbalanced, since the fulcrum is not centered and the weights are the same.To balance it, we must change the weight on one side so the product is the same.

If the product is not the same, then the lever will lean one way.

The “product” is this: (weight) x (length of side)

Here, it would read “(1kg) x (2meter)” or “(1kg) x (4 meter)”

Since 2 is not equal to 4, this lever leans one direction. To balance it, we must add weight to one side, or take some away from the other.

1 kg

2 kg

Fulcrum

4 Meters 2 meters

Now it is balanced! 2x2 = 44x1 = 4Levers do not only work

with weights, but also work with force. If you apply 1 lb of force on the left side, it turns into 2lbs of force on the right side.

Page 32: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Levers & Gears

Gears work kind of like levers: They can either decrease force output, or increase force ouput.

If you have a motor hooked up to any size gear, that gear always will turn at the same rate. If you attach a different size gear to that first gear, the rate at which it turns will change.

Big gear to small gear: The small gear turns at a greater rate

Small gear to big gear: The larger gear turns at a smaller rate

The “product” of gears is this: (speed) x (radius)To get the speed of the

second gear, you would take the “product” of the first and divide by the radius of the second.

Motor

Page 33: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Speed and Torque With gears, you saw that a big gear going to a

small gear increased speed. This also means that it lost torque. Torque is the power that the motor has to turn the

wheel. If the wheel is attached to that small gear, sure, the wheel can turn faster, but the power has decreased. If this was a really heavy robot, the wheel wouldn’t turn very well at all.

When you go from a small gear to a large gear, with the wheel attached to the large gear, the wheel turns much slower. With this loss in speed comes a gain in torque. If this

was a really heavy robot, it would still be able to move around easily, though slower.

Page 34: An Introduction to Vex Coding by Joseph Newman. What languages are available?  We will be covering two popular languages, RobotC and EasyC.  EasyC is

Pictures