Using MATLAB to Control the MePed V2 Walking Robot

by tsmit232 in Circuits > Arduino

1394 Views, 5 Favorites, 0 Comments

Using MATLAB to Control the MePed V2 Walking Robot

IMG_9145.jpg

The mePed v2 is a quadruped robot, which comes in a base kit and a complete kit. We will be using the complete kit in this tutorial, which includes an Arduino Nano controller.

Set Up Arudino Compatibility With MATLAB

Make sure you have the Arduino IDE downloaded, as well as the CH340 USB to Serial Drivers found here under "Source Files." Download the appropriate driver for your device.

Now you need to download and install the MATLAB Support Package for Arduino Hardware.

Connecting Your Robot

IMG_4255.JPG

Open MATLAB and type "arduinosetup" into the command line. If you have the Arduino support library setup properly, a user interface should pop up prompting you to "Choose Connection Type." In this tutorial, we'll be using USB, but feel free to explore the "Wifi" and "Bluetooth" options in the future!

Under "Choose Board" click "Nano3", and choose the subsequent port that is listed under "Choose Port." If you only have the robot hooked up, it's likely that only one port will be listed. By default, the libraries that will be ticked will be "I2C," "SPI," and "Servo."

Click "Program." It may take a second to upload server code to the robot. Once you see the green success message, click Next.

Declare Robot Objects in MATLAB

On a MATLAB file, you should now be able to declare an Arduino object. This is how you are able to access the pins and functions associated with the Arduino.

To declare the object:

a = arduino(port,'Nano3')

The port is the same port shown on the "Test Arduino Connection" page.

Now you'll want to declare the servos.

s2 = servo(a, 'D2')

s3 = servo(a, 'D3')

s4 = servo(a, 'D4')

s5 = servo(a, 'D5')

s6 = servo(a, 'D6')

s7 = servo(a, 'D7')

s8 = servo(a, 'D8')

s9 = servo(a, 'D9')

Move Your Robot

bow.gif

To move the servos on the robot, you need to utilize the "writePosition" function.

writePosition(s,position)

Where s is the servo object, and position is a value from 0 to 1.

writePosition(s2, 1) % moves s2 servo to position 1

You can find out what position a servo is in by using

readPosition(s)

The code to make the robot walk forward can be found here.

(Note that this code may not work if your servos are wired differently).

Using the Ultrasonic Sensors

Screen Shot 2019-04-04 at 8.17.47 PM.png

The mePed v2 comes with HC-SR04 ultrasonic sensors, which allows your robot to read distance.

First, you need to download and install the MATLAB add-on library for the sensors. Make sure to follow the full instructions on the page, and place the zip file in the correct directory.

Once installed, you will need to type "arduinosetup" in the command line again. This time, when you get the choice of including libraries, you need to tick "JRodrigoTech/HCSR04." Program the code onto the Arduino.

Declare an arduino object. Now you should be able to declare a sensor object.

a = arduino('/dev/cu.wchusbserial1440','Nano3')

sensor = addon(a, 'JRodrigoTech/HCSR04', 'A3', 'A2')

readDistance(sensor)

You can now detect how much distance is between your robot and the object it is looking at!

Creating a GUI

Screen Shot 2019-04-05 at 9.29.10 AM.png

If you want to create an easy way to control your robot, you can create a GUI using MATLAB. To get started, type "guide" in the command line.

A user interface should pop up, and prompt you to create a new GUI. You can use one of the templates, but the blank GUI may serve the robot the best.

You can resize your GUI by clicking the bottom right corner of the gridded area and dragging. To add a button, click push button from the lefthand menu, and create a button of your preferred size in the gridded area. Double click on the button you created to edit the button's settings. The "string" field changes the text you see on the button, and the "tag" field changes what the function will be referenced by in the m file.

Click the save button, and an m file will be created for your GUI.

Coding Your GUI

Screen Shot 2019-04-05 at 9.00.57 AM.png
Screen Shot 2019-04-05 at 9.05.35 AM.png

In order for the same arduino object to be accessed by all the functions in the GUI, you must create a handle for it. This is best done in the function "guiname_OpeningFcn" which executes just before the GUI is made visible.

The button that you just created will have its own function called "tag_Callback." This function will execute whenever you press the button. I created a button to make the robot walk forward, and simply pasted the walking code into this function.

Note that when you use writePosition(s,position), if you are writing to servo s2, you'll need to put handles.s2.

Make sure to put "guidata(hObject, handles)" at the end of the function to update the structure.

Using Your MePed to Control an LED

IMG_3002.JPG
IMG_3905.JPG

You can control an LED attached to the robot.

You'll need:

A breadboard (preferably a mini one)

Wires

An LED

1 10k ohm resistor

Wire the circuit as shown above. The LED has two wires coming out of it - the longer wire is the positive side. The positive side should wire to one of the A pins (we used A0). The other side will be wired to GND.

Declare an arduino object. To turn the LED on and off:

writeDigitalPin(a,pin,value)

Where a is the arduino object, pin will be the A pin you wired the LED to ('A0') and value is 0 for off, and 1 for on.