I have managed to make some progress, and have gotten the ESP to communicate with the MPU to retrieve a single byte, using the following code:
#include <Wire.h>
uint8_t adrs = 0x68;
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(adrs);
Wire.write(0x75);
Serial.print("Request for WHOAMI register, transmission result: ");
Serial.println(Wire.endTransmission());
Wire.requestFrom(adrs, (size_t)1, true);
byte data = Wire.read();
Serial.print("Byte received: ");
Serial.println(data);
}
So, I implemented what seemed to be right about that snippet into my larger project, did a little debugging, and now I am stuck on this. It is throwing a software watchdog timer when I try to run Wire.requestFrom on the ESP (in the updateData() method). I am printing out the error codes for the config which awakes and configures the module so I can see if the awakening and configuring was successful, i.e. an error code of 0 (in the configureMPU() method), and am still getting this requestFrom error even when it is. The code is as follows:
#include <Wire.h>
int adrs = 0x68;
float pitch, roll, yaw;
float gyrox=0, gyroy=0, gyroz=0;
float gyrox_o, gyroy_o, gyroz_o;
float accx, accy, accz;
void setup() {
Wire.begin();
Serial.begin(9600);
configureMPU();
calibrateGyro();
}
void configureMPU(){
Wire.beginTransmission(adrs);
Wire.write(0x6B);
Wire.write(0x00);
Serial.println(Wire.endTransmission());
Wire.beginTransmission(adrs);
Wire.write(0x1C);
Wire.write(0x10);
Serial.println(Wire.endTransmission());
Wire.beginTransmission(adrs);
Wire.write(0x1B);
Wire.write(0x08);
Serial.println(Wire.endTransmission());
}
void calibrateGyro(){
Serial.println("Calibrating gyroscope...");
double xsum=0;
double ysum=0;
double zsum=0;
int nt=2000;
for(int i=0;i<nt;i++){
updateData();
xsum+=gyrox;
ysum+=gyroy;
zsum+=gyroz;
}
gyrox_o = xsum/nt;
gyroy_o = ysum/nt;
gyroz_o = zsum/nt;
Serial.println("Calibration complete.");
}
void updateData(){
Wire.beginTransmission(adrs);
Wire.write(0x3B);
Wire.endTransmission();
Wire.requestFrom(adrs, 14);
while(Wire.available()<14){};
accx = Wire.read()<<8|Wire.read();
accy = Wire.read()<<8|Wire.read();
accz = Wire.read()<<8|Wire.read();
float temperature = Wire.read()<<8|Wire.read();
gyrox = Wire.read()<<8|Wire.read();
gyroy = Wire.read()<<8|Wire.read();
gyroz = Wire.read()<<8|Wire.read();
}
void loop() {
updateData();
}
And the decoded stack trace:
0x402014db: twi_readFrom at C:\Users\mach\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\core_esp8266_si2c.c line 246
0x4020313c: HardwareSerial::write(unsigned char const*, unsigned int) at C:\Users\mach\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266/HardwareSerial.h line 174
0x40202fbc: TwoWire::requestFrom(unsigned char, unsigned int, bool) at C:\Users\mach\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\libraries\Wire\Wire.cpp line 118
0x40202fe7: TwoWire::requestFrom(int, int) at C:\Users\mach\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\libraries\Wire\Wire.cpp line 134
0x40202b94: updateData() at C:\Users\mach\Documents\Arduino\Drone_temp/Drone_temp.ino line 51
0x40202cc1: calibrateGyro() at C:\Users\mach\Documents\Arduino\Drone_temp/Drone_temp.ino line 37
0x40202dc4: setup() at C:\Users\mach\Documents\Arduino\Drone_temp/Drone_temp.ino line 14
0x402038c4: loop_wrapper() at C:\Users\mach\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\core_esp8266_main.cpp line 122
In case it is relevant, I also occasionally get the following warning on trying to upload the sketch. It doesn't even happen all the time, just seemingly randomly:
warning: espcomm_send_command: wrong direction/command: 0x01 0x00, expected 0x01 0x08
Any help would be seriously appreciated at this point, this has been driving me crazy for 3 days now.