Search This Blog

Friday, June 3, 2016

Ultrasonic sensor: A more sophisticated scanning and decision making - The AuRGent Project

I will start by saying "its been too long", to say its due to research projects at the university. However, I made some decent improvements in my AuRGent robot project, previously you just saw a basic navigation with hard coded movement. Recently I took the liberty of purchasing an ultrasonic sensor servo mount and hooked it up to my Arduino board, thus allowing the Robot to make some sort of decision based on its precepts from the sensor. Keep on reading, more details below.

The Sensor, mount and Servo

From the figure below, you see exactly how i mounted the sensor to the mount and thus to the servo, this allows me to program the ultrasonic range sensor to scan 165 °, taking readings at 0 ° (the right distance), at 90 ° (the center distance) and at 165 ° (the left distance). Why 165 you ask? Well i left some room to accommodate the error in servo (keep in mind you have to experiment to find your value).
What happens here is, as the robot navigate, the sensor is at 90 °, now whenever there is an obstacle in >=40cm, the bot stops, scans around taking a reading of both left and right and then makes a decision to take the path (left or right) with the highest distance to another obstacle. Okay okay am just going to give you the code.
    cm = get_distance();
    motor_speed = get_motor_speed(cm);
    type = bot_move(motor_speed, cm); 
    if( type == 1){
        servo2.write(0); 
        delay(500);
        right = get_distance(); //scan to the right
        delay(500);
        servo2.write(165);
        delay(600);
        left = get_distance(); //scan to the left
        delay(500);
        servo2.write(90); //return to center
        delay(100);
        compareDistance();
    }
What you see here is the loop, we get the distance from the sensor with the
get_distance()
function.
    int get_distance(){
        // establish variables for duration of the ping,
        // and the distance result in inches and centimeters:
        long duration, cm;
        digitalWrite(trig, LOW);
        delayMicroseconds(5);
        digitalWrite(trig, HIGH);
        delayMicroseconds(15);
        digitalWrite(trig, LOW);
        duration = pulseIn(echo, HIGH);
        cm = microseconds_to_centimeters(duration); //Converting distance to cm
        return cm;
    }
Based on that (left and right), the distance is read by the function
compareDistance()
which makes the decision on which path to take next.
    void compareDistance(){
  
        if (left>right) //if left is less obstructed 
        {
            turn_left();
            delay(500); 
        }
        else if (right>left) //if right is less obstructed
        {
            turn_right();
            delay(500);
        }
        else //if they are equally obstructed
        {
            turn_right();
            delay(1000);
        }
    }
That easy right?

Well you have seen most of the rest of the code in my previous posts, however i have restructured some movements, like for instance the function calls to left, right, back and forward in specialized function calls. Am just gonna give it to you and you figure out the rest. But just for argument sake, here are some specialised function calls you can find in the code:

  • get_motor_speed()
    function just takes in a distance as argument and uses
    map()
    to find a suitable speed for the bot given the distance, setting it to the global motor_speed variable.
  • set_speed()
    function takes in the motorspeed as argument and sets it to each of the motors (front left and right, back left and right).
  • bot_move()
    function on the other hand takes both the distance and speed based on the previous functions, decides how should the bot move given this variables, it also sets the speed of the motors.
Now you ask doesn't bot_move and compareDistance() do the same thing? No not the same, you see the first decides on whether to keep going forward, go backwards, go left or go right even at the initial state where there is no current reading yet. However, the prior only apply's only when we encounter and obstacle while going in a forward direction, then it decides to turn left or right.

Current AuRGent Structure

I think i can say we covered the most important aspects of this issue, if you need the full code kindly go to my repo GitHub and the video for this feature will be made available momentarily on my channel YouTube

Coming soon: Upgrades

  • FSM (Finite State Machine): So honestly i hate my implementation with the bunch of if-then-else, on my defense, this solution was a preliminary hack solution. However in the next issue on this project, i will introduce FSM and a refactored version of my code which will also be in the repo.
  • Reinforcement Learning: I am still designing my policy matrix to enable my bot to learn from experience and come up with its own optimal policy instead of my approximate policy in the current implementation.
  • Task and path finding: I will also implement using a GPS and Compass module a feature to enable navigation from a given state to another with (as much as i can) optimal solution.
Thanks and if you love my posts and videos, don't forget to share, comment on improvements i can make to either the post, videos or projects, learning never ends.

Monday, December 21, 2015

Hooking up SainSmart Motor Shield, Ultrasonic Range Finder with Arduino to Control my Chassis.

Its been long since the last post on the series, a lot has changed since then.

Motors to Motor Shield


I had problems with the right motor shield to use, however i finally settled with the sain smart motor shield, which has its library called AFMotor with a guide at the link on how to set it up.
Once you install in your arduino IDE, all is left is to connect it to your arduino like this:

