I work on a robot, it is part of the summer workshop of robotics in our college. We use C-STAMP microcontrollers from A-WIT. I was able to make him move, turn left, turn right, move back. I even managed to do this on a black tape using a contrast sensor.
I send the robot 30-45 degrees towards the black ribbon on the table, and it aligns and begins to move along the black ribbon. This twitches a bit, maybe due to my programming logic below, it starts a while loop and constantly checks if-statements, so it tries to try turning left and right every few milliseconds, which explains the shaking part. But everything is in order, it works, not as smoothly as I want it to work, but it works! The problem is that I can not get my robot to go along the rectangular path of the black ribbon. As soon as he reaches the corner of the black ribbon, he just keeps going straight, instead of turning left or right.
My 2 sensors are located directly below the robot, next to the front wheel, almost at floor level. It has a value of "index" in the range from 0 to 8. Eight is the brightest contrast, and zero is the darkest contrast. Therefore, when the robot enters the zone of the black ribbon, the index value drops, and on the basis of this, I have an if statement telling my robot to either turn left or right.
Here is my attempt. To avoid confusion, I did not publish the entire source code, but only the logical part responsible for moving my robot along the black tape.
while(1) { // don't worry about these. // 10 and 9 represent Sensor 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 (ie 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 same code as above // but it for the 2nd sensor. if (index2 > 4) { turnRight(lp, rp, 1); goForward(lp, rp, 1); } } }
I spent all day trying to figure it out. I pretty much exhausted all the possibilities. Asking for a solution in stackoverflow is my last option.
Thanks in advance! If you have any questions about the code, let me know, but the comments should be clear.
This is my goForward function if someone is wondering:
void goForward(BYTE lp, BYTE rp, WORD t) { WORD i; for(i = 0; i < t; i = i + 1){ PULSOUT(lp, 400, 1, 1); PULSOUT(rp, 800, 1, 1); PAUSE(17); } }
UPDATE: this is what I have come up with so far. I deleted all my if statements I posted earlier and decided to write the logic from scratch: