KIK 01 Prototype 05

Dependencies:   AverageMCP3008 mbed-rtos mbed mcp3008

Fork of KIK01_Proto03 by Ryo Od

Committer:
ryood
Date:
Sat Sep 16 00:01:49 2017 +0000
Revision:
19:d9e4c66cb49b
Parent:
18:1bf4abf6895b
Child:
20:70a05941db8c
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 0:2dcec10e9199 15
ryood 17:a5d9908bd456 16 #define UART_TRACE (1)
ryood 12:5b498285d121 17 #define PIN_CHECK (1)
ryood 0:2dcec10e9199 18 #define TITLE_STR1 ("KIK01 Kick Machine")
ryood 17:a5d9908bd456 19 #define TITLE_STR2 ("20170916")
ryood 0:2dcec10e9199 20
ryood 0:2dcec10e9199 21 #define PI_F (3.1415926f)
ryood 15:8f674acdac03 22
ryood 17:a5d9908bd456 23 #define MCP3008_SPI_SPEED (1000000)
ryood 17:a5d9908bd456 24 #define AD8402_SPI_SPEED (4000000)
ryood 18:1bf4abf6895b 25 #define ENVELOPE_UPDATE_RATE (10000) // Hz
ryood 17:a5d9908bd456 26
ryood 17:a5d9908bd456 27 AnalogOut Dac1(A2);
ryood 3:f89b400cfe57 28
ryood 18:1bf4abf6895b 29 // AD8402 SPI
ryood 16:30cb822e072f 30 SPI SpiM1(SPI_MOSI, SPI_MISO, SPI_SCK);
ryood 18:1bf4abf6895b 31 DigitalOut AD8402Cs(D10);
ryood 15:8f674acdac03 32
ryood 18:1bf4abf6895b 33 // MCP3008 SPI
ryood 17:a5d9908bd456 34 SPI SpiM3(D4, D5, D3);
ryood 17:a5d9908bd456 35 MCP3008 Adc0(&SpiM3, D6);
ryood 17:a5d9908bd456 36 MCP3008 Adc1(&SpiM3, D7);
ryood 8:bb34a4894337 37
ryood 18:1bf4abf6895b 38 // Sync
ryood 18:1bf4abf6895b 39 DigitalOut SyncPin(D2);
ryood 18:1bf4abf6895b 40
ryood 14:8e96f97e261b 41 // Check pins
ryood 17:a5d9908bd456 42 DigitalOut Dout0(D8);
ryood 17:a5d9908bd456 43 DigitalOut Dout1(D9);
ryood 17:a5d9908bd456 44 DigitalOut Dout2(D14);
ryood 14:8e96f97e261b 45
ryood 11:7e11404adca0 46 class EnvelopeAR
ryood 11:7e11404adca0 47 {
ryood 0:2dcec10e9199 48 public:
ryood 0:2dcec10e9199 49 EnvelopeAR(int _attack, int _release, float _v0, float _v1, float _v2, float _attackTauRatio=0.36f, float _releaseTauRatio=0.36f) :
ryood 0:2dcec10e9199 50 amplitude(_v0),
ryood 0:2dcec10e9199 51 v0(_v0),
ryood 0:2dcec10e9199 52 v1(_v1),
ryood 0:2dcec10e9199 53 v2(_v2),
ryood 0:2dcec10e9199 54 vLast(_v0),
ryood 0:2dcec10e9199 55 attackTauRatio(_attackTauRatio),
ryood 11:7e11404adca0 56 releaseTauRatio(_releaseTauRatio) {
ryood 0:2dcec10e9199 57 setAttack(_attack);
ryood 0:2dcec10e9199 58 setRelease(_release);
ryood 0:2dcec10e9199 59 }
ryood 0:2dcec10e9199 60
ryood 0:2dcec10e9199 61 ~EnvelopeAR() {}
ryood 0:2dcec10e9199 62
ryood 0:2dcec10e9199 63 void setAttack(int _attack) {
ryood 0:2dcec10e9199 64 attack = _attack;
ryood 0:2dcec10e9199 65 tau0 = attack * attackTauRatio;
ryood 0:2dcec10e9199 66 }
ryood 11:7e11404adca0 67 int getAttack() {
ryood 11:7e11404adca0 68 return attack;
ryood 11:7e11404adca0 69 }
ryood 0:2dcec10e9199 70
ryood 0:2dcec10e9199 71 void setRelease(int _release) {
ryood 0:2dcec10e9199 72 release = _release;
ryood 0:2dcec10e9199 73 tau1 = release * releaseTauRatio;
ryood 0:2dcec10e9199 74 }
ryood 11:7e11404adca0 75 int getRelease() {
ryood 11:7e11404adca0 76 return release;
ryood 11:7e11404adca0 77 }
ryood 0:2dcec10e9199 78
ryood 5:846772a77d33 79 void setAttackTauRatio(float _attackTauRatio) {
ryood 5:846772a77d33 80 attackTauRatio = _attackTauRatio;
ryood 5:846772a77d33 81 tau0 = attack * attackTauRatio;
ryood 5:846772a77d33 82 }
ryood 11:7e11404adca0 83 float getAttackTauRatio() {
ryood 11:7e11404adca0 84 return attackTauRatio;
ryood 11:7e11404adca0 85 }
ryood 0:2dcec10e9199 86
ryood 11:7e11404adca0 87 void setReleaseTauRatio(float _releaseTauRatio) {
ryood 5:846772a77d33 88 releaseTauRatio = _releaseTauRatio;
ryood 5:846772a77d33 89 tau1 = release * releaseTauRatio;
ryood 5:846772a77d33 90 }
ryood 11:7e11404adca0 91 float getReleaseTauRatio() {
ryood 11:7e11404adca0 92 return releaseTauRatio;
ryood 11:7e11404adca0 93 }
ryood 11:7e11404adca0 94
ryood 11:7e11404adca0 95 float getTau0() {
ryood 11:7e11404adca0 96 return tau0;
ryood 11:7e11404adca0 97 }
ryood 11:7e11404adca0 98 float getTau1() {
ryood 11:7e11404adca0 99 return tau1;
ryood 11:7e11404adca0 100 }
ryood 0:2dcec10e9199 101
ryood 11:7e11404adca0 102 void setV0(float _v0) {
ryood 11:7e11404adca0 103 v0 = _v0;
ryood 11:7e11404adca0 104 }
ryood 11:7e11404adca0 105 float getV0() {
ryood 11:7e11404adca0 106 return v0;
ryood 11:7e11404adca0 107 }
ryood 11:7e11404adca0 108 void setV1(float _v1) {
ryood 11:7e11404adca0 109 v1 = _v1;
ryood 11:7e11404adca0 110 }
ryood 11:7e11404adca0 111 float getV1() {
ryood 11:7e11404adca0 112 return v1;
ryood 11:7e11404adca0 113 }
ryood 11:7e11404adca0 114 void setV2(float _v2) {
ryood 11:7e11404adca0 115 v2 = _v2;
ryood 11:7e11404adca0 116 }
ryood 11:7e11404adca0 117 float getV2() {
ryood 11:7e11404adca0 118 return v2;
ryood 11:7e11404adca0 119 }
ryood 0:2dcec10e9199 120
ryood 11:7e11404adca0 121 float getAmplitude() {
ryood 11:7e11404adca0 122 return amplitude;
ryood 11:7e11404adca0 123 }
ryood 0:2dcec10e9199 124 float getAmplitude(int tick) {
ryood 0:2dcec10e9199 125 if (tick < attack) {
ryood 0:2dcec10e9199 126 // attackの処理
ryood 0:2dcec10e9199 127 amplitude = v0 + (v1 - v0) * (1 - expf(-(float)tick / tau0));
ryood 0:2dcec10e9199 128 vLast = amplitude;
ryood 11:7e11404adca0 129 } else {
ryood 0:2dcec10e9199 130 // releaseの処理
ryood 0:2dcec10e9199 131 amplitude = (vLast - v2) * (expf(-(float)(tick - attack) / tau1)) + v2;
ryood 0:2dcec10e9199 132 }
ryood 0:2dcec10e9199 133 return amplitude;
ryood 0:2dcec10e9199 134 }
ryood 0:2dcec10e9199 135
ryood 0:2dcec10e9199 136 private:
ryood 0:2dcec10e9199 137 int attack;
ryood 0:2dcec10e9199 138 int release;
ryood 0:2dcec10e9199 139 float amplitude;
ryood 0:2dcec10e9199 140 float v0;
ryood 0:2dcec10e9199 141 float v1;
ryood 0:2dcec10e9199 142 float v2;
ryood 0:2dcec10e9199 143 float vLast;
ryood 0:2dcec10e9199 144 float tau0;
ryood 0:2dcec10e9199 145 float tau1;
ryood 0:2dcec10e9199 146 float attackTauRatio;
ryood 0:2dcec10e9199 147 float releaseTauRatio;
ryood 0:2dcec10e9199 148 };
ryood 0:2dcec10e9199 149
ryood 11:7e11404adca0 150 class EnvelopeParam
ryood 11:7e11404adca0 151 {
ryood 5:846772a77d33 152 public:
ryood 5:846772a77d33 153 int attack;
ryood 5:846772a77d33 154 int release;
ryood 5:846772a77d33 155 float v0;
ryood 5:846772a77d33 156 float v1;
ryood 5:846772a77d33 157 float v2;
ryood 5:846772a77d33 158 float attackTauRatio;
ryood 5:846772a77d33 159 float releaseTauRatio;
ryood 5:846772a77d33 160 };
ryood 11:7e11404adca0 161
ryood 9:d1e6eae9722b 162 EnvelopeAR envelopeFrequency(5, 300, 880.0f, 120.0f, 40.0f, 0.36f, 0.1f);
ryood 5:846772a77d33 163 EnvelopeAR envelopeAmplitude(50, 200, 0.99f, 1.0f, 0.0f);
ryood 5:846772a77d33 164
ryood 5:846772a77d33 165 volatile EnvelopeParam frequencyParam;
ryood 5:846772a77d33 166 volatile EnvelopeParam amplitudeParam;
ryood 0:2dcec10e9199 167
ryood 3:f89b400cfe57 168 volatile int ticks;
ryood 3:f89b400cfe57 169 volatile float frequency;
ryood 3:f89b400cfe57 170 volatile float amplitude;
ryood 3:f89b400cfe57 171
ryood 5:846772a77d33 172 volatile float bpm;
ryood 5:846772a77d33 173 volatile int envelopeLength;
ryood 14:8e96f97e261b 174 volatile int stepLength;
ryood 0:2dcec10e9199 175
ryood 17:a5d9908bd456 176 void AD8402Write(uint8_t address, uint8_t value)
ryood 17:a5d9908bd456 177 {
ryood 18:1bf4abf6895b 178 AD8402Cs = 0;
ryood 18:1bf4abf6895b 179 SpiM1.write(address);
ryood 18:1bf4abf6895b 180 SpiM1.write(value);
ryood 18:1bf4abf6895b 181 AD8402Cs = 1;
ryood 17:a5d9908bd456 182 wait_us(1);
ryood 17:a5d9908bd456 183 }
ryood 17:a5d9908bd456 184
ryood 17:a5d9908bd456 185 void DcoSetFrequency()
ryood 17:a5d9908bd456 186 {
ryood 17:a5d9908bd456 187 #if (PIN_CHECK)
ryood 17:a5d9908bd456 188 Dout1 = 1;
ryood 17:a5d9908bd456 189 #endif
ryood 17:a5d9908bd456 190
ryood 17:a5d9908bd456 191 frequency = envelopeFrequency.getAmplitude(ticks);
ryood 17:a5d9908bd456 192 const float c = 0.00000047;
ryood 17:a5d9908bd456 193 float r = 1.0f / (2.0f * PI_F * frequency * c);
ryood 17:a5d9908bd456 194 uint8_t v = 256.0f * (r - 50.0f) / 10000.0f;
ryood 17:a5d9908bd456 195
ryood 17:a5d9908bd456 196 AD8402Write(0, v);
ryood 17:a5d9908bd456 197 AD8402Write(1, v);
ryood 17:a5d9908bd456 198
ryood 17:a5d9908bd456 199 #if (PIN_CHECK)
ryood 17:a5d9908bd456 200 Dout1 = 0;
ryood 17:a5d9908bd456 201 #endif
ryood 17:a5d9908bd456 202 }
ryood 15:8f674acdac03 203
ryood 2:8dff77a1ee4d 204 void update()
ryood 2:8dff77a1ee4d 205 {
ryood 12:5b498285d121 206 #if (PIN_CHECK)
ryood 15:8f674acdac03 207 Dout0 = 1;
ryood 12:5b498285d121 208 #endif
ryood 15:8f674acdac03 209
ryood 14:8e96f97e261b 210 // Output Sync Signal per steps
ryood 17:a5d9908bd456 211 if (ticks % stepLength == 0) {
ryood 14:8e96f97e261b 212 SyncPin = 1;
ryood 14:8e96f97e261b 213 }
ryood 15:8f674acdac03 214
ryood 17:a5d9908bd456 215 // set envelope parameters
ryood 17:a5d9908bd456 216 envelopeAmplitude.setAttack(amplitudeParam.attack);
ryood 17:a5d9908bd456 217 envelopeAmplitude.setRelease(amplitudeParam.release);
ryood 17:a5d9908bd456 218 envelopeAmplitude.setV0(amplitudeParam.v0);
ryood 17:a5d9908bd456 219 envelopeAmplitude.setV1(amplitudeParam.v1);
ryood 17:a5d9908bd456 220 envelopeAmplitude.setV2(amplitudeParam.v2);
ryood 17:a5d9908bd456 221 envelopeAmplitude.setAttackTauRatio(amplitudeParam.attackTauRatio);
ryood 17:a5d9908bd456 222 envelopeAmplitude.setReleaseTauRatio(amplitudeParam.releaseTauRatio);
ryood 11:7e11404adca0 223
ryood 17:a5d9908bd456 224 envelopeFrequency.setAttack(frequencyParam.attack);
ryood 17:a5d9908bd456 225 envelopeFrequency.setRelease(frequencyParam.release);
ryood 17:a5d9908bd456 226 envelopeFrequency.setV0(frequencyParam.v0);
ryood 17:a5d9908bd456 227 envelopeFrequency.setV1(frequencyParam.v1);
ryood 17:a5d9908bd456 228 envelopeFrequency.setV2(frequencyParam.v2);
ryood 17:a5d9908bd456 229 envelopeFrequency.setAttackTauRatio(frequencyParam.attackTauRatio);
ryood 17:a5d9908bd456 230 envelopeFrequency.setReleaseTauRatio(frequencyParam.releaseTauRatio);
ryood 11:7e11404adca0 231
ryood 17:a5d9908bd456 232 DcoSetFrequency();
ryood 11:7e11404adca0 233
ryood 17:a5d9908bd456 234 ticks++;
ryood 17:a5d9908bd456 235 if (ticks >= envelopeLength) {
ryood 17:a5d9908bd456 236 ticks = 0;
ryood 2:8dff77a1ee4d 237 }
ryood 15:8f674acdac03 238
ryood 14:8e96f97e261b 239 // Output SyncSignal
ryood 14:8e96f97e261b 240 SyncPin = 0;
ryood 15:8f674acdac03 241
ryood 12:5b498285d121 242 #if (PIN_CHECK)
ryood 15:8f674acdac03 243 Dout0 = 0;
ryood 12:5b498285d121 244 #endif
ryood 2:8dff77a1ee4d 245 }
ryood 2:8dff77a1ee4d 246
ryood 5:846772a77d33 247 void setParams()
ryood 5:846772a77d33 248 {
ryood 13:43a43da257e3 249 bpm = Adc0.read_input(7) * 180.0f + 60.0f;
ryood 12:5b498285d121 250 envelopeLength = 60 * ENVELOPE_UPDATE_RATE / bpm;
ryood 14:8e96f97e261b 251 stepLength = envelopeLength / 4;
ryood 11:7e11404adca0 252
ryood 13:43a43da257e3 253 amplitudeParam.attack = Adc0.read_input(0) * envelopeLength;
ryood 13:43a43da257e3 254 amplitudeParam.release = Adc0.read_input(1) * envelopeLength;
ryood 13:43a43da257e3 255 amplitudeParam.v0 = Adc0.read_input(4);
ryood 14:8e96f97e261b 256 amplitudeParam.v1 = Adc0.read_input(5);
ryood 14:8e96f97e261b 257 amplitudeParam.v2 = Adc0.read_input(6);
ryood 5:846772a77d33 258 amplitudeParam.attackTauRatio = 0.36f;
ryood 13:43a43da257e3 259 amplitudeParam.releaseTauRatio = Adc0.read_input(3) + 0.01f;
ryood 11:7e11404adca0 260
ryood 13:43a43da257e3 261 frequencyParam.attack = Adc1.read_input(0) * envelopeLength * 0.1f;
ryood 13:43a43da257e3 262 frequencyParam.release = Adc1.read_input(1) * envelopeLength + 1;
ryood 13:43a43da257e3 263 frequencyParam.v0 = Adc1.read_input(4) * 4000.0f;
ryood 13:43a43da257e3 264 frequencyParam.v1 = Adc1.read_input(5) * 400.0f;
ryood 13:43a43da257e3 265 frequencyParam.v2 = Adc1.read_input(6) * 400.0f;
ryood 13:43a43da257e3 266 frequencyParam.attackTauRatio = Adc1.read_input(2) + 0.01f;
ryood 13:43a43da257e3 267 frequencyParam.releaseTauRatio = Adc1.read_input(3) + 0.01f;
ryood 5:846772a77d33 268 }
ryood 5:846772a77d33 269
ryood 0:2dcec10e9199 270 int main()
ryood 0:2dcec10e9199 271 {
ryood 0:2dcec10e9199 272 printf("%s %s\r\n", TITLE_STR1, TITLE_STR2);
ryood 15:8f674acdac03 273
ryood 17:a5d9908bd456 274 /*
ryood 16:30cb822e072f 275 SpiM1.format(0, 0);
ryood 16:30cb822e072f 276 SpiM1.frequency(2000000);
ryood 17:a5d9908bd456 277 */
ryood 17:a5d9908bd456 278
ryood 17:a5d9908bd456 279 SpiM3.format(8, 0);
ryood 17:a5d9908bd456 280 SpiM3.frequency(MCP3008_SPI_SPEED);
ryood 15:8f674acdac03 281
ryood 17:a5d9908bd456 282 frequency = 100.0f;
ryood 0:2dcec10e9199 283 amplitude = 1.0f;
ryood 17:a5d9908bd456 284 bpm = 120.0f;
ryood 17:a5d9908bd456 285
ryood 17:a5d9908bd456 286 setParams();
ryood 11:7e11404adca0 287
ryood 3:f89b400cfe57 288 ticks = 0;
ryood 2:8dff77a1ee4d 289 Ticker samplingTicker;
ryood 17:a5d9908bd456 290 samplingTicker.attach(&update, (1.0f/ENVELOPE_UPDATE_RATE));
ryood 11:7e11404adca0 291
ryood 3:f89b400cfe57 292 for (;;) {
ryood 13:43a43da257e3 293 #if (PIN_CHECK)
ryood 13:43a43da257e3 294 Dout2 = 1;
ryood 15:8f674acdac03 295 #endif
ryood 5:846772a77d33 296 setParams();
ryood 13:43a43da257e3 297 #if (PIN_CHECK)
ryood 13:43a43da257e3 298 Dout2 = 0;
ryood 15:8f674acdac03 299 #endif
ryood 11:7e11404adca0 300
ryood 11:7e11404adca0 301 #if UART_TRACE
ryood 6:897d6392b408 302 printf("%.1f\t%d\t", bpm, envelopeLength);
ryood 11:7e11404adca0 303
ryood 6:897d6392b408 304 printf("%d\t%d\t", amplitudeParam.attack, amplitudeParam.release);
ryood 6:897d6392b408 305 printf("%.2f\t%.2f\t%.2f\t", amplitudeParam.v0, amplitudeParam.v1, amplitudeParam.v2);
ryood 6:897d6392b408 306 printf("%.2f\t%.2f\t", amplitudeParam.attackTauRatio, amplitudeParam.releaseTauRatio);
ryood 6:897d6392b408 307
ryood 6:897d6392b408 308 printf("%d\t%d\t", frequencyParam.attack, frequencyParam.release);
ryood 6:897d6392b408 309 printf("%.2f\t%.2f\t%.2f\t", frequencyParam.v0, frequencyParam.v1, frequencyParam.v2);
ryood 6:897d6392b408 310 printf("%.2f\t%.2f\r\n", frequencyParam.attackTauRatio, frequencyParam.releaseTauRatio);
ryood 11:7e11404adca0 311 #endif
ryood 11:7e11404adca0 312
ryood 13:43a43da257e3 313 Thread::wait(1);
ryood 3:f89b400cfe57 314 }
ryood 0:2dcec10e9199 315 }