Go Down

Topic: Arduino PING Code - Turning? (Read 1 time) previous topic - next topic

Brennn10

Hey there,

This is the current code that I have to make my robot turn right when the PING sensor detects an object within 5 inches.  How do you think this code works?  Here is some pseudo-code:
Go Forward until an object is detected within 5 inches
Turn Right if an object is detected
Move forward until an object is detected within 5 inches

Does my code do this well?  Could it be refined?

Code: [Select]
#include <AFMotor.h>

// create motor #2, 64KHz pwm
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);
const int pingPin = 19;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  motor1.setSpeed(200);     // set the speed to 200/255
  motor2.setSpeed(200);
  motor3.setSpeed(200);
  motor4.setSpeed(200);
}

void loop() {
  long duration, inches;
 
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
 
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
 
  inches = microsecondstoInches(duration);
 
  if (inches > 5)
    {
      motor1.run(FORWARD);
      motor2.run(FORWARD);
      motor3.run(FORWARD);
      motor4.run(FORWARD);
    }
  else
    {
      motor1.run(FORWARD);
      motor2.run(FORWARD);
      motor3.run(BACKWARD);
      motor4.run(BACKWARD);
}
}
long microsecondstoInches (long microseconds)
{
  return microseconds / 74 / 2;
}


Thanks for your help!

AWOL

Code: [Select]
// create motor #2, 64KHz pwm
AF_DCMotor motor1(1, MOTOR12_8KHZ);


I'd probably try harder to make my code match my comments.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Brennn10

Code: [Select]



I'd probably try harder to make my code match my comments.
[/quote]

Thanks for your help?

robtillaart


A quick view at the code shows no real problems, think it should work. But (there is allways a but) it can be improved in several ways. That was what AWOL meant by his remark about the comments. In the end the code is what counts, not the comments ...

I've tinkered with your code to show you some ideas, keep loop() as simple as possible by adding functions, and merging other ones. Changed the distance to float to get a bit more precission. Also added some questions to the code... (did not try to compile it BTW)

Hopes this helps ..
Rob

Code: [Select]

#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_8KHZ);   // Position of this wheel front left ??
AF_DCMotor motor2(2, MOTOR12_8KHZ);   // Why
AF_DCMotor motor3(3, MOTOR12_1KHZ);   // WHY 1Khz?
AF_DCMotor motor4(4, MOTOR12_1KHZ);

void setup()
{
  Serial.begin(9600);  // is it used?
  setSpeed(200);
}

void loop()
{
  float inches = getDistance();
 
  if (inches > 5.0)
  {
    goForward();
  }
  else
  {
    goRight();
  }
  // emergency break :)
  if (inches < 1.0)  setSpeed(0);
}

/////////////////////////////////////

void setSpeed(uint8_t speed)
{
    motor1.setSpeed(speed);     // set the speed to 200/255
    motor2.setSpeed(speed);
    motor3.setSpeed(speed);
    motor4.setSpeed(speed);
}

void forward()
{
  setSpeed(200);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void turnRight()
{
  setSpeed(100);  // turning slow is safer :)
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}

float getDistance()
{
  const int pingPin = 19;  // only relevant here

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  return pulseIn(pingPin, HIGH) / 148.0;
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

AWOL

I'd even go a little further,
Code: [Select]
float getDistance(int pingPin)
for when you add your second (or third..) sonar.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

vulrath

Code: [Select]

#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_8KHZ);   // Position of this wheel front left ??
AF_DCMotor motor2(2, MOTOR12_8KHZ);   // Why
AF_DCMotor motor3(3, MOTOR12_1KHZ);   // WHY 1Khz?
AF_DCMotor motor4(4, MOTOR12_1KHZ);




The reason why he's got motors 3&4 declared as 1KHz is because according to the Adafruit website, motors 1 and 2 can be set to 1,2,8, or 64KHz, while motors 3 and 4 can only be set at 1KHz.  I never could get the speed definitions to work, so I just left them out (for some reason it just wouldn't compile).  Much less of a headache that way.

Brennn10

Thanks for your help, guys.  How should I make sure that the robot turns right at 90 degrees?  Is it possible to set a time for how long the robot turns?  Say I wanted to make it go backward a few inches before turning right at a 90 degrees angle.  What would be the best way to accomplish this?

Thanks for your help, its very appreciated.

AWOL

Quote
Say I wanted to make it go backward a few inches before turning right at a 90 degrees angle.

Use the rangefinder.

Accurate turns are going to be difficult without some feedback - timing will only work aas long as everything (battery state etc) remain constant.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Brennn10

What do you mean by using the rangefinder?  Do I utilize values from the sensor to make it turn right 90 degrees?  Also, how do I make it go backward for a certain amount of time (or distance).  Right now, once it detects an object, it moves backward until it is out of range, and then moves forward, and then moves backward again.  So it is basically just lurching back and forth.  I want it to move backwards about 6 inches before it starts to turn right again.

Groove

#9
Feb 12, 2011, 11:03 am Last Edit: Feb 12, 2011, 11:05 am by Groove Reason: 1
Quote
Also, how do I make it go backward for a certain amount of time (or distance

Distance is easy - use the rangefinder.
Time is easy - use the built-in timers.
Turns can use the timers, but the actual degree of turn can usually only be found by experiment.

Measuring an angle with the rangefinder isn't feasible, but I don't think that is what AWOL meant.
Per Arduino ad Astra

robtillaart


To be able to turn exactly 90 degrees you must have referencepoints, four. Two points define your "old direction" and two for the current direction. This is can be quite complex to build up these references and keep them up to date. Fortunately mother nature provided us with an free reference framework called the magnetic field. You could use a compass module e.g. - http://www.adafruit.com/index.php?main_page=product_info&cPath=35&products_id=244 - to continuously monitor your direction. When you rotate you just rotate until you have reached direction +90.



  • Wide voltage supply range (2.7-5.0 VDC)

  • I2C Serial Interface

  • Digital output to nearest 0.1°

  • Built-in calibration for an average error of 2.5°


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Kazume

I tried uploading the motor code to my arduino but i keep getting this error message:
sketch_jan21b:4: error: 'AF_DCMotor' does not name a type
sketch_jan21b:5: error: 'AF_DCMotor' does not name a type
sketch_jan21b:6: error: 'AF_DCMotor' does not name a type
sketch_jan21b:7: error: 'AF_DCMotor' does not name a type
sketch_jan21b.ino: In function 'void setup()':
sketch_jan21b:12: error: 'motor1' was not declared in this scope
sketch_jan21b:13: error: 'motor2' was not declared in this scope
sketch_jan21b:14: error: 'motor3' was not declared in this scope
sketch_jan21b:15: error: 'motor4' was not declared in this scope
sketch_jan21b.ino: In function 'void loop()':
sketch_jan21b:35: error: 'motor1' was not declared in this scope
sketch_jan21b:35: error: 'FORWARD' was not declared in this scope
sketch_jan21b:36: error: 'motor2' was not declared in this scope
sketch_jan21b:37: error: 'motor3' was not declared in this scope
sketch_jan21b:38: error: 'motor4' was not declared in this scope
sketch_jan21b:42: error: 'motor1' was not declared in this scope
sketch_jan21b:42: error: 'FORWARD' was not declared in this scope
sketch_jan21b:43: error: 'motor2' was not declared in this scope
sketch_jan21b:44: error: 'motor3' was not declared in this scope
sketch_jan21b:44: error: 'BACKWARD' was not declared in this scope
sketch_jan21b:45: error: 'motor4' was not declared in this scope
sketch_jan21b.ino: In function 'long int microsecondstoInches(long int)':
sketch_jan21b:49: error: expected `}' at end of input

AWOL

Did you install the library and restart the IDE?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up