Society of Robots - Robot Forum

Software => Software => Topic started by: ccdjr106 on March 05, 2010, 07:16:17 PM

Title: simple edge detection
Post by: ccdjr106 on March 05, 2010, 07:16:17 PM
what im trying to do is just make a simple program that makes my robot drive forward and stop when it hits a edge. im using a pololu qtr reflectance sensor. when calibrated and on the table it normally returns a value of 0 to 600ish  when off the table it always gives me 1000 in my code im using a while loop but my robot just keeps on driving and if not stopped by me will just drive right off the table can someone please help me  ??? ??? ???

Code: [Select]

#include <PololuQTRSensors.h>



PololuQTRSensorsRC qtr((unsigned char[2]) {14, 15}, 0);
int pwm1 = 5 ;
int en1 = 4 ;
int pwm2 = 6 ;
int en2 = 7 ;



void setup()
{
 
   
  int i;
  for (i = 0 ; i < 250; i++)
  {
  qtr.calibrate();
  delay(20);
  }


pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
pinMode(en1, OUTPUT);
pinMode(en2, OUTPUT);
}


void loop(){
 
 
  unsigned int sensors[2];
 
while(sensors[0] != 1000){
  analogWrite(pwm1, 255);
analogWrite(pwm2, 255);
digitalWrite(en1, HIGH); //turn on the motor
digitalWrite(en2, HIGH);
delay (100);
 analogWrite(pwm1, 0);
analogWrite(pwm2, 0);
delay(1000);


                     }
            }
Title: Re: simple edge detection
Post by: hopslink on March 06, 2010, 01:45:34 AM
Code: [Select]
while (sensors [0] !=1000){...is true for all values 'not equal' to 1000 so values 0-999 and 1001-...  all return true.

What you want is - true for 0-999 and false for 1000-.... Like this:
Code: [Select]
while (sensors [0] <=1000){...
Title: Re: simple edge detection
Post by: ccdjr106 on March 06, 2010, 06:31:26 AM
it still drives right off the table
Title: Re: simple edge detection
Post by: hopslink on March 06, 2010, 07:25:00 AM
Try a lower threshold value - say 700. If you hold your robot on the table and move the sensors over the edge do the wheels stop spinning?

It is also possible it is just momentum carrying it over the edge. Try setting your driving pwm value lower so the 'bot does not travel so fast, or move the sensors further ahead of the wheels.
Title: Re: simple edge detection
Post by: ccdjr106 on March 06, 2010, 06:16:30 PM
ive tried a ton of different numbers and it still doesnt work  >:(
Title: Re: simple edge detection
Post by: Ro-Bot-X on March 06, 2010, 07:09:48 PM
No matter what threshold value you are using, there is nothing that tell the motors to stop after they are started...

How are the motors connected? I see a pwm pin and an enable pin. There should be a direction pin (or 2 pins sometimes...). If you set the enable pin LOW, the motors should be disconected, but free wheel spinning, that will keep the robot running off the table. You need to brake the motors or switch direction from forward to reverse. There you need the direction pin(s). If you are using 2 direction pins, set them both HIGH or LOW and the motors will brake. If you use only one direction pin, set the direction pin LOW and the pwm=0 and the motors will brake.

Perhaps your enable pin is actually a direction pin? In this case, if you set the pwm pin to 255 (HIGH) and the enable pin to LOW, the motor will spin in one direction (forward). If you set the pwm pin to 0 (LOW) and the enable pin to HIGH, the motor will spin in the other direction (reverse). If BOTH pins are set either HIGH or LOW the motor will be braked.
Title: Re: simple edge detection
Post by: ccdjr106 on March 08, 2010, 07:44:58 PM
If I set my pwmpin to 255 and my enable pin to high it will go forward if I set pwm to 0 it will stop and if I set my enable pin to low it will go in reverse. The motors are geared and they do not roll