Per non perdere tempo sto cercando altre soluzioni come quella di decidere di usare questa libreria per leggere i dati della IMU (invece che usarla per gli encoder, se proprio non riuscissi a leggere i dati in entrambi i versi), ma il problema qui è che dopo il primo interrupt il codice si pianta:
PRIMA PARTE
#include <PinChangeInt.h>
#include <SPI.h>
#include <math.h>
#define OUTPUT_READABLE_ROLLPITCHYAW
#define PIN1 14 //voglio usare un pin A0 come interrupt
// MPU 6000 registers
#define MPUREG_WHOAMI 0x75
#define MPUREG_SMPLRT_DIV 0x19
#define MPUREG_CONFIG 0x1A
#define MPUREG_GYRO_CONFIG 0x1B
//TAGLIO...............................
const unsigned char dmpMemory[1929] PROGMEM = {
// bank 0, 256 bytes
0xFB, 0x00, 0x00, 0x3E, 0x00, 0x0B, 0x00, 0x36, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00,
0x00, 0x65, //VE LI RISPARMIO TUTTI......................................
};
// This array contains the DMP configurations that gets loaded during dmpInitialize().
// thanks to Noah Zerkin for piecing this stuff together!
const unsigned char dmpConfig[192] PROGMEM = {
// BANK OFFSET LENGTH [DATA]
0x03, 0x7B, 0x03, 0x4C, 0xCD, 0x6C, // FCFG_1 inv_set_gyro_calibration
0x03, 0xAB, 0x03, 0x36, 0x56, 0x76, // FCFG_3 inv_set_gyro_calibration
0x00, 0x68, 0x04, 0x02, 0xCB, 0x47, 0xA2, // D_0_104 inv_set_gyro_calibration
0x02, 0x18, 0x04, 0x00, 0x05, 0x8B, 0xC1, // D_0_24 inv_set_gyro_calibration
0x01, 0x0C, 0x04, 0x00, 0x00, 0x00, 0x00, // D_1_152 inv_set_accel_calibration
0x03, 0x7F, 0x06, 0x0C, 0xC9, 0x2C, 0x97, 0x97, 0x97, // FCFG_2 inv_set_accel_calibration
0x03, 0x89, 0x03, 0x26, 0x46, 0x66, // FCFG_7 inv_set_accel_calibration
0x00, 0x6C, 0x02, 0x20, 0x00, // D_0_108 inv_set_accel_calibration
0x02, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_00 inv_set_compass_calibration
0x02, 0x44, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_01
0x02, 0x48, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_02
0x02, 0x4C, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_10
0x02, 0x50, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_11
0x02, 0x54, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_12
0x02, 0x58, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_20
0x02, 0x5C, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_21
0x02, 0xBC, 0x04, 0x00, 0x00, 0x00, 0x00, // CPASS_MTX_22
0x01, 0xEC, 0x04, 0x00, 0x00, 0x40, 0x00, // D_1_236 inv_apply_endian_accel
0x03, 0x7F, 0x06, 0x0C, 0xC9, 0x2C, 0x97, 0x97, 0x97, // FCFG_2 inv_set_mpu_sensors
0x04, 0x02, 0x03, 0x0D, 0x35, 0x5D, // CFG_MOTION_BIAS inv_turn_on_bias_from_no_motion
0x04, 0x09, 0x04, 0x87, 0x2D, 0x35, 0x3D, // FCFG_5 inv_set_bias_update
0x00, 0xA3, 0x01, 0x00, // D_0_163 inv_set_dead_zone
// SPECIAL 0x01 = enable interrupts
0x00, 0x00, 0x00, 0x01, // SET INT_ENABLE at i=22, SPECIAL INSTRUCTION
0x07, 0x86, 0x01, 0xFE, // CFG_6 inv_set_fifo_interupt
0x07, 0x41, 0x05, 0xF1, 0x20, 0x28, 0x30, 0x38, // CFG_8 inv_send_quaternion
0x07, 0x7E, 0x01, 0x30, // CFG_16 inv_set_footer
0x07, 0x46, 0x01, 0x9A, // CFG_GYRO_SOURCE inv_send_gyro
0x07, 0x47, 0x04, 0xF1, 0x28, 0x30, 0x38, // CFG_9 inv_send_gyro -> inv_construct3_fifo
0x07, 0x6C, 0x04, 0xF1, 0x28, 0x30, 0x38, // CFG_12 inv_send_accel -> inv_construct3_fifo
0x02, 0x16, 0x02, 0x00, 0x01 // D_0_22 inv_set_fifo_rate
// This very last 0x01 WAS a 0x09, which drops the FIFO rate down to 20 Hz. 0x07 is 25 Hz,
// 0x01 is 100 Hz. Going faster than 100 Hz (0x00 = 200 Hz) tends to result in very noisy data.
// DMP output frequency is calculated easily using this equation: (200 Hz / (1 + value)).
// It is important to make sure the host processor can keep up with reading and processing
// the FIFO output at the desired rate. Handling FIFO overflow cleanly is also a good idea.
};
// This array contains the DMP updates that get loaded during dmpInitialize().
const unsigned char dmpUpdates[47] PROGMEM = {
0x01, 0xB2, 0x02, 0xFF, 0xFF,
0x01, 0x90, 0x04, 0x09, 0x23, 0xA1, 0x35,
0x01, 0x6A, 0x02, 0x06, 0x00,
0x01, 0x60, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x60, 0x04, 0x40, 0x00, 0x00, 0x00,
0x01, 0x62, 0x02, 0x00, 0x00,
0x00, 0x60, 0x04, 0x00, 0x40, 0x00, 0x00
};
const int ChipSelPin1 = 10; //10 su arduino uno
// MPU control & status variables
boolean dmpReady = false; // set true if DMP initialization was successful
unsigned int packetSize = 42; // number of unique bytes of data written by the DMP each time (FIFO can hold multiples of 42-bytes)
unsigned int fifoCount; // count of all bytes currently in FIFO
byte fifoBuffer[64]; // FIFO storage buffer (in fact only 42 used...)
// INTERRUPT FROM MPU-6000 DETECTION ROUTINE
volatile boolean mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady()
{
mpuInterrupt = true;
}
void MPU6000_SPI_write(byte reg, byte data)
{
byte dump;
digitalWrite(ChipSelPin1, LOW);
dump = SPI.transfer(reg);
dump = SPI.transfer(data);
digitalWrite(ChipSelPin1, HIGH);
}
//DEFINIZIONE VARIABILI
float x=0;
float rpy_rol=0;
float rpy_pit=0;
float rpy_yaw=0;
float ADegX=0;
float ADegY=0;
//variabili per la libreria pin change interrupts
uint8_t latest_interrupted_pin;
uint8_t interrupt_count[20]={0}; // 20 possible arduino pins
//(PIN1, &quicfunc1, CHANGE)
void quicfunc1() {
latest_interrupted_pin=PCintPort::arduinoPin;
interrupt_count[latest_interrupted_pin]++;
};
void setup()
{
Serial.begin(115200);
while(Serial.available() > 0) Serial.read(); // Flush serial buffer to clean up remnants from previous run
Serial.println();
Serial.println("############# MPU-6000 Data Acquisition #############");
//--- SPI settings ---//
Serial.println("Initializing SPI Protocol...");
SPI.begin(); // start the SPI library
SPI.setClockDivider(SPI_CLOCK_DIV16); // ArduIMU+ V3 board runs on 16 MHz: 16 MHz / SPI_CLOCK_DIV16 = 1 MHz
// 1 MHz is the maximum SPI clock frequency according to the MPU-6000 Product Specification
SPI.setBitOrder(MSBFIRST); // data delivered MSB first as in MPU-6000 Product Specification
SPI.setDataMode(SPI_MODE0); // latched on rising edge, transitioned on falling edge, active low
Serial.println("...SPI Protocol initializing done.");
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
delay(100);
//--- Configure the chip select pin as output ---//
pinMode(ChipSelPin1, OUTPUT);
// write & verify dmpMemory, dmpConfig and dmpUpdates into the DMP, and make all kinds of other settings
// !!! this is the main routine to make the DMP work, and get the quaternion out of the FIFO !!!
Serial.println("Initializing Digital Motion Processor (DMP)...");
byte devStatus; // return status after each device operation (0 = success, !0 = error)
devStatus = dmpInitialize();
// make sure it worked: dmpInitialize() returns a 0 in devStatus if so
if (devStatus == 0)
{
// now that it's ready, turn on the DMP
Serial.print("Enabling DMP... ");
SPIwriteBit(0x6A, 7, true, ChipSelPin1); // USER_CTRL_DMP_EN
Serial.println("done.");
// enable Arduino interrupt detection, this will execute dmpDataReady whenever there is an interrupt,
// independing on what this sketch is doing at that moment
// http://arduino.cc/en/Reference/AttachInterrupt
Serial.print("Enabling interrupt detection... ");
// attachInterrupt(interrupt, function, mode) specifies a function to call when an external interrupt occurs
// ArduIMU+ V3 has ATMEGA328 INT0 / D2 pin 32 (input) connected to MPU-6000 INT pin 12 (output)
[b] // attachInterrupt(0, dmpDataReady, RISING); // the 0 points correctly to INT0 / D2
pinMode(PIN1, INPUT); digitalWrite(PIN1, LOW);
PCintPort::attachInterrupt(PIN1, &dmpDataReady, RISING);[/b]
// -> if there is an interrupt from MPU-6000 to ATMEGA328, boolean mpuInterrupt will be made true