KIK 01 Prototype 05

Dependencies:   AverageMCP3008 mbed-rtos mbed mcp3008

Fork of KIK01_Proto03 by Ryo Od

Committer:
ryood
Date:
Thu Oct 19 06:10:00 2017 +0000
Revision:
26:866d672ba446
Parent:
25:b4977c7e0db7
Child:
27:b07a55935230
Add NOS1 Controller

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 26:866d672ba446 5 * 2017.10.19 Proto05: Add Noise Generator 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 22:a3bb7594f9bb 16 #include "EnvelopeAR.h"
ryood 0:2dcec10e9199 17
ryood 17:a5d9908bd456 18 #define UART_TRACE (1)
ryood 12:5b498285d121 19 #define PIN_CHECK (1)
ryood 0:2dcec10e9199 20 #define TITLE_STR1 ("KIK01 Kick Machine")
ryood 26:866d672ba446 21 #define TITLE_STR2 ("20171019")
ryood 0:2dcec10e9199 22
ryood 0:2dcec10e9199 23 #define PI_F (3.1415926f)
ryood 15:8f674acdac03 24
ryood 17:a5d9908bd456 25 #define MCP3008_SPI_SPEED (1000000)
ryood 21:a527f51381d6 26 #define AD8402_SPI_SPEED (10000000)
ryood 23:da7c5f7feff1 27 #define ENVELOPE_UPDATE_RATE (32000) // Hz
ryood 17:a5d9908bd456 28
ryood 20:70a05941db8c 29 #define AD8402_RMAX (10000.0f)
ryood 20:70a05941db8c 30 #define AD8402_RMIN (50.0f)
ryood 20:70a05941db8c 31
ryood 26:866d672ba446 32 AnalogOut Dac1(PA_4);
ryood 3:f89b400cfe57 33
ryood 26:866d672ba446 34 // AD8402 SPI (SPI2)
ryood 26:866d672ba446 35 // SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC)
ryood 26:866d672ba446 36 SPI SpiMAD8402(PB_15, PB_14, PB_13);
ryood 26:866d672ba446 37 DigitalOut AD8402Cs(PC_4);
ryood 15:8f674acdac03 38
ryood 18:1bf4abf6895b 39 // MCP3008 SPI
ryood 26:866d672ba446 40 // SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC)
ryood 26:866d672ba446 41 SPI SpiMAdc(PB_5, PB_4, PB_3);
ryood 26:866d672ba446 42 MCP3008 Adc1(&SpiMAdc, PB_10);
ryood 26:866d672ba446 43 MCP3008 Adc2(&SpiMAdc, PA_8);
ryood 26:866d672ba446 44 MCP3008 Adc3(&SpiMAdc, PA_9);
ryood 8:bb34a4894337 45
ryood 18:1bf4abf6895b 46 // Sync
ryood 26:866d672ba446 47 DigitalOut SyncPin(PA_10);
ryood 18:1bf4abf6895b 48
ryood 14:8e96f97e261b 49 // Check pins
ryood 26:866d672ba446 50 DigitalOut Dout1(PB_9);
ryood 26:866d672ba446 51 DigitalOut Dout2(PB_8);
ryood 26:866d672ba446 52 DigitalOut Dout3(PC_9);
ryood 14:8e96f97e261b 53
ryood 9:d1e6eae9722b 54 EnvelopeAR envelopeFrequency(5, 300, 880.0f, 120.0f, 40.0f, 0.36f, 0.1f);
ryood 5:846772a77d33 55 EnvelopeAR envelopeAmplitude(50, 200, 0.99f, 1.0f, 0.0f);
ryood 5:846772a77d33 56
ryood 25:b4977c7e0db7 57 EnvelopeParam frequencyParam;
ryood 25:b4977c7e0db7 58 EnvelopeParam amplitudeParam;
ryood 0:2dcec10e9199 59
ryood 3:f89b400cfe57 60 volatile int ticks;
ryood 3:f89b400cfe57 61 volatile float frequency;
ryood 3:f89b400cfe57 62 volatile float amplitude;
ryood 3:f89b400cfe57 63
ryood 5:846772a77d33 64 volatile float bpm;
ryood 5:846772a77d33 65 volatile int envelopeLength;
ryood 14:8e96f97e261b 66 volatile int stepLength;
ryood 0:2dcec10e9199 67
ryood 17:a5d9908bd456 68 void AD8402Write(uint8_t address, uint8_t value)
ryood 17:a5d9908bd456 69 {
ryood 21:a527f51381d6 70 #if (PIN_CHECK)
ryood 26:866d672ba446 71 Dout2 = 1;
ryood 21:a527f51381d6 72 #endif
ryood 21:a527f51381d6 73
ryood 18:1bf4abf6895b 74 AD8402Cs = 0;
ryood 21:a527f51381d6 75 SpiMAD8402.write(address);
ryood 21:a527f51381d6 76 SpiMAD8402.write(value);
ryood 18:1bf4abf6895b 77 AD8402Cs = 1;
ryood 17:a5d9908bd456 78 wait_us(1);
ryood 21:a527f51381d6 79
ryood 21:a527f51381d6 80 #if (PIN_CHECK)
ryood 26:866d672ba446 81 Dout2 = 0;
ryood 21:a527f51381d6 82 #endif
ryood 17:a5d9908bd456 83 }
ryood 17:a5d9908bd456 84
ryood 23:da7c5f7feff1 85 void DcoSetFrequency(float freq)
ryood 17:a5d9908bd456 86 {
ryood 17:a5d9908bd456 87 const float c = 0.00000047;
ryood 17:a5d9908bd456 88 float r = 1.0f / (2.0f * PI_F * frequency * c);
ryood 20:70a05941db8c 89 if (r < AD8402_RMIN) r = AD8402_RMIN;
ryood 20:70a05941db8c 90 if (r > AD8402_RMAX) r = AD8402_RMAX;
ryood 20:70a05941db8c 91
ryood 20:70a05941db8c 92 uint8_t v = 256.0f * (r - AD8402_RMIN) / AD8402_RMAX;
ryood 21:a527f51381d6 93
ryood 17:a5d9908bd456 94 AD8402Write(0, v);
ryood 17:a5d9908bd456 95 AD8402Write(1, v);
ryood 23:da7c5f7feff1 96 }
ryood 17:a5d9908bd456 97
ryood 23:da7c5f7feff1 98 void DcaSetAmplitude(float amp)
ryood 23:da7c5f7feff1 99 {
ryood 23:da7c5f7feff1 100 Dac1.write(amp);
ryood 17:a5d9908bd456 101 }
ryood 15:8f674acdac03 102
ryood 2:8dff77a1ee4d 103 void update()
ryood 2:8dff77a1ee4d 104 {
ryood 12:5b498285d121 105 #if (PIN_CHECK)
ryood 26:866d672ba446 106 Dout1 = 1;
ryood 12:5b498285d121 107 #endif
ryood 15:8f674acdac03 108
ryood 14:8e96f97e261b 109 // Output Sync Signal per steps
ryood 17:a5d9908bd456 110 if (ticks % stepLength == 0) {
ryood 14:8e96f97e261b 111 SyncPin = 1;
ryood 14:8e96f97e261b 112 }
ryood 15:8f674acdac03 113
ryood 17:a5d9908bd456 114 // set envelope parameters
ryood 25:b4977c7e0db7 115 envelopeAmplitude.setParam(amplitudeParam);
ryood 25:b4977c7e0db7 116 envelopeFrequency.setParam(frequencyParam);
ryood 11:7e11404adca0 117
ryood 23:da7c5f7feff1 118 frequency = envelopeFrequency.getAmplitude(ticks);
ryood 23:da7c5f7feff1 119 amplitude = envelopeAmplitude.getAmplitude(ticks);
ryood 23:da7c5f7feff1 120
ryood 23:da7c5f7feff1 121 DcoSetFrequency(frequency);
ryood 23:da7c5f7feff1 122 DcaSetAmplitude(amplitude);
ryood 11:7e11404adca0 123
ryood 17:a5d9908bd456 124 ticks++;
ryood 17:a5d9908bd456 125 if (ticks >= envelopeLength) {
ryood 17:a5d9908bd456 126 ticks = 0;
ryood 2:8dff77a1ee4d 127 }
ryood 15:8f674acdac03 128
ryood 14:8e96f97e261b 129 // Output SyncSignal
ryood 14:8e96f97e261b 130 SyncPin = 0;
ryood 15:8f674acdac03 131
ryood 12:5b498285d121 132 #if (PIN_CHECK)
ryood 26:866d672ba446 133 Dout1 = 0;
ryood 12:5b498285d121 134 #endif
ryood 2:8dff77a1ee4d 135 }
ryood 2:8dff77a1ee4d 136
ryood 25:b4977c7e0db7 137 void readParams()
ryood 5:846772a77d33 138 {
ryood 26:866d672ba446 139 bpm = Adc1.read_input(7) * 180.0f + 60.0f;
ryood 12:5b498285d121 140 envelopeLength = 60 * ENVELOPE_UPDATE_RATE / bpm;
ryood 14:8e96f97e261b 141 stepLength = envelopeLength / 4;
ryood 11:7e11404adca0 142
ryood 26:866d672ba446 143 amplitudeParam.attack = Adc1.read_input(0) * envelopeLength;
ryood 26:866d672ba446 144 amplitudeParam.release = Adc1.read_input(1) * envelopeLength;
ryood 26:866d672ba446 145 amplitudeParam.v0 = Adc1.read_input(4);
ryood 26:866d672ba446 146 amplitudeParam.v1 = Adc1.read_input(5);
ryood 26:866d672ba446 147 amplitudeParam.v2 = Adc1.read_input(6);
ryood 26:866d672ba446 148 amplitudeParam.attackTauRatio = Adc1.read_input(2) + 0.01f;
ryood 26:866d672ba446 149 amplitudeParam.releaseTauRatio = Adc1.read_input(3) + 0.01f;
ryood 11:7e11404adca0 150
ryood 26:866d672ba446 151 frequencyParam.attack = Adc2.read_input(0) * envelopeLength * 0.1f;
ryood 26:866d672ba446 152 frequencyParam.release = Adc2.read_input(1) * envelopeLength + 1;
ryood 26:866d672ba446 153 frequencyParam.v0 = Adc2.read_input(4) * 4000.0f;
ryood 26:866d672ba446 154 frequencyParam.v1 = Adc2.read_input(5) * 400.0f;
ryood 26:866d672ba446 155 frequencyParam.v2 = Adc2.read_input(6) * 400.0f;
ryood 26:866d672ba446 156 frequencyParam.attackTauRatio = Adc2.read_input(2) + 0.01f;
ryood 26:866d672ba446 157 frequencyParam.releaseTauRatio = Adc2.read_input(3) + 0.01f;
ryood 5:846772a77d33 158 }
ryood 5:846772a77d33 159
ryood 0:2dcec10e9199 160 int main()
ryood 0:2dcec10e9199 161 {
ryood 0:2dcec10e9199 162 printf("%s %s\r\n", TITLE_STR1, TITLE_STR2);
ryood 15:8f674acdac03 163
ryood 21:a527f51381d6 164 SpiMAD8402.format(8, 0);
ryood 21:a527f51381d6 165 SpiMAD8402.frequency(AD8402_SPI_SPEED);
ryood 17:a5d9908bd456 166
ryood 26:866d672ba446 167 SpiMAdc.format(8, 0);
ryood 26:866d672ba446 168 SpiMAdc.frequency(MCP3008_SPI_SPEED);
ryood 15:8f674acdac03 169
ryood 17:a5d9908bd456 170 frequency = 100.0f;
ryood 0:2dcec10e9199 171 amplitude = 1.0f;
ryood 17:a5d9908bd456 172 bpm = 120.0f;
ryood 17:a5d9908bd456 173
ryood 25:b4977c7e0db7 174 readParams();
ryood 11:7e11404adca0 175
ryood 3:f89b400cfe57 176 ticks = 0;
ryood 2:8dff77a1ee4d 177 Ticker samplingTicker;
ryood 17:a5d9908bd456 178 samplingTicker.attach(&update, (1.0f/ENVELOPE_UPDATE_RATE));
ryood 11:7e11404adca0 179
ryood 3:f89b400cfe57 180 for (;;) {
ryood 13:43a43da257e3 181 #if (PIN_CHECK)
ryood 26:866d672ba446 182 Dout3 = 1;
ryood 15:8f674acdac03 183 #endif
ryood 25:b4977c7e0db7 184
ryood 25:b4977c7e0db7 185 readParams();
ryood 25:b4977c7e0db7 186
ryood 13:43a43da257e3 187 #if (PIN_CHECK)
ryood 26:866d672ba446 188 Dout3 = 0;
ryood 15:8f674acdac03 189 #endif
ryood 11:7e11404adca0 190
ryood 11:7e11404adca0 191 #if UART_TRACE
ryood 6:897d6392b408 192 printf("%.1f\t%d\t", bpm, envelopeLength);
ryood 11:7e11404adca0 193
ryood 6:897d6392b408 194 printf("%d\t%d\t", amplitudeParam.attack, amplitudeParam.release);
ryood 6:897d6392b408 195 printf("%.2f\t%.2f\t%.2f\t", amplitudeParam.v0, amplitudeParam.v1, amplitudeParam.v2);
ryood 6:897d6392b408 196 printf("%.2f\t%.2f\t", amplitudeParam.attackTauRatio, amplitudeParam.releaseTauRatio);
ryood 6:897d6392b408 197
ryood 6:897d6392b408 198 printf("%d\t%d\t", frequencyParam.attack, frequencyParam.release);
ryood 6:897d6392b408 199 printf("%.2f\t%.2f\t%.2f\t", frequencyParam.v0, frequencyParam.v1, frequencyParam.v2);
ryood 6:897d6392b408 200 printf("%.2f\t%.2f\r\n", frequencyParam.attackTauRatio, frequencyParam.releaseTauRatio);
ryood 11:7e11404adca0 201 #endif
ryood 11:7e11404adca0 202
ryood 13:43a43da257e3 203 Thread::wait(1);
ryood 3:f89b400cfe57 204 }
ryood 0:2dcec10e9199 205 }