Monkeys on a typewriter coding
coding code
Faced with some sample code that doesn’t compile, what do you do? Call for help or tinker with it. Help might be slow on a weekend, or never come. Drilling into the code code can provide a more timely fix.
I was intending to implement some Arduino code for a MPU6050 9-axis gyroscope device. But one line of code from the given sample did not compile, just one line with some dependant lines. What to do? I tinkered with it a little so I posted an email to source and also posted the issue on GitHub in the repository. Spoiler alert: I did solve the issue!
I was working with the sketch project for the MPU6050 in the PDF Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi ( Go to sketch 27.1). I installed the library from the given Zip file. When I compiled the code I get an error:
#include <MPU6050_tockn.h>
#include <Wire.h>
MbedI2C iic(6,7); <---This line fails
MPU6050 mpu6050(iic);//Attach the IIC <-- An as a consequence, so does this.
int16_t ax,ay,az;//define acceleration values of 3 axes
int16_t gx,gy,gz;//define variables to save the values in 3 axes of gyroscope
The error on the mbed line is error: 'MbedI2C' does not name a type
A search here there and everywhere( in the the repository, Arduino libraries, on the web etc) sheds no light on the issue. So I posted an issue in the GitHub repository github.com/Freenove where the PDF and code came from. I also posted an email query to the Freenove, the vendor.
I could not find a suitable reference for MbedI2C even amongst Mbed Arduino examples.
At this point I am reminded of when I was a lecturer in Computer Engineering. Having written some code and it doesn’t work, some students had what it takes to tinker with the code to get it to work by a bit of analysis and a bit of trial-and-error.WriteLine statements were always a good fallback with runtime errors. Others with no such skills or ideas would just put their hands up and say “It doesn’t work!”.
So I started “drilling into the code”. I looked into the MPU6050 class because its instantiation took the MbedI2C instance as an input. The relevant constructor is:
MPU6050(TwoWire &w);
So next level drill in is into the TwoWire class, This is in Wire.h
. The constructor for that is:
TwoWire(i2c_inst_t *i2c, pin_size_t sda, pin_size_t scl);
So the second and third parameters woyuld be the 6 and 7 pins in MbedI2C iic(6,7);
. If I created an uninstatiated instance of i2c_inst_t and used that it compiled but failed to produce data at runtime:
i2c_inst_t tt;
/MbedI2C iic(6,7);
TwoWire tw(tt,6,7)
MPU6050 mpu6050(&tw);
Nearly there!
In i2c.h
the struct i2c_inst_t
is defined. There is also:
typedef struct i2c_inst i2c_inst_t;
extern i2c_inst_t i2c0_inst;
extern i2c_inst_t i2c1_inst;
#define i2c0 (&i2c0_inst) ///< Identifier for I2C HW Block 0
#define i2c1 (&i2c1_inst) ///< Identifier for I2C HW Block 1
So can use i2c0 or i2c1 depending upon which pins are used!
So back in the sketch code, it becomes:
//MbedI2C iic(6,7);
TwoWire iic(i2c1,6,7);
//TwoWire iic(i2c0,4,5);
MPU6050 mpu6050(iic);//Attach the IIC
… and this works 😀
The 4,5 version is if I2C0 and pins 4 and 5 are used.
PS: The “pin numbers” used here are the GPIO pin numbers, not the actual pin numbers.
Summary
This is just an example what ones does time and time again to solve a coding problem when you hit a brick wall!.
Topic | Subtopic | |
Next: > | RPI-Pico-Arduino-AzSDK | A Bridge too far ... |
< Prev: | RPI-Pico-Arduino-AzSDK | Addendum |
This Category Links | ||
Category: | Coding Index: | Coding |
Next: > | Nuget updates | With errors such as NU1605 and NU1301 |
< Prev: | C# Async Threading in Console Apps |