Hi,
I am a beginner in arduino programming, I have this code below
#include <MPU9250_WE.h>
//#include "I2Cdev.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
#define MPU9250_ADDR 0x68
MPU9250_WE myMPU9250 = MPU9250_WE(MPU9250_ADDR);
//float data[11];
unsigned long runTime;
RF24 radio(7, 8); //指定 Arduino Nano 腳位對應 nRF24L01 之 (CE, CSN)
const byte address[6] = "1andy"; //節點位址為 5 bytes + \0=6 bytes
typedef struct
{
short gyrRawx;
short gyrRawy;
short gyrRawz;
long accRawx;
long accRawy;
long accRawz;
unsigned long runTime;
short magValuex;
short magValuey;
short magValuez;
}value;
value data;
void setup() {
Serial.begin(115200);
Wire.begin();
if(!myMPU9250.init()){
Serial.println("MPU9250 does not respond");
}
else{
Serial.println("MPU9250 is connected");
}
if(!myMPU9250.initMagnetometer()){
Serial.println("Magnetometer does not respond");
}
else{
Serial.println("Magnetometer is connected");
}
Serial.println("Position you MPU9250 flat and don't move it - calibrating...");
delay(1000);
myMPU9250.autoOffsets();
Serial.println("Done!");
myMPU9250.setSampleRateDivider(20);
myMPU9250.setSampleRateDivider(20);
myMPU9250.setAccRange(MPU9250_ACC_RANGE_2G);
myMPU9250.setGyrRange(MPU9250_GYRO_RANGE_250);
myMPU9250.enableAccDLPF(true);
myMPU9250.enableGyrDLPF();
myMPU9250.setAccDLPF(MPU9250_DLPF_6);
myMPU9250.setGyrDLPF(MPU9250_DLPF_6);
myMPU9250.setMagOpMode(AK8963_CONT_MODE_100HZ);
radio.begin(); //初始化 nRF24L01 模組
radio.openWritingPipe(address); //開啟寫入管線
radio.setPALevel(RF24_PA_MIN); //設為低功率, 預設為 RF24_PA_MAX
radio.setChannel(108); //設定頻道=108 (0~125, 較高的頻道似乎比較 open)
//radio.setDataRate(RF24_2MBPS);
radio.stopListening(); //傳送端不需接收, 停止傾聽
delay(200);
}
void loop() {
xyzFloat gyrRaw = myMPU9250.getCorrectedGyrRawValues();
xyzFloat magValue = myMPU9250.getMagValues(); // returns magnetic flux density [µT]
xyzFloat accRaw = myMPU9250.getAccRawValues();
//float resultantG = myMPU9250.getResultantG(gValue);
gyrRaw.x=gyrRaw.x*100;
gyrRaw.y=gyrRaw.y*100;
gyrRaw.z=gyrRaw.z*100;
data.gyrRawx=short(gyrRaw.x);
data.gyrRawy=short(gyrRaw.y);
data.gyrRawz=short(gyrRaw.z);
data.accRawx=long(accRaw.x);
data.accRawy=long(accRaw.y);
data.accRawz=long(accRaw.z);
magValue.x=magValue.x*100;
magValue.y=magValue.y*100;
magValue.z=magValue.z*100;
data.magValuex=short(magValue.x);
data.magValuey=short(magValue.y);
data.magValuez=short(magValue.z);
data.runTime=millis(); // the time when the data was ready to be sent
Serial.println(data.runTime);
radio.write(&data, sizeof(data));
}
Here I send the mpu9250 sensor data through NRF24L01, which includes the time when the data was ready to be sent. When I check the delay time between two data with Serial.println, the delay time is about 4 millisecond to 6 millisecond, the time series is like this, 28798, 28803, 28808, 28812, 28816..., but when I checked the delay time at the received side, the delay is like this, 28798, 28803, 28808, 55278, 55282, 55287. It's obvious that there were data lossing, so is there any solution to solve this data lossing problem ? Or if there is any wireless transceiver, receiver module which is easy to use, and work better than NRF24L01?