Pages: [1]   Go Down
Author Topic: Se me bloquea los servos al incorporar GPS  (Read 555 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Buenos dias,

Estoy haciendo un robot explorador autonomo, dicho robot, lleva incorporado un modulo gps, un modulo sd (para guardar el track por donde va circulando), y un par de servos que uno se encarga de llevar la direccion mediante una rueda loca y el otro se encarga de mover el sensor de proximidad para detectar objetos. Si trabajo con el modulo gps, sd por separado, funciona correctamente y si trabajo con los servos y el sensor de proximidad tambien funciona correctamente, el problema bien cuando junto el codigo que el gps bloquea literalmente el funcionamiento de los servos y el sensor de proximidad. Alguien sabe porque pasa esto?
Otro dato que se me olvida es que utilizo un placa Arduino Mega 2560 ADK for android.

Gracias!!
Logged

Donostia
Offline Offline
God Member
*****
Karma: 0
Posts: 740
elektronikadonbosco
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

¿Estas alimentando todo tus componentes desde la placa Arduino o tienes tu propio regulador?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hola!

Te explico, alimento la placa de arduino con pilas. Luego desde los pines de 5V y GND saco su positivo y negativo respectivamente y los conecto a un pcb donde conecto todos los sensores, servos, modulos etc... que requieren esos 5V.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

He probado alimentar los circuitos a parte y que la conexion de informacion se haga mediante Arduino, es decir, que los pines de 5V y GND de la placa no los utilizo. Os voy adjuntar el codigo haber si es problema de codigo pero no lo creo...

// Librerias para el GPS, modulo SD y servo
#include <SoftwareSerial.h>
#include <SD.h>
#include <Servo.h>

// Definicion de constantes
#define rxPin 4
#define txPin 2
#define MEM_PW 8

// Definicion de variables
const int pingPin = 3;
int pos = 0;
int i = 0;
int indices[13];
int cont = 0;
int conta = 0;
int k = 0;
char inBuffer[300] = "";
char *p;
byte byteGPS = 0;

SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
File myFile;
Servo myservo, myservo2;

void setup() {
 
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  myservo.attach(5);
  myservo2.attach(6);
 
  //setup for mySerial port
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  mySerial.begin(4800);

  //setup for Serial port
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  pinMode(MEM_PW, OUTPUT);
  digitalWrite(MEM_PW, HIGH);

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  if (SD.exists("GPS1.csv")) {
    Serial.println("exists.");
  }
  else {
    Serial.println("doesn't exist.");
  }

  // open a new file and immediately close it:
  // setup the GPS module en la posicion
  Serial.println("Configurando GPS...");
  delay(1000);
  mySerial.println("$PSTMNMEACONFIG,0,4800,64,1");     // configure NMEA sentences to show only GGA sentence
  delay(100);

  // command for setting time and position
  mySerial.println("$PSTMINITGPS,4315.280,N,0255.267,W,0016.0,24,01,2008,17,15,00");
}

void loop()
{
    for(int j = 0; j<=1000;j++)
  {
   // nothing happens after setup finishes.
  byteGPS = 0;
  i = 0;
  while(byteGPS != 42){          // read the GGA sentence
    byteGPS = mySerial.read();
    inBuffer=byteGPS;
    i++;
  }
  k = 0;
//Serial.println("Escribiendo en el fichero...");
  //while(inBuffer[k] != 42){
      myFile = SD.open("GPS1.CSV", FILE_WRITE); //{
     
      //Serial.println(inBuffer[k]); // write the GGA sentence
      //inBuffer[k]= p[k];
     
      //myFile.print(inBuffer[k]);
      if (strncmp(inBuffer, "$GPRMC",6) == 0)
      {
       Serial.print(inBuffer[k]);
       /*if(inBuffer[k]=='W')
       {
         Serial.println();
       }*/
        Serial.print(inBuffer[7]);
        Serial.print(inBuffer[8]);
        Serial.print(':');
        Serial.print(inBuffer[9]);
        Serial.print(inBuffer[10]);
        Serial.print(':');
        Serial.print(inBuffer[11]);
        Serial.print(inBuffer[12]);
        Serial.print(inBuffer[17]);
        Serial.print(inBuffer[20]);
        Serial.print(inBuffer[21]);
        Serial.print(inBuffer[22]);
        Serial.print(inBuffer[23]);
        Serial.print(inBuffer[24]);
        Serial.print(inBuffer[25]);
        Serial.print(inBuffer[26]);
        Serial.print(inBuffer[27]);
        Serial.print(inBuffer[28]);
        Serial.print(inBuffer[31]);
        Serial.print(inBuffer[32]);
        Serial.print(inBuffer[33]);
        Serial.print(inBuffer[34]);
        Serial.print(inBuffer[35]);
        Serial.print(inBuffer[36]);
        Serial.print(inBuffer[37]);
        Serial.print(inBuffer[38]);
        Serial.print(inBuffer[39]);
        Serial.println();
         myFile.print(inBuffer[7]);
        myFile.print(inBuffer[8]);
        myFile.print(':');
        myFile.print(inBuffer[9]);
        myFile.print(inBuffer[10]);
        myFile.print(':');
        myFile.print(inBuffer[11]);
        myFile.print(inBuffer[12]);
        myFile.print(inBuffer[17]);
        myFile.print(inBuffer[20]);
        myFile.print(inBuffer[21]);
        myFile.print(inBuffer[22]);
        myFile.print(inBuffer[23]);
        myFile.print(inBuffer[24]);
        myFile.print(inBuffer[25]);
        myFile.print(inBuffer[26]);
        myFile.print(inBuffer[27]);
        myFile.print(inBuffer[28]);
        myFile.print(inBuffer[31]);
        myFile.print(inBuffer[32]);
        myFile.print(inBuffer[33]);
        myFile.print(inBuffer[34]);
        myFile.print(inBuffer[35]);
        myFile.print(inBuffer[36]);
        myFile.print(inBuffer[37]);
        myFile.print(inBuffer[38]);
        myFile.print(inBuffer[39]);
      //  myFile.print(inBuffer[40]);
      //  myFile.print(inBuffer[41]);
        myFile.println();
      }
myFile.close();
      //Serial.println("done.");
    //}
    /*else {
      // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
    }*/
    k++;
   

    // 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);
    if(cm<=70)
    {
//      digitalWrite(13,HIGH);
      myservo.write(130);
      delay(1000);
      long durationI, inchesI, cmI;

      // 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);
      durationI = pulseIn(pingPin, HIGH);

      // convert the time into a distance
      inchesI = microsecondsToInches(durationI);
      cmI = microsecondsToCentimeters(durationI);
      delay(100);

      //digitalWrite(13,LOW);
      myservo.write(30);
      delay(1000);

      long durationD, inchesD, cmD;

      // 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);
      durationD = pulseIn(pingPin, HIGH);

      // convert the time into a distance
      inchesD = microsecondsToInches(durationD);
      cmD = microsecondsToCentimeters(durationD);
     
      myservo.write(100);
      delay(1000);
   
      delay(100);
      if(cmI<cmD)
      {
        Serial.println("girar izquierda");
        myservo2.write(140);
        delay(1000);
      }
      if(cmI>=cmD)
      {
        Serial.println("girar derecha");
              digitalWrite(13,HIGH);
        myservo2.write(60);
        delay(1000);
      }
    }
    if(cm>=71)
    {
      //myservo.write(100);
      myservo2.write(100);
            digitalWrite(13,LOW);
    }
  }
}

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;
}
Logged

Pages: [1]   Go Up
Jump to: