Room navigation/Object avoidance program

The latest code to talk about is an intelligent room navigation/object avoidance program.

The lovely people at arcbotics (the designers and makers of Sparki found here: http://arcbotics.com/products/sparki/) have written and included some example programs in the SparkiDuino library.

This is really helpful and means that Sparki comes with a basic wall avoidance example program in which Sparki will move backward and move right 30 degrees if he encounters an object. The example code looks like this:

#include <Sparki.h> // include the sparki library
 
void setup() {
  sparki.servo(SERVO_CENTER); // Center the Servo
}

void loop()
{
    sparki.RGB(RGB_GREEN); // turn the light green
    sparki.moveForward(); // move Sparki forward
    int cm = sparki.ping(); // measures the distance with Sparki's eyes
        
    if(cm != -1) // make sure its not too close or too far
    { 
        if(cm < 20) // if the distance measured is less than 10 centimeters
        {
            sparki.RGB(RGB_RED); // turn the light red
            sparki.beep(); // beep!
            sparki.moveBackward(10); // back up 10 centimeters
            sparki.moveRight(30); // rotate right 30 degrees
        }
    }
    delay(100); // wait 0.1 seconds (100 milliseconds)
}

This is a good place to start, but this is the code that I have written to allow Sparki to make a decision based on whether to turn left or right, deciding on which direction has the largest free space.

My room navigation code looks like this in arduino:

#include <Sparki.h>  // include the sparki library
void setup()
{}
void loop()
{
 
                  while(sparki.ping()>10)
		  {
			sparki.moveForward();
			sparki.servo(0);
                    }
                        sparki.moveStop();
                          sparki.servo(90);
                          delay(1000);
                          int R=sparki.ping();
                          sparki.ping();
                          delay(2000);
                          sparki.clearLCD();
                          sparki.println(R);
                          sparki.updateLCD();
                          delay(1000);
                          sparki.servo(-90);
                          delay(1000);
                          int L = sparki.ping();
                          sparki.ping();
                          delay(2000);
                          sparki.clearLCD();
                          sparki.println(L);
                          sparki.updateLCD();
                          delay(1000);
                          sparki.servo(0);
                          if((R>10) && (R>=L))
                          {
                          sparki.moveRight(90);
                          sparki.servo(0);
                          }
                          if ((L>10) && (L>=R))
                          {
                            sparki.moveLeft(90);
                            sparki.servo(0);
                          }
                         if ((R<10) && (L >10))
                         {
                           sparki.moveLeft(90);
                           sparki.servo(0);
                         }
                         if ((R<=10) && (L <= 10))
                         {
                           sparki.moveRight(180);
                           sparki.RGB(0,0,220);
                           sparki.servo(0);
                         }
}

In my program Sparki looks left and right at 90 degrees, records the distance to the next object and moves in the direction of the largest clear space. This works really well for navigating my home, although it doesn’t account for the very corners of things and sometimes Sparki will still get stuck.

Here is a video of Sparki using the program, but as my floorspace is quite large, I thought it would take ages to film him navigating my living room and instead just gave a small demonstration of what the programming can do.

https://drive.google.com/file/d/0B6y9ITteBcqLZGZINGJEN0Z4ZDg/view?usp=sharing

Just a hint for the future, I have used a much more complicated variation of this program to try and get a much more intelligent room navigation system going on, which involves home zones, headings and way-points. But I’m trying to upload my programs in chronological order so that it will make sense to me in the future, so that program will be uploaded in a few weeks time.

———————EDIT——————–

Thanks to Kevin Cudby at  http://techogeny.com/ (see comments section)

The code above has been edited as my last conditional initially read:

 if ((R<10) && (L < 10))
                         {
                           sparki.moveRight(180);
                           sparki.RGB(0,0,220);
                           sparki.servo(0);
                         }
Advertisements

5 thoughts on “Room navigation/Object avoidance program

    • wow I never thought of that 🙂 do you know it has never happened yet….I suppose my room has too much free space…hmm any other possibilities I may have missed?? thank you by the way 🙂

      Like

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