stanleyhuangyc / ArduinoOBD

OBD-II library and sketches for Arduino
http://freematics.com
968 stars 519 forks source link

OBD-II UART Adapter V2.1 says haven't MEMS #56

Closed ivanpajon closed 6 years ago

ivanpajon commented 6 years ago

Function memsInit returns false when I initialize it and I don't know why.

bool COBD::memsInit(bool fusion)
{
    char buf[16];

    if (sendCommand("ATTEMP\r", buf, sizeof(buf)) <= 0 || strchr(buf, '?'))
        return false;
    if (fusion) {
        m_fusion = true;
        return sendCommand("ATQU1\r", buf, sizeof(buf));
    } else {
        if (m_fusion) {
            m_fusion = false;
            sendCommand("ATQU0\r", buf, sizeof(buf));
        }
        return true;
    }
}

I checked it and it always perform first IF and rest doesn't.

if (sendCommand("ATTEMP\r", buf, sizeof(buf)) <= 0 || strchr(buf, '?'))
        return false;

I'm testing with orientation_demo included in ArduinoOBD libraries and MEMS still doesn't work, other functions like readPID works fine.

#include <OBD2UART.h>

// On Arduino Leonardo, Micro, MEGA or DUE, hardware serial can be used for output as the adapter occupies Serial1
// On Arduino UNO and those have no Serial1, we use software serial for output as the adapter uses Serial
#ifdef ARDUINO_AVR_UNO
#include <SoftwareSerial.h>
SoftwareSerial mySerial(A2, A3);
#else
#define mySerial Serial
#endif

#if defined(ESP32) && !defined(Serial1)
HardwareSerial Serial1(1);
#endif

COBD obd;
bool hasMEMS;

void setup()
{
  mySerial.begin(115200);
  while (!mySerial);

  // this will begin serial

  for (;;) {
    delay(1000);
    byte version = obd.begin();
    mySerial.print("Freematics OBD-II Adapter ");
    if (version > 0) {
      mySerial.println("detected");
      break;
    } else {
      mySerial.println("not detected");
    }
  }

  // initialize MEMS with sensor fusion enabled
  hasMEMS = obd.memsInit(true);
  mySerial.print("MEMS:");
  mySerial.println(hasMEMS ? "OK" : "NO");
  if (!hasMEMS) {
    for (;;);
  }
}

void loop()
{
  int16_t acc[3] = {0};
  int16_t gyro[3] = {0};
  int16_t mag[3] = {0};

  if (!obd.memsRead(acc, gyro, mag)) return;

  mySerial.print("ACC:");
  mySerial.print(acc[0]);
  mySerial.print('/');
  mySerial.print(acc[1]);
  mySerial.print('/');
  mySerial.print(acc[2]);

  mySerial.print(" GYRO:");
  mySerial.print(gyro[0]);
  mySerial.print('/');
  mySerial.print(gyro[1]);
  mySerial.print('/');
  mySerial.print(gyro[2]);

  mySerial.print(" MAG:");
  mySerial.print(mag[0]);
  mySerial.print('/');
  mySerial.print(mag[1]);
  mySerial.print('/');
  mySerial.print(mag[2]);

  mySerial.println();

  float yaw, pitch, roll;
  if (obd.memsOrientation(yaw, pitch, roll)) {
    mySerial.print("Orientation: ");
    mySerial.print(yaw, 2);
    mySerial.print(' ');
    mySerial.print(pitch, 2);
    mySerial.print(' ');
    mySerial.println(roll, 2);
  }

  delay(100);
}

I need some help please. Thanks.

ivanpajon commented 6 years ago

There was a packaging issue and I received OBD-II UART Adapter V1 instead of V2.1.