MATLAB Examples

Line Follower with Arm and Claw Position Control using Line Tracker, Potentiometer and Encoder

This example shows you how to use Simulink Coder Support Package for ARM Cortex-based VEX Microcontroller to create a line follower robot with robotic arm and claw control using the VEX Clawbot. The robot, with an object manually placed between its claw, follows a black line on a white surface. When the end of black line is reached, which is indicated by a wider black line, the robot stops, lowers the robotic arm and drops the object held between its claws. Line trackers will be used for the purpose of following a black line, potentiometer for the control of robotic arm position and integrated encoder module for the control of the claw opening.

Contents

Introduction

Simulink Coder Support Package for ARM Cortex-based VEX Microcontroller enables you to create and run Simulink models on a VEX microcontroller.

In this example, you will learn how to use VEX Line trackers to create a line follower robot, VEX Potentiometer to control the position of the robotic arm and VEX Integrated Encoder Module to control the claw position. You will also learn about using the Analog Input block for interfacing analog sensors to the VEX microcontroller. In this application, the VEX Clawbot is placed on a black line track with an object placed between its claws. The robot moves along the black line path. When it encounters the end of line indicated by a wider black line, the robot stops, lowers the robotic arm and drops the object held between its claws. The figure below illustrates the application.

The algorithm used for the line following function of the robot is explained below:

The line trackers are mounted on the robot such that they face downwards towards the ground and the spacing between them is such that when the robot is placed on the black line, the center line tracker is above the black line and the other two line trackers are above the white surface. The VEX line tracker provides a value from 0 to 4095 based on the light intensity. The value increases with decrease in light intensity. As the line trackers are mounted facing downwards, the value returned by them depends on the light intensity reflected from the surface. When the robot is directly above the black line, the center line tracker reading will be higher than the left and right line tracker readings. In order to differentiate between the black and white surface using the line tracker readings, a cut off value/threshold that lies approximately between the black and white surface readings is chosen. If the line tracker reading is above the threshold, the sensor is above the black surface and hence, the comparison output is 1. The comparison output will be 0 for the line trackers above the white surface. The comparison outputs are multiplied by weights to give a unique value for each combination of the outputs as illustrated in the figure below.

The product value obtained from the weight multiplication signifies the current status of the robot with respect to the black line and is used to take appropriate action as shown in the below table.

Prerequisites

Required Hardware

To run this example you will need the following hardware:

Below is a figure of a clawbot with two DC motors connected to the two wheels, one DC motor connected to the robotic arm and one DC motor connected to the claw. Three line trackers are connected on the front of the clawbot facing towards the ground, a potentiometer inserted through the shaft of the robotic arm motor and an integrated encoder module installed on the claw motor.

Task 1 - Hardware Connections

1. The two DC motors drive the left and right set of wheels on the robot platform. The motors are installed on the robot with the motor shafts facing outwards to enable connecting the wheels to the shafts. Connect the left DC motor and right DC motor to motor pins 4 and 3 respectively on the VEX microcontroller. Use the Motor Controller 29 cables to establish the connection between the motor leads and the pins on the VEX Microcontroller. Connect them as described on Page 4 of the VEX Microcontroller and VEXnet Joystick User Guide.

2. Mount the three VEX Line Trackers on the front of the robot platform such that they face downwards as shown in the figure below. This enables the line trackers to sense white or black surface based on the infrared light reflected from the surface. Since the range for the VEX line tracker is approximately 0.02 to 0.25 inch from the ground, ensure that the line trackers are mounted within this range from the ground. The gap between the line trackers should be such that when the robot is placed on the center of a black line, the center line tracker is above the black line and left and right line trackers are above the white surface. Connect the line trackers beginning from left to right to analog ports 1, 2 and 3 respectively on the VEX microcontroller.

