Vexlib

=VexLib= <-- Prev - Contents - Next -->

The Vex library handles all communication with the Vex Robot. The Functions or commands that are available to use are listed below. All of the examples refer to a variable named "vc". For now, assume that vc is a previously defined VexConnection (more on this later). The VexConnection contains all of the VexLib functions that we will need, so we access them by putting a "." after the vc variable, followed by the function name. For example, vc.InitVex is the first function we will talk about.

InitVex (Com #)
The InitVex Function sets up the communications channel to the Robot. It needs a Com Port Number for input that you should be able to find when pairing the robot with the computer. This will need to be placed near the beginning of your program. Before you send any commands to the robot, you will need to initialize the connection using this function.

Example: code vc.InitVex("COM5") code Note that the string "COM5" may change (it may be "COM6", "COM15", etc), depending on your computer's settings... we'll explain more when you make your own program.

[[image:motor_connection.JPG width="325" height="317" align="right"]]
The addMotor Function tells the Vex Controller that there is a motor connected on a port. It needs a Motor port number for input that you can find by looking at which port of the controller you connected the motor to. The value should be between 1 and 8. If I were to plug in a motor where the picture at right indicates (aka in Port 1), the code would look like this: code vc.addMotor(1); code

The number in parentheses should be the port number you plugged a motor into. You will have to call this function for each motor plugged into your robot.

addServo(Port #)
The addServo Function works the same way as the addMotor Function. Look for the port number that you connected the Servo Motor to on the Vex Controller and place that into the function.The value should be between 1 and 8. code vc.addServo(1); code

addBumpSensor(Port#)
The addBumpSensor Function tells the Vex Controller that you plugged a Bump Sensor into it, and what Port it is plugged into. It's code will look similar to what is below. The value should be between 1 and 15. The following code assumes a bump sensor has been plugged into port 3. code vc.addBumpSensor(3); code

addLimitSensor(Port #)
The addLimitSensor is the same as the bump sensor code, but indicates that a Limit Switch is plugged into the controller. The value should be between 1 and 15. The following code assumes a Limit Switch is plugged into port 4. code vc.addLimitSensor(4); code

addLightSensor
The addLightSensor is the same as the bump sensor code, but indicates that a Light Sensor is plugged into the controller. The value should be between 1 and 15. The following assumes a Light Sensor is plugged into port 6. code vc.addLightSensor(6); code

addUSSensor(Port #, Interrupt #)
The addUSSensor function tells the robot that an Ultrasound Sensor is plugged into the controller. The addUSSensor function requires a little bit more input in order to properly work. The first number in parentheses, like before, is the port number. The port number should be a value between 1 and 15, and indicates which sensor port the US Sensor is plugged into. The second number in parentheses is the interrupt number, and tells the program which interrupt port you connected the Ultrasound Sensor to. The interrupt port should be a value between 1 and 6. The following indicates that an Ultrasound sensor has been plugged into sensor port 10, and interrupt port 2. code vc.addUSSensor(10,2); code

finalizePorts
The finalizePorts function tells the vex controller that you are now done adding sensors, motors, or servos and it should start expecting to receive commands that move the robot or return information to you. You MUST call this function before you start sending and receiving commands to control the motors and sensors. code vc.finalizePorts; code

CloseVex
This ends the connection to the Vex Robot. It should only be used if you are shutting down your program, because once it is called, you will not be able to communicate with the robot until you restart the robot and the server's program. code vc.CloseVex; code

SendMotorCommand(motor#, value)
The SendMotorCommand gets your robot moving. It tells the robot to set a particular motor to a particular value. The motor number should be between 1 and 8, and indicates the port the motor is plugged into. The second number in parenthesis will indicate what value to set the motor to. 14 is stop, 0 is full speed counterclockwise, 28 is full speed clockwise, and any number in between these gives a faster or slower rotation. code //Stop motor 3 vc.SendMotorCommand(3, 14); //Full Speed Clockwise on motor 2 vc.SendMotorCommand(2, 28); //Full Speed Counter-Clockwise on motor 6 vc.SendMotorCommand(6, 0); code Remember that once you set a motor to a certain value, it will continue to move at that speed until you send it another command (or turn the robot off).

GetPortValue
The GetPortValue Function checks the sensor at the specified sensor port number and gets its current value. The meaning of the value it returns depends on what kind of sensor is plugged into the port you specified. For example, if you plugged a bump sensor into port 3 (and had vc.addBumpSensor(3) earlier in your code), then the following code would get the value of the bump sensor, and store it in the variable called "result". code format="csharp" int result = vc.getPortValue(3); code So, "result" will be 1 if the bump sensor on port 3 was being pushed, and 0 if it was not.

Similarly, if an ultrasound sensor had been plugged into port 3, then result would hold a number indicating the distance the sensor read at that time.

This means that in your program, you must keep track of what ports you plugged your sensors into!

<-- Prev - Contents - Next -->