KIK 01 Prototype 05
Dependencies: AverageMCP3008 mbed-rtos mbed mcp3008
Fork of KIK01_Proto03 by
main.cpp@28:387c5a6d5206, 2017-10-19 (annotated)
- Committer:
- ryood
- Date:
- Thu Oct 19 07:15:45 2017 +0000
- Revision:
- 28:387c5a6d5206
- Parent:
- 27:b07a55935230
- Child:
- 29:947992b9904f
Use AverageMCP3008
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 3:f89b400cfe57 | 1 | /* |
ryood | 3:f89b400cfe57 | 2 | * KIK01 |
ryood | 3:f89b400cfe57 | 3 | * Kick Machine |
ryood | 3:f89b400cfe57 | 4 | * |
ryood | 27:b07a55935230 | 5 | * 2017.10.19 Proto05: Add NOS01 Controller |
ryood | 17:a5d9908bd456 | 6 | * 2017.09.16 Proto04: SPI1 for AD8402 Wein Bridge DCO & Internal DAC for Dual-OTA-VCA |
ryood | 15:8f674acdac03 | 7 | * 2017.07.04 Proto03: MCP4922 DCA |
ryood | 13:43a43da257e3 | 8 | * 2017.06.19 Proto02 |
ryood | 3:f89b400cfe57 | 9 | * 2017.06.04 created. |
ryood | 3:f89b400cfe57 | 10 | * |
ryood | 3:f89b400cfe57 | 11 | */ |
ryood | 3:f89b400cfe57 | 12 | |
ryood | 0:2dcec10e9199 | 13 | #include "mbed.h" |
ryood | 0:2dcec10e9199 | 14 | #include "rtos.h" |
ryood | 13:43a43da257e3 | 15 | #include "mcp3008.h" |
ryood | 28:387c5a6d5206 | 16 | #include "AverageMCP3008.h" |
ryood | 22:a3bb7594f9bb | 17 | #include "EnvelopeAR.h" |
ryood | 0:2dcec10e9199 | 18 | |
ryood | 17:a5d9908bd456 | 19 | #define UART_TRACE (1) |
ryood | 12:5b498285d121 | 20 | #define PIN_CHECK (1) |
ryood | 0:2dcec10e9199 | 21 | #define TITLE_STR1 ("KIK01 Kick Machine") |
ryood | 26:866d672ba446 | 22 | #define TITLE_STR2 ("20171019") |
ryood | 0:2dcec10e9199 | 23 | |
ryood | 0:2dcec10e9199 | 24 | #define PI_F (3.1415926f) |
ryood | 15:8f674acdac03 | 25 | |
ryood | 17:a5d9908bd456 | 26 | #define MCP3008_SPI_SPEED (1000000) |
ryood | 21:a527f51381d6 | 27 | #define AD8402_SPI_SPEED (10000000) |
ryood | 23:da7c5f7feff1 | 28 | #define ENVELOPE_UPDATE_RATE (32000) // Hz |
ryood | 17:a5d9908bd456 | 29 | |
ryood | 20:70a05941db8c | 30 | #define AD8402_RMAX (10000.0f) |
ryood | 20:70a05941db8c | 31 | #define AD8402_RMIN (50.0f) |
ryood | 20:70a05941db8c | 32 | |
ryood | 26:866d672ba446 | 33 | AnalogOut Dac1(PA_4); |
ryood | 27:b07a55935230 | 34 | AnalogOut Dac2(PA_5); |
ryood | 3:f89b400cfe57 | 35 | |
ryood | 26:866d672ba446 | 36 | // AD8402 SPI (SPI2) |
ryood | 26:866d672ba446 | 37 | // SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC) |
ryood | 26:866d672ba446 | 38 | SPI SpiMAD8402(PB_15, PB_14, PB_13); |
ryood | 26:866d672ba446 | 39 | DigitalOut AD8402Cs(PC_4); |
ryood | 15:8f674acdac03 | 40 | |
ryood | 18:1bf4abf6895b | 41 | // MCP3008 SPI |
ryood | 26:866d672ba446 | 42 | // SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC) |
ryood | 26:866d672ba446 | 43 | SPI SpiMAdc(PB_5, PB_4, PB_3); |
ryood | 26:866d672ba446 | 44 | MCP3008 Adc1(&SpiMAdc, PB_10); |
ryood | 26:866d672ba446 | 45 | MCP3008 Adc2(&SpiMAdc, PA_8); |
ryood | 26:866d672ba446 | 46 | MCP3008 Adc3(&SpiMAdc, PA_9); |
ryood | 28:387c5a6d5206 | 47 | AverageMCP3008 AvgAdc1(&Adc1, 8); |
ryood | 28:387c5a6d5206 | 48 | AverageMCP3008 AvgAdc2(&Adc2, 8); |
ryood | 28:387c5a6d5206 | 49 | AverageMCP3008 AvgAdc3(&Adc3, 8); |
ryood | 8:bb34a4894337 | 50 | |
ryood | 18:1bf4abf6895b | 51 | // Sync |
ryood | 26:866d672ba446 | 52 | DigitalOut SyncPin(PA_10); |
ryood | 18:1bf4abf6895b | 53 | |
ryood | 14:8e96f97e261b | 54 | // Check pins |
ryood | 26:866d672ba446 | 55 | DigitalOut Dout1(PB_9); |
ryood | 26:866d672ba446 | 56 | DigitalOut Dout2(PB_8); |
ryood | 27:b07a55935230 | 57 | DigitalOut Dout3(PB_6); |
ryood | 14:8e96f97e261b | 58 | |
ryood | 9:d1e6eae9722b | 59 | EnvelopeAR envelopeFrequency(5, 300, 880.0f, 120.0f, 40.0f, 0.36f, 0.1f); |
ryood | 5:846772a77d33 | 60 | EnvelopeAR envelopeAmplitude(50, 200, 0.99f, 1.0f, 0.0f); |
ryood | 5:846772a77d33 | 61 | |
ryood | 25:b4977c7e0db7 | 62 | EnvelopeParam frequencyParam; |
ryood | 25:b4977c7e0db7 | 63 | EnvelopeParam amplitudeParam; |
ryood | 0:2dcec10e9199 | 64 | |
ryood | 3:f89b400cfe57 | 65 | volatile int ticks; |
ryood | 3:f89b400cfe57 | 66 | volatile float frequency; |
ryood | 3:f89b400cfe57 | 67 | volatile float amplitude; |
ryood | 3:f89b400cfe57 | 68 | |
ryood | 5:846772a77d33 | 69 | volatile float bpm; |
ryood | 5:846772a77d33 | 70 | volatile int envelopeLength; |
ryood | 14:8e96f97e261b | 71 | volatile int stepLength; |
ryood | 0:2dcec10e9199 | 72 | |
ryood | 17:a5d9908bd456 | 73 | void AD8402Write(uint8_t address, uint8_t value) |
ryood | 17:a5d9908bd456 | 74 | { |
ryood | 21:a527f51381d6 | 75 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 76 | Dout2 = 1; |
ryood | 21:a527f51381d6 | 77 | #endif |
ryood | 21:a527f51381d6 | 78 | |
ryood | 18:1bf4abf6895b | 79 | AD8402Cs = 0; |
ryood | 21:a527f51381d6 | 80 | SpiMAD8402.write(address); |
ryood | 21:a527f51381d6 | 81 | SpiMAD8402.write(value); |
ryood | 18:1bf4abf6895b | 82 | AD8402Cs = 1; |
ryood | 17:a5d9908bd456 | 83 | wait_us(1); |
ryood | 21:a527f51381d6 | 84 | |
ryood | 21:a527f51381d6 | 85 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 86 | Dout2 = 0; |
ryood | 21:a527f51381d6 | 87 | #endif |
ryood | 17:a5d9908bd456 | 88 | } |
ryood | 17:a5d9908bd456 | 89 | |
ryood | 23:da7c5f7feff1 | 90 | void DcoSetFrequency(float freq) |
ryood | 17:a5d9908bd456 | 91 | { |
ryood | 17:a5d9908bd456 | 92 | const float c = 0.00000047; |
ryood | 17:a5d9908bd456 | 93 | float r = 1.0f / (2.0f * PI_F * frequency * c); |
ryood | 20:70a05941db8c | 94 | if (r < AD8402_RMIN) r = AD8402_RMIN; |
ryood | 20:70a05941db8c | 95 | if (r > AD8402_RMAX) r = AD8402_RMAX; |
ryood | 20:70a05941db8c | 96 | |
ryood | 20:70a05941db8c | 97 | uint8_t v = 256.0f * (r - AD8402_RMIN) / AD8402_RMAX; |
ryood | 21:a527f51381d6 | 98 | |
ryood | 17:a5d9908bd456 | 99 | AD8402Write(0, v); |
ryood | 17:a5d9908bd456 | 100 | AD8402Write(1, v); |
ryood | 23:da7c5f7feff1 | 101 | } |
ryood | 17:a5d9908bd456 | 102 | |
ryood | 27:b07a55935230 | 103 | void DcaSetAmplitude(int channel, float amp) |
ryood | 23:da7c5f7feff1 | 104 | { |
ryood | 27:b07a55935230 | 105 | switch (channel) { |
ryood | 27:b07a55935230 | 106 | case 1: |
ryood | 27:b07a55935230 | 107 | Dac1.write(amp); |
ryood | 27:b07a55935230 | 108 | break; |
ryood | 27:b07a55935230 | 109 | case 2: |
ryood | 27:b07a55935230 | 110 | Dac2.write(amp * 0.8f); // Avoid LED,s Non-Linearity |
ryood | 27:b07a55935230 | 111 | break; |
ryood | 27:b07a55935230 | 112 | default: |
ryood | 27:b07a55935230 | 113 | error("DcaSetAmplitude(): invalid channel"); |
ryood | 27:b07a55935230 | 114 | } |
ryood | 17:a5d9908bd456 | 115 | } |
ryood | 15:8f674acdac03 | 116 | |
ryood | 2:8dff77a1ee4d | 117 | void update() |
ryood | 2:8dff77a1ee4d | 118 | { |
ryood | 12:5b498285d121 | 119 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 120 | Dout1 = 1; |
ryood | 12:5b498285d121 | 121 | #endif |
ryood | 15:8f674acdac03 | 122 | |
ryood | 14:8e96f97e261b | 123 | // Output Sync Signal per steps |
ryood | 17:a5d9908bd456 | 124 | if (ticks % stepLength == 0) { |
ryood | 14:8e96f97e261b | 125 | SyncPin = 1; |
ryood | 14:8e96f97e261b | 126 | } |
ryood | 15:8f674acdac03 | 127 | |
ryood | 17:a5d9908bd456 | 128 | // set envelope parameters |
ryood | 25:b4977c7e0db7 | 129 | envelopeAmplitude.setParam(amplitudeParam); |
ryood | 25:b4977c7e0db7 | 130 | envelopeFrequency.setParam(frequencyParam); |
ryood | 11:7e11404adca0 | 131 | |
ryood | 23:da7c5f7feff1 | 132 | frequency = envelopeFrequency.getAmplitude(ticks); |
ryood | 23:da7c5f7feff1 | 133 | amplitude = envelopeAmplitude.getAmplitude(ticks); |
ryood | 23:da7c5f7feff1 | 134 | |
ryood | 23:da7c5f7feff1 | 135 | DcoSetFrequency(frequency); |
ryood | 27:b07a55935230 | 136 | DcaSetAmplitude(1, amplitude); // DCO |
ryood | 27:b07a55935230 | 137 | DcaSetAmplitude(2, amplitude); // NOS01 |
ryood | 11:7e11404adca0 | 138 | |
ryood | 17:a5d9908bd456 | 139 | ticks++; |
ryood | 17:a5d9908bd456 | 140 | if (ticks >= envelopeLength) { |
ryood | 17:a5d9908bd456 | 141 | ticks = 0; |
ryood | 2:8dff77a1ee4d | 142 | } |
ryood | 15:8f674acdac03 | 143 | |
ryood | 14:8e96f97e261b | 144 | // Output SyncSignal |
ryood | 14:8e96f97e261b | 145 | SyncPin = 0; |
ryood | 15:8f674acdac03 | 146 | |
ryood | 12:5b498285d121 | 147 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 148 | Dout1 = 0; |
ryood | 12:5b498285d121 | 149 | #endif |
ryood | 2:8dff77a1ee4d | 150 | } |
ryood | 2:8dff77a1ee4d | 151 | |
ryood | 25:b4977c7e0db7 | 152 | void readParams() |
ryood | 5:846772a77d33 | 153 | { |
ryood | 28:387c5a6d5206 | 154 | bpm = AvgAdc1.read_input(7) * 180.0f + 60.0f; |
ryood | 12:5b498285d121 | 155 | envelopeLength = 60 * ENVELOPE_UPDATE_RATE / bpm; |
ryood | 14:8e96f97e261b | 156 | stepLength = envelopeLength / 4; |
ryood | 11:7e11404adca0 | 157 | |
ryood | 28:387c5a6d5206 | 158 | amplitudeParam.attack = AvgAdc1.read_input(0) * envelopeLength; |
ryood | 28:387c5a6d5206 | 159 | amplitudeParam.release = AvgAdc1.read_input(1) * envelopeLength; |
ryood | 28:387c5a6d5206 | 160 | amplitudeParam.v0 = AvgAdc1.read_input(4); |
ryood | 28:387c5a6d5206 | 161 | amplitudeParam.v1 = AvgAdc1.read_input(5); |
ryood | 28:387c5a6d5206 | 162 | amplitudeParam.v2 = AvgAdc1.read_input(6); |
ryood | 28:387c5a6d5206 | 163 | amplitudeParam.attackTauRatio = AvgAdc1.read_input(2) + 0.01f; |
ryood | 28:387c5a6d5206 | 164 | amplitudeParam.releaseTauRatio = AvgAdc1.read_input(3) + 0.01f; |
ryood | 11:7e11404adca0 | 165 | |
ryood | 28:387c5a6d5206 | 166 | frequencyParam.attack = AvgAdc2.read_input(0) * envelopeLength * 0.1f; |
ryood | 28:387c5a6d5206 | 167 | frequencyParam.release = AvgAdc2.read_input(1) * envelopeLength + 1; |
ryood | 28:387c5a6d5206 | 168 | frequencyParam.v0 = AvgAdc2.read_input(4) * 4000.0f; |
ryood | 28:387c5a6d5206 | 169 | frequencyParam.v1 = AvgAdc2.read_input(5) * 400.0f; |
ryood | 28:387c5a6d5206 | 170 | frequencyParam.v2 = AvgAdc2.read_input(6) * 400.0f; |
ryood | 28:387c5a6d5206 | 171 | frequencyParam.attackTauRatio = AvgAdc2.read_input(2) + 0.01f; |
ryood | 28:387c5a6d5206 | 172 | frequencyParam.releaseTauRatio = AvgAdc2.read_input(3) + 0.01f; |
ryood | 5:846772a77d33 | 173 | } |
ryood | 5:846772a77d33 | 174 | |
ryood | 0:2dcec10e9199 | 175 | int main() |
ryood | 0:2dcec10e9199 | 176 | { |
ryood | 27:b07a55935230 | 177 | #if UART_TRACE |
ryood | 27:b07a55935230 | 178 | printf("\r\n\n%s %s\r\n", TITLE_STR1, TITLE_STR2); |
ryood | 27:b07a55935230 | 179 | #endif |
ryood | 15:8f674acdac03 | 180 | |
ryood | 21:a527f51381d6 | 181 | SpiMAD8402.format(8, 0); |
ryood | 21:a527f51381d6 | 182 | SpiMAD8402.frequency(AD8402_SPI_SPEED); |
ryood | 17:a5d9908bd456 | 183 | |
ryood | 26:866d672ba446 | 184 | SpiMAdc.format(8, 0); |
ryood | 26:866d672ba446 | 185 | SpiMAdc.frequency(MCP3008_SPI_SPEED); |
ryood | 15:8f674acdac03 | 186 | |
ryood | 17:a5d9908bd456 | 187 | frequency = 100.0f; |
ryood | 0:2dcec10e9199 | 188 | amplitude = 1.0f; |
ryood | 17:a5d9908bd456 | 189 | bpm = 120.0f; |
ryood | 17:a5d9908bd456 | 190 | |
ryood | 25:b4977c7e0db7 | 191 | readParams(); |
ryood | 11:7e11404adca0 | 192 | |
ryood | 3:f89b400cfe57 | 193 | ticks = 0; |
ryood | 2:8dff77a1ee4d | 194 | Ticker samplingTicker; |
ryood | 17:a5d9908bd456 | 195 | samplingTicker.attach(&update, (1.0f/ENVELOPE_UPDATE_RATE)); |
ryood | 11:7e11404adca0 | 196 | |
ryood | 3:f89b400cfe57 | 197 | for (;;) { |
ryood | 13:43a43da257e3 | 198 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 199 | Dout3 = 1; |
ryood | 15:8f674acdac03 | 200 | #endif |
ryood | 25:b4977c7e0db7 | 201 | |
ryood | 25:b4977c7e0db7 | 202 | readParams(); |
ryood | 25:b4977c7e0db7 | 203 | |
ryood | 13:43a43da257e3 | 204 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 205 | Dout3 = 0; |
ryood | 15:8f674acdac03 | 206 | #endif |
ryood | 11:7e11404adca0 | 207 | |
ryood | 11:7e11404adca0 | 208 | #if UART_TRACE |
ryood | 6:897d6392b408 | 209 | printf("%.1f\t%d\t", bpm, envelopeLength); |
ryood | 11:7e11404adca0 | 210 | |
ryood | 6:897d6392b408 | 211 | printf("%d\t%d\t", amplitudeParam.attack, amplitudeParam.release); |
ryood | 6:897d6392b408 | 212 | printf("%.2f\t%.2f\t%.2f\t", amplitudeParam.v0, amplitudeParam.v1, amplitudeParam.v2); |
ryood | 6:897d6392b408 | 213 | printf("%.2f\t%.2f\t", amplitudeParam.attackTauRatio, amplitudeParam.releaseTauRatio); |
ryood | 6:897d6392b408 | 214 | |
ryood | 6:897d6392b408 | 215 | printf("%d\t%d\t", frequencyParam.attack, frequencyParam.release); |
ryood | 6:897d6392b408 | 216 | printf("%.2f\t%.2f\t%.2f\t", frequencyParam.v0, frequencyParam.v1, frequencyParam.v2); |
ryood | 6:897d6392b408 | 217 | printf("%.2f\t%.2f\r\n", frequencyParam.attackTauRatio, frequencyParam.releaseTauRatio); |
ryood | 11:7e11404adca0 | 218 | #endif |
ryood | 11:7e11404adca0 | 219 | |
ryood | 13:43a43da257e3 | 220 | Thread::wait(1); |
ryood | 3:f89b400cfe57 | 221 | } |
ryood | 0:2dcec10e9199 | 222 | } |