KIK 01 Prototype 05

Dependencies:   AverageMCP3008 mbed-rtos mbed mcp3008

Fork of KIK01_Proto03 by Ryo Od

Committer:
ryood
Date:
Thu Oct 19 07:57:15 2017 +0000
Revision:
29:947992b9904f
Parent:
28:387c5a6d5206
Child:
30:1291e20b1c53
ReadParams() from ADC3

Who changed what in which revision?

UserRevisionLine numberNew 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 29:947992b9904f 26 #define MCP3008_SPI_SPEED (1312500)
ryood 21:a527f51381d6 27 #define AD8402_SPI_SPEED (10000000)
ryood 29:947992b9904f 28 #define ENVELOPE_UPDATE_RATE (8000) // 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 29:947992b9904f 61 EnvelopeAR envelopeNoiseAmplitude(50, 200, 0.99f, 1.0f, 0.0f);
ryood 5:846772a77d33 62
ryood 25:b4977c7e0db7 63 EnvelopeParam frequencyParam;
ryood 25:b4977c7e0db7 64 EnvelopeParam amplitudeParam;
ryood 29:947992b9904f 65 EnvelopeParam noiseAmplitudeParam;
ryood 0:2dcec10e9199 66
ryood 3:f89b400cfe57 67 volatile int ticks;
ryood 3:f89b400cfe57 68 volatile float frequency;
ryood 3:f89b400cfe57 69 volatile float amplitude;
ryood 29:947992b9904f 70 volatile float noiseAmplitude;
ryood 3:f89b400cfe57 71
ryood 5:846772a77d33 72 volatile float bpm;
ryood 5:846772a77d33 73 volatile int envelopeLength;
ryood 14:8e96f97e261b 74 volatile int stepLength;
ryood 0:2dcec10e9199 75
ryood 17:a5d9908bd456 76 void AD8402Write(uint8_t address, uint8_t value)
ryood 17:a5d9908bd456 77 {
ryood 21:a527f51381d6 78 #if (PIN_CHECK)
ryood 26:866d672ba446 79 Dout2 = 1;
ryood 21:a527f51381d6 80 #endif
ryood 21:a527f51381d6 81
ryood 18:1bf4abf6895b 82 AD8402Cs = 0;
ryood 21:a527f51381d6 83 SpiMAD8402.write(address);
ryood 21:a527f51381d6 84 SpiMAD8402.write(value);
ryood 18:1bf4abf6895b 85 AD8402Cs = 1;
ryood 17:a5d9908bd456 86 wait_us(1);
ryood 21:a527f51381d6 87
ryood 21:a527f51381d6 88 #if (PIN_CHECK)
ryood 26:866d672ba446 89 Dout2 = 0;
ryood 21:a527f51381d6 90 #endif
ryood 17:a5d9908bd456 91 }
ryood 17:a5d9908bd456 92
ryood 23:da7c5f7feff1 93 void DcoSetFrequency(float freq)
ryood 17:a5d9908bd456 94 {
ryood 17:a5d9908bd456 95 const float c = 0.00000047;
ryood 17:a5d9908bd456 96 float r = 1.0f / (2.0f * PI_F * frequency * c);
ryood 20:70a05941db8c 97 if (r < AD8402_RMIN) r = AD8402_RMIN;
ryood 20:70a05941db8c 98 if (r > AD8402_RMAX) r = AD8402_RMAX;
ryood 20:70a05941db8c 99
ryood 20:70a05941db8c 100 uint8_t v = 256.0f * (r - AD8402_RMIN) / AD8402_RMAX;
ryood 21:a527f51381d6 101
ryood 17:a5d9908bd456 102 AD8402Write(0, v);
ryood 17:a5d9908bd456 103 AD8402Write(1, v);
ryood 23:da7c5f7feff1 104 }
ryood 17:a5d9908bd456 105
ryood 27:b07a55935230 106 void DcaSetAmplitude(int channel, float amp)
ryood 23:da7c5f7feff1 107 {
ryood 27:b07a55935230 108 switch (channel) {
ryood 27:b07a55935230 109 case 1:
ryood 27:b07a55935230 110 Dac1.write(amp);
ryood 27:b07a55935230 111 break;
ryood 27:b07a55935230 112 case 2:
ryood 27:b07a55935230 113 Dac2.write(amp * 0.8f); // Avoid LED,s Non-Linearity
ryood 27:b07a55935230 114 break;
ryood 27:b07a55935230 115 default:
ryood 27:b07a55935230 116 error("DcaSetAmplitude(): invalid channel");
ryood 27:b07a55935230 117 }
ryood 17:a5d9908bd456 118 }
ryood 15:8f674acdac03 119
ryood 2:8dff77a1ee4d 120 void update()
ryood 2:8dff77a1ee4d 121 {
ryood 12:5b498285d121 122 #if (PIN_CHECK)
ryood 26:866d672ba446 123 Dout1 = 1;
ryood 12:5b498285d121 124 #endif
ryood 15:8f674acdac03 125
ryood 14:8e96f97e261b 126 // Output Sync Signal per steps
ryood 17:a5d9908bd456 127 if (ticks % stepLength == 0) {
ryood 14:8e96f97e261b 128 SyncPin = 1;
ryood 14:8e96f97e261b 129 }
ryood 15:8f674acdac03 130
ryood 17:a5d9908bd456 131 // set envelope parameters
ryood 25:b4977c7e0db7 132 envelopeAmplitude.setParam(amplitudeParam);
ryood 25:b4977c7e0db7 133 envelopeFrequency.setParam(frequencyParam);
ryood 11:7e11404adca0 134
ryood 23:da7c5f7feff1 135 frequency = envelopeFrequency.getAmplitude(ticks);
ryood 23:da7c5f7feff1 136 amplitude = envelopeAmplitude.getAmplitude(ticks);
ryood 23:da7c5f7feff1 137
ryood 23:da7c5f7feff1 138 DcoSetFrequency(frequency);
ryood 27:b07a55935230 139 DcaSetAmplitude(1, amplitude); // DCO
ryood 27:b07a55935230 140 DcaSetAmplitude(2, amplitude); // NOS01
ryood 11:7e11404adca0 141
ryood 17:a5d9908bd456 142 ticks++;
ryood 17:a5d9908bd456 143 if (ticks >= envelopeLength) {
ryood 17:a5d9908bd456 144 ticks = 0;
ryood 2:8dff77a1ee4d 145 }
ryood 15:8f674acdac03 146
ryood 14:8e96f97e261b 147 // Output SyncSignal
ryood 14:8e96f97e261b 148 SyncPin = 0;
ryood 15:8f674acdac03 149
ryood 12:5b498285d121 150 #if (PIN_CHECK)
ryood 26:866d672ba446 151 Dout1 = 0;
ryood 12:5b498285d121 152 #endif
ryood 2:8dff77a1ee4d 153 }
ryood 2:8dff77a1ee4d 154
ryood 25:b4977c7e0db7 155 void readParams()
ryood 5:846772a77d33 156 {
ryood 28:387c5a6d5206 157 bpm = AvgAdc1.read_input(7) * 180.0f + 60.0f;
ryood 12:5b498285d121 158 envelopeLength = 60 * ENVELOPE_UPDATE_RATE / bpm;
ryood 14:8e96f97e261b 159 stepLength = envelopeLength / 4;
ryood 11:7e11404adca0 160
ryood 28:387c5a6d5206 161 amplitudeParam.attack = AvgAdc1.read_input(0) * envelopeLength;
ryood 28:387c5a6d5206 162 amplitudeParam.release = AvgAdc1.read_input(1) * envelopeLength;
ryood 28:387c5a6d5206 163 amplitudeParam.v0 = AvgAdc1.read_input(4);
ryood 28:387c5a6d5206 164 amplitudeParam.v1 = AvgAdc1.read_input(5);
ryood 28:387c5a6d5206 165 amplitudeParam.v2 = AvgAdc1.read_input(6);
ryood 28:387c5a6d5206 166 amplitudeParam.attackTauRatio = AvgAdc1.read_input(2) + 0.01f;
ryood 28:387c5a6d5206 167 amplitudeParam.releaseTauRatio = AvgAdc1.read_input(3) + 0.01f;
ryood 11:7e11404adca0 168
ryood 28:387c5a6d5206 169 frequencyParam.attack = AvgAdc2.read_input(0) * envelopeLength * 0.1f;
ryood 28:387c5a6d5206 170 frequencyParam.release = AvgAdc2.read_input(1) * envelopeLength + 1;
ryood 28:387c5a6d5206 171 frequencyParam.v0 = AvgAdc2.read_input(4) * 4000.0f;
ryood 28:387c5a6d5206 172 frequencyParam.v1 = AvgAdc2.read_input(5) * 400.0f;
ryood 28:387c5a6d5206 173 frequencyParam.v2 = AvgAdc2.read_input(6) * 400.0f;
ryood 28:387c5a6d5206 174 frequencyParam.attackTauRatio = AvgAdc2.read_input(2) + 0.01f;
ryood 28:387c5a6d5206 175 frequencyParam.releaseTauRatio = AvgAdc2.read_input(3) + 0.01f;
ryood 29:947992b9904f 176
ryood 29:947992b9904f 177 noiseAmplitudeParam.attack = AvgAdc3.read_input(0) * envelopeLength;
ryood 29:947992b9904f 178 noiseAmplitudeParam.release = AvgAdc3.read_input(1) * envelopeLength;
ryood 29:947992b9904f 179 noiseAmplitudeParam.v0 = AvgAdc3.read_input(4);
ryood 29:947992b9904f 180 noiseAmplitudeParam.v1 = AvgAdc3.read_input(5);
ryood 29:947992b9904f 181 noiseAmplitudeParam.v2 = AvgAdc3.read_input(6);
ryood 29:947992b9904f 182 noiseAmplitudeParam.attackTauRatio = AvgAdc3.read_input(2) + 0.01f;
ryood 29:947992b9904f 183 noiseAmplitudeParam.releaseTauRatio = AvgAdc3.read_input(3) + 0.01f;
ryood 5:846772a77d33 184 }
ryood 5:846772a77d33 185
ryood 0:2dcec10e9199 186 int main()
ryood 0:2dcec10e9199 187 {
ryood 27:b07a55935230 188 #if UART_TRACE
ryood 27:b07a55935230 189 printf("\r\n\n%s %s\r\n", TITLE_STR1, TITLE_STR2);
ryood 27:b07a55935230 190 #endif
ryood 15:8f674acdac03 191
ryood 21:a527f51381d6 192 SpiMAD8402.format(8, 0);
ryood 21:a527f51381d6 193 SpiMAD8402.frequency(AD8402_SPI_SPEED);
ryood 17:a5d9908bd456 194
ryood 26:866d672ba446 195 SpiMAdc.format(8, 0);
ryood 26:866d672ba446 196 SpiMAdc.frequency(MCP3008_SPI_SPEED);
ryood 15:8f674acdac03 197
ryood 17:a5d9908bd456 198 frequency = 100.0f;
ryood 0:2dcec10e9199 199 amplitude = 1.0f;
ryood 29:947992b9904f 200 noiseAmplitude = 1.0f;
ryood 17:a5d9908bd456 201 bpm = 120.0f;
ryood 17:a5d9908bd456 202
ryood 25:b4977c7e0db7 203 readParams();
ryood 11:7e11404adca0 204
ryood 3:f89b400cfe57 205 ticks = 0;
ryood 2:8dff77a1ee4d 206 Ticker samplingTicker;
ryood 17:a5d9908bd456 207 samplingTicker.attach(&update, (1.0f/ENVELOPE_UPDATE_RATE));
ryood 11:7e11404adca0 208
ryood 3:f89b400cfe57 209 for (;;) {
ryood 13:43a43da257e3 210 #if (PIN_CHECK)
ryood 26:866d672ba446 211 Dout3 = 1;
ryood 15:8f674acdac03 212 #endif
ryood 25:b4977c7e0db7 213
ryood 25:b4977c7e0db7 214 readParams();
ryood 25:b4977c7e0db7 215
ryood 13:43a43da257e3 216 #if (PIN_CHECK)
ryood 26:866d672ba446 217 Dout3 = 0;
ryood 15:8f674acdac03 218 #endif
ryood 11:7e11404adca0 219
ryood 11:7e11404adca0 220 #if UART_TRACE
ryood 6:897d6392b408 221 printf("%.1f\t%d\t", bpm, envelopeLength);
ryood 11:7e11404adca0 222
ryood 6:897d6392b408 223 printf("%d\t%d\t", amplitudeParam.attack, amplitudeParam.release);
ryood 6:897d6392b408 224 printf("%.2f\t%.2f\t%.2f\t", amplitudeParam.v0, amplitudeParam.v1, amplitudeParam.v2);
ryood 6:897d6392b408 225 printf("%.2f\t%.2f\t", amplitudeParam.attackTauRatio, amplitudeParam.releaseTauRatio);
ryood 6:897d6392b408 226
ryood 6:897d6392b408 227 printf("%d\t%d\t", frequencyParam.attack, frequencyParam.release);
ryood 6:897d6392b408 228 printf("%.2f\t%.2f\t%.2f\t", frequencyParam.v0, frequencyParam.v1, frequencyParam.v2);
ryood 29:947992b9904f 229 printf("%.2f\t%.2f\t", frequencyParam.attackTauRatio, frequencyParam.releaseTauRatio);
ryood 29:947992b9904f 230
ryood 29:947992b9904f 231 printf("%d\t%d\t", noiseAmplitudeParam.attack, noiseAmplitudeParam.release);
ryood 29:947992b9904f 232 printf("%.2f\t%.2f\t%.2f\t", noiseAmplitudeParam.v0, noiseAmplitudeParam.v1, noiseAmplitudeParam.v2);
ryood 29:947992b9904f 233 printf("%.2f\t%.2f\r\n", noiseAmplitudeParam.attackTauRatio, noiseAmplitudeParam.releaseTauRatio);
ryood 11:7e11404adca0 234 #endif
ryood 11:7e11404adca0 235
ryood 13:43a43da257e3 236 Thread::wait(1);
ryood 3:f89b400cfe57 237 }
ryood 0:2dcec10e9199 238 }