Main Content

Autonomous Mode and Driver Mode with VEX EDR V5 Robot Brain

This example shows how to use Simulink® Coder™ Support Package for VEX® EDR V5 Robot Brain to implement Pre-Autonomous, Autonomous, and Driver Controlled modes in the same Simulink model.

Pre-Autonomous mode, Autonomous mode, Driver Controlled mode and the switch of states among the three is a format that applies only to the VEX Robotics Competition. This model should be used only in a VEX Competition field where a VEX field controller controls the modes of the VEX Robot (you can also use a VEXnet Competition Switch that simulates a VEX field controller for match practice).

Introduction

Simulink Coder Support Package for VEX EDR V5 Robot Brain enables you to create and run Simulink models on a VEX EDR V5 Robot Brain.

In this example, you will learn how to program Pre-Autonomous, Autonomous, and Driver Controlled modes in the same Simulink model that is deployed on V5 Robot Brain. You will create the control logic to perform these example actions:

  • Use the Pre-Autonomous mode to lift the robot's arm to a vertical position

  • Use the Autonomous mode to lower the robot's arm to a horizontal position

  • Use the Driver Controlled mode to control the position of the robot's arm by using the joystick on V5 Controller gamepad

Prerequisites

  • We recommend completing Getting Started with VEX EDR V5 Robot Brain Support Package example.

Required Hardware

To run this example, you will need the following hardware:

  • VEX EDR V5 Robot Brain

  • DC motor and Motor Controller 29

  • V5 Robot Battery

  • V5 Controller

  • V5 Robot Radio

  • VEXnet Competition Switch (or the VEX field controller)

  • Potentiometer

  • Two-wheel robot platform with arm

  • 300mm Smart Cable

  • USB A to Micro Cable

Task 1 - Hardware Connections

After you assemble the robot and attach the other components listed in the previous section, proceed with the connections listed below:

1. Connect the V5 Robot Battery to the VEX EDR V5 Robot Brain.

2. Connect the VEX EDR V5 Robot Brain to your computer using the USB cable.

