Skip to content

Carbot is Successful-ish

January 24, 2015

So it works, after a fashion. With the sharp IR sensor mounted facing forward and the ultrasonic sensor facing sideways, carbot circles the arena without hitting the walls. It’s not staying very close to the walls but you can see that it’s trying. It starts out a bit too far from the wall and you can see it moving closer and maintaining it. The first turn points it too far out but you can see it corrects. The third turn is way too sharp and it can’t recover. After that there are a few good turns and another clunker. This is, of course, better than hitting the wall but i’m not quite sure what’s going on. Not clear to me whether the turn is too granular or what.

Update: looking at the code, I realized that the code to do the left turn obstacle avoidance initiated the turn but never really stopped it. It was just depending on the straight line code to reset the motors. The time it took to check the wall distance and react was in the several millisecond range so the turn would always be too sharp. Fixing that gave much better results although there is one rogue turn at the end. I’m going to think hard about what might still be going on but i’m done for the day.

Here’s the Carbot in all its glory. At the very back is the black battery pack for the logic power supply.  Ahead of that is the white motor power supply battery.  Jumping past the car’s motor cover you see the olduino board with the 1802 cpu card under it – the momentary switch at the rear is reset.  Above them is the carbot driver board: at the very back of that is the sharp IR sensor mounted on its tower, then the logic power supply, then the motor driver, and finally the header connector for the ultrasonic sensor which is mounted on the side you can’t see.
15-01-24 on the carpet

		printf("@ %d:",ttl);
		digitalWrite(7,LOW);fprox=sharpy(); digitalWrite(7,HIGH);
		printf("<%d ",fprox);
		if (fprox<maxfprox){ // more than 1 ft from barrier
			killdrive();//kill any turn that was going on
			cruiseAlongWall();//set a course to track the wall
		ttl--; //reduce time to live
void hardleft(){
	digitalWrite(pwmb,HIGH);//analogWrite(pwmb,255);//full power right
	digitalWrite(pwma,HIGH);//analogWrite(pwma,255);//full power left
	digitalWrite(bin1,HIGH); digitalWrite(bin2,LOW);
	digitalWrite(ain1,LOW); digitalWrite(ain2,HIGH); //reverse left wheel
	delay(60); //turn left a bit more directly
void cruiseAlongWall(){
	if (0!=wdist) oldwdist=wdist; //track the wall distance after the first time
	wdist=pingQ()/2; //get the wall distance
	printf("cw %d\n",wdist);
	if (wdist<minwdist){
	digitalWrite(bin1,HIGH); digitalWrite(bin2,LOW);
	digitalWrite(ain1,HIGH); digitalWrite(ain2,LOW);
	pwmcycleN(15);//forward a bit
void tooclose(){
    if (wdist>oldwdist) {//but going the right way
		analogWrite(pwma,basespeed);analogWrite(pwmb,basespeed); //proceed
		printf(" wf-1 ");
		analogWrite(pwma,lowspeed);analogWrite(pwmb,basespeed); //bear left
		printf(" wvlft ");
void toofar(){
    if (wdist<oldwdist) {//but going the right way
		analogWrite(pwma,basespeed);analogWrite(pwmb,basespeed); //proceed
		printf(" wf-2 ");
		analogWrite(pwma,basespeed);analogWrite(pwmb,lowspeed); //bear right
		printf(" wvrt ");

The wall distance maintenance code is something i cribbed from the first wall-racer i ever encountered. The software pwm technique was suggested by a fellow on the Cosmac Elf mailing list.


From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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

%d bloggers like this: