在arduino uno上一起使用超声波传感器HC-SR04和Gps(neogps 6m)

时间:2018-04-11 19:14:16

标签: arduino-uno

我正在尝试从传感器和gps读取数据(逐个可以)。传感器单独工作良好,但超声波传感器不提供任何输出。我是arduino的新手,所以我只是使用NewPing Library和TinyGPS库混合了两个例子中的代码。这是代码。请建议需要对代码进行哪些添加才能使两个设备协同工作。

/*********************
 *10 to GPS Module TX*
 *09 to GPS Module RX*
 *********************/
//  1.TESTED USING LED
// 2. added ultrasound libraries 
 #include <NewPing.h>

#define TRIGGER_PIN  5 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     4 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

#include <SoftwareSerial.h>
#include <TinyGPS.h>



SoftwareSerial mySerial(10, 11);

TinyGPS gps;

float gpsdump(TinyGPS &gps);


void setup()  
{
  // Oploen serial communications and wait for port to open:
  Serial.begin(9600);

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  delay(1000);




}

void loop() // run over and over
{
  bool newdata = false;
  unsigned long start = millis();
  // Every 5 seconds we print an update
  while (millis() - start < 5000) 
  {
    if (mySerial.available()) 

    {
      char c = mySerial.read();
      //Serial.print(c);  // uncomment to see raw GPS data
      if (gps.encode(c)) 
      {
        newdata = true;
        break;  // uncomment to print new data immediately!
      }
    }
  }

  if (newdata) 
  {
    Serial.println("Acquired Data");
    Serial.println("-------------");
    gpsdump(gps);

    Serial.println("-------------");
    Serial.println();
  }

}


float gpsdump(TinyGPS &gps)
{

  // On Arduino, GPS characters may be lost during lengthy Serial.print()
  // On Teensy, Serial prints to USB, which has large output buffering and
  //   runs very fast, so it's not necessary to worry about missing 4800
  //   baud GPS characters.


  Serial.println("speed");
   Serial.println(gps.f_speed_kmph()) ;
   Serial.print(sonar.ping_cm());
  ;

}

1 个答案:

答案 0 :(得分:0)

主要问题:

  • 如果不处理字符,则不能等待5秒钟。 Arduino接收缓冲区只有64个字符的空间。 GPS设备可能在此期间发送了5000个字符,因此大多数都会被丢弃。这可以防止GPS库永远解析一个完整的句子。

  • ping会干扰软件串口。您必须等待GPS quiet time执行ping操作。否则,ping进程将导致字符丢失。

其他问题:

  • 您正在打印速度值,即使它可能无效。如果您没有移动,或者您没有良好的卫星接收,GPS设备可能无法提供速度。

  • Arduino millis()时钟不会与GPS时钟同步。您可以将GPS更新用作精确 1秒钟。在他们到达时简单地计算5个修复,这意味着已经过了5秒。

  • 您应该使用其他串行端口和/或库。 This answer描述了各种选择:HardwareSerial(即引脚0和1上的SerialAltSoftSerial(UNO上的8&amp; 9)或NeoSWSerial (任意两个引脚)。

以下是草图的NeoGPS版本,可解决这些问题:

/*********************
 *10 to GPS Module TX*
 *09 to GPS Module RX*
 *********************/
//  1.TESTED USING LED
// 2. added ultrasound libraries 
 #include <NewPing.h>

#define TRIGGER_PIN  5 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     4 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

#include <NeoSWSerial.h>
#include <NMEAGPS.h>


NeoSWSerial gpsPort(10, 11);

NMEAGPS gps;          // the parser
gps_fix fix;          // all the parsed values from GPS
uint8_t fixCount = 0; // a one-second "clock"

float gpsdump();


void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  // set the data rate for the SoftwareSerial port
  gpsPort.begin(9600);
  delay(1000);

}



void loop() // run over and over
{
  // Check for available GPS characters and parse them
  if (gps.available( gpsPort ))
  {
    //  Once per second, a complete fix structure is ready.
    fix = gps.read();
    fixCount++;

    //  The GPS device is going to be quiet for a while,
    //     *now* is a good time to do a ping.
    Serial.print( "ping " );
    Serial.println( sonar.ping_cm() );

    // Every 5 seconds we print an update
    if (fixCount >= 5)
    {
      fixCount = 0; // reset counter

      Serial.println("Acquired Data");
      Serial.println("-------------");
      gpsdump();
      Serial.println("-------------");
      Serial.println();
    }
  }
}


float gpsdump()
{
  // On Arduino, GPS characters may be lost during lengthy Serial.print()
  // On Teensy, Serial prints to USB, which has large output buffering and
  //   runs very fast, so it's not necessary to worry about missing 4800
  //   baud GPS characters.

  Serial.println("speed ");
  if (fix.valid.speed)
    Serial.println( fix.speed_kph() );

}

如果您想尝试,可以从IDE库管理器中的菜单草图 - &gt;下获得NeoGPS,AltSoftSerial和NeoSWSerial。包含图书馆 - &gt;管理图书馆。 NeoGPS比所有其他库更小,更快,更可靠,更准确。

即使您不使用它,安装和故障排除页面上也有许多建议。