在arduino uno上一起使用超声波传感器HC-SR04和Gps(neogps 6m)
Using Ultrasonic sensor HC-SR04 and Gps (neogps 6m) together on arduino uno
我正在尝试从传感器和 GPS 读取数据(一个一个地读)。传感器单独工作良好,但超声波传感器不提供任何输出。我是 arduino 的新手,所以我只是使用 NewPing 库和 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());
;
}
主要问题:
您不能等待 5 秒而不处理字符。 Arduino 接收缓冲区只有 64 个字符的空间。 GPS 设备在那段时间可能发送了 5000 个字符,因此其中大部分将被丢弃。这会阻止 GPS 库永远 解析一个完整的句子。
ping 会干扰软件串口。您将不得不等待 GPS quiet time 执行 ping。否则ping过程会导致字符丢失。
其他问题:
您正在打印速度值,尽管它可能无效。如果您没有移动,或者卫星接收信号不好,则 GPS 设备可能无法提供速度。
Arduino millis()
时钟将不会与 GPS 时钟同步。您可以将 GPS 更新用作 exact 1 秒时钟。简单地计算 5 个修复,这意味着已经过去了 5 秒。
你应该使用不同的串口and/or库。
This answer 描述了各种选择:HardwareSerial
(即引脚 0 和 1 上的 Serial
)、AltSoftSerial
(UNO 上的 8 和 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() );
}
如果您想尝试,NeoGPS、AltSoftSerial 和 NeoSWSerial 可以从 IDE 库管理器中获得,在菜单 Sketch -> Include Library -> Manage Libraries. NeoGPS 比所有其他库更小、更快、更可靠和更准确。
即使您不使用它,安装和故障排除页面上也有很多建议。
我正在尝试从传感器和 GPS 读取数据(一个一个地读)。传感器单独工作良好,但超声波传感器不提供任何输出。我是 arduino 的新手,所以我只是使用 NewPing 库和 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());
;
}
主要问题:
您不能等待 5 秒而不处理字符。 Arduino 接收缓冲区只有 64 个字符的空间。 GPS 设备在那段时间可能发送了 5000 个字符,因此其中大部分将被丢弃。这会阻止 GPS 库永远 解析一个完整的句子。
ping 会干扰软件串口。您将不得不等待 GPS quiet time 执行 ping。否则ping过程会导致字符丢失。
其他问题:
您正在打印速度值,尽管它可能无效。如果您没有移动,或者卫星接收信号不好,则 GPS 设备可能无法提供速度。
Arduino
millis()
时钟将不会与 GPS 时钟同步。您可以将 GPS 更新用作 exact 1 秒时钟。简单地计算 5 个修复,这意味着已经过去了 5 秒。你应该使用不同的串口and/or库。 This answer 描述了各种选择:
HardwareSerial
(即引脚 0 和 1 上的Serial
)、AltSoftSerial
(UNO 上的 8 和 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() );
}
如果您想尝试,NeoGPS、AltSoftSerial 和 NeoSWSerial 可以从 IDE 库管理器中获得,在菜单 Sketch -> Include Library -> Manage Libraries. NeoGPS 比所有其他库更小、更快、更可靠和更准确。
即使您不使用它,安装和故障排除页面上也有很多建议。