KIK 01 Prototype 05

Dependencies:   AverageMCP3008 mbed-rtos mbed mcp3008

Fork of KIK01_Proto03 by Ryo Od

Committer:
ryood
Date:
Thu Jul 06 07:20:41 2017 +0000
Revision:
16:30cb822e072f
Parent:
15:8f674acdac03
Child:
17:a5d9908bd456
2 SPI bus

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 15:8f674acdac03 5 * 2017.07.04 Proto03: MCP4922 DCA
ryood 13:43a43da257e3 6 * 2017.06.19 Proto02
ryood 3:f89b400cfe57 7 * 2017.06.04 created.
ryood 3:f89b400cfe57 8 *
ryood 3:f89b400cfe57 9 */
ryood 3:f89b400cfe57 10
ryood 0:2dcec10e9199 11 #include "mbed.h"
ryood 0:2dcec10e9199 12 #include "rtos.h"
ryood 13:43a43da257e3 13 #include "mcp3008.h"
ryood 0:2dcec10e9199 14
ryood 15:8f674acdac03 15 #include "SpiAmpController.h"
ryood 15:8f674acdac03 16
ryood 15:8f674acdac03 17 #define UART_TRACE (0)
ryood 12:5b498285d121 18 #define PIN_CHECK (1)
ryood 0:2dcec10e9199 19 #define TITLE_STR1 ("KIK01 Kick Machine")
ryood 15:8f674acdac03 20 #define TITLE_STR2 ("20170704")
ryood 0:2dcec10e9199 21
ryood 0:2dcec10e9199 22 #define PI_F (3.1415926f)
ryood 16:30cb822e072f 23 #define SAMPLING_RATE (48000)
ryood 0:2dcec10e9199 24 #define SAMPLING_PERIOD (1.0f/SAMPLING_RATE)
ryood 15:8f674acdac03 25 #define ENVELOPE_UPDATE_RATE SAMPLING_RATE
ryood 15:8f674acdac03 26
ryood 15:8f674acdac03 27 #define AMP_VREF 0x0fff
ryood 3:f89b400cfe57 28
ryood 13:43a43da257e3 29 AnalogOut Dac1(PA_4);
ryood 0:2dcec10e9199 30
ryood 16:30cb822e072f 31 SPI SpiM1(SPI_MOSI, SPI_MISO, SPI_SCK);
ryood 16:30cb822e072f 32 MCP3008 Adc0(&SpiM1, D10);
ryood 16:30cb822e072f 33 MCP3008 Adc1(&SpiM1, D9);
ryood 15:8f674acdac03 34
ryood 16:30cb822e072f 35 SPI SpiM2(PB_15, PB_14, PB_13);
ryood 16:30cb822e072f 36 SpiAmpController ampController(&SpiM2, PB_1, PC_4);
ryood 8:bb34a4894337 37
ryood 14:8e96f97e261b 38 // Check pins
ryood 14:8e96f97e261b 39 DigitalOut Dout0(D2);
ryood 14:8e96f97e261b 40 DigitalOut Dout1(D3);
ryood 14:8e96f97e261b 41 DigitalOut Dout2(D4);
ryood 14:8e96f97e261b 42
ryood 14:8e96f97e261b 43 // Sync
ryood 14:8e96f97e261b 44 DigitalOut SyncPin(D7);
ryood 12:5b498285d121 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 int envelopeTicks;
ryood 3:f89b400cfe57 170 volatile float frequency;
ryood 3:f89b400cfe57 171 volatile float phi;
ryood 3:f89b400cfe57 172 volatile float phiDelta;
ryood 3:f89b400cfe57 173 volatile float amplitude;
ryood 3:f89b400cfe57 174
ryood 5:846772a77d33 175 volatile float bpm;
ryood 5:846772a77d33 176 volatile int envelopeLength;
ryood 14:8e96f97e261b 177 volatile int stepLength;
ryood 0:2dcec10e9199 178
ryood 15:8f674acdac03 179 //-----------------------------------------------------------------------------//
ryood 15:8f674acdac03 180 // Internal DCA
ryood 15:8f674acdac03 181 //
ryood 15:8f674acdac03 182 void generateWave_DcaInternal()
ryood 0:2dcec10e9199 183 {
ryood 0:2dcec10e9199 184 phi += phiDelta;
ryood 0:2dcec10e9199 185 if (phi >= 1.0f) {
ryood 0:2dcec10e9199 186 phi -= 2.0f;
ryood 0:2dcec10e9199 187 }
ryood 10:79134dbb339d 188 float level = cosf(PI_F * phi) * amplitude;
ryood 1:f9b967ae26e4 189
ryood 3:f89b400cfe57 190 Dac1.write((level * 0.7f + 1.0f) / 2.0f);
ryood 0:2dcec10e9199 191 }
ryood 0:2dcec10e9199 192
ryood 15:8f674acdac03 193 void generateEnvelope_DcaInternal()
ryood 0:2dcec10e9199 194 {
ryood 0:2dcec10e9199 195 // Frequency Envelope
ryood 0:2dcec10e9199 196 frequency = envelopeFrequency.getAmplitude(envelopeTicks);
ryood 0:2dcec10e9199 197 phiDelta = 2.0f * frequency / SAMPLING_RATE;
ryood 0:2dcec10e9199 198
ryood 0:2dcec10e9199 199 // Amplitude Envelope
ryood 0:2dcec10e9199 200 amplitude = envelopeAmplitude.getAmplitude(envelopeTicks);
ryood 0:2dcec10e9199 201
ryood 0:2dcec10e9199 202 envelopeTicks++;
ryood 7:6735a343780e 203 if (envelopeTicks >= envelopeLength) {
ryood 0:2dcec10e9199 204 envelopeTicks = 0;
ryood 12:5b498285d121 205 phi = PI_F / 2.0f;
ryood 0:2dcec10e9199 206 }
ryood 0:2dcec10e9199 207 }
ryood 0:2dcec10e9199 208
ryood 15:8f674acdac03 209 //-----------------------------------------------------------------------------//
ryood 15:8f674acdac03 210 // External DCA
ryood 15:8f674acdac03 211 //
ryood 15:8f674acdac03 212 void generateWave_DcaExternal()
ryood 15:8f674acdac03 213 {
ryood 15:8f674acdac03 214 phi += phiDelta;
ryood 15:8f674acdac03 215 if (phi >= 1.0f) {
ryood 15:8f674acdac03 216 phi -= 2.0f;
ryood 15:8f674acdac03 217 }
ryood 15:8f674acdac03 218 //float level = cosf(PI_F * phi) * amplitude;
ryood 15:8f674acdac03 219 float level = cosf(PI_F * phi);
ryood 15:8f674acdac03 220
ryood 15:8f674acdac03 221 Dac1.write((level * 0.7f + 1.0f) / 2.0f);
ryood 15:8f674acdac03 222 }
ryood 15:8f674acdac03 223
ryood 15:8f674acdac03 224 void generateEnvelope_DcaExternal()
ryood 15:8f674acdac03 225 {
ryood 15:8f674acdac03 226 // Frequency Envelope
ryood 15:8f674acdac03 227 frequency = envelopeFrequency.getAmplitude(envelopeTicks);
ryood 15:8f674acdac03 228 phiDelta = 2.0f * frequency / SAMPLING_RATE;
ryood 15:8f674acdac03 229
ryood 15:8f674acdac03 230 // Amplitude Envelope
ryood 15:8f674acdac03 231 amplitude = envelopeAmplitude.getAmplitude(envelopeTicks);
ryood 15:8f674acdac03 232
ryood 15:8f674acdac03 233 ampController.outDca(amplitude * 4096);
ryood 15:8f674acdac03 234
ryood 15:8f674acdac03 235 envelopeTicks++;
ryood 15:8f674acdac03 236 if (envelopeTicks >= envelopeLength) {
ryood 15:8f674acdac03 237 envelopeTicks = 0;
ryood 15:8f674acdac03 238 phi = PI_F / 2.0f;
ryood 15:8f674acdac03 239 }
ryood 15:8f674acdac03 240 }
ryood 15:8f674acdac03 241
ryood 2:8dff77a1ee4d 242 void update()
ryood 2:8dff77a1ee4d 243 {
ryood 12:5b498285d121 244 #if (PIN_CHECK)
ryood 15:8f674acdac03 245 Dout0 = 1;
ryood 12:5b498285d121 246 #endif
ryood 15:8f674acdac03 247
ryood 14:8e96f97e261b 248 // Output Sync Signal per steps
ryood 14:8e96f97e261b 249 if (envelopeTicks % stepLength == 0) {
ryood 14:8e96f97e261b 250 SyncPin = 1;
ryood 14:8e96f97e261b 251 }
ryood 15:8f674acdac03 252
ryood 3:f89b400cfe57 253 ticks++;
ryood 12:5b498285d121 254 if (ticks >= SAMPLING_RATE / ENVELOPE_UPDATE_RATE) {
ryood 12:5b498285d121 255 #if (PIN_CHECK)
ryood 12:5b498285d121 256 Dout1 = 1;
ryood 15:8f674acdac03 257 #endif
ryood 3:f89b400cfe57 258 ticks = 0;
ryood 11:7e11404adca0 259
ryood 5:846772a77d33 260 // set envelope parameters
ryood 5:846772a77d33 261 envelopeAmplitude.setAttack(amplitudeParam.attack);
ryood 5:846772a77d33 262 envelopeAmplitude.setRelease(amplitudeParam.release);
ryood 5:846772a77d33 263 envelopeAmplitude.setV0(amplitudeParam.v0);
ryood 5:846772a77d33 264 envelopeAmplitude.setV1(amplitudeParam.v1);
ryood 5:846772a77d33 265 envelopeAmplitude.setV2(amplitudeParam.v2);
ryood 5:846772a77d33 266 envelopeAmplitude.setAttackTauRatio(amplitudeParam.attackTauRatio);
ryood 5:846772a77d33 267 envelopeAmplitude.setReleaseTauRatio(amplitudeParam.releaseTauRatio);
ryood 11:7e11404adca0 268
ryood 6:897d6392b408 269 envelopeFrequency.setAttack(frequencyParam.attack);
ryood 6:897d6392b408 270 envelopeFrequency.setRelease(frequencyParam.release);
ryood 5:846772a77d33 271 envelopeFrequency.setV0(frequencyParam.v0);
ryood 6:897d6392b408 272 envelopeFrequency.setV1(frequencyParam.v1);
ryood 6:897d6392b408 273 envelopeFrequency.setV2(frequencyParam.v2);
ryood 6:897d6392b408 274 envelopeFrequency.setAttackTauRatio(frequencyParam.attackTauRatio);
ryood 6:897d6392b408 275 envelopeFrequency.setReleaseTauRatio(frequencyParam.releaseTauRatio);
ryood 11:7e11404adca0 276
ryood 15:8f674acdac03 277 generateEnvelope_DcaExternal();
ryood 12:5b498285d121 278 #if (PIN_CHECK)
ryood 12:5b498285d121 279 Dout1 = 0;
ryood 12:5b498285d121 280 #endif
ryood 2:8dff77a1ee4d 281 }
ryood 15:8f674acdac03 282 generateWave_DcaExternal();
ryood 15:8f674acdac03 283
ryood 14:8e96f97e261b 284 // Output SyncSignal
ryood 14:8e96f97e261b 285 SyncPin = 0;
ryood 15:8f674acdac03 286
ryood 12:5b498285d121 287 #if (PIN_CHECK)
ryood 15:8f674acdac03 288 Dout0 = 0;
ryood 12:5b498285d121 289 #endif
ryood 2:8dff77a1ee4d 290 }
ryood 2:8dff77a1ee4d 291
ryood 5:846772a77d33 292 void setParams()
ryood 5:846772a77d33 293 {
ryood 13:43a43da257e3 294 bpm = Adc0.read_input(7) * 180.0f + 60.0f;
ryood 12:5b498285d121 295 envelopeLength = 60 * ENVELOPE_UPDATE_RATE / bpm;
ryood 14:8e96f97e261b 296 stepLength = envelopeLength / 4;
ryood 11:7e11404adca0 297
ryood 13:43a43da257e3 298 amplitudeParam.attack = Adc0.read_input(0) * envelopeLength;
ryood 13:43a43da257e3 299 amplitudeParam.release = Adc0.read_input(1) * envelopeLength;
ryood 13:43a43da257e3 300 amplitudeParam.v0 = Adc0.read_input(4);
ryood 14:8e96f97e261b 301 amplitudeParam.v1 = Adc0.read_input(5);
ryood 14:8e96f97e261b 302 amplitudeParam.v2 = Adc0.read_input(6);
ryood 5:846772a77d33 303 amplitudeParam.attackTauRatio = 0.36f;
ryood 13:43a43da257e3 304 amplitudeParam.releaseTauRatio = Adc0.read_input(3) + 0.01f;
ryood 11:7e11404adca0 305
ryood 13:43a43da257e3 306 frequencyParam.attack = Adc1.read_input(0) * envelopeLength * 0.1f;
ryood 13:43a43da257e3 307 frequencyParam.release = Adc1.read_input(1) * envelopeLength + 1;
ryood 13:43a43da257e3 308 frequencyParam.v0 = Adc1.read_input(4) * 4000.0f;
ryood 13:43a43da257e3 309 frequencyParam.v1 = Adc1.read_input(5) * 400.0f;
ryood 13:43a43da257e3 310 frequencyParam.v2 = Adc1.read_input(6) * 400.0f;
ryood 13:43a43da257e3 311 frequencyParam.attackTauRatio = Adc1.read_input(2) + 0.01f;
ryood 13:43a43da257e3 312 frequencyParam.releaseTauRatio = Adc1.read_input(3) + 0.01f;
ryood 5:846772a77d33 313 }
ryood 5:846772a77d33 314
ryood 0:2dcec10e9199 315 int main()
ryood 0:2dcec10e9199 316 {
ryood 0:2dcec10e9199 317 printf("%s %s\r\n", TITLE_STR1, TITLE_STR2);
ryood 15:8f674acdac03 318
ryood 16:30cb822e072f 319 SpiM1.format(0, 0);
ryood 16:30cb822e072f 320 SpiM1.frequency(2000000);
ryood 16:30cb822e072f 321
ryood 16:30cb822e072f 322 SpiM2.format(0, 0);
ryood 16:30cb822e072f 323 SpiM2.frequency(20000000);
ryood 15:8f674acdac03 324
ryood 0:2dcec10e9199 325 frequency = 1000.0f;
ryood 0:2dcec10e9199 326 phiDelta = 2.0f * frequency / SAMPLING_RATE;
ryood 0:2dcec10e9199 327 amplitude = 1.0f;
ryood 11:7e11404adca0 328
ryood 3:f89b400cfe57 329 ticks = 0;
ryood 2:8dff77a1ee4d 330 envelopeTicks = 0;
ryood 11:7e11404adca0 331
ryood 5:846772a77d33 332 bpm = 120.0f;
ryood 5:846772a77d33 333 setParams();
ryood 5:846772a77d33 334
ryood 2:8dff77a1ee4d 335 Ticker samplingTicker;
ryood 2:8dff77a1ee4d 336 samplingTicker.attach(&update, SAMPLING_PERIOD);
ryood 11:7e11404adca0 337
ryood 3:f89b400cfe57 338 for (;;) {
ryood 13:43a43da257e3 339 #if (PIN_CHECK)
ryood 13:43a43da257e3 340 Dout2 = 1;
ryood 15:8f674acdac03 341 #endif
ryood 5:846772a77d33 342 setParams();
ryood 13:43a43da257e3 343 #if (PIN_CHECK)
ryood 13:43a43da257e3 344 Dout2 = 0;
ryood 15:8f674acdac03 345 #endif
ryood 11:7e11404adca0 346
ryood 11:7e11404adca0 347 #if UART_TRACE
ryood 6:897d6392b408 348 printf("%.1f\t%d\t", bpm, envelopeLength);
ryood 11:7e11404adca0 349
ryood 6:897d6392b408 350 printf("%d\t%d\t", amplitudeParam.attack, amplitudeParam.release);
ryood 6:897d6392b408 351 printf("%.2f\t%.2f\t%.2f\t", amplitudeParam.v0, amplitudeParam.v1, amplitudeParam.v2);
ryood 6:897d6392b408 352 printf("%.2f\t%.2f\t", amplitudeParam.attackTauRatio, amplitudeParam.releaseTauRatio);
ryood 6:897d6392b408 353
ryood 6:897d6392b408 354 printf("%d\t%d\t", frequencyParam.attack, frequencyParam.release);
ryood 6:897d6392b408 355 printf("%.2f\t%.2f\t%.2f\t", frequencyParam.v0, frequencyParam.v1, frequencyParam.v2);
ryood 6:897d6392b408 356 printf("%.2f\t%.2f\r\n", frequencyParam.attackTauRatio, frequencyParam.releaseTauRatio);
ryood 11:7e11404adca0 357 #endif
ryood 11:7e11404adca0 358
ryood 13:43a43da257e3 359 Thread::wait(1);
ryood 3:f89b400cfe57 360 }
ryood 0:2dcec10e9199 361 }