and then first import the library with some few definitions.

#include 
#define MAX_SPEED 120
#define MIN_SPEED 95

You speed can vary, so make sure you experiment with various speed.
In my case i have 4 motors i want to control, so i hooked up the front motors to M1 and M2 respectively and the back to M3 and M4, then declare the motor instances like this:

 AF_DCMotor motorlf(1);
 AF_DCMotor motorrf(2);
 AF_DCMotor motorlb(3);
 AF_DCMotor motorrb(4);


after this, you will need to put this code in your setup. Setting the speed

   motorlf.setSpeed(MIN_SPEED);
   motorrf.setSpeed(MIN_SPEED);
   motorlb.setSpeed(SLOW_SPEED);
   motorrb.setSpeed(SLOW_SPEED);


then for each motor you can use for example
 motorlf.run(FORWARD);
 motorrf.run(BACKWARD);

The rest is basic programming. Also take not that if you want to turn right, you can set the left wheel to run forward and the right wheel to run backward, the converse for turning left.

Ultrasonic Range Finder


For the sensor, hook up the VCC to arduino board 5v, Gnd to Gnd, Trig to pin 12, echo to pin 13 like in the image above, you can see the 2 wires running into the board at both ends.
Then also define this in your code
 #define trig  12 
 #define echo 13 

and in setup add
   pinMode (trig,OUTPUT);
   pinMode (echo,INPUT);
  // initialize serial communication:
   Serial.begin(9600);  

then in your loop
   digitalWrite(trig, LOW);
   delayMicroseconds(2);
   digitalWrite(trig, HIGH);
   delayMicroseconds(10);
   digitalWrite(trig, LOW);
   duration = pulseIn(echo, HIGH);
   cm = (duration / 2) / 29.1;
    if(cm > 25){

     move_forward();

   }else{
    
     turn_right();
     delay(300);
    
   }

So what this basically does is it sets a low pulse for about 2 microseconds to ensure a clean high pulse for a longer period, set back to low and reads the echo using pulseIn. Thats the duration. You can see the calculation to CM in the code above.
Finally our robot looks like this

Upload your code and start your bot. Anytime it encounters an obstacle less than 25 cm, it turns right, from this you can make even more sophisticated computations.
Check out my Youtube video for detailed explanation. Earn free bitcoin

Monday, August 3, 2015

Multi Chassis 4WD Assembly

So its been quite sometime now, since i last blogged. Well recently i took upon myself an interesting hardware hack project which will last for sometime i think, but i promise to blog every step of it. Today was the first step towards this and i managed to assemble the multi chassis kit together. First of, the kit comes together with a short manual of the assembly, however i find it not really comprehensible, but it does get the job done.
This are all the parts, yea, you heard me right really all the parts, so basically the DC Motors go in first, you screw them with an M'2.5*20 (the shorter round head) screw at the top, and the longer M'2.5*25 screw at the bottom holding them with M2.5 nylon screw. So it basically looks like this with all the motors in.
Next we attach the battery case to the chassis top frame and screw it with the M'3*8 flat-head screw. Then you will need to attach four distance holders at the area specified with M3.8 screw so we can attach the chassis top frame to the bottom. Now in total you will need 8 M3.8 screws, 4 to hold the four distance holders to the bottom chassis frame, and four to attach the top chassis frame to the holders. After which to make sure your wires are exposed so as to easily attach them to whatever micro-controller you are going to be using. In my case i will be using the Arduino UNO R3 which i will introduce in the next phase of the project. You can see how the finished product looks like.
If you are interested in a visual look, I posted a Video to YouTube highlighting some important things one might need, but overall the assembly shouldn't take more than 10 minutes. Until next time. Tschüß

Saturday, January 24, 2015

Money hinders potentials

This is not the traditional write up for this blog, however something in my mind that have to be said.
I have been and still wondering why money should be a factor against potentials, there are lots of geniuses out there, raw ideas from bright minds and skills yet to be furnaced. But most of the time, all those cannot be mined without money, why?
Why can't an organization see a good idea and fund it? Why can't an institution see a determined potential and sponsor it? Why can't a country recognize bright minds and buy them. Why do people have to always struggle in order to contribute? I just can't understand the world's logic.

Potentials denied the right to mature in innovators, skilled tarnished and turned into homeless men, great minds ignored in place of radicals that destroy systems. Why does the world have to operate this way, why is it difficult to find a few .5 percent that really understand what is important?

Why why and why????

Monday, September 8, 2014

Good/Bad Software Engineers

After reading Katsuya Noguchi post on Good Software Engineers vs Bad Software Engineers, i was driven to re-share this post because he outlined all the important qualities of a Good Software Engineer, from Problem Solving, good coding practice, documentation to learning for other developers through reviews.

This is a must read blog post for all developers, the link below to the post. Good Software Engineer / Bad Software Engineer

Friday, April 18, 2014

Login problem after upgrade to Ubuntu 14.04 (Trusty Tahr) from 12.04 (Precise Pangolin) via Terminal

So as most people know, yesterday a new LTS version of Ubuntu was released, version 14.04 code name Trusty Tahr. I upgraded to this version, but not without some complications, if you are like me who only upgrade to Long Term Support versions and you are a bit too comfortable with the terminal, well my first advice will be DO NOT upgrade directly from the terminal, use the Update Manager instead. Although complications can happen either ways, but graphically it might be better.

If you are like me, and you happen to run the command;

$do-realease-upgrade -d

After running this, i simply followed the steps of normal upgrade, and it was successful with minor errors which is almost unavoidable, after restarting my system, i first i booted to the login and was still seeing 12.04, that was weird but then tried to login, i landed on a black page.

Well since at first i thought it was an issue with the unity, which probably is, i opened one of my TTY's and tried running an update and then re installing ubuntu-desktop  but still the error persisted, even created a new user as though a similar problem happened in 12.04, it was a problem with the config file and creating a new user will actually resolve the issue, but not in my case.

I am an opensource guy and one of the habbit is hitting the communities, i visited the Ubuntu G+ community, asked for suggestion, obviously, at least someone have encountered the same problem as you did, well the first response i got was to backup and reload my OS, however i was a bit positive a waited a few more hours.  This morning i got a response that made my breakthrough from +Matthew Rhodes who had same problem as i did and suggested an easy fix.

Since you have access to your TTY, go into one by hitting CTR+ALT+F1 then login with your credentials. After that, run this commands;

$sudo apt-get update

$sudo apt-get upgrade


Wait for it to complete the upgrade and then restart. This should fix your problem, but optionally if it doesn't, do this instead;

$sudo apt-get update

$sudo apt-get dist-upgrade


PROBLEM
You might encounter a minor error and you can't run apt utility with an error saying, "unmet dependencies" you can resolve this with

$sudo apt-get -f install

$sudo apt-get autoclean

$sudo apt-get autoremove

$sudo apt-get update


I wish you an adventurous Linux hack.

Monday, April 7, 2014

You wanna learn to Code?

Recently i have been getting questions:  "how can i learn to program?", "How can i become a professional programmer?" Well i have rather than an answer, but a guideline. I can only show you the door, you will be the one to walk through it.

First things first, to go into this world you need to prepare your mind, be ready to learn, be ready to code at least bunch of hours a week, if you are not enthusiastic or curious about how things work, its time to start changing your mindset. Be ready to face problems that will literally turn your brain into a hyper multitasking machine, most of all be ready to see the world differently because this world you are stepping into, you haven't heard of anything like it before.

OK i didn't mean to scare you that way, fortunate for you, this days there are more easier ways to get exposed to the programming world, unlike years ago that we literally go through books and manuals in order to learn. A bunch of web applications are developed to guide you through the basics of programming in various languages, and i am going to talk about a few here.

Codecademy 
This site my favorite, why you ask? well because it has a comprehensive introduction to Python which happens to be my favorite language, aside that if you are interested in web development, there are courses for HTML, CSS, JavaScript, Ruby and each contains a project at the end to test if you actually learnt something. This site presents you with an interactive way of learning, using an online interpreter for various languages, you can follow instructions from each chapter, write the code in the editor instantly and test your code if it works. Also there are hints to somewhat "difficult" tasks in case you can't find your way. Also you gather points from actually completing modules, and they have something called streak, which is just a measurement of  consecutive days you spent completing tasks. Additionally, after completing a language training, you can choose to start learning some API's like that of twitter, facebook e.t.c and guess what? its completely free.

Code School
This web platform is similar to Codecademy in a way, with HTML, CSS, JavaScript, Ruby (Ruby on Rails). But it additionally have courses on Objective-C, iOS development, using version control systems (GIT), JQuery and a bunch of other API's. Though unlike Codecademy, some courses have charges.

Haskell
If you are curious about functional programming languages, haskell is a  place to start, this site gives a short introduction to haskell programming language interactively, giving you an overview of the concept and learning some basic syntax.

There are many other sites you can use to learn aside this one's, sites such as  Cousera  and  Udacity  where you can find full university course lectures in video with interactive course works and  tasks, which you can do to test your knowledge and also certificates are awarded in some courses to those who satisfy the requirements of the courses.

Not being in school is not an excuse to not educate yourself, this is the 21st century, you can go to school online and self educate yourself at home.