具有C#WF应用程序的Arduino加速度计

时间:2019-10-16 17:24:54

标签: c# arduino mpu6050

我有一个问题是:Arduino将MPU-6050加速度计数据保存到外部SRAM。当我询问时,它会读回阵列并将其串行发送到PC。如果不移动传感器,它完全可以与2加速度计一起使用。如果我移动传感器,则会得到零的lof。这是三张图片:When it works-no moving; When it doesn't work Only serial plot from raw data

通过简单的序列图对传感器进行测试。完美的作品。如果我测量几乎恒定的重力,它就可以工作。如果我开始缓慢移动传感器,我会在第二张图片中看到类似的东西。

如果我不使用C#程序,请仅使用Arduino自己的串行监视器。我将数字读入数组(以下称为:缓冲区)。 Serial.println一一列出所有数字。无论我是否移动传感器,它都可以工作。我不明白为什么我的C#代码和绘图仅在传感器不移动时才能起作用?采样率是相同的,零,-2056、11562等...所有数字都是16位有符号整数。

有人知道吗? 谢谢。: 胖子

#include<SRAM_23LC.h> //Copyright (c) 2017, JustinMattair(justin@mattair.net)
#include<Wire.h>
#include<SPI.h>
//Sram settings
#define SPI_PERIPHERAL    SPI
#define CHIP_SELECT_PIN   27
extern TwoWire Wire1;
int16_t b;

SRAM_23LC SRAM(&SPI_PERIPHERAL, CHIP_SELECT_PIN, SRAM_23LCV512);


const int MPU_addr = 0x68;
int16_t Raw1, Raw2;
const int16_t prec = 2048;
byte rl1, rh1, rl2, rh2;
int led1Pin = 50;
int led2Pin = 51;
int led3Pin = 52;
String inputString = "";
boolean stringComplete = false;
String commandString = "";
boolean isConnected = false;
int bite = 16384;
int16_t raw2;
//byte buf2[8192];

void setup() {
  //----------------------------
 pinMode(50, OUTPUT);
 pinMode(51, OUTPUT);
 pinMode(52, OUTPUT);
 //----------------------------
 Wire.begin();
 Wire.setClock(100000);
 Wire.beginTransmission(MPU_addr);
 Wire.write(0x6B);                         //PWR_MGMT_1 register
 Wire.write(0);                            //set to zero in order to wake up MPU6050
 Wire.endTransmission(true);
 Wire.beginTransmission(0b1101000);        // I2C adress of MPU6050
 Wire.write(0x1C);                         //Accelerometer config
 //Wire.write(0b00000000);                   //Setting the accel to +/- 2g
 //Wire.write(0b00001000);                   //Setting the accel to +/- 4g
 //Wire.write(0b00010000);                   //Setting the accel to +/- 8g
 Wire.write(0b00011000);                   //Setting the accel to +/- 16g
 Wire.endTransmission();
 //---------------------------
 Wire1.begin();
 Wire1.setClock(100000);
 Wire1.beginTransmission(MPU_addr);
 Wire1.write(0x6B);                         //PWR_MGMT_1 register
 Wire1.write(0);                            //set to zero in order to wake up MPU6050
 Wire1.endTransmission(true);
 Wire1.beginTransmission(0b1101000);        // I2C adress of MPU6050
 Wire1.write(0x1C);
 //Wire1.write(0b00000000);                     // Setting to +- 2g
 Wire1.write(0b00011000);                   //Setting the accel to +/- 16g
 Wire1.endTransmission();

 SRAM.begin(20000000UL);                    // 10Mhz-el megnyitva az spi max20MHz
 Serial.begin(115200);
 digitalWrite(led3Pin, HIGH);

}

void loop() {
  if (stringComplete) {
    stringComplete = false;
getCommand();
if (commandString.equals("START")) {
  digitalWrite(led3Pin, LOW);
  digitalWrite(led1Pin, HIGH);
  for (int i = 0; i < bite / 4; i++) {
    Raw1 = olvas1();
    Raw2 = olvas2();
    //rl2 = lowByte(Raw2);         //Szetosztas 2 byte-ra
    //rh2 = highByte(Raw2);
    rl1 = Raw1 & 0xff;
    rh1 = (Raw1 >> 8);
    rl2 = Raw2 & 0xff;
    rh2 = (Raw2 >> 8);
    SRAM.writeByte(4 * i, rl1);
    SRAM.writeByte(4 * i + 1, rh1);
    SRAM.writeByte(4 * i + 2, rl2);
    SRAM.writeByte(4 * i + 3, rh2);
  }
  digitalWrite(led1Pin, LOW);
  digitalWrite(led3Pin, HIGH);
}
if (commandString.equals("IMPOR")) {
  digitalWrite(led2Pin, HIGH);
  digitalWrite(led3Pin, LOW);
  byte buf1[8192];
  for (int i = 0; i < bite / 4; i++) {
    byte lb1 = SRAM.readByte(4 * i);
    byte hb1 = SRAM.readByte(4 * i + 1);
    //raw2 = word(hb, lb);
    buf1[2 * i] = lb1;
    buf1[2 * i + 1] = hb1;
  }
  Serial.write(buf1, sizeof(buf1));
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, HIGH);
}
if (commandString.equals("IMPOT")) {
  digitalWrite(led2Pin, HIGH);
  digitalWrite(led3Pin, LOW);
  byte buf2[8192];
  for (int i = 0; i < bite / 4; i++) {
    byte lb2 = SRAM.readByte(4 * i + 2);
    byte hb2 = SRAM.readByte(4 * i + 3);
    buf2[2 * i] = lb2;
    buf2[2 * i + 1] = hb2;
  }
  Serial.write(buf2, sizeof(buf2));
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, HIGH);

}
inputString = "";
 }

}


int16_t olvas1() {
  int16_t a;
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3F);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr, 2, true);
  a = Wire.read() << 8 | Wire.read();
  return a;
}

int16_t olvas2() {
  Wire1.beginTransmission(MPU_addr);
  Wire1.write(0x3F);
  Wire1.endTransmission(false);
  Wire1.requestFrom(MPU_addr, 2, true);
  b = Wire1.read() << 8 | Wire1.read();
  return b;
}

void getCommand() {
  if (inputString.length() > 0) {
  commandString = inputString.substring(1, 6);
  }
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

0 个答案:

没有答案