Arduino MEGA:使用软件串行使GPS和IMU协同工作

时间:2018-06-21 23:01:01

标签: arduino gps serial-port adafruit software-serial

我已经将IMU (Adafruit Precision NXP 9-DOF Breakout Board - FXOS8700 + FXAS21002)和GPS (Ultimate Breakout v3)设备分开工作,现在我正在尝试使用相同的Arduino MEGA使它们一起工作。我的一个标准是,我只需要使用一根USB电缆和一根arduino即可将信息从IMU和GPS传输到笔记本电脑上。

我正在使用软件串行来尝试立即使它工作,但是由于某种原因,单独工作的2条代码无法一起工作。这是包含循环和设置功能的代码:

// this software serial code is based off the TwoPortReceive code by Tom Igoe
#include <Adafruit_GPS.h>
#include <Adafruit_FXAS21002C.h>
#include <Adafruit_FXOS8700.h>
#include <Wire.h>
#include <SoftwareSerial.h>

SoftwareSerial IMU(20, 21);
SoftwareSerial GPS(18, 19);

void setup() {
  Serial.begin(9600);
  while (!Serial) {}

  // Start each software serial port
  IMU.begin(9600);
  GPS.begin(9600);
  displaySensorDetails();

}

void loop() {
  IMU.listen();
  Serial.println("Data from port one:");
  while (IMU.available() > 0) {
    char inByte = IMU.read();
    Serial.write(inByte);
  }
  imuloop();

  GPS.listen();
  Serial.println("Data from port two:");
  while (GPS.available() > 0) {
    char inByte = GPS.read();
    Serial.write(inByte);
  }
  gpsloop();
}

gpsloop()代码在这里:

#define GPSSerial Serial2
HardwareSerial mySerial = Serial1;

void gpsloop(){
  if (Serial.available()) {
    char c = Serial.read();
    GPSSerial.write(c);
  }
  if (GPSSerial.available()) {
    char c = GPSSerial.read();
    Serial.write(c);
  }
}

同样,我的imuloop()函数位于此另一个文件中(仍然是相同的草图):

Adafruit_FXAS21002C gyro      = Adafruit_FXAS21002C(0x0021002C);
Adafruit_FXOS8700   accel_mag = Adafruit_FXOS8700(0x8700A, 0x8700B);
sensors_event_t aevent, gevent, mevent;

void imuloop(void){
    String readString;
    while (Serial.available()){
      delay(3); //allowing buffer to fill
      if (Serial.available() > 0){
        char c = Serial.read();
        readString += c;
      }
    }
    if (readString.length() > 0){
      printToSerial();
    }
}


void printToSerial(void){
  // Get a new sensor event
  gyro.getEvent(&gevent);
  accel_mag.getEvent(&aevent, &mevent);

  // Display the gyro results [x|y|z] (rad/s)
  Serial.print(gevent.gyro.x); Serial.print("|");
  Serial.print(gevent.gyro.y); Serial.print("|");
  Serial.print(gevent.gyro.z);

  Serial.print(":");

  // Display the accel results [x|y|z] (m/s^2)
  Serial.print(aevent.acceleration.x, 4); Serial.print("|");
  Serial.print(aevent.acceleration.y, 4); Serial.print("|");
  Serial.print(aevent.acceleration.z, 4);

  Serial.println("");

  // Final data format: gX|gY|gZ:aX|aY|aZ
  delay(1);
}

我只是想让他们都在这里工作,这让我发疯。我知道TX和RX不能同时与Software Serial一起使用,但是我真的只需要一次从GPS / IMU接收信息。预先感谢您的帮助!

0 个答案:

没有答案