Adafruit is open, safely ... and shipping all orders at this time! Read more!
0

How to control motors with Motor shield using PING sensor
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to control motors with Motor shield using PING sensor

by GGipson on Sun Oct 10, 2010 3:21 am

I was wondering how I would program and wire PING sensors to control my DC motors. I've ran the motor test code from this site and all worked well. I've added wing shield so I can still utilize in-out pins on the Arduino. My goal is to make an underwater autonomous vehicle. I just need to control motors using PING sensors. Thanks for any ones help. :mrgreen:
GGipson
 
Posts: 11
Joined: Sun Oct 10, 2010 2:59 am
Location: San Marcos, CA

Re: How to control motors with Motor shield using PING sensor

by adafruit_support_bill on Sun Oct 10, 2010 6:33 am

First you will need to find a sonar sensor suitable for underwater use. The Maxbotix and Devantech ultrasonic sensors commonly sold for hobbyist use do not work underwater.

Then you need to decide what you want your UAV to do with the distance information returned by the sensor. Do you want it to avoid obstacles? Search? Follow? 3/4 of most programming projects is just defining what you want it to do.

adafruit_support_bill
 
Posts: 77290
Joined: Sat Feb 07, 2009 10:11 am

Re: How to control motors with Motor shield using PING sensor

by GGipson on Sun Oct 10, 2010 2:12 pm

Thanks for your reply. I've ordered Water proof ultra sonic sensors that look just like the original PING sensors except they are completely sealed. I plan to retrofit the PING sensor board with theses sensors then seal the board itself. I've found quite a few people including Parallax interested if this will work or not so I'm gonna give it a shot and report back. Ive built and programmed the Parallax Boe-Bot and added the PING sensor to it successfully but for this project I felt that with all the different shields that are out for the Arduino(Motor shield in particular)that it would be a more suitable platform to use. I want to use these retrofitted PING sensors for object detection. I have planned to use 3 PING sensors for left, right, and depth. I figured if I could get just one of the sensors to control one of the motors connected to the motor shield, I could figure out the programming for the other 2.

Thanks again.
GGipson
 
Posts: 11
Joined: Sun Oct 10, 2010 2:59 am
Location: San Marcos, CA

Re: How to control motors with Motor shield using PING sensor

by adafruit_support_bill on Sun Oct 10, 2010 4:04 pm

Are these just waterproof, or actually designed for underwater use? At the very least, you will need to re-calibrate the ping-sensor timing. Sound-wave propagation under water is much faster than in air.

Beyond that, you still haven't described how you want the motor to react to the ping sensor readings. The ping sensor will give you a timed pulse proportional to the distance to the nearest reflecting surface. The motor needs a direction
Code: Select all | TOGGLE FULL SIZE
 motor.run(FORWARD);

and a speed from 0-255.
Code: Select all | TOGGLE FULL SIZE
motor.setSpeed(255);

How you map one to the other depends on what you want your UAV to do.

adafruit_support_bill
 
Posts: 77290
Joined: Sat Feb 07, 2009 10:11 am

Re: How to control motors with Motor shield using PING sensor

by GGipson on Thu Oct 14, 2010 1:04 am

I guess the main problem I'm having is trying to figure out what outputs from the Arduino affect which outputs on the motor shield. I would like to have a motor connected to the M1 output of the motor shield and run continuously at a high speed and when the ping sensor detects an object 3 ft away it causes the motor to run at half speed and if it detects an object at 2 ft then it stops the motor. then when the sensor doesn't detect the object any more the motor begins to run again.
I think I need to send the ping signal to an input on the Arduino so it will control one of the Arduino's outputs that the motor shield uses but I can not seem to find out which motor shield input controls M1.
Gosh I hope that made sense! lol
Thanks for your patience.
GGipson
 
Posts: 11
Joined: Sun Oct 10, 2010 2:59 am
Location: San Marcos, CA

Re: How to control motors with Motor shield using PING sensor

by adafruit_support_bill on Thu Oct 14, 2010 5:48 am

I think I need to send the ping signal to an input on the Arduino so it will control one of the Arduino's outputs that the motor shield uses but I can not seem to find out which motor shield input controls M1.

You need to write some code to do this. The code would look something like:
Code: Select all | TOGGLE FULL SIZE
void loop ()
{
   float distance = readPingSensor();  // read the sensor and convert into feet

   if (distance > 3)  // more than 3 feet
      {
      motor.run(FORWARD);
      motor.SetSpeed(255);  // full speed
      }
   else if (distance > 2)  // between 2 and 3 feet
      {
      motor.run(FORWARD);
      motor.SetSpeed(127);  // hal;f speed
      }
   else  // closer than 2 feet
      {
      motor.run(RELEASE);  // stop motors
      }
}

adafruit_support_bill
 
Posts: 77290
Joined: Sat Feb 07, 2009 10:11 am

Re: How to control motors with Motor shield using PING sensor

by GGipson on Fri Oct 22, 2010 6:12 pm

Thanks That was very helpful and it looks like the code I'm trying to write! I used the PING code example and tried to blend it with the motor libraries and your code but still to no avail! I still get an error because I havent declared readPingSensor1! :) Also am I on the right path thinking if I get PingSensor1 to control Motor1 that it would make sense that PingSensor2 and 3 would control Motor2 and 3 independently? Here's what I have so far and I know I have a lot to learn about programming! lol I figure all I can do is keep plugging away at it and I'll get it sooner or later! :) Thanks again for all your help!

//Ping motor control

#include <AMotorC.h>

#include <AFMotor.h>

const int PingSensor1 = 4; // Defines ping sensor 1

AF_DCMotor motor1 (1, MOTOR12_1KHZ); //Creates motor 1 at 1khz



void setup()
{
Serial.begin(9600);// initialize serial comms
}




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(PingSensor1, OUTPUT);
digitalWrite(PingSensor1, LOW);
delayMicroseconds(2);
digitalWrite(PingSensor1, HIGH);
delayMicroseconds(5);
digitalWrite(PingSensor1, 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(PingSensor1, INPUT);
duration = pulseIn(PingSensor1, 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);
}

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/ac ... G-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;
}

float distance = readPingSensor1(); // read the sensor and convert into feet
{
if (distance > 3) // more than 3 feet
{
motor1.run(FORWARD);
motor1.SetSpeed(255); // full speed
}
else if (distance > 2) // between 2 and 3 feet
{
motor1.run(FORWARD);
motor1.SetSpeed(127); // half speed
}
else // closer than 2 feet
{
motor1.run(RELEASE); // stop motors
}
}
GGipson
 
Posts: 11
Joined: Sun Oct 10, 2010 2:59 am
Location: San Marcos, CA

Re: How to control motors with Motor shield using PING sensor

by GGipson on Fri Oct 22, 2010 6:16 pm

Shoot! How did you put your code in that nifty box? Mine got a little messed up and it's LONG! I'm such a noob!
GGipson
 
Posts: 11
Joined: Sun Oct 10, 2010 2:59 am
Location: San Marcos, CA

Re: How to control motors with Motor shield using PING sensor

by adafruit_support_bill on Fri Oct 22, 2010 6:34 pm

Look up above the text entry box for the reply. There is a button marked "Code". It preserves the formatting and makes the code easier to read.

adafruit_support_bill
 
Posts: 77290
Joined: Sat Feb 07, 2009 10:11 am

Re: How to control motors with Motor shield using PING sensor

by GGipson on Sat Oct 23, 2010 9:13 pm

Ok thanks!

Code: Select all | TOGGLE FULL SIZE
//Ping motor control

#include <AMotorC.h>

#include <AFMotor.h>

const int PingSensor1 = 4; // Defines ping sensor 1

AF_DCMotor motor1 (1, MOTOR12_1KHZ); //Creates motor 1 at 1khz



void setup()
{
  Serial.begin(9600);// initialize serial comms
}


 

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(PingSensor1, OUTPUT);
  digitalWrite(PingSensor1, LOW);
  delayMicroseconds(2);
  digitalWrite(PingSensor1, HIGH);
  delayMicroseconds(5);
  digitalWrite(PingSensor1, 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(PingSensor1, INPUT);
  duration = pulseIn(PingSensor1, 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);
}

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;
}
 
float distance = readPingSensor1();  // read the sensor and convert into feet
{
   if (distance > 3)  // more than 3 feet
      {
      motor1.run(FORWARD);
      motor1.SetSpeed(255);  // full speed
      }
   else if (distance > 2)  // between 2 and 3 feet
      {
      motor1.run(FORWARD);
      motor1.SetSpeed(127);  // hal;f speed
      }
   else  // closer than 2 feet
      {
      motor1.run(RELEASE);  // stop motors
      }
}


I hope this looks better!
GGipson
 
Posts: 11
Joined: Sun Oct 10, 2010 2:59 am
Location: San Marcos, CA

Re: How to control motors with Motor shield using PING sensor

by adafruit_support_bill on Sun Oct 24, 2010 5:43 am

Code: Select all | TOGGLE FULL SIZE
#include <AMotorC.h>


What library is this? I think you only need to include "AFMotor.h"

Code: Select all | TOGGLE FULL SIZE
float distance = readPingSensor1();  // read the sensor and convert into feet
{
   if (distance > 3)  // more than 3 feet
      {
      motor1.run(FORWARD);
      motor1.SetSpeed(255);  // full speed
      }
   else if (distance > 2)  // between 2 and 3 feet
      {
      motor1.run(FORWARD);
      motor1.SetSpeed(127);  // hal;f speed
      }
   else  // closer than 2 feet
      {
      motor1.run(RELEASE);  // stop motors
      }
}


This bit of code is not a function. Did you intend to put this inside your loop?

adafruit_support_bill
 
Posts: 77290
Joined: Sat Feb 07, 2009 10:11 am

Re: How to control motors with Motor shield using PING sensor

by GGipson on Tue Feb 01, 2011 6:25 pm

After many weeks of hair pulling and coding I found that AFMotor.h library is for stepper motors so now I'm using AFMotorC.h for motor control. I recently opened this library and found that I'm adding code that is already defined by the library. However turning a motor off when a PING sensor detects an object 32 inches or less has still baffled me! What I'm trying to do is write a sketch so that a PING sensor mounted to the right side of my boat will turn off the motor on the back, left so the boat will turn to avoid the object in it's path. and if the PING on the left detects an object 32 inches or less it stops the motor on the right to turn the boat and avoid that object. Here is what I have so for...

Code: Select all | TOGGLE FULL SIZE
//Ping motor control

#include <AMotorC.h>


const int PingSensor1 = 4; // Defines ping sensor 1

AF_DCMotor motor1 (1, MOTOR12_1KHZ); //Creates motor 1 at 1khz



void setup()
{
  Serial.begin(9600);// initialize serial comms
}


 

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(PingSensor1, OUTPUT);
  digitalWrite(PingSensor1, LOW);
  delayMicroseconds(2);
  digitalWrite(PingSensor1, HIGH);
  delayMicroseconds(5);
  digitalWrite(PingSensor1, 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(PingSensor1, INPUT);
  duration = pulseIn(PingSensor1, 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);
}

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;
}
 
float distance = readPingSensor1();  // read the sensor and convert into feet
{
   if (distance > 3)  // more than 3 feet
      {
      motor1.run(FORWARD);
      motor1.SetSpeed(255);  // full speed
      }
   else if (distance > 2)  // between 2 and 3 feet
      {
      motor1.run(FORWARD);
      motor1.SetSpeed(127);  // hal;f speed
      }
   else  // closer than 2 feet
      {
      motor1.run(RELEASE);  // stop motors
      }
}


This is just for one motor for now to keep things simple. lol Any help would be very very much appreciated!
GGipson
 
Posts: 11
Joined: Sun Oct 10, 2010 2:59 am
Location: San Marcos, CA

Re: How to control motors with Motor shield using PING sensor

by adafruit_support_bill on Tue Feb 01, 2011 6:33 pm

After many weeks of hair pulling and coding I found that AFMotor.h library is for stepper motors so now I'm using AFMotorC.h for motor control.
:?:
The AFMotor library handles both DC and Stepper motors. I have never heard of AFMotorC, (or AMotorC as you have it in your code).

adafruit_support_bill
 
Posts: 77290
Joined: Sat Feb 07, 2009 10:11 am

Re: How to control motors with Motor shield using PING sensor

by GGipson on Tue Feb 01, 2011 6:44 pm

I got the libraries here
https://github.com/adafruit/Adafruit-Motor-Shield-library

Their were two folders in this zip folder. AFMotor and AMotorC
OOOps! I guess it's AMotorC!
GGipson
 
Posts: 11
Joined: Sun Oct 10, 2010 2:59 am
Location: San Marcos, CA

Re: How to control motors with Motor shield using PING sensor

by adafruit_support_bill on Tue Feb 01, 2011 6:47 pm

There are two files in the library: AFMotor.cpp and AFMotor.h. AFMotor.h is the one you need to include in your sketch.

Code: Select all | TOGGLE FULL SIZE
#include <AFMotor.h>

adafruit_support_bill
 
Posts: 77290
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.