KIK 01 Prototype 05

Dependencies:   AverageMCP3008 mbed-rtos mbed mcp3008

Fork of KIK01_Proto03 by Ryo Od

Committer:
ryood
Date:
Sun Sep 17 23:24:49 2017 +0000
Revision:
23:da7c5f7feff1
Parent:
22:a3bb7594f9bb
Child:
24:e9fbadd15e90
Impl. Amplitude Envelope.

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 17:a5d9908bd456 5 * 2017.09.16 Proto04: SPI1 for AD8402 Wein Bridge DCO & Internal DAC for Dual-OTA-VCA
ryood 15:8f674acdac03 6 * 2017.07.04 Proto03: MCP4922 DCA
ryood 13:43a43da257e3 7 * 2017.06.19 Proto02
ryood 3:f89b400cfe57 8 * 2017.06.04 created.
ryood 3:f89b400cfe57 9 *
ryood 3:f89b400cfe57 10 */
ryood 3:f89b400cfe57 11
ryood 0:2dcec10e9199 12 #include "mbed.h"
ryood 0:2dcec10e9199 13 #include "rtos.h"
ryood 13:43a43da257e3 14 #include "mcp3008.h"
ryood 22:a3bb7594f9bb 15 #include "EnvelopeAR.h"
ryood 0:2dcec10e9199 16
ryood 17:a5d9908bd456 17 #define UART_TRACE (1)
ryood 12:5b498285d121 18 #define PIN_CHECK (1)
ryood 0:2dcec10e9199 19 #define TITLE_STR1 ("KIK01 Kick Machine")
ryood 17:a5d9908bd456 20 #define TITLE_STR2 ("20170916")
ryood 0:2dcec10e9199 21
ryood 0:2dcec10e9199 22 #define PI_F (3.1415926f)
ryood 15:8f674acdac03 23
ryood 17:a5d9908bd456 24 #define MCP3008_SPI_SPEED (1000000)
ryood 21:a527f51381d6 25 #define AD8402_SPI_SPEED (10000000)
ryood 23:da7c5f7feff1 26 #define ENVELOPE_UPDATE_RATE (32000) // Hz
ryood 17:a5d9908bd456 27
ryood 20:70a05941db8c 28 #define AD8402_RMAX (10000.0f)
ryood 20:70a05941db8c 29 #define AD8402_RMIN (50.0f)
ryood 20:70a05941db8c 30
ryood 17:a5d9908bd456 31 AnalogOut Dac1(A2);
ryood 3:f89b400cfe57 32
ryood 18:1bf4abf6895b 33 // AD8402 SPI
ryood 21:a527f51381d6 34 SPI SpiMAD8402(SPI_MOSI, SPI_MISO, SPI_SCK);
ryood 18:1bf4abf6895b 35 DigitalOut AD8402Cs(D10);
ryood 15:8f674acdac03 36
ryood 18:1bf4abf6895b 37 // MCP3008 SPI
ryood 17:a5d9908bd456 38 SPI SpiM3(D4, D5, D3);
ryood 17:a5d9908bd456 39 MCP3008 Adc0(&SpiM3, D6);
ryood 17:a5d9908bd456 40 MCP3008 Adc1(&SpiM3, D7);
ryood 8:bb34a4894337 41
ryood 18:1bf4abf6895b 42 // Sync
ryood 18:1bf4abf6895b 43 DigitalOut SyncPin(D2);
ryood 18:1bf4abf6895b 44
ryood 14:8e96f97e261b 45 // Check pins
ryood 17:a5d9908bd456 46 DigitalOut Dout0(D8);
ryood 17:a5d9908bd456 47 DigitalOut Dout1(D9);
ryood 17:a5d9908bd456 48 DigitalOut Dout2(D14);
ryood 14:8e96f97e261b 49
ryood 9:d1e6eae9722b 50 EnvelopeAR envelopeFrequency(5, 300, 880.0f, 120.0f, 40.0f, 0.36f, 0.1f);
ryood 5:846772a77d33 51 EnvelopeAR envelopeAmplitude(50, 200, 0.99f, 1.0f, 0.0f);
ryood 5:846772a77d33 52
ryood 5:846772a77d33 53 volatile EnvelopeParam frequencyParam;
ryood 5:846772a77d33 54 volatile EnvelopeParam amplitudeParam;
ryood 0:2dcec10e9199 55
ryood 3:f89b400cfe57 56 volatile int ticks;
ryood 3:f89b400cfe57 57 volatile float frequency;
ryood 3:f89b400cfe57 58 volatile float amplitude;
ryood 3:f89b400cfe57 59
ryood 5:846772a77d33 60 volatile float bpm;
ryood 5:846772a77d33 61 volatile int envelopeLength;
ryood 14:8e96f97e261b 62 volatile int stepLength;
ryood 0:2dcec10e9199 63
ryood 17:a5d9908bd456 64 void AD8402Write(uint8_t address, uint8_t value)
ryood 17:a5d9908bd456 65 {
ryood 21:a527f51381d6 66 #if (PIN_CHECK)
ryood 21:a527f51381d6 67 Dout1 = 1;
ryood 21:a527f51381d6 68 #endif
ryood 21:a527f51381d6 69
ryood 18:1bf4abf6895b 70 AD8402Cs = 0;
ryood 21:a527f51381d6 71 SpiMAD8402.write(address);
ryood 21:a527f51381d6 72 SpiMAD8402.write(value);
ryood 18:1bf4abf6895b 73 AD8402Cs = 1;
ryood 17:a5d9908bd456 74 wait_us(1);
ryood 21:a527f51381d6 75
ryood 21:a527f51381d6 76 #if (PIN_CHECK)
ryood 21:a527f51381d6 77 Dout1 = 0;
ryood 21:a527f51381d6 78 #endif
ryood 17:a5d9908bd456 79 }
ryood 17:a5d9908bd456 80
ryood 23:da7c5f7feff1 81 void DcoSetFrequency(float freq)
ryood 17:a5d9908bd456 82 {
ryood 17:a5d9908bd456 83 const float c = 0.00000047;
ryood 17:a5d9908bd456 84 float r = 1.0f / (2.0f * PI_F * frequency * c);
ryood 20:70a05941db8c 85 if (r < AD8402_RMIN) r = AD8402_RMIN;
ryood 20:70a05941db8c 86 if (r > AD8402_RMAX) r = AD8402_RMAX;
ryood 20:70a05941db8c 87
ryood 20:70a05941db8c 88 uint8_t v = 256.0f * (r - AD8402_RMIN) / AD8402_RMAX;
ryood 21:a527f51381d6 89
ryood 17:a5d9908bd456 90 AD8402Write(0, v);
ryood 17:a5d9908bd456 91 AD8402Write(1, v);
ryood 23:da7c5f7feff1 92 }
ryood 17:a5d9908bd456 93
ryood 23:da7c5f7feff1 94 void DcaSetAmplitude(float amp)
ryood 23:da7c5f7feff1 95 {
ryood 23:da7c5f7feff1 96 Dac1.write(amp);
ryood 17:a5d9908bd456 97 }
ryood 15:8f674acdac03 98
ryood 2:8dff77a1ee4d 99 void update()
ryood 2:8dff77a1ee4d 100 {
ryood 12:5b498285d121 101 #if (PIN_CHECK)
ryood 15:8f674acdac03 102 Dout0 = 1;
ryood 12:5b498285d121 103 #endif
ryood 15:8f674acdac03 104
ryood 14:8e96f97e261b 105 // Output Sync Signal per steps
ryood 17:a5d9908bd456 106 if (ticks % stepLength == 0) {
ryood 14:8e96f97e261b 107 SyncPin = 1;
ryood 14:8e96f97e261b 108 }
ryood 15:8f674acdac03 109
ryood 17:a5d9908bd456 110 // set envelope parameters
ryood 17:a5d9908bd456 111 envelopeAmplitude.setAttack(amplitudeParam.attack);
ryood 17:a5d9908bd456 112 envelopeAmplitude.setRelease(amplitudeParam.release);
ryood 17:a5d9908bd456 113 envelopeAmplitude.setV0(amplitudeParam.v0);
ryood 17:a5d9908bd456 114 envelopeAmplitude.setV1(amplitudeParam.v1);
ryood 17:a5d9908bd456 115 envelopeAmplitude.setV2(amplitudeParam.v2);
ryood 17:a5d9908bd456 116 envelopeAmplitude.setAttackTauRatio(amplitudeParam.attackTauRatio);
ryood 17:a5d9908bd456 117 envelopeAmplitude.setReleaseTauRatio(amplitudeParam.releaseTauRatio);
ryood 11:7e11404adca0 118
ryood 17:a5d9908bd456 119 envelopeFrequency.setAttack(frequencyParam.attack);
ryood 17:a5d9908bd456 120 envelopeFrequency.setRelease(frequencyParam.release);
ryood 17:a5d9908bd456 121 envelopeFrequency.setV0(frequencyParam.v0);
ryood 17:a5d9908bd456 122 envelopeFrequency.setV1(frequencyParam.v1);
ryood 17:a5d9908bd456 123 envelopeFrequency.setV2(frequencyParam.v2);
ryood 17:a5d9908bd456 124 envelopeFrequency.setAttackTauRatio(frequencyParam.attackTauRatio);
ryood 17:a5d9908bd456 125 envelopeFrequency.setReleaseTauRatio(frequencyParam.releaseTauRatio);
ryood 11:7e11404adca0 126
ryood 23:da7c5f7feff1 127 frequency = envelopeFrequency.getAmplitude(ticks);
ryood 23:da7c5f7feff1 128 amplitude = envelopeAmplitude.getAmplitude(ticks);
ryood 23:da7c5f7feff1 129
ryood 23:da7c5f7feff1 130 DcoSetFrequency(frequency);
ryood 23:da7c5f7feff1 131 DcaSetAmplitude(amplitude);
ryood 11:7e11404adca0 132
ryood 17:a5d9908bd456 133 ticks++;
ryood 17:a5d9908bd456 134 if (ticks >= envelopeLength) {
ryood 17:a5d9908bd456 135 ticks = 0;
ryood 2:8dff77a1ee4d 136 }
ryood 15:8f674acdac03 137
ryood 14:8e96f97e261b 138 // Output SyncSignal
ryood 14:8e96f97e261b 139 SyncPin = 0;
ryood 15:8f674acdac03 140
ryood 12:5b498285d121 141 #if (PIN_CHECK)
ryood 15:8f674acdac03 142 Dout0 = 0;
ryood 12:5b498285d121 143 #endif
ryood 2:8dff77a1ee4d 144 }
ryood 2:8dff77a1ee4d 145
ryood 5:846772a77d33 146 void setParams()
ryood 5:846772a77d33 147 {
ryood 13:43a43da257e3 148 bpm = Adc0.read_input(7) * 180.0f + 60.0f;
ryood 12:5b498285d121 149 envelopeLength = 60 * ENVELOPE_UPDATE_RATE / bpm;
ryood 14:8e96f97e261b 150 stepLength = envelopeLength / 4;
ryood 11:7e11404adca0 151
ryood 13:43a43da257e3 152 amplitudeParam.attack = Adc0.read_input(0) * envelopeLength;
ryood 13:43a43da257e3 153 amplitudeParam.release = Adc0.read_input(1) * envelopeLength;
ryood 13:43a43da257e3 154 amplitudeParam.v0 = Adc0.read_input(4);
ryood 14:8e96f97e261b 155 amplitudeParam.v1 = Adc0.read_input(5);
ryood 14:8e96f97e261b 156 amplitudeParam.v2 = Adc0.read_input(6);
ryood 5:846772a77d33 157 amplitudeParam.attackTauRatio = 0.36f;
ryood 13:43a43da257e3 158 amplitudeParam.releaseTauRatio = Adc0.read_input(3) + 0.01f;
ryood 11:7e11404adca0 159
ryood 13:43a43da257e3 160 frequencyParam.attack = Adc1.read_input(0) * envelopeLength * 0.1f;
ryood 13:43a43da257e3 161 frequencyParam.release = Adc1.read_input(1) * envelopeLength + 1;
ryood 13:43a43da257e3 162 frequencyParam.v0 = Adc1.read_input(4) * 4000.0f;
ryood 13:43a43da257e3 163 frequencyParam.v1 = Adc1.read_input(5) * 400.0f;
ryood 13:43a43da257e3 164 frequencyParam.v2 = Adc1.read_input(6) * 400.0f;
ryood 13:43a43da257e3 165 frequencyParam.attackTauRatio = Adc1.read_input(2) + 0.01f;
ryood 13:43a43da257e3 166 frequencyParam.releaseTauRatio = Adc1.read_input(3) + 0.01f;
ryood 5:846772a77d33 167 }
ryood 5:846772a77d33 168
ryood 0:2dcec10e9199 169 int main()
ryood 0:2dcec10e9199 170 {
ryood 0:2dcec10e9199 171 printf("%s %s\r\n", TITLE_STR1, TITLE_STR2);
ryood 15:8f674acdac03 172
ryood 21:a527f51381d6 173 SpiMAD8402.format(8, 0);
ryood 21:a527f51381d6 174 SpiMAD8402.frequency(AD8402_SPI_SPEED);
ryood 17:a5d9908bd456 175
ryood 17:a5d9908bd456 176 SpiM3.format(8, 0);
ryood 17:a5d9908bd456 177 SpiM3.frequency(MCP3008_SPI_SPEED);
ryood 15:8f674acdac03 178
ryood 17:a5d9908bd456 179 frequency = 100.0f;
ryood 0:2dcec10e9199 180 amplitude = 1.0f;
ryood 17:a5d9908bd456 181 bpm = 120.0f;
ryood 17:a5d9908bd456 182
ryood 17:a5d9908bd456 183 setParams();
ryood 11:7e11404adca0 184
ryood 3:f89b400cfe57 185 ticks = 0;
ryood 2:8dff77a1ee4d 186 Ticker samplingTicker;
ryood 17:a5d9908bd456 187 samplingTicker.attach(&update, (1.0f/ENVELOPE_UPDATE_RATE));
ryood 11:7e11404adca0 188
ryood 3:f89b400cfe57 189 for (;;) {
ryood 13:43a43da257e3 190 #if (PIN_CHECK)
ryood 13:43a43da257e3 191 Dout2 = 1;
ryood 15:8f674acdac03 192 #endif
ryood 5:846772a77d33 193 setParams();
ryood 13:43a43da257e3 194 #if (PIN_CHECK)
ryood 13:43a43da257e3 195 Dout2 = 0;
ryood 15:8f674acdac03 196 #endif
ryood 11:7e11404adca0 197
ryood 11:7e11404adca0 198 #if UART_TRACE
ryood 6:897d6392b408 199 printf("%.1f\t%d\t", bpm, envelopeLength);
ryood 11:7e11404adca0 200
ryood 6:897d6392b408 201 printf("%d\t%d\t", amplitudeParam.attack, amplitudeParam.release);
ryood 6:897d6392b408 202 printf("%.2f\t%.2f\t%.2f\t", amplitudeParam.v0, amplitudeParam.v1, amplitudeParam.v2);
ryood 6:897d6392b408 203 printf("%.2f\t%.2f\t", amplitudeParam.attackTauRatio, amplitudeParam.releaseTauRatio);
ryood 6:897d6392b408 204
ryood 6:897d6392b408 205 printf("%d\t%d\t", frequencyParam.attack, frequencyParam.release);
ryood 6:897d6392b408 206 printf("%.2f\t%.2f\t%.2f\t", frequencyParam.v0, frequencyParam.v1, frequencyParam.v2);
ryood 6:897d6392b408 207 printf("%.2f\t%.2f\r\n", frequencyParam.attackTauRatio, frequencyParam.releaseTauRatio);
ryood 11:7e11404adca0 208 #endif
ryood 11:7e11404adca0 209
ryood 13:43a43da257e3 210 Thread::wait(1);
ryood 3:f89b400cfe57 211 }
ryood 0:2dcec10e9199 212 }