Familiar Problems with DC Motors and Ultrasonic

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Familiar Problems with DC Motors and Ultrasonic

Postby grinan » Fri Jul 04, 2008 12:54 am

First let me say that my experience with the motor shield has been pretty good up until now.. the first real application for a robot.

The problem I am having right now is that when i run the code below, i will get one ping, the motors will start, and then everything blanks out. This problem is almost exactly what is described in this post.. http://forums.ladyada.net/viewtopic.php?t=6346. Although the poster said he found an answer by adding a release for both motors at the end of the loop().

I attempted to do the same... with less than desirable effects. First i couldn't decide where in my loop() to place the release. No where in my loop() did it seem to make a difference, had the same effects.

I eventually added the release statement to all of my movements.. and that fix just isn't going to work. While it did finally get the bot to move, pan and ping.. it would only roll forward for a second.. then there was a delay.. then it would move.. and a delay.. and move.. etc.. etc.. etc..

Bot's don't look cool when they roll, stop, roll, stop.. etc.

Background on my hardware..
Arduino diecimilla, two small dc motors, which have been used previously with no issues, on a boe bot conversion.
This is a parallax ping))) unit.. with standard futaba unmodified servo for pan.
Running two power supplies.. one for motors, one for arduino.
Have caps installed across my motors at the base.. so interference should not be an issue.

Here is the code that i am currently running:



Code: Select all
#include <ServoTimer1.h>
#include <AFMotor.h>

/*
* My First Robot
*
* This robot drive around and avoids objects with the Ping sonar.
*
*/

