Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Vex Coding

Similar presentations

Presentation on theme: "An Introduction to Vex Coding"— Presentation transcript:

1 An Introduction to Vex Coding
by Joseph Newman

2 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.

3 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.

4 How to download RobotC firmware:

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

6 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.

7 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. ;

8 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.

9 Defining Variables 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 ,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”) Boolean – bool bool variable = false; Integer – int int number = 5280; Long Integer – long long large = 63360; Character – char char letter = ‘c’; Floating Decimals – float float decimal = 2.54; String – string string word = “RobotC”;

10 task main() { This is where your code goes. }
Basic Commands task main() { This is where your code 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.

11 Basic Commands task main() { motor[port2] = 127; wait1Msec(2000); }
Again, this is what this code should look like in RobotC. 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. task main() { motor[port2] = 127; wait1Msec(2000); } 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.

12 Communication from the 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. This is the Vexnet Joystick: 3 2 4 1

13 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 is not pressed. 1 is pressed. Other examples: vexRT[Btn6D] vexRT[Btn7R] 7 8 8 7 6 5

14 VEXnet Joystick - Accelerometer
The VEXnet Joystick also has a built-in, 3- axis accelerometer. All bring in a value between 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. X-Axis Y-Axis Z-Axis

15 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.”

16 Loops for(variable; condition; increment)
“n++” means the same as “n=n+1.” You are adding 1 to n. 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.

17 Loops While Loop What does this mean? while(condition)
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. This clears the timer, T1. RobotC has 4 different prewritten timers, called T1, T2, T3, and T4. 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.

18 Loops Do-While Loop This works just like a while loop? 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. Do-While Loop task main() { int n = 0; do motor[port2] = 63; motor[port3] = 63; wait1Msec(500); n++; } while(n < 5) This works just like a while loop?

19 If Condition 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.

20 This is what RobotC looks like
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.

21 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.

22 Since 1 is always equal to 1 , this loop will run forever.
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; else if(vexRT[Btn8D] == 1) motor[port3] = -63.5; else if(vexRT[Btn8L] == 1) 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. In this case, it tells both motors to turn at half power, moving the robot forward. This time, it tells both motors to turn at half power the opposite direction, moving the robot backward. 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. And of course, if nothing is pressed, it tells the motors to do nothing.


24 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.

25 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.

26 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.


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

29 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)

30 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)

31 Levers & Gears The “product” is this: (weight) x (length of side)
Here, it would read “(1kg) x (2meter)” or “(1kg) 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. 1 kg 2 kg Fulcrum 4 Meters 2 meters 1 kg Fulcrum 3 Meters 1 kg Fulcrum 4 Meters 2 meters Now this lever is unbalanced, since the fulcrum is not centered and the weights are the same. 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. Now it is balanced! 2x2 = 4 4x1 = 4 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. Levers 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.

32 The “product” of gears is this:
(speed) x (radius) Levers & Gears 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. 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 Motor

33 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.

34 Pictures

Download ppt "An Introduction to Vex Coding"

Similar presentations

Ads by Google