BYOAQ-BAT VI: Performance

If you haven’t already, please read the previous BYOAQ-BAT articles first – just search for the BYOAQ-BAT tag.


This is another short article about getting the code to run as fast as possible. I’ve taken numerous steps to ensure the code itself is fast including:

  • i2c 1 x 14 byte read rather than 14 x 1 byte reads
  • time.time() called once per cycle at the point the next sample is read – there’s an irony that calling time.time() takes a lot of time!
  • calculations of values used multiple times is done once – see the various matrices for an example
  • apply calibration / scaling once per motion not once per sample
  • separating data collection and motion processing into separate threads (although to be honest this is buggered by the GIL)
  • data logging to RAM disk and copied to SD card on flight completion

but without the best from the OS, they are pointless.

We’ve already overclocked the CPU and given as much memory as possible to the CPU.  Two last steps.

Get GPIO.tgz from GitHub is you don’t have it already.  Do the following:

tar xvf GPIO.tgz
sudo apt-install remove python-rpi.gpio
cd GPIO
sudo python setup.py install

That installs the GPIO replacement which is fast enough to catch the 50μS hardware interrupts from the sensors to say there is new data ready.

Then to read the data as fast as possible, you’ll need to edit /boot/config.txt adding:

dtparam=i2c_arm_baudrate=400000

Together I’m able to get about 700 loops per second each reading 14 bytes of data (a data rate of 78.4kbps) and process them.  The sensors is updating this data at 1kHz or 112kbps which is why the baudrate needs to be increased from its default of 100kbps.

Leave a Reply

Your email address will not be published. Required fields are marked *