KIK 01 Prototype 05

Dependencies:   AverageMCP3008 mbed-rtos mbed mcp3008

Fork of KIK01_Proto03 by Ryo Od

Committer:
ryood
Date:
Thu Sep 28 02:32:53 2017 +0000
Revision:
25:b4977c7e0db7
Parent:
24:e9fbadd15e90
Child:
26:866d672ba446
clean up

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 25:b4977c7e0db7 20 #define TITLE_STR2 ("20170928")
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 25:b4977c7e0db7 53 EnvelopeParam frequencyParam;
ryood 25:b4977c7e0db7 54 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 25:b4977c7e0db7 111 envelopeAmplitude.setParam(amplitudeParam);
ryood 25:b4977c7e0db7 112 envelopeFrequency.setParam(frequencyParam);
ryood 11:7e11404adca0 113
ryood 23:da7c5f7feff1 114 frequency = envelopeFrequency.getAmplitude(ticks);
ryood 23:da7c5f7feff1 115 amplitude = envelopeAmplitude.getAmplitude(ticks);
ryood 23:da7c5f7feff1 116
ryood 23:da7c5f7feff1 117 DcoSetFrequency(frequency);
ryood 23:da7c5f7feff1 118 DcaSetAmplitude(amplitude);
ryood 11:7e11404adca0 119
ryood 17:a5d9908bd456 120 ticks++;
ryood 17:a5d9908bd456 121 if (ticks >= envelopeLength) {
ryood 17:a5d9908bd456 122 ticks = 0;
ryood 2:8dff77a1ee4d 123 }
ryood 15:8f674acdac03 124
ryood 14:8e96f97e261b 125 // Output SyncSignal
ryood 14:8e96f97e261b 126 SyncPin = 0;
ryood 15:8f674acdac03 127
ryood 12:5b498285d121 128 #if (PIN_CHECK)
ryood 15:8f674acdac03 129 Dout0 = 0;
ryood 12:5b498285d121 130 #endif
ryood 2:8dff77a1ee4d 131 }
ryood 2:8dff77a1ee4d 132
ryood 25:b4977c7e0db7 133 void readParams()
ryood 5:846772a77d33 134 {
ryood 13:43a43da257e3 135 bpm = Adc0.read_input(7) * 180.0f + 60.0f;
ryood 12:5b498285d121 136 envelopeLength = 60 * ENVELOPE_UPDATE_RATE / bpm;
ryood 14:8e96f97e261b 137 stepLength = envelopeLength / 4;
ryood 11:7e11404adca0 138
ryood 13:43a43da257e3 139 amplitudeParam.attack = Adc0.read_input(0) * envelopeLength;
ryood 13:43a43da257e3 140 amplitudeParam.release = Adc0.read_input(1) * envelopeLength;
ryood 13:43a43da257e3 141 amplitudeParam.v0 = Adc0.read_input(4);
ryood 14:8e96f97e261b 142 amplitudeParam.v1 = Adc0.read_input(5);
ryood 14:8e96f97e261b 143 amplitudeParam.v2 = Adc0.read_input(6);
ryood 24:e9fbadd15e90 144 amplitudeParam.attackTauRatio = Adc0.read_input(2) + 0.01f;
ryood 13:43a43da257e3 145 amplitudeParam.releaseTauRatio = Adc0.read_input(3) + 0.01f;
ryood 11:7e11404adca0 146
ryood 13:43a43da257e3 147 frequencyParam.attack = Adc1.read_input(0) * envelopeLength * 0.1f;
ryood 13:43a43da257e3 148 frequencyParam.release = Adc1.read_input(1) * envelopeLength + 1;
ryood 13:43a43da257e3 149 frequencyParam.v0 = Adc1.read_input(4) * 4000.0f;
ryood 13:43a43da257e3 150 frequencyParam.v1 = Adc1.read_input(5) * 400.0f;
ryood 13:43a43da257e3 151 frequencyParam.v2 = Adc1.read_input(6) * 400.0f;
ryood 13:43a43da257e3 152 frequencyParam.attackTauRatio = Adc1.read_input(2) + 0.01f;
ryood 13:43a43da257e3 153 frequencyParam.releaseTauRatio = Adc1.read_input(3) + 0.01f;
ryood 5:846772a77d33 154 }
ryood 5:846772a77d33 155
ryood 0:2dcec10e9199 156 int main()
ryood 0:2dcec10e9199 157 {
ryood 0:2dcec10e9199 158 printf("%s %s\r\n", TITLE_STR1, TITLE_STR2);
ryood 15:8f674acdac03 159
ryood 21:a527f51381d6 160 SpiMAD8402.format(8, 0);
ryood 21:a527f51381d6 161 SpiMAD8402.frequency(AD8402_SPI_SPEED);
ryood 17:a5d9908bd456 162
ryood 17:a5d9908bd456 163 SpiM3.format(8, 0);
ryood 17:a5d9908bd456 164 SpiM3.frequency(MCP3008_SPI_SPEED);
ryood 15:8f674acdac03 165
ryood 17:a5d9908bd456 166 frequency = 100.0f;
ryood 0:2dcec10e9199 167 amplitude = 1.0f;
ryood 17:a5d9908bd456 168 bpm = 120.0f;
ryood 17:a5d9908bd456 169
ryood 25:b4977c7e0db7 170 readParams();
ryood 11:7e11404adca0 171
ryood 3:f89b400cfe57 172 ticks = 0;
ryood 2:8dff77a1ee4d 173 Ticker samplingTicker;
ryood 17:a5d9908bd456 174 samplingTicker.attach(&update, (1.0f/ENVELOPE_UPDATE_RATE));
ryood 11:7e11404adca0 175
ryood 3:f89b400cfe57 176 for (;;) {
ryood 13:43a43da257e3 177 #if (PIN_CHECK)
ryood 13:43a43da257e3 178 Dout2 = 1;
ryood 15:8f674acdac03 179 #endif
ryood 25:b4977c7e0db7 180
ryood 25:b4977c7e0db7 181 readParams();
ryood 25:b4977c7e0db7 182
ryood 13:43a43da257e3 183 #if (PIN_CHECK)
ryood 13:43a43da257e3 184 Dout2 = 0;
ryood 15:8f674acdac03 185 #endif
ryood 11:7e11404adca0 186
ryood 11:7e11404adca0 187 #if UART_TRACE
ryood 6:897d6392b408 188 printf("%.1f\t%d\t", bpm, envelopeLength);
ryood 11:7e11404adca0 189
ryood 6:897d6392b408 190 printf("%d\t%d\t", amplitudeParam.attack, amplitudeParam.release);
ryood 6:897d6392b408 191 printf("%.2f\t%.2f\t%.2f\t", amplitudeParam.v0, amplitudeParam.v1, amplitudeParam.v2);
ryood 6:897d6392b408 192 printf("%.2f\t%.2f\t", amplitudeParam.attackTauRatio, amplitudeParam.releaseTauRatio);
ryood 6:897d6392b408 193
ryood 6:897d6392b408 194 printf("%d\t%d\t", frequencyParam.attack, frequencyParam.release);
ryood 6:897d6392b408 195 printf("%.2f\t%.2f\t%.2f\t", frequencyParam.v0, frequencyParam.v1, frequencyParam.v2);
ryood 6:897d6392b408 196 printf("%.2f\t%.2f\r\n", frequencyParam.attackTauRatio, frequencyParam.releaseTauRatio);
ryood 11:7e11404adca0 197 #endif
ryood 11:7e11404adca0 198
ryood 13:43a43da257e3 199 Thread::wait(1);
ryood 3:f89b400cfe57 200 }
ryood 0:2dcec10e9199 201 }