AF_DCMotor motor(2, MOTOR12_8KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(4, MOTOR12_8KHZ); // create motor #2, 64KHz pwm



int pingPin = 14;

int servoPin = 9;
int servoRange[] = {550, 2450};  // The min/max pulse lengths to send to the servo
int servoRotateTime = 600; // How long it takes to turn the servo 180 degrees

int dangerDist = 100; // How far should something be before we react?
int servoTime = 700; // How long it takes to turn the servo
int turnTime = 250; // How long it takes to turn 90 degrees with the motors


ServoTimer1 servo1;

// This is called when the Arduino starts up
void setup(){
  Serial.begin(9600);
  printString("Start\n");
 
  motor.setSpeed(230);
motor2.setSpeed(230);


  // Servo
  servo1.attach(servoPin);
  servo1.setMinimumPulse(500);
  servo1.setMaximumPulse(2500);
  turn_servo(100);
}

// Arduino calls this infinite loop after setup()
void loop(){

  int dist = read_ping(); // Read distance ahead

  // There's nothing right in front of us
  if(dist > dangerDist){
    Serial.println("All Good\n");
    drive_straight();
    delay(50);
     
  }
  // Object ahead, find a better route
  else{
    Serial.println("Object Ahead\n");
        change_direction();
    delay(50);
  }

}

// Something is in front of us, so find the best alternate route.
// Look left and right and decide which has more distance to drive.
void change_direction(){
  motor_stop();
 
  // Look left
  turn_servo(180);
  int left = read_ping_avg();
 
  // Look right
  turn_servo(0);
  int right = read_ping_avg();
 
  // Neither are good options, turn around
  if(left < dangerDist &&  right < dangerDist){
    turn_right();
    delay(turnTime * 2);
    drive_straight();
  }
  // Right is better
  else if(left < right){
    turn_right();
    delay(turnTime);
    drive_straight();
  }
  // Left is better
  else {
    turn_left();
    delay(turnTime);
    drive_straight();
  }

}

// Read the distance from the Ping sensor
int read_ping(){
  int timecount = 0;

  /*
   * Send trigger pulse to ping to tell it to start
   */
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(pingPin, HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(pingPin, LOW); // Holdoff
 
  /*
   * Listen for echo pulses
   */
   
  // Wait for input
  int val = digitalRead(pingPin); // Append signal value to val
  pinMode(pingPin, INPUT); // Switch signalpin to input
  while(val == LOW) { // Loop until pin reads a high value
    val = digitalRead(pingPin);
  }
 
  // Count pulses
  while(val == HIGH) { // Loop until pin reads a low value
    val = digitalRead(pingPin);
    timecount = timecount +1;
  }
 
  delay(10);
 
  return timecount;
}

// Get the average distance from 3 pings
int read_ping_avg(){
 
  int total = 0;
  total += read_ping();
  total += read_ping();
  total += read_ping();
 
  int avg = total/3;
 
  return avg;
}

// Turn the servo to a degree between 1 - 180.
void turn_servo(int degree){
  int servoStopAt = 0;
 
  // Set stop time
  if(servoStopAt == 0){
    servoStopAt = millis() + servoRotateTime;
  }
 
  servo1.write(degree);
 
  // Refresh until the servo is done moving
  // (using approximate timing set by servoRotateTime)
  while(servoStopAt > millis()){
    delay(20);
  }
}

/*
* Motor movements
*/

void motor_stop(){
  motor.run(RELEASE);
  motor2.run(RELEASE);
}

void drive_straight(){
  turn_servo(90); // Look straight ahead
 
motor.run(FORWARD);
motor2.run(FORWARD);
delay(100);
motor.run(RELEASE);
motor2.run(RELEASE);

}

void turn_left(){
motor.run(FORWARD);
motor2.run(BACKWARD);
delay(100);
motor.run(RELEASE);
motor2.run(RELEASE);

}

void turn_right(){
motor.run(BACKWARD);
motor2.run(FORWARD);
delay(100);
motor.run(RELEASE);
motor2.run(RELEASE);

}


Any help at all would be much appreciate.. if the original poster that had issues such as this would post his fix, that would be appreciated.. ladyada.. if you have ears on and are listening.. can you throw me a bone? Point a lost person in the right direction?

It seems to me that it's some kind of interrupt problem.. but what do i know..
grinan
 
Posts: 21
Joined: Fri Jul 04, 2008 12:22 am

Postby grinan » Fri Jul 04, 2008 12:41 pm

as a side note on this.. i reprogrammed my bot this morning.. with the arduino, eliminating the motor shield.

Although i had to adjust my code.. and use my own h-bridge, i was able to gain control of the bot, with ping and pan that works.

This bothers me.. I would really rather use the motorshield.. just because it looks better than my h-bridge.. and I trust it for reliability that i don't have with my h-bridge.. plus all the features of controlling steppers, and servos at the same time.

Can anyone help?
grinan
 
Posts: 21
Joined: Fri Jul 04, 2008 12:22 am

Postby adafruit » Fri Jul 04, 2008 12:57 pm

try running everything with the motor shield removed (theres no feedback so it should still 'run')
try running everything but with the motors not installed
try running everything with one motor installed
User avatar
adafruit
 
Posts: 10491
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Postby grinan » Fri Jul 04, 2008 5:57 pm

Thanks for the reply ladyada..

try running everything with the motor shield removed (theres no feedback so it should still 'run')

Not sure what you mean by this.. i did try removing the shield.. and everything stopped... then i figured you meant wiring everything but the motors and servo straight to the arduino.. I did.. and apparently missed something.. because it still wouldn't run. I will try this again, as i think it may very well be an interference issue.. or the fact that my two power supplies don't seem to be isolated from each other enough. Which brings a question up, Do the two servos that the motor shield supplies get their power from the arduino, or do they get it from the motor power supply? If they are supposed to be getting it from the motor supply, i have a problem.. cause with it unplugged, my servo's still move.

try running everything but with the motors not installed

I did.. and it performs the same.. as far as i can tell.. Wired led's to my motor connections.. and they light up.

try running everything with one motor installed

Same performance as when both motors are installed.. very choppy movement.. tried each motor independently.

I have also tried a couple of other fixes, as in changing my code back to the original "digitalwrite" pin high- low h-bridge. When i did this, i found out that i was having some brownout issues when my servo rotated.. Was able to rewire and totally isolate my motors, and servos from the arduino power supply, and had no more issues. This leads me to believe that i may have originally been having brownout issues or that my two power supplies were not isolated well enough.

I will go back to the drawing board.. but please explain a little better what you meant by running everything with the shield removed.. that confused the bejesus outta me.
grinan
 
Posts: 21
Joined: Fri Jul 04, 2008 12:22 am

Postby adafruit » Fri Jul 04, 2008 6:40 pm

i think im not quite sure precisely what is going on.

so you want the robot to constantly be moving the 2 motors
at the same time you'd like to move the servo back and forth and read from the ping sensor
however, the moment it pings the entire system 'crashes'? or does it just 'stop for a while'? how long does it stop for?
use Serial.print() statements to determine exactly where it stops/'crashes'

EDIT: the servos could def be causing a brownout. the servos use the same 5V supply as the arduino. you could put a hefty cap on there or make sure you're not running of something puny like, say, a 9V battery. or alternately put a 7805 on the motor supply to provide a seperate power to the servos.
User avatar
adafruit
 
Posts: 10491
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Postby grinan » Fri Jul 04, 2008 7:06 pm

The end product I am looking for is a roving robot... with the ping ultrasonic panning and pinging for obstacles. It is mainly an educational thing for me... and i am learning a lot.

I have done this with a basic stamp, an h-bridge, and servo with the ping sensor.. but, the basic stamp is so limited in what you can do with it. The next logical step for me, i thought, was the arduino platform.. after reading and reading and reading some more, i decided to give it a try with the help of your motor shield.

I will continue to work on it.. i am 90% sure it has something to do with brownouts. Or interference.. as I can get everything to work seperately, but when i put it all together i start having these issues.

Thanks for your help ladyada.. if nothing else, the comments have made me thing about possible issues.. and i think i am really close to getting it to work.
grinan
 
Posts: 21
Joined: Fri Jul 04, 2008 12:22 am

I sorted it out

Postby ColinEdwardRhodes » Fri Jul 04, 2008 9:32 pm

I found two things in the course of my debugging.

(1) do not run the sensor in a tight loop after using the motor. You need a 1-200ms delay.

(2) the ultrasonic ping hangs are usually a product of voltage dipping.

In short - motors draw amps, this pushes us under 5V and the sensor decides to hang.

With some delays and a good strong battery I have a working platform.

The shield seems fine.

Colin
ColinEdwardRhodes
 
Posts: 7
Joined: Sat Jun 28, 2008 9:12 pm

Postby grinan » Sat Jul 05, 2008 12:52 am

Thanks colin..
I had suspected that voltage amounts might be the problem. Not sure what to do to power it now.. need to find a power source that's small, and outputs enough juice to keep things going.

I appreciate the advice very much.
grinan
 
Posts: 21
Joined: Fri Jul 04, 2008 12:22 am


Return to Arduino Shields from Adafruit

Who is online

Users browsing this forum: No registered users and 2 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [103]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[61]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]