3. Connect the DC motor on the robotic arm to motor pin 5 on the VEX microcontroller. Insert the shaft of the VEX potentiometer through the motor shaft as shown in the figure below. Since the potentiometer has a limited rotation range of about 260 degrees, ensure that the potentiometer is not at its extreme position when inserting it through the motor shaft so that the robotic arm is not restricted from rotating due to the potentiometer's placement. Connect the potentiometer to analog port 4 on the VEX microcontroller.

4. Connect the DC motor on the claw to motor pin 7 on the VEX microcontroller. Use the Motor Controller 29 cable to establish the connection between the motor lead and the pin on the VEX Microcontroller. Connect them as described on Page 4 of the VEX Microcontroller and VEXnet Joystick User Guide. Install the Integrated Encoder Module on the motor as given here. Plug one end of a 4-wire cable to the DC motor and the other end to the I2C port on the VEX Microcontroller.

5. Connect the battery supply to the VEX Microcontroller.

Task 2 - Configure a Simulink model in External mode and calculate the threshold value between white and black surfaces

In this task, you will configure a Simulink model in External mode to read the light intensity reflected from a white surface and a black surface using the line trackers and calculate the threshold value between white and black surfaces.

1. Open the pre-configured model. In this model, the outputs of three Analog Input blocks are connected to the inputs of three Display blocks. The Analog Input blocks are named as Left Line Tracker, Center Line Tracker and Right Line Tracker respectively. These blocks are used to interface the line trackers, which are analog sensors to the VEX microcontroller. The values for the parameter Analog port (1 to 8) for these blocks are chosen according to the line tracker connections in Task 1.

2. See Signal Monitoring and Paramter Tuning with External Mode to prepare and run this model in External mode.

3. While the model is running in External mode, turn ON the battery supply. Place the robot on the white surface of the track and note the left, center and right line tracker readings from the corresponding Display blocks in the Simulink model. Let us call these line tracker readings on the white surface as $White\ surface{_l}{_e}{_f}{_t}$, $White\ surface{_c}{_e}{_n}{_t}{_e}{_r}$ and $White\ surface{_r}{_i}{_g}{_h}{_t}$. Similarly, note the line tracker readings by placing the robot such that all the three sensors are directly below the black line. Let us call these line tracker readings on the black line as $Black\ line{_l}{_e}{_f}{_t}$, $Black\ line{_c}{_e}{_n}{_t}{_e}{_r}$ and $Black\ line{_r}{_i}{_g}{_h}{_t}$. Keep in mind that the line tracker readings are affected by the lighting conditions, position of the light source and shadows in the robot's environment. Therefore, it is good to take the readings in an environment which has lighting conditions similar to the place where you would like to drive the line follower robot.

4. Calculate the average $White\ surface{_a}{_v}{_g}$ of the left line tracker $White\ surface{_l}{_e}{_f}{_t}$, center line tracker $White\ surface{_c}{_e}{_n}{_t}{_e}{_r}$ and right line tracker $White\ surface{_r}{_i}{_g}{_h}{_t}$ readings using the formula below:

$$White\ surface{_a}{_v}{_g} = \frac{White\ surface{_l}{_e}{_f}{_t} + White\ surface{_c}{_e}{_n}{_t}{_e}{_r} + White\ surface{_r}{_i}{_g}{_h}{_t}}{3}$$

Similarly, calculate the average $Black\ line{_a}{_v}{_g}$ of the left line tracker $Black\ line{_l}{_e}{_f}{_t}$, center line tracker $Black\ line{_c}{_e}{_n}{_t}{_e}{_r}$ and right line tracker $Black\ line{_r}{_i}{_g}{_h}{_t}$ readings using the formula below:

$$Black\ line{_a}{_v}{_g} = \frac{Black\ line{_l}{_e}{_f}{_t} + Black\ line{_c}{_e}{_n}{_t}{_e}{_r} + Black\ line{_r}{_i}{_g}{_h}{_t}}{3}$$

