STM32F767_ANC_IMC.ino 3.95 KB
Newer Older
1
2
3
4
#include "ANCcontroller.h"
//#include <STM32ADC.h>
//#include <libmaple/adc.h>

杉下大河's avatar
杉下大河 committed
5
6
7
HardwareTimer Timer1(TIM1);
// the setup function runs once when you press reset or power the board
uint8_t sw = LOW;
8
9
10
11
12
const int audioInErrPin = A6;    // select the input pin for the potentiometer
const int audioInRefPin = A7;    // select the input pin for the potentiometer

const int biasPin = D27;
const int audioOutPin = A13;
13
14
15

const int Q_BIT_NUM = 12;
const int AS_SYS_SAMPLERATE = 4000*2;
16
17
const int AS_MAX_BIT_NUM = 4095;
//const int AUDIO_BIAS = 2048;//3.3V中の1.65V(12bit) 3.3Vの中心で交流を受け付け+-1.65Vのレンジを作る
18
19

char anc_state;
杉下大河's avatar
杉下大河 committed
20
bool Sendflg = true;
21
bool calocReadyFlg = false;
杉下大河's avatar
杉下大河 committed
22
int sendTimingCnt=0;
23
24
25
26
27
28
29
30
int inValueErr = 0;  // variable to store the value coming from the sensor
int inValueRef = 0;

float32_t inValueErrf32=0;
float32_t inValueReff32=0;

float32_t ancSig=0;

31
32
33
34
35
36
int audioOutValue = 0;

uint32_t t1,t2;

WaveGenerator s_sound(AS_SYS_SAMPLERATE,Q_BIT_NUM);
ANC_Controller ANC;
杉下大河's avatar
杉下大河 committed
37
38
39
40

void sendDebugdat(){
  sw ^=1;
  digitalWrite(LED_GREEN, sw);
41
42
43
  Serial.println(t2-t1);
  //Serial.println(ancSig);
  //Serial.println(audioOutValue);
杉下大河's avatar
杉下大河 committed
44
45
}

46
//adcは最適値を設定すれば高速化できる余地あり現在30μs
杉下大河's avatar
杉下大河 committed
47
void audioreader() {
48

49
50
51
52
  inValueErr = analogRead(audioInErrPin);//12bitで読み取る range:+-1
  inValueErrf32 = (((float32_t)inValueErr - 2048) /2048);
  inValueRef = analogRead(audioInRefPin);//12bitで読み取る range:+-1
  inValueReff32 = (((float32_t)inValueRef - 2048) /2048);
53
54
55
56
57
58
59
60
61
  analogWrite(audioOutPin,audioOutValue);
  sendTimingCnt++;
  calocReadyFlg = true;
}


void audioreader_debug() {
  t1 = micros();

62
  inValueErr = analogRead(audioInErrPin);//多分12bitで読み取る
杉下大河's avatar
杉下大河 committed
63
  sendTimingCnt++;
64
65

  t2 = micros();
66
  printf(" t=%f \n",t2-t1);
杉下大河's avatar
杉下大河 committed
67
68
69
}

void setup() {
70
  Serial.begin(9600);
杉下大河's avatar
杉下大河 committed
71
72
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_GREEN, OUTPUT);
73
74
75
76
77
  pinMode(LED_RED, OUTPUT);
  pinMode(biasPin,OUTPUT);
  digitalWrite(LED_RED,LOW);
  digitalWrite(biasPin,HIGH);

78
  pinMode(audioOutPin, OUTPUT);
79
80
81
82
  //pinMode(biasPin, OUTPUT);

  pinMode(audioInErrPin,INPUT_ANALOG);
  pinMode(audioInRefPin,INPUT_ANALOG);
杉下大河's avatar
杉下大河 committed
83

84
85
  analogWriteResolution(Q_BIT_NUM);//analogwriteを12bit(MAX4095)で利用できるように設定
  analogReadResolution(Q_BIT_NUM);
86
  //analogWrite(biasPin,AUDIO_BIAS);//MAX3.3vの2.0V
杉下大河's avatar
杉下大河 committed
87
88
89


  Timer1.pause();                   // タイマー停止
90
91

  Timer1.setOverflow(AS_SYS_SAMPLERATE,HERTZ_FORMAT);//サンプルレートHzでよびだす
杉下大河's avatar
杉下大河 committed
92
93
94
95
96
97
98
  Timer1.attachInterrupt(           // 割り込みハンドラの登録
      audioreader                  // 呼び出す関数
    );

  Timer1.setCount(0);               // カウンタを0に設定
  Timer1.refresh();                 // タイマ更新
  Timer1.resume();                  // タイマースタート
99

100
101

  anc_state = ANC.IDENTIFICAT_COEFC;
杉下大河's avatar
杉下大河 committed
102
103
}

104

杉下大河's avatar
杉下大河 committed
105
106
// the loop function runs over and over again forever
void loop() {
107
108
  //デバグ情報の送信
  if(sendTimingCnt >= AS_SYS_SAMPLERATE / 4){
杉下大河's avatar
杉下大河 committed
109
110
111
    sendDebugdat();
    sendTimingCnt=0;
  }
112
113
114
115
116
117


  //ANC状態の管理
  if (anc_state == ANC.IDENTIFICAT_COEFC) {
    //伝達関数の推定をしている。非同期処理なのでタイミングがずれるかも
    if(calocReadyFlg){
118
119
      //ANC.calc2ndPassTF(inValueErr-AUDIO_BIAS,audioOutValue);
      //audioOutValue = s_sound.playWaveCharp(s_sound.UNSINGED_INT_WAVE);
120
121
122
123
124

      calocReadyFlg=false;
    }

  //起動から15sでモード移行 正確に処理したい場合はタイマーを使おう!
125
    if (millis() > 5 * 1000) {
126
127
      ANC.printCoefC();
      anc_state = ANC.ACTIVATION_ANC;
128
      digitalWrite(LED_RED,HIGH);
129
130
    }
  }
131

132
  else if (anc_state == ANC.ACTIVATION_ANC) {
133
134
135
136
137
138
139
140
141
142
143
144
145

    if(calocReadyFlg){
      t1 = micros();
      ancSig = ANC.calcANCwave(AS_NP_FF,inValueErrf32,inValueReff32);
      t2 = micros();
      audioOutValue = (int)((ancSig+1)/2 * AS_MAX_BIT_NUM);
      //audioOutValue = s_sound.playWaveSin(UNSINGED_INT_WAVE);

      //audioOutValue = 0;

      calocReadyFlg=false;
    }

146
147
  }

杉下大河's avatar
杉下大河 committed
148
}