Hey everyone, I'm new to Arduino and this forum also. So much so in fact that Im not how to start a new discussion. I am trying to send 6 axis IMU data from a MEGA to a nano using the NRF24.
Receiver code...
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8 ); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
uint8_t pitchYaw[2]; //my attempt at an array
void setup() {
Serial.begin(115200);
radio.begin();
radio.openWritingPipe(addresses[0]); // 00001
radio.openReadingPipe(1, addresses[1]); // 00002
radio.setPALevel(RF24_PA_MIN);
}
void loop() {
radio.startListening();
if ( radio.available()) {
while (radio.available()) {
radio.read( &pitchYaw[2], sizeof(pitchYaw[2]));//my attempt at an array
Serial.println(pitchYaw[2]);//my attempt at an array
}
}
}
Transmitter code...
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8 ); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
uint8_t pitchYaw[2]; //my attempt at an array
MPU6050 mpu;
#define OUTPUT_READABLE_QUATERNION
#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
bool blinkState = false;
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer
Quaternion q; // [w, x, y, z] quaternion container
VectorInt16 aa; // [x, y, z] accel sensor measurements
VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements
VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements
VectorFloat gravity; // [x, y, z] gravity vector
float euler[3]; // [psi, theta, phi] Euler angle container
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
uint8_t teapotPacket[14] = { '$', 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\r', '\n' };
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}
void setup() {
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
//Serial.begin(115200);
//while (!Serial); // wait for Leonardo enumeration, others continue immediately
mpu.initialize();
devStatus = mpu.dmpInitialize();
mpu.setXGyroOffset(220);
mpu.setYGyroOffset(76);
mpu.setZGyroOffset(-85);
mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
if (devStatus == 0) {
mpu.setDMPEnabled(true);
attachInterrupt(0, dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();
dmpReady = true;
packetSize = mpu.dmpGetFIFOPacketSize();
//} else {
//Serial.print(F("DMP Initialization failed (code "));
//Serial.print(devStatus);
//Serial.println(F(")"));
}
// configure LED for output
pinMode(LED_PIN, OUTPUT);
radio.begin();
radio.openWritingPipe(addresses[1]); // 00002
radio.openReadingPipe(1, addresses[0]); // 00001
radio.setPALevel(RF24_PA_MIN);
}
void loop() {
if (!dmpReady) return;
while (!mpuInterrupt && fifoCount < packetSize) {
}
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();
// get current FIFO count
fifoCount = mpu.getFIFOCount();
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
mpu.resetFIFO();
} else if (mpuIntStatus & 0x02) {
// wait for correct available data length, should be a VERY short wait
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
mpu.getFIFOBytes(fifoBuffer, packetSize);
fifoCount -= packetSize;
#ifdef OUTPUT_READABLE_QUATERNION
// display quaternion values in easy matrix form: w x y z
mpu.dmpGetQuaternion(&q, fifoBuffer);
//Serial.print(q.w);
//Serial.print(" ");
//Serial.print(q.x);
//Serial.print(" ");
//Serial.print(q.y);
//Serial.print(" ");
//Serial.print(q.z);
//Serial.print(" ");
//Serial.println("end");
#endif
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}
radio.stopListening();
pitchYaw[0] = (q.x);//my attempt at an array
pitchYaw[1] = (q.z);//my attempt at an array
//int angleValue = map(XVAL, 0, 1023, 0, 180);
//int angleValue = map(YVAL, 0, 1023, 0, 180);
radio.write(&pitchYaw[2], sizeof(pitchYaw[2]));//my attempt at an array
//Serial.println(q.x, q.z);
}
The IMU works and sends out valid data on the nano's serial port. I modified the receiver code from an example sketch and tested both transmit (on the NANO) and receive (on the MEGA) with sending a "hello world" message to make sure the boards are communicating through the NRF's.
Im pretty sure the problem lies in the use of the array i have named as pitchYaw in the code. Everything uploads with no errors but I have tried using the array i can't seem to get anything out on the receiver's serial monitor. The array may not be the way to go, but all i need is to be able to get the IMU data over and I can finish this thing up. I imagine the solution is really simple but i am stuck, please help :o