Go Down

Topic: Help please to improve this obstacle avoiding robot project. (Read 8293 times) previous topic - next topic

biotech

#15
Apr 27, 2014, 03:54 am Last Edit: Apr 27, 2014, 04:57 pm by biotech Reason: 1
I removed the two delays in the turn subroutines. Now the robot goes forward until it finds an obstacle. But when it finds an it instead of turning right or left continuously the wheels kind of jamm in the same place.

I think the problem is that I don´t know how to change from true to false so the routinge of turning left ( or right ) keeps going until the obstacule is sorted.  I have tried variations to the if statement like do-while of for-loop. None had given any good results. That´s why an example would be great to clarify concepts.

Thanks!

biotech

#16
Apr 27, 2014, 06:12 pm Last Edit: Apr 27, 2014, 09:01 pm by biotech Reason: 1
Well I keep on trying. In this new variation of the code I introduce the boolean value change ' turnLeft = !turnLeft; ' when there is no obstacle detected. So when an obstacle is detected, the robot rotates in one direction or the other depending on that value.

At  this point I think the code is ok to make the robot turn in alternating manner to one side and then when the next obstacle is detected to the other. But instead the robot turns ramdomly to one side or the other.

Variation of the code:
Code: [Select]

#include <Servo.h>

Servo motorLeft;          // Define left motor
Servo motorRight;        // Define right motor
#define trigPin A2
#define echoPin A3
boolean turnLeft = true;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
motorLeft.attach(A5);    // Set left motor to pin A5
motorRight.attach(A4);  // Set right motor to pin A4

}

void loop() {
 
 int duration, distance;                               //ultrasonic sensor routine
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(1000);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 
 if (distance >= 30 || distance <= 0){    
   Serial.println("Out of range");
   motorLeft.write(20);
   motorRight.write(20);  
   turnLeft = !turnLeft;
 }
 
 if (distance <= 30 && turnLeft == true){
   Serial.print(distance);
   Serial.println( "cm");    
   motorLeft.write(170);
   motorRight.write(10);        
   }

 if (distance <= 30 && turnLeft == false){        
   Serial.print(distance);
   Serial.println( "cm");    
   motorLeft.write(10);
   motorRight.write(170);        
   }  
   
 }


Getting much closer but not there yet. Help please to get to understand what could still be failing.

wildbill

Move this:
Code: [Select]

    turnLeft = !turnLeft;


into both the if blocks below it. i.e. only do it when you do a turn.

biotech

That doesn´t work wildbill. If I move the ' turnLeft = !turnLeft; ' to the turn subroutines the wheels try to go forward and backward at the same time. I tried before that variation and now again. That´s the result.

Any other approach?

wildbill

Yup, that suggestion was rubbish. Let's try again.

You need to keep the turn left variable the same while the range is short & flip it only when the turn has brought you to a clear path again.

So, keep an OldDistance variable with the prior value of distance. When OldDistance <=30 and Distance > 30, you've just finished a turn. At that point, you can flip turnLeft.

biotech

#20
Apr 28, 2014, 04:28 am Last Edit: Apr 28, 2014, 04:59 am by biotech Reason: 1
I like the idea wildbill. Following your advise I introduced the following if statement:

Code: [Select]

if (distance >= 30 && OldDistance <= 29);{
turnLeft = !turnLeft;


So no the new code is this one:
Code: [Select]

#include <Servo.h>

Servo motorLeft;          // Define left motor
Servo motorRight;        // Define right motor
#define trigPin A2
#define echoPin A3
boolean turnLeft = true;
int OldDistance;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
motorLeft.attach(A5);    // Set left motor to pin A5
motorRight.attach(A4);  // Set right motor to pin A4
}

void loop() {
 
 int duration, distance;                               //ultrasonic sensor routine
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(1000);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 
 if (distance >= 30 && OldDistance <= 29);{
 turnLeft = !turnLeft;
 }
 
 if (distance >= 30) {
   Serial.println("Out of range");
   motorLeft.write(20);
   motorRight.write(20);
   }
 
 if (distance <= 29 && turnLeft == true){
   Serial.print(distance);
   Serial.println( "cm");    
   motorLeft.write(170);
   motorRight.write(10);        
   }

 if (distance <= 29 && turnLeft == false){        
   Serial.print(distance);
   Serial.println( "cm");    
   motorLeft.write(10);
   motorRight.write(170);        
   }  
   
  OldDistance = distance;
}  


To my understanding now the robot should alternate turns from one obstacle to the next one as I wanted. Instead when it finds and obstacle the weels try to go forward and backward at the same time so they get stucked. I really don´t know what is wrong now.

skullbucks

connect your trig and echo pins to digiral pins not with analog pins if ur using HC-SR04 distance sensor
check:http://skullbucks.in/projects/arduino/distancesensor.html

wildbill

Oops:
Code: [Select]
  if (distance >= 30 && OldDistance <= 29);{

Get rid of the semicolon.

biotech

#23
Apr 28, 2014, 07:08 pm Last Edit: Apr 28, 2014, 07:18 pm by biotech Reason: 1
Made the two mods. Thanks wildbill and skullbucks. However, it still doesn´t works. The robot turns to one side or the other ramdomly.

I really don´t understand what´s not working. The code looks ok for me to do the task.

May it be a problem with the ' turnLeft = !turnLeft' statement ? I have seen that some people use ' bool turnLeft = !turnLeft ' instead. However I also tried this other way and there is no improvements.

skullbucks


Made the two mods. Thanks wildbill and skullbucks. However, it still doesn´t works. The robot turns to one side or the other ramdomly.

I really don´t understand what´s not working. The code looks ok for me to do the task.

May it be a problem with the ' turnLeft = !turnLeft' statement ? I have seen that some people use ' bool turnLeft = !turnLeft ' instead. However I also tried this other way and there is no improvements.


plz mail me your circuit diagram with full pin details:mail.skullbucks@gmail.com
i'll mail you source code
one more thing dnt forget to mention your motors and sensors serial or model no. coz it needed for library

Northof49

It's handy to put some serial print statements in your code

Serial.print("turning left");

or

Serial.print(yourvariable);


and set your robot with its wheels up, usb cable attached, and watch what parts of the code and choices are being accessed and when.  You could have it serial print out your variable, for instance, and watch whether the expected or unexpected are going on.

biotech

Sorry guys for not being able to catch up yesterday and today so far.

I took off the serial prints statements just to see if the delays that went with them were causing any problem. That was not the case so I will put them back in again. Additionally,  I'll send an email to skullbucks with my circuit diagram as requested.

Thanks again to you all for helping me!




Go Up