Articles

Building an Arduino Robot (for Testing Whiley)

The Whiley programming language is about developing more reliable software and, of course, embedded systems is one of the biggest areas that could benefit. Obviously, then, we need an “embedded system” to test Whiley with, right?  At least, that’s the thinking behind my recent endeavor to create an Arduino-based Robot.

After some discussion with our workshop folks, I managed to acquire the following bits:

Here, we have an Arduino Uno (top right), a Motor Shield (bottom right) and a Tamiya dual motor gearbox.  The Motor shield sits on top of the Arduino Uno, and provides necessary power for driving the two DC motors on the Tamiya gearbox.  I also acquired an Infra-Red sensor from our workshop, which can be used for determining distance to objects ahead:

This sensor plugs into the I/O pins of the Arduino and we can then read its current value from our Arduino program and make decisions based on this.  To complete the robot, we cut a base plate on to fix everything on to … and a robot was born:

The robot really needs a better front wheel, and probably some more interesting sensors!  But, for now, it’s enough to get the hang of programming the Arduino.  Here’s my first program for it:

/* ============================================= */
/* Motor Code */
/* ============================================= */
const int pwmA = 3;
const int pwmB = 11;
const int dirA = 12;
const int dirB = 13;
const int brakeA = 9;
const int brakeB = 8;

const int STOP = 0;
const int SLOW = 50;
const int MEDIUM = 75;
const int FAST = 100;

void setMotors(int left, int right) {
 analogWrite(pwmA, left);
 analogWrite(pwmB, right);
}

void setReverse() {
  digitalWrite(dirA,HIGH);
  digitalWrite(dirB,HIGH);
}

void setForward() {
  digitalWrite(dirA,LOW);
  digitalWrite(dirB,LOW);
}

void brakesOff() {
 digitalWrite(brakeA,LOW);
 digitalWrite(brakeB,LOW);
}

/* ============================================= */
/* Sensor Code */
/* ============================================= */
const int VERY_CLOSE = 0;
const int CLOSE = 1;
const int FAR = 2;
const int NOTHING = 3;

int frontSensor() {
 int x = analogRead(0);
 if(x > 400) {
  return VERY_CLOSE;
 } else if(x > 300) {
  return CLOSE;
 } else if(x > 200) {
  return FAR;
 } else {
  return NOTHING;
 }
}

/* ============================================= */
/* Setup Code */
/* ============================================= */

void setup() {
 pinMode(pwmA, OUTPUT);  //Set control pins to be outputs
 pinMode(pwmB, OUTPUT);
 pinMode(dirA, OUTPUT);
 pinMode(dirB, OUTPUT);

 analogWrite(pwmA, STOP);
 analogWrite(pwmB, STOP);

 setForward();
 brakesOff();

 delay(3000);
}

/* ============================================= */
/* Controller Code */
/* ============================================= */

void loop()
{
  int sensor = frontSensor();

  switch(sensor) {
    case VERY_CLOSE:
      setReverse();
      setMotors(FAST,FAST);
      break;
    case CLOSE:
      setForward();
      setMotors(STOP,STOP);
      break;
    case FAR:
      setForward();
      setMotors(MEDIUM,MEDIUM);
      break;
    case NOTHING:
      setForward();
      setMotors(FAST,FAST);
      break;
  }
  delay(100);
}

This program moves the robot forward at the FAST speed, until an object comes into view.  When the object is FAR away, the motors are reduced to MEDIUM speed; when the object is CLOSE, the motors are set to STOP;  finally, when the object is VERY_CLOSE, the motors are reversed.  Here’s a video of it working:




Building the robot wasn’t too hard, although I did have to dig out my soldering iron for the first time in quite a few years! And, having the workshop folks around was a great help, as they gave me useful advice and had access to equipment and tools (esp. for cutting the base plate).

For now, I’m still learning and will look to improve the robot’s capability to get a more interesting level of complexity.  Ultimately, I want to be able to compile Whiley code down to run on the Arduino.  We’re currently working on a C back-end for Whiley, and this will provide the launching pad for getting Whiley onto the Arduino (and, perhaps, other similar devices as well).

Comments Welcome!

8 comments to Building an Arduino Robot (for Testing Whiley)

  • James

    I love the way the cat comes past… wouldn’t be right for you without one turning up…

  • Yeah, Spikey is the star of the show!!

  • ali

    if i have to use an ultrasonic sensor instead of the sensor you have used what changes should be made to the arduino sketch?
    And it would be great if you provided a circuit diagram for the robot.plzz help out
    hoping for an early reply

  • krimo

    Hi,

    thank you very much for your explanations. Also, I have some questions:

    Is it possible to get a top view for visualizing the cabling in more detail?

    The color of the common tamiya kits does not seem to be the same that yours. Why? Is it the correct link?

    The motor shield that is illustrated is not the same that the motor shield associated to the URL. Why? Is it the correct link?

    It seems that you employ a single nine-volt battery for the entire system (arduino uno + shield + sensor + motor). Please, can you confirm this information.

    Is this code directly usable in an arduino IDE?

    What are the roles of the yellow and Brown cables. Where are they plugged?

    Thank you in advance for your assistance.

  • Hi Krimo,

    You’re right that the motor shield is not exactly the same one. The reason is that we have a custom motor shield developed at my University, which is very similar although not identical.

    Yes, I use a single 9v battery — which seems plenty enough.

    Yes, I think the code should work directly in the Arduino IDE — that’s where I developed it.

    I’m not sure about the colouring for the Tamya kit. That’s how it came! And, also for the cabling, there aren’t any yellow and brown cables — only, red, black and white I think. There is one sensor on the front which has three cables, one of which is plugged into an I/O pin on the Arduino.

    Cheers,

    Dave

  • Krimo

    Hi Dave,

    thank you very much for the answers.

    Also, I have some questions:

    Regarding the cables? (visually brown and yellow?) that are shown in the fourth image, we can observe two cables passing between the arduino board and the chassis as well as under the sensor. Please, what are their roles and where are they plugged? We cannot see the cable extremities.

    What is the distance measuring range of your Infra-Red sensor?

    Best regards,
    Krimo

  • Hey Krimo,

    Hmmm, I think the deal is that they’re not cables. It’s a piece of copper wire which I’m using in place of a front wheel! To summarise all the cables:

    1) 2 cables at read of robot from motor shield to each of the motors in the gearbox.

    2) 1 cable at front of robot from sensor to Arduino IO pin (can’t remember which number).

    And, that’s all. I actually don’t know what the specs are for the Infra-Red sensor. It’s just one that was lying around and seemed to work!

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>