Servo Control Using a V5 Controller Button

This example shows you how to use Simulink Coder Support Package for VEX® EDR V5 Robot Brain to control a servo motor using a button on V5 Controller gamepad.

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. This example uses a button on the V5 Controller gamepad to control the angle position of a servo motor connected to a VEX EDR V5 Robot Brain. This example also shows how to latch the state of the servo motor to a specific angle until the button is pressed (and released) again.

Prerequisites

  • If you are new to Simulink, watch the Simulink Quick Start video.

  • Complete the Getting Started with VEX EDR V5 Support Package example.

Required Hardware

To run this example, you need the following hardware:

  • VEX EDR V5 Robot Brain

  • V5 Controller Gamepad

  • Servo motor

  • V5 Robot Battery

  • V5 Robot Radio

  • 300mm Smart Cable

  • USB A to Micro Cable

Task 1 - Hardware Connections

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 servo motor to the 3-wire port A on the VEX EDR V5 Robot Brain.

4. Connect the V5 Robot Radio to Smart Port 21 on the V5 Robot Brain using the Smart Cable. (This connection enables wireless connectivity between V5 Robot Brain and the gamepad after they are switched on.)

Task 2 - Set the Servo Motor Position Using a Gamepad Button

In this task, you will create a Simulink model that configures the button on the V5 Controller (gamepad) to control the angular position of a servo motor.

1. Launch Simulink and open a Blank Model.

2. In the Simulink Library Browser, navigate to Simulink Coder Support Package for VEX EDR V5 Robot Brain.

3. Drag and drop the Gamepad Button block from the Gamepad category into your Simulink model. Rename the block to ServoButton. Set the Button parameter to X to indicate the button that controls the servo motor.

4. Drag the Servo Motor block from the Actuators category to your model and rename it to SetServo. Open the block mask and set the Port parameter to A. This setting corresponds to the 3-wire port (on the V5 Robot Brain) to which the servo motor is connected.

5. Drag the Gain block from the Math Library category to your model. Set the Gain parameter to *127.

6. Connect the output port of the ServoButton block to the input port of the Gain block. Similarly, connect the output port of the Gain block to the input port of the SetServo block.

7. Drag the Latch block from the Utilities category to your model and insert it between the ServoButton and Gain blocks. The Latch block holds on to the previous state value until the button is pressed and released again (that is, a rising and a falling edge).

8. Save the Simulink model.

Task 3 - Build and Download the Simulink Model

Open the Simulink model created in Task 2, build the model, and download it to the VEX EDR V5 Robot Brain.

1. Connect the V5 Robot Brain to your computer using the USB cable.

2. Switch on the V5 Robot Brain.

3. Open the Simulink model created in Task 2. A pre-configured model is also available.

4. On the Simulink toolbar, click the Configuration Parameters icon.

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 the Solver pane and select the option Treat each discrete rate as a separate task. Click OK.

7. In your Simulink model, click the Build Model icon on the toolbar. The model is deployed to the V5 Robot Brain.

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

9. Observe the change in angle position of the servo motor as you control it using button X on the V5 Controller gamepad.

Other Things to Try Using the Button on V5 Controller Gamepad

  • Create and run a Simulink model that controls a servo motor using a gamepad button without using the Latch block in the model.

  • Create and run a Simulink model that rotates a DC motor at a specific speed using a gamepad button.