Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPU-9250 Nine-Axis (Gyro + Accelerometer + Compass) sensor Lua driver #3223

Closed
vsky279 opened this issue Jul 18, 2020 · 1 comment
Closed
Labels

Comments

@vsky279
Copy link
Contributor

vsky279 commented Jul 18, 2020

Missing feature

I'd like to have an MPU-9250 Nine-Axis (Gyro + Accelerometer + Compass) sensor driver in NodeMCU Lua.

I have the GY-91 sensor which consist of BMP280 (air pressue sensor) and MPU-9250 sensor. This sensor itself consist of MPU-6500 (accelerometer + gyroscope) and AK8963 (magnetometer).

The communication goes through I2C.

For BMP280 I use the Lua + C BME280 driver (PR #3132).

Following the discussion in #3128 I decided to write the driver purely in Lua. There is not any sophisticated mathematics needed (compared to BME280) and all the work is basically just talking to the sensor and doing some simple calculation.s
Basically I ported the Arduino MPU9250 driver to Lua - see mpu9250.lua.

A working example is here: vario.lua.

For the time being the driver is still using some tmr.delay. I intend to get rid off majority or all of them once the driver is fully working - I face some issues - see below. Optimization will come later.

Limitations

For the moment the driver works only under Lua 5.3 due to several reasons:

  • // operator - this can be replaced with math.floor
  • bitwise operations - bit module is here but the expressions will get difficult to read
  • trigonometric functions - sin, asin and atan is needed for extended functionality of the driver. While these are available in Lua 5.3 a special module or backport of math library would be need under Lua 5.1
  • "main function has more than 50 local variables" - the driver needs to store all the registry addresses. Though only those that are used in the code are used (the rest is commented out) the number of local variables easily surpasses the threshold. This can be overcome but not for cloud builder users. Lua 5.3 has probably the threshold higher so the module is ok for it.

Given all this it is very tempting to stay just in Lua 5.3.

Questions

  • Do you think it was a good decision to do the implementation purely in Lua? The file is large. Almost 1000 lines of code (many of it being comments). So to make it run smooth it needs to be run from LFS.
  • The arithmetic in MadgwickQuaternionUpdate function is not too much difficult calculations (addition, multiplication) but I am not sure how this is efficient in Lua and whether a C implementation would make sense here. In the testing application the calculation speed is not an issue.
  • The implementation uses a lot of arrays usually with 3 items. It would not be difficult to change it to local values (a[1], a[2], a[3] -> a1, a2, a3) which would be probably more efficient. But the code would be more difficult to read. Does it worth the change?

Issues

I face an issue with the accelerometer. The Y and Z axis seem to work correctly. But for X axis I receive 0 value (registers MPU9250_ACCEL_XOUT_H and MPU9250_ACCEL_XOUT_L content is 0x80 and 0x00 respectively all the time).

Can anyone test the driver? I'm suspicious that these can be a sensor hardware issue. The sensor setup sequence seems to be correct.

@stale
Copy link

stale bot commented Jul 21, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 21, 2021
@stale stale bot closed this as completed Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant