Go Down

Topic: Autonomous Arduino tank with seeedstudio motor shield and ping sensor. (Read 3805 times) previous topic - next topic

Jdawg

Im having trouble figuring out how to write a sketch for this, Can anyone point me to a good place to start. I am using An arduino uno, a seeedstudio motor shield, a paralax ping sensor attached to a servo, and my dual motor tank chassis. So far this gent has basically the setup im after, minus the ir sensor and the temp sensor. http://errincode.blogspot.com/2013/02/arduino-autonomous-tracked-robot-build_27.html  Also the sketch on this site comes up with tons of errors, which i attached a picture of.

AWOL

Code: [Select]
analogWrite(speedpinA,spead);//input a simulation value to set the speed
That comment doesn't match what the code is doing - I don't see where "simulation" comes into the picture.
Consistent spelling of "speed" would help understanding what is going on.

Quote
where to plug the servo

What servo?
"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.

AWOL

"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.

AWOL

Quote
i also dont see a servo being used in this sketch either

No, me neither
"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.

Jdawg

Ok this is my sketch:
Code: [Select]
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Arduino powered RC tank  By: Stanley Deakins                                                                  
//                                                                                                          
//This is a basic sketch used to power an RC tank using a Arduino and a seeedstudio ping sensor. It uses a      
//Parallax ping sensor to navigate. Please feel free to modify this sketch anyway you feel like. This is simply
//just enough to get you started on the project. Thank and enjoy.                                                
//http://www.instructables.com/id/E2Q88MBHC9MJAFY/                                                                                                              
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Built atop Ping example code by Tom Igoe
// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead =127;//define the spead of motor

void setup()
{
 pinMode(pinI1,OUTPUT);
 pinMode(pinI2,OUTPUT);
 pinMode(speedpinA,OUTPUT);
 pinMode(pinI3,OUTPUT);
 pinMode(pinI4,OUTPUT);
 pinMode(speedpinB,OUTPUT);
}

void forward()
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
    digitalWrite(pinI1,HIGH);
}
void backward()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
}
void left()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
}
void right()//
{
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
    digitalWrite(pinI1,HIGH);
}
void stop()//
{
    digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
    digitalWrite(speedpinB,LOW);
    delay(1000);

}

void loop()
{
 // establish variables for duration of the ping,
 // and the distance result in inches and centimeters:
 long duration, inches, cm;

 // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);

 // The same pin is used to read the signal from the PING))): a HIGH
 // pulse whose duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(pingPin, INPUT);
 duration = pulseIn(pingPin, HIGH);

 // convert the time into a distance
 inches = microsecondsToInches(duration);

 //
 //if objects are less than 12 inches away
 //the robot reverses and turns to the right
 //for 2 seconds
 //

 if (inches < 8){
 
   //brake drive motor and pause 1/10 second
 stop();

   //
   //setting motors backwards
   //
 
 backward();
 delay(700);
 stop();
 
   //
   //setting motors to turn
   //
 
 left();
 delay(1100);
 stop();
 }

 //
 //when nothing is within 8"
 //the robot simply drives forwards
 //

 else{
 
   //
   //Setting drive motor
   //
 
 forward();
 }
  delay(100);
 }
 long microsecondsToInches(long microseconds)
 {
 // According to Parallax's datasheet for the PING))), there are
 // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
 // second).  This gives the distance travelled by the ping, outbound
 // and return, so we divide by 2 to get the distance of the obstacle.
 // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
 return microseconds / 74 / 2;
 }
I have an arduino uno with a seeedstudio motor shield and a ping sensor, I need to include a servo sketch to sweep a servo. But for now its as is till I progress with this sketch. All the tank does is back up, turn and repeat. The ping sensor seems to be active but its not going forward ,when nothing is clearly in its path. ( I switched my wires around so the tank goes forward now. It seems like the ping sensor Is not giving the motors any instructions on where to go. I will look at my Ping later today and see what kind of readings its getting in the serial monitor, I have not had a chance to do so yet. Its light barely blinks when everything is running, which makes me assume that it is not functioning properly)

Jdawg

I guess I am asking: How do I change this so it drives forward and avoids walls with the ping sensor.

PaulS

Quote
I guess I am asking: How do I change this so it drives forward and avoids walls with the ping sensor.

It looks like the code should do that.

If it doesn't, you need to explain what it does, instead. What distance are you actually getting from the ping sensor? Is it reasonable? With no Serial.begin() and no Serial.print() statements, you don't know. The first thing to do is to correct that. You must know whether the data is accurate and reasonable before it is meaningful to use it.

Quote
The ping sensor seems to be active but its not, going forward at all, when nothing is clearly in its path.

I suspect that some of the commas in that sentence are in the wrong place, because I'm having real troubles parsing it.

Jdawg

I am currently still reading and trying to learn the sketch language, so im not sure how to correct the Serial.begin() and Serial.print() statements ( I see know that these statements are not in the sketch). I have not yet attempted to modify this sketch, I know it needs a little work to do what I require.

AWOL

Just like about every other sketch that uses serial communications, the "Serial.begin()" goes in your "setup()", and the Serial.print()"s go wherever you want to print a significant value or event.
The IDE comes with examples of how to use them, just click on "File", then "Examples" then "Communication"
"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.

Jdawg

So I need to have something that looks more like this?
Code: [Select]
int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int speed =127;//define the speed of motor

void setup()
{
 // start serial port at 9600 bps:
 Serial.begin(9600);
 
 pinMode(pinI1,OUTPUT);
 pinMode(pinI2,OUTPUT);
 pinMode(speedpinA,OUTPUT);
 pinMode(pinI3,OUTPUT);
 pinMode(pinI4,OUTPUT);
 pinMode(speedpinB,OUTPUT);
}

And something similar to this at the end of the script, to tell the motors to move according to what the ping sensor reads.
Code: [Select]
; // wait for serial port to connect. Needed for Leonardo only
 }

 pinMode(2, INPUT);   // digital sensor is on digital pin 2
 establishContact();  // send a byte to establish contact until receiver responds
}

void loop()
{
 // if we get a valid byte, read analog ins:
 if (Serial.available() > 0) {
   // get incoming byte:
   inByte = Serial.read();
   // read first analog input, divide by 4 to make the range 0-255:
   firstSensor = analogRead(A0)/4;
   // delay 10ms to let the ADC recover:
   delay(10);
   // read second analog input, divide by 4 to make the range 0-255:
   secondSensor = analogRead(1)/4;
   // read  switch, map it to 0 or 255L
   thirdSensor = map(digitalRead(2), 0, 1, 0, 255);  
   // send sensor values:
   Serial.write(firstSensor);
   Serial.write(secondSensor);
   Serial.write(thirdSensor);              
 }
}

void establishContact() {
 while (Serial.available() <= 0) {
   Serial.print('A');   // send a capital A
   delay(300);
 }
}
This is part of one of the examples in communications

Jdawg

All of these sketchs I did not actually type myself, Most are copied out of arduino examples someone else wrote, so I understand very little of what is going on here.

1ChicagoDave


All of these sketchs I did not actually type myself, Most are copied out of arduino examples someone else wrote, so I understand very little of what is going on here.


Do you want to understand it better? Or, do you just want something that works?
If you want to understand, what are you doing to try to figure it out?

zoomkat


All of these sketchs I did not actually type myself, Most are copied out of arduino examples someone else wrote, so I understand very little of what is going on here.


You probably need to break your project into several parts and test each until you see what is going on. Start with testing the ping sensor by having the output sent to the serial monitor to see if what you get is what you expect.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Andakwa

I know the problem he has, I have the same issue. I have a Uno, Seeed Motor Shield v 2 & the ping sensor with 3 connectors, +, - & signal. I run the code for without the motor sheld code & the ping works fine. Once I add the motor code the sensor reading goes to 0in 0 cm in the serial monitor.... be low is the code.

#include "MotorDriver.h"

const int pingPin = 7;
int obsticle;
void setup() {
  motordriver.init();
  motordriver.setSpeed(200,MOTORB);
  motordriver.setSpeed(200,MOTORA);
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(100);
  obsticle = inches;
  if (obsticle < 15){
   motordriver.goLeft();
     }
  else{
   motordriver.goForward();
     }
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

I made the variable "obsticle" because when I tried to use :inches" in the if condition the compiler nagged me about inches not being defined for this scope or something similar.
I have this on a magician chassis & it was just a simple if the is an obstacle within 13 inches it turns left....... 

Go Up