USING REMOTES
Since we are able to run motors on the robot, the next logical step is to drive the robot. The basic form to create a robot drive code is as follows:
task main()
{
while (true)
{
(Insert Drive Code Here)
}
}
The while loop continuously changes the motor values to reflect that of the remote. To retrieve a value from the remote, you would use “vexRT[Ch#];” The # would be replaced by the number of the channel. On the Vex joystick, channels ONLY APPLY to the joystick on the remote, NOT THE buttons which will be discussed in the next tutorial. The Vex joystick lists the channels with their corresponding numbers:
(image of channels with numbers)
For the purposes of this part of the tutorial we will only be using channels 3 and 2 on the main controller since they are the up and down channels for the right and left joysticks respectively. Additionally, we will only use ports 1 and 2 on the robot to simplify the process. To set the value of a motor to the value of the joystick, you would write “motor[portN] = vexRT[Ch#];” The N would be replaced with the port number of the motor and the # would be replaced with the channel of the joystick. Since the joystick channels go from 127 to -127, there is no problem with the conversion of remote values to motor values. The code for the joystick, as shown above, is inserted within the while loop to continue refreshing the motor value. Hence, the code to pair ports 1 and 2 with channels 3 and 2 looks like:
task main()
{
while (true)
{
motor[port1] = vexRT[Ch2]; // map port 1 to channel 2 on the main remote
motor[port2] = vexRT[Ch3]; // map port 2 to channel 3 on the main remote
}
}
However, since there are two controllers for driving the robot, a slight alteration to the code must be made if we wish to program for the partner controller. To program the joysticks on the partner controller, all you have to do is write “vexRT[Ch#Xmtr2];” where the # is replaced with the channel number. The only difference between programming the partner controller and programming the main controller is the addition of “Xmtr2” after the channel number. Thus, to link ports 3 and 4 on the robot to channels 2 and 3 on the partner controller, you would write the following:
task main()
{
while (true)
{
motor[port3] = vexRT[Ch2Xmtr2]; // map port 3 to channel 2 on the partner remote
motor[port4] = vexRT[Ch3Xmtr2]; // map port 4 to channel 3 on the partner remote
}
}
Ergo, the complete code to run ports 1 – 4 on the robot with both joysticks, one would write:
task main()
{
while (true)
{
motor[port1] = vexRT[Ch2]; // map port 1 to channel 2 on the main remote
motor[port2] = vexRT[Ch3]; // map port 2 to channel 3 on the main remote
motor[port3] = vexRT[Ch2Xmtr2]; // map port 3 to channel 2 on the partner remote
motor[port4] = vexRT[Ch3Xmtr2]; // map port 4 to channel 3 on the partner remote
}
}
Copy this code into RobotC. To program for controllers in RobotC, you must change one compiler setting. Go to Robot -> VEX Cortex Communication Mode -> VEXnet or USB. This compiler setting will only run your code WHEN THE REMOTE IS PLUGGED INTO THE ROBOT. Now download the code to the robot (which you should know how to do by now). Make sure the robot is turned off at this point. Plug the main remote into the robot using the A to A cable. Then use the following plug to link the main and partner remotes together:
(image of the plug and how to link the controllers)
Make sure both remotes have batteries by unscrewing their backs and checking:
(image of remote back off)
Plug the main controller into the robot using an A to A cable. Turn both remotes on. Then turn the robot on. When all lights on the main remote are green, you can start testing your code! Once done, turn the robot as well as the remotes off. By connecting the main remote to the robot via an A to A cable, you have paired the remote to the robot. This means that when you use a wireless connection between the robot and controller, known as Vexnet, the robot should recognize the remote. To perform this wireless connection, you must use a pair of Vexnet keys as displayed below:
(show image of Vexnet keys)
Remove the A to A cable from both the robot and the main controller. Insert a Vexnet key into the main controller as well as into the robot. Turn both controllers on and make sure a blue light appears on the Vexnet key:
(Image of Vexnet key with blue light)
Insert a Vexnet key into the robot and turn the robot on. Similarly, make sure a blue light appears on the Vexnet key in the robot. Now test your program to make sure the link between controller and robot is correct. From now on, when we use controllers with your Vex Cortex, we will be using Vexnet to connect them. Go have fun playing with your now drivable robot.
Cheers!
task main()
{
while (true)
{
(Insert Drive Code Here)
}
}
The while loop continuously changes the motor values to reflect that of the remote. To retrieve a value from the remote, you would use “vexRT[Ch#];” The # would be replaced by the number of the channel. On the Vex joystick, channels ONLY APPLY to the joystick on the remote, NOT THE buttons which will be discussed in the next tutorial. The Vex joystick lists the channels with their corresponding numbers:
(image of channels with numbers)
For the purposes of this part of the tutorial we will only be using channels 3 and 2 on the main controller since they are the up and down channels for the right and left joysticks respectively. Additionally, we will only use ports 1 and 2 on the robot to simplify the process. To set the value of a motor to the value of the joystick, you would write “motor[portN] = vexRT[Ch#];” The N would be replaced with the port number of the motor and the # would be replaced with the channel of the joystick. Since the joystick channels go from 127 to -127, there is no problem with the conversion of remote values to motor values. The code for the joystick, as shown above, is inserted within the while loop to continue refreshing the motor value. Hence, the code to pair ports 1 and 2 with channels 3 and 2 looks like:
task main()
{
while (true)
{
motor[port1] = vexRT[Ch2]; // map port 1 to channel 2 on the main remote
motor[port2] = vexRT[Ch3]; // map port 2 to channel 3 on the main remote
}
}
However, since there are two controllers for driving the robot, a slight alteration to the code must be made if we wish to program for the partner controller. To program the joysticks on the partner controller, all you have to do is write “vexRT[Ch#Xmtr2];” where the # is replaced with the channel number. The only difference between programming the partner controller and programming the main controller is the addition of “Xmtr2” after the channel number. Thus, to link ports 3 and 4 on the robot to channels 2 and 3 on the partner controller, you would write the following:
task main()
{
while (true)
{
motor[port3] = vexRT[Ch2Xmtr2]; // map port 3 to channel 2 on the partner remote
motor[port4] = vexRT[Ch3Xmtr2]; // map port 4 to channel 3 on the partner remote
}
}
Ergo, the complete code to run ports 1 – 4 on the robot with both joysticks, one would write:
task main()
{
while (true)
{
motor[port1] = vexRT[Ch2]; // map port 1 to channel 2 on the main remote
motor[port2] = vexRT[Ch3]; // map port 2 to channel 3 on the main remote
motor[port3] = vexRT[Ch2Xmtr2]; // map port 3 to channel 2 on the partner remote
motor[port4] = vexRT[Ch3Xmtr2]; // map port 4 to channel 3 on the partner remote
}
}
Copy this code into RobotC. To program for controllers in RobotC, you must change one compiler setting. Go to Robot -> VEX Cortex Communication Mode -> VEXnet or USB. This compiler setting will only run your code WHEN THE REMOTE IS PLUGGED INTO THE ROBOT. Now download the code to the robot (which you should know how to do by now). Make sure the robot is turned off at this point. Plug the main remote into the robot using the A to A cable. Then use the following plug to link the main and partner remotes together:
(image of the plug and how to link the controllers)
Make sure both remotes have batteries by unscrewing their backs and checking:
(image of remote back off)
Plug the main controller into the robot using an A to A cable. Turn both remotes on. Then turn the robot on. When all lights on the main remote are green, you can start testing your code! Once done, turn the robot as well as the remotes off. By connecting the main remote to the robot via an A to A cable, you have paired the remote to the robot. This means that when you use a wireless connection between the robot and controller, known as Vexnet, the robot should recognize the remote. To perform this wireless connection, you must use a pair of Vexnet keys as displayed below:
(show image of Vexnet keys)
Remove the A to A cable from both the robot and the main controller. Insert a Vexnet key into the main controller as well as into the robot. Turn both controllers on and make sure a blue light appears on the Vexnet key:
(Image of Vexnet key with blue light)
Insert a Vexnet key into the robot and turn the robot on. Similarly, make sure a blue light appears on the Vexnet key in the robot. Now test your program to make sure the link between controller and robot is correct. From now on, when we use controllers with your Vex Cortex, we will be using Vexnet to connect them. Go have fun playing with your now drivable robot.
Cheers!