Tank Robot Control Using V5 Smart Motor and V5 Controller Joystick
This example shows you how to use the Simulink® Coder™ Support Package for VEX® EDR V5 Robot Brain to control a two-wheeled tank robot using two V5 Smart Motors and the joystick on the V5 Controller gamepad.
This example creates a Simulink model that uses inputs from two analog joysticks on the gamepad (V5 Controller) to drive a two-wheeled tank robot. The two wheels of the robot are powered by two V5 Smart Motors, which are connected to the VEX EDR V5 Robot Brain. The Simulink model maps two analog joysticks on the gamepad to control the speed of the two motors. When both the joysticks are pushed forward, the tank robot moves forward; when both the joysticks are pushed backward, the robot moves backward. When the two joysticks are pushed in opposite directions, the robot turns right or left accordingly.
If you are new to Simulink, watch the Simulink Quick Start video.
Complete the Getting Started with VEX EDR V5 Support Package example.
To run this example, you need the following hardware:
VEX EDR V5 Robot Brain
V5 Controller Gamepad
V5 Smart Motors (2)
Two-wheel robot platform
V5 Robot Battery
V5 Robot Radio
600mm Smart Cable (2)
300mm Smart Cable
USB A to Micro Cable
The image below shows a simple two-wheel robot platform with two V5 Smart Motors connected to its two wheels. The two motors are controlled by a VEX EDR V5 Robot Brain.
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 two V5 Smart Motors to two Smart Ports on the V5 Robot Brain, using the two 600mm Smart Cables. Connect the right motor to the Smart Port 11 and the left motor to Smart Port 20.
4. 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.)
5. Connect the two V5 Smart Motors to the two wheels of the robot platform.
Task 2 - Map the Joysticks to the V5 Smart Motors for Tank Control
Create a Simulink model that configures the two analog joysticks on the V5 Controller gamepad to control the two V5 Smart Motors of the robot to obtain tank control. Tank control is achieved when the robot moves forward or backward when the two joysticks are pushed together forward or backward respectively, and the wheels make turns when the joysticks are pushed in opposite directions.
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 Joystick block twice from the Gamepad category into your Simulink model. Rename the two blocks to
4. Open the block mask and set the Analog Channel parameter of
RightAnalogStick to 2 and the
LeftAnalogStick to 3. These settings correspond to the vertical axis on the right and left analog joysticks.
5. Drag the Smart Motor Write block twice from the Actuators category to your model and rename them to
LeftMotor. Set the Port parameter of
RightMotor to 11, and the Port parameter of
LeftMotor to 20. These settings match the Smart Ports to which the two Smart Motors are connected on the V5 Robot Brain.
6. Connect the output port of the
RightAnalogStick to the input port of the
RightMotor block. Similarly, connect the output port of the
LeftAnalogStick to the input port of the
7. Drag and drop the Gain block from the Math Library category into your Simulink model, and insert it between the
LeftMotor blocks. Change the Gain parameter of the block to -1. This block inverts the signal going to the left motor to compensate for the alignment of the motors on the robot. This compensation ensures that the robot moves forward or backwards even though the motors are spinning in the opposite direction when the joysticks are pushed together.
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.
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 the 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.
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. Drive the tank robot by moving the two analog joysticks along the vertical axes 2 and 3.
A pre-configured model(vexv5_tankrobot_setup) is included for your convenience.
Other Things to Try Using the Two-Wheel Robot Configuration
Use vexv5_tankrobot_withGear.slx model to control the speed of the V5 Smart Motor using the Gear Transmission block which is built using Stateflow®.
Use vexv5_tankrobot_halfSpeed_MLfcn.slx model to switch between half and full speed by the press of a button on the gamepad (V5 Controller). The logic to switch between the two states is built using a MATLAB Function block.
Use vexv5_arcaderobot.slx model to configure the two-wheel robot in arcade or skidsteer mode. In this mode, you can drive the robot using just one analog joystick, instead of two. Move the joystick along the vertical axis to drive the robot forward and backward, and move it along the horizontal axis to make turns.