Using the Controller can make it easier to drive and move your robot to complete a task. However, there are limitations to using the Drive program, and depending on your robot build or the task at hand, you may want different controls. Coding the Controller allows you to optimize the Controller to make it fit your robot and the task at hand better. There are several ways to code the Controller in VEXcode EXP. Each has its advantages and limitations, and some methods are better suited for certain situations depending on the desired outcome.
This article will walk you through three different options for custom coding the Controller in VEXcode EXP. Each method will be described with its advantages, limitations, and an example use case to help guide you when choosing a method. For the purposes of this article, all code examples shown were created for the Clawbot. However, the same concepts could be applied to numerous other builds found on builds.vex.com, and custom builds.
Option 1: Assigning Buttons in the Device Configuration
This option is great when you are using a standard build, like a BaseBot or Clawbot, and want to get up and running quickly.
This option lets you assign motors, a drivetrain, or motor groups to buttons on the Controller in the Device Configuration. For more information about how to assign buttons to the Controller in the Device Configuration, view this article.
Summary of Option 1: Assigning Buttons in the Device Configuration
Advantages |
Limitations |
Example Situation |
---|---|---|
|
|
|
Option 2: Using a Forever Loop
If you are using a custom build instead of a standard build, or want to be able to have more customization in your Controller, this option is a good one. Using a Forever loop is a great introduction to create custom code for your Controller.
This option places all of the conditions for the Controller, and its associated buttons, in a Forever loop. This provides more flexibility, especially with custom build designs, but also requires some coding experience. One consideration when using this option however, is the length and complexity of your project. The more conditions that are added, the longer the code stack may become. This means that multiple blocks have to be executed in order, and when there are a lot of blocks, this can slow down project execution. Slower project execution may create a lag between pressing Controller buttons and seeing the robot behavior.
The specific example shown below is one way that you can use a Forever loop with a custom design robot (such as a robot with a custom drivetrain) to drive the robot and manipulate the claw and arm in order to interact with objects.
Download the "Option 2" VEXcode EXP project file >
Explanation of Option 2 Code.
Code Piece |
Explanation |
---|---|
A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released. |
|
A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released. A Forever loop is used in order to continuously check for which buttons are being pressed on the Controller. The [Set motor velocity] blocks are used to set the motor velocity to the current Controller’s position along the 2 and 3 axes. This is the equivalent to setting a car in drive. That doesn’t necessarily make the car move, it just sets it. Each joystick axis returns a value between -100 to +100, and returns a value of zero when centered. This then means that the joystick axes, when pushed, equate to -100% to 100%. The farther towards 100 or -100 the axes are pushed, the faster the motor will spin. The [Spin] block is then used to actually move the motor. This is the equivalent to pressing the gas on the car once the direction has been set. This allows each motor to be controlled by one of the four Controller axes. |
|
A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released. The [If then else if then else] block is used to map certain behaviors to buttons being pressed or released on the Controller. In this section of code, the conditions set are if the Up or Down buttons are pressed. If so, certain behaviors will happen, such as the arm raising and lowering. There is also the else portion of the condition, if neither button is pressed, the arm is set to stop moving. Note the following section of code in the project for the Claw follows the same explanation. |
Summary of Option 2: Using a Forever Loop
Advantages |
Limitations |
Example Situation |
|
|
|
Option 3: Using Events
If you want a lot of customization to your Controller, Using Events is the best option for you. One button press can trigger multiple robot behaviors, like pressing a button to open the claw, raise the arm, and drive forward for a set distance. Trying to code multiple behaviors per button within a Forever loop would cause the project execution to slow dramatically – using Events allows you to do this more effectively.
This option uses Events to break up the project flow. This is similar to using a Forever loop, but allows for the code to be more organized, so that the button execution has a faster response time. Faster response time means you will not see a lag between pressing Controller buttons and seeing the robot behavior. This example shows the same behaviors as the previous project, but done using the Events instead of the Forever Loop.
Download the "Option 3" VEXcode EXP project file >
Explanation of Option 3 Code.
Code Piece |
Explanation |
---|---|
A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released. |
|
A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released. {When controller axis} Event blocks are used to trigger certain behaviors when one of the four axes on the Controller are changed by using the joysticks. The [Set motor velocity] blocks are used to set the motor velocity to the current Controller’s position along the A and D axes. This is the equivalent to setting a car in drive. That doesn’t necessarily make the car move, it just sets it. Each joystick axis returns a value between -100 to +100, and returns a value of zero when centered. This then means that the joystick axes, when pushed, equate to -100% to 100%. The farther towards 100 or -100 the axes are pushed, the faster the motor will spin. The [Spin] block is then used to actually move the motor. This is the equivalent to pressing the gas on the car once the direction has been set. This allows each motor to be controlled by one of the four Controller axes. |
|
A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released. {When controller axis} Event blocks are used to map certain behaviors to buttons being pressed or released on the Controller. In this section of code, the conditions set are if the Up or Down buttons are pressed. If so, certain behaviors will happen, such as the arm raising, lowering, or stopping. Note the last section of code in the project for the Claw follows the same explanation. |
Summary of Option 3: Using Events
Advantages |
Limitations |
Example Situation |
|
|
|