5. Calculate the threshold value between the black and white surface by taking the average of white and black surface averages as shown below:

$$Threshold = \frac{White\ surface{_a}{_v}{_g} + Black\ line{_a}{_v}{_g}}{2}$$

The calculated threshold value gives a good indication of value range for black and white surfaces for a particular environment. Note that it may be required to vary the threshold slightly from the calculated value based on trials and observations on the line follower robot.

6. Press the Stop button in the Simulink Editor to terminate the External mode simulation.

In this task, you learnt how to read the VEX Line tracker value using Analog Input block and calculate the threshold to detect white and black surfaces.

Task 3 - Configure a Simulink model in External mode and find the potentiometer values at desired robotic arm positions

In this task, you will learn how to find the potentiometer values at different positions of the robotic arm. These values can be used to control the arm position.

1. Open the pre-configured model. In this model, the output of an Analog Input block is connected to the input of a Display block. This block is used to interface the potentiometer, which is an analog sensor to the VEX microcontroller. The value for the parameter Analog port (1 to 8) for the Analog Input block is chosen as per the potentiometer connection in Task 1.

2. See Signal Monitoring and Parameter Tuning with External Mode to prepare and run this model in External mode.

3. While the model is running in External mode, turn ON the battery supply. Rotate the robotic arm manually so that it is almost upright or a desired position close to being upright. Note the potentiometer reading in this position from the Display block in the Simulink model. Now, rotate the robotic arm manually so that it is almost horizontal to the ground. Note the potentiometer reading in this position from the Display block in the Simulink model. Make sure that you do not rotate the robotic arm beyond the limits of the potentiometer rotation range as it may damage the potentiometer.

4. Press the Stop button in the Simulink Editor to terminate the External mode simulation.

In this task, you learnt how to read the VEX Potentiometer using Analog Input block and find the values at desired robotic arm positions.

Task 4 - Configure a model in External mode and find the tick count to open the claw

In this task, you will learn how to use the Integrated Encoder Module to drive the claw motor until the claw opens to a desired position to release the object.

1. Open the pre-configured model. In this model, the output of an Integrated Encoder Module block is connected to the input of a Display block. The value for parameter Sequence order from I2C port is '0' as the encoder is direclty connected to the I2C port on the VEX microcontroller.

2. Place an object between the claws of the robot. We will measure the number of ticks required to open the claw to release the object from this position where an object is held between the claws.

3. See Signal Monitoring and Parameter Tuning with External Mode to prepare and run this model in External mode.

4. While the model is running in External mode, you will notice that the output of Integrated Encoder Module is 0 as observed from the Display block. Open the claw motor manually to a required position to release the object. Note the tick count in Display block. This is the number of ticks required to drive the claw motor to release the object from the initial position in which the object is held between the claws.

5. Press the Stop button in the Simulink Editor to terminate the External mode simulation.

In this task, you learnt how to use the encoder output to drive a motor to a desired position from a known initial position.

Task 5 - Build and Download the Simulink model for Line Follower Robot with Arm and Claw Position Control

In this task, you will learn how to use the line trackers to model the logic to control the motion of a 4-wheeled robot to follow the black line on a white surface and stop when it reaches the end of the black line. You will also learn how to use the potentiometer to control the movement of the robotic arm and integrated encoder module to control the claw movement. You will then build and download the model to the VEX microcontroller.

1. Open the pre-configured model.

2. Note the following points in the Simulink model:

  • The parameter values Analog port (1 to 8) for the Analog Input blocks are chosen as per the Line Tracker and Potentiometer connections in Task 1. The parameter Motor Channel for the Left Motor block is chosen as '4', for the Right Motor block as '3', for the Arm Motor as '5' and Claw Motor as '7' as per the hardware connections in Task 1. The parameter values Motor type and Sequence order from I2C port for the Integrated Encoder Module are chosen based on the motor type and hardware connection in Task 1.
  • The light intensity is read by the three line trackers when the robot is placed on the line follower track. The light intensity read is compared against the threshold value specified using a Constant block. This Constant block is named as Threshold. Change the value of parameter Constant value for Threshold to that obtained in Task 2. If a line tracker reading is greater than the threshold, it means that the tracker is on the black line and the output of the comparison is 1. Similarly, if the line tracker reading is less than the threshold, it means that the tracker is on the white surface and the comparison output will be 0. Therefore, after the comparison, either 0 or 1 will be the output corresponding to each line tracker reading.
  • Each comparison output is multiplied by a weight specified by Gain block and the three products are added together to provide a number between 0 to 7. Each output value signifies a status of the robot on the track and appropriate action required as shown in the table in the Introduction section.
  • As the left and right motors are attached to the robot platform with their shafts facing outwards, providing speed input of opposite signs to the motors makes them rotate in same direction, causing the robot to go straight. To make the robot turn, the two motors are provided with speed input of same sign so that the motors rotate in opposite direction. The left and right motors are provided a speed input of '-20' and '20' respectively for straight motion. The motors are provided a speed of '30' to turn the robot left and a speed of '-30' to turn the robot right.
  • The logic for motion control based on the line tracker readings is implemented in the Stateflow chart as shown below. See Getting Started with Stateflow for an introduction to implementing states in Stateflow.

  • The logic for controlling the arm motor and claw motor is implemented in an Enabled Subsystem. Enabled Subsystem is a subsystem that executes at time steps for which the enable input has a positive value. When end of the black line is reached, the output StartRoboticArm of the Stateflow Chart changes to 1 enabling the subsystem to control the arm motor. Refer Create an Enabled Subsystem for more information on enabled subsystems.
  • The subsystem Enabled Subsystem for Arm Control shown below includes the implementation for driving the arm motor depending on the potentiometer reading. The potentiometer value is compared against the values in Constant blocks named as Potentiometer value when arm is vertical and Potentiometer value when arm is horizontal. Change the values in these Constant blocks to the potentiometer readings obtained in Task 3 for arm vertical and horizontal positions. The comparison outputs are ANDed using Logical Operator block and multiplied by a speed value of '25' from Constant block and provided as the speed input to the Arm motor. Therefore, when this subsystem is enabled, the arm motor is provided with a non-zero speed value as long as the potentiometer reading is between the limits of arm's vertical and horizontal positions.
  • When the potentiometer reading exceeds the value for the arm to reach a horizontal position, the subsystem Enabled Subsystem for Claw Control is enabled. This subsystem shown below implements the logic for driving the claw motor until the tick count is reached, similar to the logic in the subsystem to control arm motor. Change the value in the Constant block named as Encoder tick count when claw releases the object to that obtained in Task 4.

3. In the Simulink model, click on the Configuration Parameters button.

4. When the Configuration Parameters page opens up, navigate to the Hardware Implementation pane.

  • Set the Hardware board to ARM Cortex-based VEX Microcontroller.

5. In the Configuration Parameters page, navigate to Solver pane and set the Solver to discrete (no continuous states).

6. Click OK.

7. Before building the model, make sure to connect the VEX Microcontroller to your computer with a USB A-Male to A-Male cable.

8. In the Simulink model, click the Build Model button on the toolbar. The model will now be deployed to the VEX microcontroller.

9. Disconnect the USB cable from the PC and connect the VEXnet key to the VEX microcontroller. Place the robot at the beginning of the black line, with an object held between its claws. Turn ON the VEX microcontroller and the gamepad. Once the VEXnet keys are paired, the robot begins to move along the black line. When the end of line is reached, the robot stops and motors on the arm and claw are driven to lower the robotic arm and release the object.

Summary

This example showed you how to use Simulink Coder Support Package for ARM Cortex-based VEX Microcontroller to interface analog sensors using Analog Input blocks and create a a line follower with motor position control using line trackers, potentiometer and integrated encoder module.