I am trying to write a flight control program for quadcopter, and all my code can be found on https://github.com/sgsdxzy/adc
My 10DOF sensor board is GY87, which consists of 3 I2C devices: MPU6050, HMC5883L and BMP180(same API as BMP085). I use i2cdevlib and pigpio to get data from sensors.
MPU6050 can use I2C slaves, and I set HMC5883L as its slave. I successfully got DMP running. MPU6050 can generate an interrupt when DMP data is ready and send the data to a fifo. I wired the interrupt to a gpio, and used pigpio's gpioSetAlertFunc() to monitor it's status. If an interrupt is generated, my program will read the fifo and get DMP data, then write them to some (thread-safe) global variables. This alone is working well. BMP180 is a relatively simpler device, which you set some register to change mode and wait for some time, then read other registers to get results. This alone is working well too.
However, when I combined the two, a random system lockup occured: I set the interrupt handler, then enter a loop: every 0.1s I set BMP180 to measure and get it's data, then print all global variables on screen; MPU6050 just generate 100 interrupts per second and my program handled them in time, so every 0.1s I get the latest data. This works quite well with correct results for several loops, but then at a random point the program stucks, and top shows it's using up 2 cores. Remove either 1) the interrupt handling 2) or the measure of BMP180 in every loop and the program will just run flawlessly and I tested it can ran stable for at least half an hour. But combining two it will always stuck using up all processor power, either after 10 seconds or 1 minute.
I just can't understand why, could someone point out what's wrong? Or at least teach me how to debug what causes the lockup (using which tool, as the lockup happens at a random point and there are too many procedures, using gdb to run the program step-by-step isn't an option)
Thanks in advance.
All right, I found out the i2cdevlib is not thread-safe, and caused the problem.
As i2cdevlib's api is so far from pigpio, porting all these codes to pure pigpio is not trival. Currently I will get do with a pthread mutex to let only one thread access i2c at the same time.