Blog
Refactoring for performance
Date: 26/8/2016
Tags: mc2
A few weeks into actually using the MC2 I realized that the response time of the expression pedal was quite poor, running in the 100-200ms range. After doing some tests I worked out that it was because of the way messages were passed from the hardware thread that received the events, like analog to digital reads for the expression pedal, to the user interface thread, where they would be processed and sent back out the hardware thread to the Axefx. The problem with this was the the Raspberry PI is quite slow (or at least the version I'm using) and the user interface update speed was lagging. If the user interface is being updated, it's not processing messages. Hence the delay. But fixing it wasn't straight forward, because all the user interface code and Axefx code were all jumbled up like this:


So I took all the code that relates to the Axefx, and all the code that updates the user interface and separated them out into their own source code files. I called the Axefx specific code the "logic". Now I had 3 separate bits of code:
  • The user interface code. Responsible for updating the screen. Changing settings, like IA button actions etc.
  • The Axefx logic code. Which responds to MIDI inputs, converts hardware events to outgoing MIDI etc.
  • The hardware interface code. This is responsible for talking to all the hardware, both inputs and outputs.
Now I originally had 2 threads. And I decided that unless I really had to I would stay with those 2 threads, but the logic had to move from the UI thread, into the hardware thread. This meant that responding to MIDI and hardware events was decoupled from the user interface updating. Like this:


So hardware events, like the expression pedal value changing, would by processed immediately in the same thread and an outgoing MIDI message could be sent almost immediately, even if the UI was busy doing something. The latency was hugely improved. It's got to the point that it no longer has an impact musically. It seems to respond instantly.

In doing this I realized that I could hide all the details of each block of code behind a message passing interface. Which is excellent for thread safety. There was a little shared data that I wrapped in a mutex. But all in all the design feels nice and clean.

In fact I loved the design so much I'm rewriting i.Ftp to use the same architecture. Oh yeah, and because it was kinda badly written and wouldn't port to modern UI systems that don't have thread safe UI APIs.
(0) Comments | Add Comment

Negative ADS1115 readings
Date: 1/8/2016
Tags: mc2
I've been using an ADS1115 to convert the analog voltage from the expression pedal input, which is just a potentiometer really, to a 16 bit value. On the weekend I had a gig and both at the rehearsal and the set the expression pedal would flip to 127 (the maximum) when in the minimum position. It was also behaving erratically, the full range of output values was compressed into a very short throw of physical movement. I didn't technically need it for the gig so I just unplugged it.

So today I thought I'd get to the bottom of it. After switching on the logging and reproducing the issue it was apparent that a lot of "plugged" / "unplugged" events were happening. This was due to the values coming from the ADS1115 were bouncing between very low values (say 0 - 100) to very high values (65530 - 65535). I started by putting the multi-meter in voltage mode over the input pin of the ADS1115 and ground and found that the lowest voltage was about 0.1V which is, well, correct for the minimum position. So that led me to believe it was solely a software issue. Looking at the code that does the read, I found it was assuming the 16bit value was a unsigned integer. But that fact that the value was jumping to 65500 made me wonder if maybe the ADS1115 was giving me a signed value instead. So I changed the code to assume signed behaviour and now the values all seemed to follow each other as the pedal moves back and forth. Then checking with the data sheet:
The ADS1113/4/5 provide 16 bits of data in binary twos complement format
Ugh, ok so I should read that more carefully. Still it going slightly negative like that even with a positive voltage is a bit weird to me. Oh well, I'm just going with max(0, AdcReadValue) for the moment and it seems to be working just fine now.
(0) Comments | Add Comment