An Introduction to Vex Coding by Joseph Newman

An Introduction to Vex Coding by Joseph Newman

What languages are available? � We will be covering two popular languages, Robot. C and Easy. C. � Easy. C is a drag and drop program, that involves less typing, because of the way it is formatted. Robot. C has no prewritten commands, so the user must type everything they wish to happen. � Robot. C is first on the agenda.

Transitioning from Robot. C to Easy. C � When you switch programs, from Robot. C to Easy. C, no extra software is needed. When you compile and download the Easy. C 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 Robot. C, you must download the Robot. C firmware back onto it, or it won’t be able to communicate with the Vex Cortex.

How to download Robot. C firmware:

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

Why Robot. C? � Robot. C 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 Robot. C can be applied to almost any other program that is based off of C.

Syntax � Uncapitalized and capitalized words ARE different. Robot. C is case sensitive. � e. g. “task” is different from “Task” � Robot. C colors words that it recognizes. � e. g. When “task” is typed, it turns into “task, ” and “port 2” turns into “port 2. ” � Most lines end with a semicolon. ;

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.

Defining Variables �A Boolean variable can either be Boolean – bool true or false. bool variable = false; � An integer is just that – any Integer – integer from -32, 768 to 32, 767. int number = 5280; � A long integer must be between Long Integer – long -2, 147, 483, 648 and long large = 63360; 2, 147, 483, 647. Character – char � A character can be any letter or char letter = ‘c’; symbol on the keyboard. Floating Decimals – float � Floating decimals allow for float decimal = 2. 54; variables to carry decimals, not String – string just the integer value. string word = “Robot. C”; � A string can be a long string of characters – (e. g. “Hello” or

Basic Commands task main() { This is where your code goes. } The brackets tell the robot when each function starts and stops within the code, as well as different commands and loops. 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 Robot. C.

hat w is is h t , in Aga ok lo ld u o h s e d this co C. like in Robot Basic Commands task main() { motor[port 2] = 127; wait 1 Msec(2000); } Motor powers range from -127 to 1 27. � This task tells the robot to give full power to the motor attached to port 2, for two seconds. � The motor[port 2] 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[port 3] ) � The wait 1 Msec(2000) command Similar commands are works this way—the “ 1 Msec” tells wait 10 Msec(), which counts in 10 the Vex to count in 1 millisecond increments, and wait 100 Msec(), which counts in 100 increments. Hence, it counts 2000 milliseconds, or 2 seconds, before millisecond increments. ending.

Communication from the Joystick � This is the Vexnet Joystick: 3 2 vex. RT[Ch 2] – 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.
![Communication from the Joystick vex. RT[Btn 8 U] - This brings in the value Communication from the Joystick vex. RT[Btn 8 U] - This brings in the value](http://slidetodoc.com/presentation_image/b86b849d78f15e2c62420b5b175cbed0/image-13.jpg)
Communication from the Joystick vex. RT[Btn 8 U] - 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: vex. RT[Btn 6 D] vex. RT[Btn 7 R]

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. X-Axis � vex. RT[Accel. X] of the x-axis. � (Left/Right acceleration) � vex. RT[Accel. Y] of the y-axis. � – Brings in the value (Forward/Back acceleration) � The z-axis doesn’t return any values, because it wasn’t implemented into the hardware. Z-Axis

Sensors � There are two types of sensory inputs: Analog, and Digital. int value = Sensor. Value[in 2]; � This � Analog inputs can be a wide range of numbers. inputs, however, only have two options: 0 and 1. Just like the buttons on the Joystick. brings in the value of the sensor on Analog Port 2, and assigns it to the variable “value. ” � Digital int value = Sensor. Value[dgtl 8]; � This brings in the value of the sensor on Digital Port 8, and assigns it to the variable

e “n++” means th 1. ” same as “n=n+ to n. 1 g in d d a re a u Yo For Loop task main() { for(int n = 0; n < 5; n++) { motor[port 2] = 127; motor[port 3] = 127; wait 1 Msec(500); motor[port 2] = 63; motor[port 3] = -63; wait 1 Msec[1000]; } } Loops What does this mean? � 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

This clears the timer, T 1. Robot. C has 4 different prewritten timers, called T 1, T 2, T 3, and T 4. Loops While Loop Like the other timer, “time 1[T 1]” allots the timer to count 1 millisecond increments. This counts 5 seconds. If it read “time 10[T 1], ” it would count 50 seconds. What does this mean? � while(condition) task main() { Clear. Timer(T 1); while(time 1[T 1] < 5000) { motor[port 2] = 63; motor[port 3] = 63; } } � 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. �
![Loops Do-While Loop task main() { int n = 0; do { motor[port 2] Loops Do-While Loop task main() { int n = 0; do { motor[port 2]](http://slidetodoc.com/presentation_image/b86b849d78f15e2c62420b5b175cbed0/image-18.jpg)
Loops Do-While Loop task main() { int n = 0; do { motor[port 2] = 63; motor[port 3] = 63; wait 1 Msec(500); n++; } while(n < 5) } do { Instructions } while(condition) This works just like a while loop? � 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.
![If Condition task main() { Clear. Timer(T 1); while(time 10[T 1] < 6000) { If Condition task main() { Clear. Timer(T 1); while(time 10[T 1] < 6000) {](http://slidetodoc.com/presentation_image/b86b849d78f15e2c62420b5b175cbed0/image-19.jpg)
If Condition task main() { Clear. Timer(T 1); while(time 10[T 1] < 6000) { if(vex. RT[Btn 6 U] != 0) { motor[port 2] = 127; motor[port 3] = 127; } Since there is not a wait } statement ins ide the if statement, th } e chang instantaneou s. es are � 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. � (time 10[T 1] counts in 10 millisecond increments, and 6000 of those is equal to 60, 000 milliseconds, or 60 seconds)

Errors can be found by clicking this button down here, if they aren’t already being shown. This is what Robot. C looks like. All new programs start out like this, with nothing pre -written.

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 F 7 key on your keyboard.

Here is some code: Actual Code What does this mean? task main() Since 1 is always equal to { while(1 == 1) 1 , this loop will run { forever. if (vex. RT[Btn 8 U] == 1) { , it tells both motor[port 2] = 63. 5; In this case rn at half motor[port 3] = 63. 5; motors to tu } g the robot in v o m r, e w else if(vex. RT[Btn 8 R] == 1) po { forward. motor[port 2] = -63. 5; tells both motor[port 3] = 63. 5; This time, it rn at half } motors to tu else if(vex. RT[Btn 8 D] == 1) pposite power the o { moving the motor[port 2] = -63. 5; direction, motor[port 3] = -63. 5; robot backward. } else if(vex. RT[Btn 8 L] == 1) { motor[port 2] = 63. 5; motor[port 3] = -63. 5; } else { motor[port 2] = 0; motor[port 3] = 0; } And of course, wait 1 Msec(20); } nothing is } 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. 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 pressed, it tells the motors to do If the Left button is pressed, the right motor turns forward, and the left motor turns backward.


Now for Easy. C! � The same concepts from Robot. C are present in Easy. C; all those loops you learned about earlier are there, except for the do-while loops. � Easy. C 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

Variables in Easy. C � 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.

Compiling and Downloading � Easy. C compiling is somewhat similar to Robot. C in its hotkeys. � F 7 is the Compile key and Download key. � The control key (Ctrl) plus the F 7 key just Compiles the program.


Two Basic Motor Drive Types Arcade Drive (Single Joystick) � Uses one joystick. one hand free to control other objects on the robot. Tank Drive (Two Joysticks) � Uses � Leaves � Both � Less � More control on individual wheels. � More multitasking ability. both joysticks. hands are occupied with the motors. individual control over the wheels. � Less multitasking ability.

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)

Drive Demo � Go to “Arcade. Tank Drive. c” in the folder on the desktop. � Channel � (Hold 6 also changes the drive type. it down for a second or two)

The “product” is this: (weight) x (length of side) Levers & Gears Here, it would read “(1 kg) x (2 meter)” or “(1 kg) x (4 meter)” This is a balanced lever. The distance on each side is the same, and the weights are the same, so the lever is balanced. Now this lever is unbalanced, since the fulcrum is not centered and the weights are the same. Fulcrum 1 kg 1 2 g k 1 kg 3 Meters 4 Meters 3 2 meters Meters Since 2 is not equal to 4, Now it is balanced! 2 x 2 = 4 To balance it, we must change this lever leans one 4 x 1 = 4 the weight on one side so the direction. To balance it, we must add weight to product is the same. Levers do not only work with one side, or take some weights, but also work with away from the other. If the product is not the same, force. If you apply 1 lb of then the lever will lean one force on the left side, it turns way. into 2 lbs of force on the right

Levers & Gears 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. � 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. Motor � 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

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

Pictures
- Slides: 34