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

Drivers Code Cleanup #9279

Closed
thomasgubler opened this issue Apr 10, 2018 · 6 comments
Closed

Drivers Code Cleanup #9279

thomasgubler opened this issue Apr 10, 2018 · 6 comments

Comments

@thomasgubler
Copy link
Contributor

thomasgubler commented Apr 10, 2018

Goals:

@dagar
Copy link
Member

dagar commented Apr 10, 2018

There's quite a lot that could be discussed here. For me the worst part is that most of the drivers appear to have been developed largely by copy and paste. New drivers are added by duplicating something close, then making a few changes. Huge portions of near boilerplate code are duplicated multiple times. This has resulted in a large sprawling code base that's difficult to maintain and uses significantly more flash than is necessary.

As an illustration here's a relatively simple pull request that changed the way raw/unfiltered IMU was published. #8517
Every single accel and gyro driver in the official repo does the same filtering, integration, scaling, publication, etc. This could easily be pushed into a base class (PX4Accel, PX4Gyro) instead of making the same identical change many times.

@mcsauder
Copy link
Contributor

I agree with the comments above.

@mcsauder
Copy link
Contributor

@thomasgubler , @dagar , We are working on a basic driver parent class to begin unifying driver construction and are interested in your input. Recent work on the lis3mdl driver and current work on the batt_smbus and mpu9250 driver represents some of the structure we have implemented, however, there might still be better architectures we haven't identified. If you have opinions you would like to see implemented please let me know.

-Mark

@dagar dagar added this to the Release v2.0.0 milestone May 19, 2018
@dagar
Copy link
Member

dagar commented May 19, 2018

I'm excited to see interest here. From a code maintenance, platform scalability, and developer sanity standpoint I think abstracting the common pieces of each driver type is long overdue. As a start take a look at the Airspeed class used by all differential pressure sensor drivers. https://github.com/PX4/Firmware/tree/master/src/lib/drivers/airspeed

As much as I love to eliminate code bloat and make things as simple as possible, the most valuable changes we need really are in the sensor pipeline. In terms of system capabilities I view this as an opportunity to implement some of the core improvements that are particularly important for racers and system integrators right now. The most important is splitting the IMU drivers into a high rate publication path to the rate controller (400 Hz - 2 kHz), and a separate delta angle and delta velocity publication (250 Hz) for the EKF. We also need to increase the sampling rate as much as possible (>= 4 kHz) and do all of the filtering PX4 side to help with aliasing and clipping. This will require getting the drivers out of ISRs and enabling SPI DMA.

image

Notes

  • Virtually all code used to manage the IMU drivers (start, stop, status, test, calibration) should be the same. Integration and publication should also be something common provided by the base class.
  • All IOCTL code except for applying calibration and getting the device id could be gutted immediately. Eventually I want to get rid of it entirely (calibration).
  • the current Ringbuffers aren't needed at all (CDev read)
  • for the most part nearly all drivers should be independent of the physical interface (I2C or SPI). Other than the unnecessary use of CDev (eg /dev/bmp280_i2c_int) the way this is handled now is actually pretty good. I'd also like to see this extended so that simulation data can enter the driver at this level.
  • properly hanlde multiple instances of each driver (eg this must stop https://github.com/PX4/Firmware/blob/master/src/drivers/imu/mpu9250/main.cpp#L121-L137)
  • the split between mpu6000 and mpu9250 likely shouldn't exist, unless we get to a point where the actual specific driver code is so minimal it makes more sense to have separate mpu6000, mpu6500, icm20602, icm20689, mpu9250, etc
  • calibration (a longer discussion)
  • split CDev from the Device driver base class (I'll explain separately)
  • depending on the calibration discussion we may not need CDev at all for drivers. At a minimum we definitely don't need a CDev per driver interface (eg /dev/mpu9250_accel1).
  • NuttX SPI DMA can only be enabled across all SPI simultaneously and we might be short on DMA channels (especially with interest in DSHOT)
  • synchronization of IMU. We want a synchronized IMU (accel + gyro) message for the EKF (easy with most sensors we support), but also have to support doing this with completely separate accel + gyro.
  • PX4 side something like a high priority work queue per sensor bus is needed
  • mechanism for syncing the entire rate pipeline with output (50-400 Hz PWM ESCs, oneshot, dshot, UAVCAN, etc). I want to the entire block (gyro sample -> rate controller -> output) synchronized with effectively all latency and jitter removed.
  • support data ready interrupts
  • regardless of the specific usage all drivers should build on all platforms. PX4 I2C and SPI already support the differences across NuttX, Linux, QuRT

Relevant issues/PRs

@stale stale bot closed this as completed Feb 4, 2019
@TSC21 TSC21 reopened this Feb 4, 2019
@stale stale bot removed the Admin: Wont fix label Feb 4, 2019
@dagar dagar self-assigned this Feb 4, 2019
@PX4 PX4 deleted a comment from stale bot Feb 4, 2019
@PX4 PX4 deleted a comment from stale bot Feb 4, 2019
@dagar dagar mentioned this issue Mar 2, 2019
2 tasks
@mcsauder
Copy link
Contributor

Distance sensor drivers are progressing, see #11891, #11853, #11859, et. al.

@dagar
Copy link
Member

dagar commented Apr 8, 2020

Believe it or not nearly all of this has been done.

@dagar dagar closed this as completed Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants