How to make my robot move in a rectangular path along the black tape?

Posted by Sahat on Stack Overflow See other posts from Stack Overflow or by Sahat
Published on 2010-06-11T00:57:32Z Indexed on 2010/06/11 1:02 UTC
Read the original article Hit count: 294

Filed under:
|
|

I am working on a robot, it's part of the summer robotics workshop in our college. We are using C-STAMP micro controllers by A-WIT. I was able to make it move, turn left, turn right, move backward. I have even managed to make it go along the black tape using a contrast sensor.

I send the robot at 30-45 degrees toward the black tape on the table and it aligns itself and starts to move along the black tape. It jerks a little, probably due to my programming logic below, it's running a while loop and constantly checking if statements, so it ends up trying to turn left and right every few milliseconds, which explains the jerking part. But it's okay, it works, not as smooth as I want it to work but it works! Problem is that I can't make my robot go into a rectangular path of the black tape. As soon as it reaches the corner it just keeps going straight instead of making a left/right turn. Here's my attempt. The following code is just part of the code.

My 2 sensors are located right underneath the robot, next to the front wheel, almost at the floor level. It has "index" value ranging from 0 to 8. I believe it's 8 when you have a lot of light coming into the sensor , and 0 when it's nearly pitch black. So when the robot moves into the black-tape-zone, the index value drops, and based on that I have an if-statement telling my robot to either turn left or right.

To avoid confusion I didn't post the entire source code, but only the logical part responsible for the movement of my robot along the black tape.

while(1) {

// don't worry about these.
// 10 and 9 represent Sensor's PIN location on the motherboard
V = ANALOGIN(10, 1, 0, 0, 0);
V2 = ANALOGIN(9, 1, 0, 0, 0);

// i got this "formula" from the example in my Manual. 
// V stands for voltage of the sensor.
// it gives me the index value of the sensor. 0 = darkest, 8 = lightest.
index = ((-(V - 5) / 5) * 8 + 0.5);
index2 = ((-(V2 - 5) / 5) * 8 + 0.5);

// i've tweaked the position of the sensors so index > 7 is just right number.
// the robot will move anywhere on the table just fine with index > 7. 
// as soon as it drops to or below 7 (i.e. finds black tape), the robot will 
// either turn left or right and then go forward.

// lp & rp represent left-wheel pin and right-wheel pin, 1 means run forever.
// if i change it from 1 to 100, it will go forward for 100ms.
if (index > 7 && index2 > 7)
        goForward(lp, rp, 1);

    if (index <= 7) {
        turnLeft(lp, rp, 1);
        goForward(lp, rp, 1);
        // this is the tricky part. i've added this code last minute
            // trying to make my robot turn, but i didn't work.
            if (index > 4) {
            turnLeft(lp, rp, 1);
            goForward(lp, rp, 1); }
    }

    else if (index2 <= 7) {
        turnRight(lp, rp, 1);
        goForward(lp, rp, 1);
            // this is also the last minute addition. it's same code as above
            // but it's for the 2nd sensor.
        if (index2 > 4) {
            turnRight(lp, rp, 1);
            goForward(lp, rp, 1); }
    }

I've spent the entire day trying to figure it out. I've pretty much exhausted all avenues. Asking for the solution on stackoverflow is my very last option now.

Thanks in advance! If you have any questions about the code, let me know, but comments should be self-explanatory.

© Stack Overflow or respective owner

Related posts about c

    Related posts about logic