awulff-pico-playground
repository, and navigate to the pico-light-voice
folder. This is the main folder for this project.
if (ix == ...)
part of the code in main.cpp
. Your “start” and “stop” keywords should match with what’s in the code.thresh
in main.cpp
to control the sensitivity of the keyword detection.NUM_LIGHTS
in lights.cpp
to the number of LEDs in your strip.main
code loop. You can find this in source/main.cpp
.
Input Parameters
At the top of the file, you can find parameters for the model inputs and sample rates. A CLOCK_DIV
of 12,000 gives a sample rate of 4 kHz. This is low for audio, but still intelligible to a machine learning model. This value needs to match the sample rate that you used to collect data in the data collection tutorial! We trained the model to respond to sample lengths of 4,000 samples (NSAMP
), which works out to be exactly 1 second of data.
For more information on ADC sampling and DMA with Pico, check out my tutorial on this subject.
In this code I also do something called continuous inferencing, which can improve the performance of the keyword detection drastically by passing in overlapping buffers of samples into the model. All the math takes under 250ms to execute, so I maintain a sliding buffer of data and run the model on it every quarter of a second. Edge Impulse doesn’t support continuous inferencing natively on Pico, so I implemented a form of it in software.
Setup Code
All the code in main()
before the while (true)
portion gets executed once. This is all setup code used to configure the model and sampling.
Main Code Loop
We take advantage of Pico’s many hardware features to form a very efficient data processing pipeline, which you can find in the while (true)
portion of the code. The basic operation of this is as follows:
dma_channel_wait_for_finish_blocking(dma_chan)
NSAMP
); every time this collection is done, we shift samples out of the ADC buffer and into an intermediate buffer with past audio data.
The green LED on the Pico is configured to be on while the Pico is busy executing the ML code and off while the Pico is waiting for the ADC. Thus, you can use the duty cycle of the flashing to get a handle on how close your model is to the 250 ms inferencing limit. You are losing data if the light stays on continuously—if this is the case, try reducing the execution time of your model, or increase NSAMP
so the model runs more often.
source/lights.cpp
.
The second core executes a simple state machine based on input from the first core. Let’s look at the core1_entry()
function to see how this works.
Setup Code
The first part of this code sets up the light strip. The Adafruit_NeoPixel
library API should be the same as that which you find in other tutorials online, with some caveats. See the library’s GitHub page for more information.
Lighting Loop
The lights operate in a relatively simple manner. If we get the off keyword, the lights will stay off. When we get the “on” keyword, the lights will turn on in the previous lighting mode active when the lights were turned off. When we get the “on” keyword when the lights are already on, the code will cycle to a new lighting state. Add new lighting modes as you see fit, and make sure to update NUM_STATES
to reflect how many states you’d like to use.
If your lighting code is relatively computationally intensive, make sure you periodically check for state updates (like I do in the rainbow mode) to ensure that your lighting system is responsive.
Multicore Communications
The update_state()
function handles the communication between the two cores. Pico implements this communication using two FIFO queues — we can use this as a bi-directional pipe to send information back and forth between the cores. From the lighting core we tell the sampling core that we’re ready for data using multicore_fifo_push_blocking(0)
. If the sampling core sees that the lighting core is ready for a state update, and it has a state update to give, it will send this update to the lighting core. Once the lighting core receives an update it will change the state accordingly.
This code initially looks a little complicated, but it’s a relatively simple way to synchronize two independent processing cores. Read the Raspberry Pi documentation on FIFO and multicore processing if you’re stuck.
CMake
and make
, and flash it using the .uf2
file. If your code is failing to build, you might not have copied all the folders you needed from your deployed C++ Edge Impulse model, or the path to the Pico SDK might not be set correctly.
uint16_t
s to the model when it was expecting floatsNSAMP/INSIZE
small!)