3. Connect the DC motor (attached to the arm's shaft) to the 3-wire port A on the VEX V5 Robot Brain. Use the Motor Controller 29 cables to establish the connection between the motors leads and the port on V5 Robot Brain.

4. Connect the Potentiometer (attached to the arm's shaft) to the 3-wire port B on the VEX V5 Robot Brain.

5. Connect the V5 Robot Radio to Smart Port 21 on the V5 Robot Brain, using the 300 mm Smart Cable. (This connection will enable wireless connectivity between the V5 Robot Brain and the gamepad after they are switched on.)

6. Connect the VEXnet Competition Switch to the Ethernet port located at the back of the V5 Controller (gamepad), using an Ethernet cable. On the VEXnet Competition Switch, set the Enable/Disable switch to Disable state and the Driver/Autonomous switch to Autonomous state.

Task 2 - Create the Basic Model with Competition Switch

In this task, you will create the basic model that incorporates separate control logic based on the various modes available with the VEXnet Competition Switch:

1. Launch Simulink and open a Blank Model.

2. In the Simulink Library Browser, navigate to Simulink > Ports & Subsystems, and drag and drop the Enabled Subsystem block to your model. Create two more copies of the block.

3. Rename the three Enabled Subsystem blocks as Pre-Autonomous, Autonomous and Driver.

4. In the Simulink Library Browser, navigate to Simulink Coder Support Package for VEX EDR V5 Robot Brain > Gamepad, and drag and drop the Competition Switch block into your Simulink model.

5. Connect the Pre-Auto port of the Competition Switch to the Pre-Autonomous subsystem, the Auto port to the Autonomous subsystem, and the Driver port to the Driver subsystem.

Tip: This basic model is also available as a template named Competition Model on the Simulink Start page (Launch Simulink, navigate to Simulink Coder Support Package for VEX EDR V5 Robot Brain, and select Competition Model).

Task 3 - Create Logic for Pre-Autonomous Mode

In this task, you will create control logic required for the Pre-Autonomous mode, which will set the robot's arm at a vertical position:

1. Double click and open the Pre-Autonomous subsystem. Delete the existing Inport, Outport and the connection between them.

2. In the Simulink Library Browser, navigate to Simulink Coder Support Package for VEX EDR V5 Robot Brain > Sensors, and drag and drop the Analog Input block into the Pre-Autonomous subsystem. Rename the block to Potentiometer.

3. Double-click the Potentiometer block, and select the Port as B. Click Apply and then OK.

4. Drag and drop the Compare to Constant block from the Logical Operations category in the VEX V5 library to the subsystem. Double-click the block, select the Operator value as ≤= and set the Constant Value parameter to a value that represents the vertical position of the robot's arm (based on the calibration).

5. Connect the output of the Potentiometer block to the input of the Compare to Constant block.

6. Drag and drop the Gain block from the Math Library category in the VEX V5 library to the subsystem. Set the Gain to 50 (user-defined).

7. Connect the output of the Compare to Constant block to the input of the Gain block.

8. Drag and drop the DC Motor block from the Actuators category in the VEX V5 library to the subsystem. Rename the block to Robot Arm Motor.

9. Double-click the Robot Arm Motor block, and select the Port as A. Click Apply and then OK.

10. Connect the output of the Gain block to the input of the Robot Arm Motor block.

This completes the logic required for the Pre-Autonomous mode that sets the robot's arm at a vertical position.

Task 4 - Create Logic for Autonomous Mode

In this task, you will create control logic required for the Autonomous mode, which will set the robot's arm at a horizontal position.

1. Double click and open the Autonomous subsystem. Delete the existing Inport, Outport and the connection between them.

2. In the Simulink Library Browser, navigate to Simulink Coder Support Package for VEX EDR V5 Robot Brain > Actuators, and drag and drop the DC Motor block into the Pre-Autonomous subsystem. Rename the block to Robot Arm Motor.

3. Double-click the Robot Arm Motor block, and select the Port as A. Click Apply and then OK.

4. Drag and drop the Constant block from the Utilities category in the VEX V5 library to the subsystem. Set the Constant value to -127.

5. Connect the output of the Constant block to the input of the Robot Arm Motor block.

This completes the logic required for the Autonomous mode that sets the robot's arm at a horizontal position.

Task 5 - Implement Logic for Driver Controlled Mode

In this task, you will create control logic required for the Driver Controlled mode, which helps you to control the robot's arm using the joystick (analog channel 1) on the V5 Controller gamepad:

1. Double click and open the Driver subsystem. Delete the existing Inport, Outport and the connection between them.

2. In the Simulink Library Browser, navigate to Simulink Coder Support Package for VEX EDR V5 Robot Brain > Actuators, and drag and drop the DC Motor block into the Driver subsystem. Rename the block to Robot Arm Motor.

3. Double-click the Robot Arm Motor block, and select the Port as A. Click Apply and then OK.

4. Drag and drop the Gamepad Joystick block from the Gamepad category in the VEX V5 library to the subsystem. Rename the block to Horizontal Analog Stick.

5. Connect the output of the Horizontal Analog Stick block to the input of the Robot Arm Motor block.

This completes the logic required for the Driver Controlled mode that helps you to control the robot's arm using the joystick (analog channel 1) on the V5 Controller gamepad.

6. Save the whole model, which includes the basic model created in Task 2 and the Enabled Subsystems created in Task 3, Task 4, and Task 5.

Task 6 - Build and Download the Simulink Model

In this task, you will build and download the completed Simulink model to the VEX EDR V5 Robot Brain.

1. Connect the V5 Robot Brain to the host computer with a USB A to Micro cable.

2. Switch on the V5 Robot Brain.

3. Open the completed model, or open the pre-configured vexv5_CompetitionSwitch model included for your convenience.

Tip: If you have used the Competition Model template to create the complete Simulink model, you can directly proceed to Step 7 below.

4. Open the Modeling tab and press CTRL+E to open Configuration Parameters dialog box.

5. In the Configuration Parameters dialog box, navigate to the Hardware Implementation pane:

  • Set the Hardware board to VEX V5 Robot Brain.

  • In the Target Hardware Resources section, set the Build options to Build, load and run to automatically download the generated binary file on to the connected V5 Robot Brain.

6. Navigate to Solver pane and select the option Treat each discrete rate as a separate task. Click OK.

7. Open the Hardware tab and click Build, Deploy & Start. The model is deployed to the V5 Robot Brain.

The Simulink model starts running in the V5 Robot Brain.

Task 7 - Use the VEXnet Competition Switch to Set the Model in Pre-Autonomous, Autonomous, and Driver Modes

In this task, you will learn how to set the example model in the three different modes, based on the position of the Competition Switch.

The following table shows the output from the Competition Block based on the mode selected using the switch positions of the Competition Switch.

1. Disconnect the USB cable from the host computer.

2. Switch on the V5 Controller gamepad. Change the setting on the V5 Controller to enable Vexnet (for wireless connectivity to V5 Robot Brain).

3. Connect the VEXnet Competition Switch to the V5 Controller using the Ethernet cable, and ensure that the Enable/Disable switch is at Disable state and the Driver/Autonomous switch is at Autonomous state (this represents the Pre-Autonomous mode).

The Pre-Autonomous mode is enabled immediately, and the program runs the corresponding logic (moving the robot's arm to a vertical position).

4. Set the Enable/Disable switch to Enable state (the Driver/Autonomous switch was already set to Autonomous state in Step 3). The Autonomous mode is enabled and the program runs the corresponding logic (moving the robot's arm to a horizontal position).

5. Set the Driver/Autonomous switch to Driver state. The Driver Controlled mode is enabled and you can control the motor using the joystick (analog channel 1) on the V5 Controller gamepad.

6. Set the Enable/Disable to Disable state. This will disable the robot. The DC Motor is inactive in this state.

To run the program again, select the program (the model that you downloaded) from the LCD touchscreen of the V5 Controller.