|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Arm Limited and Contributors. All rights reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | + |
| 10 | +#include "pico/stdlib.h" |
| 11 | +#include "hardware/pwm.h" |
| 12 | + |
| 13 | +extern "C" { |
| 14 | +#include "pico/pdm_microphone.h" |
| 15 | +} |
| 16 | + |
| 17 | +#include "tflite_model.h" |
| 18 | + |
| 19 | +#include "dsp_pipeline.h" |
| 20 | +#include "ml_model.h" |
| 21 | + |
| 22 | +// constants |
| 23 | +#define SAMPLE_RATE 16000 |
| 24 | +#define FFT_SIZE 256 |
| 25 | +#define SPECTRUM_SHIFT 4 |
| 26 | +#define INPUT_BUFFER_SIZE ((FFT_SIZE / 2) * SPECTRUM_SHIFT) |
| 27 | +#define INPUT_SHIFT 0 |
| 28 | + |
| 29 | +// microphone configuration |
| 30 | +const struct pdm_microphone_config pdm_config = { |
| 31 | + // GPIO pin for the PDM DAT signal |
| 32 | + .gpio_data = 2, |
| 33 | + |
| 34 | + // GPIO pin for the PDM CLK signal |
| 35 | + .gpio_clk = 3, |
| 36 | + |
| 37 | + // PIO instance to use |
| 38 | + .pio = pio0, |
| 39 | + |
| 40 | + // PIO State Machine instance to use |
| 41 | + .pio_sm = 0, |
| 42 | + |
| 43 | + // sample rate in Hz |
| 44 | + .sample_rate = SAMPLE_RATE, |
| 45 | + |
| 46 | + // number of samples to buffer |
| 47 | + .sample_buffer_size = INPUT_BUFFER_SIZE, |
| 48 | +}; |
| 49 | + |
| 50 | +q15_t capture_buffer_q15[INPUT_BUFFER_SIZE]; |
| 51 | +volatile int new_samples_captured = 0; |
| 52 | + |
| 53 | +q15_t input_q15[INPUT_BUFFER_SIZE + (FFT_SIZE / 2)]; |
| 54 | + |
| 55 | +DSPPipeline dsp_pipeline(FFT_SIZE); |
| 56 | +MLModel ml_model(tflite_model, 128 * 1024); |
| 57 | + |
| 58 | +int8_t* scaled_spectrum = nullptr; |
| 59 | +int32_t spectogram_divider; |
| 60 | +float spectrogram_zero_point; |
| 61 | + |
| 62 | +void on_pdm_samples_ready(); |
| 63 | + |
| 64 | +int main( void ) |
| 65 | +{ |
| 66 | + // initialize stdio |
| 67 | + stdio_init_all(); |
| 68 | + |
| 69 | + printf("hello pico fire alarm detection\n"); |
| 70 | + |
| 71 | + gpio_set_function(PICO_DEFAULT_LED_PIN, GPIO_FUNC_PWM); |
| 72 | + |
| 73 | + uint pwm_slice_num = pwm_gpio_to_slice_num(PICO_DEFAULT_LED_PIN); |
| 74 | + uint pwm_chan_num = pwm_gpio_to_channel(PICO_DEFAULT_LED_PIN); |
| 75 | + |
| 76 | + // Set period of 256 cycles (0 to 255 inclusive) |
| 77 | + pwm_set_wrap(pwm_slice_num, 256); |
| 78 | + |
| 79 | + // Set the PWM running |
| 80 | + pwm_set_enabled(pwm_slice_num, true); |
| 81 | + |
| 82 | + if (!ml_model.init()) { |
| 83 | + printf("Failed to initialize ML model!\n"); |
| 84 | + while (1) { tight_loop_contents(); } |
| 85 | + } |
| 86 | + |
| 87 | + if (!dsp_pipeline.init()) { |
| 88 | + printf("Failed to initialize DSP Pipeline!\n"); |
| 89 | + while (1) { tight_loop_contents(); } |
| 90 | + } |
| 91 | + |
| 92 | + scaled_spectrum = (int8_t*)ml_model.input_data(); |
| 93 | + spectogram_divider = 64 * ml_model.input_scale(); |
| 94 | + spectrogram_zero_point = ml_model.input_zero_point(); |
| 95 | + |
| 96 | + // initialize the PDM microphone |
| 97 | + if (pdm_microphone_init(&pdm_config) < 0) { |
| 98 | + printf("PDM microphone initialization failed!\n"); |
| 99 | + while (1) { tight_loop_contents(); } |
| 100 | + } |
| 101 | + |
| 102 | + // set callback that is called when all the samples in the library |
| 103 | + // internal sample buffer are ready for reading |
| 104 | + pdm_microphone_set_samples_ready_handler(on_pdm_samples_ready); |
| 105 | + |
| 106 | + // start capturing data from the PDM microphone |
| 107 | + if (pdm_microphone_start() < 0) { |
| 108 | + printf("PDM microphone start failed!\n"); |
| 109 | + while (1) { tight_loop_contents(); } |
| 110 | + } |
| 111 | + |
| 112 | + while (1) { |
| 113 | + // wait for new samples |
| 114 | + while (new_samples_captured == 0) { |
| 115 | + tight_loop_contents(); |
| 116 | + } |
| 117 | + new_samples_captured = 0; |
| 118 | + |
| 119 | + dsp_pipeline.shift_spectrogram(scaled_spectrum, SPECTRUM_SHIFT, 124); |
| 120 | + |
| 121 | + // move input buffer values over by INPUT_BUFFER_SIZE samples |
| 122 | + memmove(input_q15, &input_q15[INPUT_BUFFER_SIZE], (FFT_SIZE / 2)); |
| 123 | + |
| 124 | + // copy new samples to end of the input buffer with a bit shift of INPUT_SHIFT |
| 125 | + arm_shift_q15(capture_buffer_q15, INPUT_SHIFT, input_q15 + (FFT_SIZE / 2), INPUT_BUFFER_SIZE); |
| 126 | + |
| 127 | + for (int i = 0; i < SPECTRUM_SHIFT; i++) { |
| 128 | + dsp_pipeline.calculate_spectrum( |
| 129 | + input_q15 + i * ((FFT_SIZE / 2)), |
| 130 | + scaled_spectrum + (129 * (124 - SPECTRUM_SHIFT + i)), |
| 131 | + spectogram_divider, spectrogram_zero_point |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + float y = ml_model.predict(); |
| 136 | + |
| 137 | + printf("y = %f\n", y); |
| 138 | + |
| 139 | + pwm_set_chan_level(pwm_slice_num, pwm_chan_num, y * 255); |
| 140 | + } |
| 141 | + |
| 142 | + return 0; |
| 143 | +} |
| 144 | + |
| 145 | +void on_pdm_samples_ready() |
| 146 | +{ |
| 147 | + // callback from library when all the samples in the library |
| 148 | + // internal sample buffer are ready for reading |
| 149 | + |
| 150 | + // read in the new samples |
| 151 | + new_samples_captured = pdm_microphone_read(capture_buffer_q15, INPUT_BUFFER_SIZE); |
| 152 | +} |
0 commit comments