R2D2 Project(On Going)

Though I am a math major, I often times find myself working on fun computer science, engineering, and robotic projects in my spare time. The most recent of which has been turning an 18″ R2D2 toy I bought from WalMart into a functioning robot.


To begin the project I tore the toy R2D2 apart to see what I had to work with. It became clear right away, that the manufacturer had at some point planned on making the toy robotic as there were many places to mount speakers, motors, and LEDS. 

After taking the R2 apart, I began laying out plans for how I could mount and wire the motors. I then began outlining the general structure of the code needed to drive the R2.

A few weeks passed before I got a chance to return to the R2. It now has two motors in the feet to make it move, and is controlled by an Arduino Uno. I will try to post the code below.

The following link is a video of the first test run to make sure both the motors were wired correctly and the code worked.
Motor Test- Video


Here is a copy of the test code I wrote:

int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface 
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface 
int pinI4=13;//define I4 interface 
int speedpinB=10;//enable motor B
int spead =10000;//defines the speed of motor
void setup()
{
 pinMode(pinI1,OUTPUT);
 pinMode(pinI2,OUTPUT);
 pinMode(speedpinA,OUTPUT);
 pinMode(pinI3,OUTPUT);
 pinMode(pinI4,OUTPUT);
 pinMode(speedpinB,OUTPUT);
}
void forward()//
{
 analogWrite(speedpinA,spead);//input a simulation value to set the speed
 analogWrite(speedpinB,spead);
 digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
 digitalWrite(pinI3,LOW);
 digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
 digitalWrite(pinI1,HIGH);
}
void backward()//
{
 analogWrite(speedpinA,spead);//input a simulation value to set the speed
 analogWrite(speedpinB,spead);
 digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
 digitalWrite(pinI3,HIGH);
 digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
 digitalWrite(pinI1,LOW);
}
void left()//
{
 analogWrite(speedpinA,spead);//input a simulation value to set the speed
 analogWrite(speedpinB,spead);
 digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
 digitalWrite(pinI3,LOW);
 digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
 digitalWrite(pinI1,LOW);
}
void right()//
{
 analogWrite(speedpinA,spead);//input a simulation value to set the speed
 analogWrite(speedpinB,spead);
 digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
 digitalWrite(pinI3,HIGH);
 digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
 digitalWrite(pinI1,HIGH);
}
void stop()//
{
 digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor. 
 digitalWrite(speedpinB,LOW);
 delay(1000);
}
/*
*As I plan on recycling this code as a continue building/upgrading my R2D2
*I added extra code, variations of the "forward()" method, that can be 
*implemented at a later date.
*/
void loop()
{
 forward();
}

It is proving much more difficult than I had originally anticipated reading data from an SD card, and have an Arduino Mega play the data back as sound. I am struggling with the libraries.

I should note on the subject of having the bot playback noise, I have two choices:
-Read a .wav file from an SD card
-Translate a .wav to a series of integers, store those values into an array and play them back as frequencies at set time intervals.

I prefer the SD card method as it requires the use of less onboard memory. It is my suspicion that the problem I am having is related to how the SD card board is powered.

 


20Feb17

Wow! it’s been a long time since I’ve had a chance to work on R2… I promise R2, you’re the droid I’ve been looking for. I’ve just been busy. Still planning to finish you!

 


21Jun17

Well, it’s finally summer again and I’m finally able to work on some of these projects again. As such I have gotten back to it with R2.

I have discovered that my suspicions were correct, there was a problem with the SD card reader. Apparently, there are 2 kinds of SD card readers available for use with Arduino microcontrollers. One allows you to only read and write .txt files and the other allows for use with sound, image, and video files.

As a warning to anyone interested in a project like this, the tell-tell difference in appearance seems to be the existence of a 5v pin on the SD card reader allowing for use with sound, images, and videos.

I should also note, I have abandoned using the Arduino Uno, and am now using the Mega 2560. I did this for two reasons:

  1. More memory for coding
  2. More pins available allowing for a wider range of functionality

With that said, here is a copy of the code I threw together using the TMRpcm library(TMRpcm is available on GitHub at- https://github.com/TMRh20/TMRpcm):

/* Description:
This program allows the use to select a sound from a list
of sounds labeled 2-62.

Author:
Matt Tucker

Date:
18Jun17
*/

#include <SD.h>
#define SDPIN 53
#include <TMRpcm.h>

TMRpcm tmrpcm;
String label;
int i;

void setup()
{
 Serial.begin(9600);
 tmrpcm.speakerPin = 46; //Can be: 5,6,11 or 46
 tmrpcm.setVolume(5);
 if (!SD.begin(SDPIN))
 {
  Serial.println("Initialization Failed!");
  return;
 }
 tmrpcm.play("60.wav");
 Serial.println("Select the name of a .wav file to play(2-63): ");
}

void loop()
{
 if (Serial.available())
 {
  i = Serial.parseInt();
  //File names begin at 2 and end at 63
  if (i < 2 || i > 63)
   Serial.println("Not a valid sound");
  else
  {
   label = String(i) + ".wav";
   Serial.println("playing: " + label);
   tmrpcm.play(label.c_str());
  }
 }
}

You may notice I named the files 2-63, this was actually an accident that resulted from Python code I wrote to rename each of the files. Apparently, there was a data file in the folder that was renamed first. As a result, all of the sound files were named starting at 2, and given the are 62 target files in the folder that meant the final file was named 63. Here is a copy of the code that I threw together to rename the files:

# Author: Matt Tucker
# Date: 15June17
# Description: This program runs through a folder and renames every file numerically. For example, the first file is
#              labeled 1, the second is labeled 2, and so on.
# Note: As an observation I had to delete the file labeled 1 when done, it was .DS_Store -> 1

import os
# Path to folder which files exist in
path = input("Input the directory path here: ")
print(path)
# Starts naming files at 1
i = 1
for filename in os.listdir(path):
    print(filename)
    # Rest of Code
    filename_without_ext = os.path.splitext(filename)[0]
    extension = os.path.splitext(filename)[1]
    # Name file current value of i
    new_file_name = str(i)
    new_file_name_with_ext = new_file_name + extension
    print(new_file_name_with_ext)
    os.rename(os.path.join(path, filename), os.path.join(path, new_file_name_with_ext))
    # increase value by 1 so the next value is the next integer value
    i += 1

I’ve also gone ahead and dremeled out a hole for a servo motor to be mounted into that will turn the head.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s