BASIC4MCU | 질문게시판 | 아두이노 l298n frequency sweep 관련 질문
페이지 정보
작성자 sdwdjangwon 작성일2022-12-07 19:11 조회1,513회 댓글2건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=22098
작성일
본문
reference code로 l298n에 frequency sweep sine sample로 전달함수를 매트랩 systemIdentification으로 구하려고 하고 있습니다.
코드 해석을 해봤을 때, frequency가 0.1Hz일 때 sample이 한 주기, 즉 500개, 0.2Hz일 때 한 주기 500개, 0.3Hz에서 2 주기, 1000개, ... 14Hz까지 변화시켰을 때 70 주기, 35000개라고 파악했고 sample의 갯수를 더해서 계산하면 432000개가 나오고, 이와 똑같은 갯수의 output을 serial monitor로 받았습니다.
그려보면 다음과 같구요.
하지만 input 함수를 파악하지 못해 systemIdentification을 사용하는 데 제약이 있습니다. input함수와 output함수를 어떻게 파악할 수 있을까요?
#include // For the ATOMIC_BLOCK macro#define encoder0PinA 2 //encoder pin 2 and 3#define encoder0PinB 3int encoder0Pos = 0;const int elements = 40; // number of frequenciesint Fs = 500; //sampling frequency of sin waveint n = 500;//500; // number of samplesbyte samples[500];// frequncy look up tablefloat t ,freq_arra[elements] = {0.1000000000000, 0.200000000000000, 0.300000000000000, 0.400000000000000, 0.500000000000000, 0.600000000000000, 0.700000000000000, 0.800000000000000, 0.900000000000000, 1.00000000000000, 1.10000000000000, 1.20000000000000, 1.30000000000000, 1.40000000000000, 1.50000000000000, 1.60000000000000, 1.70000000000000, 1.80000000000000, 1.90000000000000, 2, 2.50000000000000, 3, 3.50000000000000, 4, 4.50000000000000, 5, 5.50000000000000, 6, 6.50000000000000, 7, 7.50000000000000, 8, 8.50000000000000, 9, 9.50000000000000, 10, 11,12,13,14};int samling_interval, no_samples;void setup() {pinMode(encoder0PinA, INPUT_PULLUP);pinMode(encoder0PinB, INPUT_PULLUP);attachInterrupt(0, doEncoderA, CHANGE); //interrupts to capture encoder dataattachInterrupt(1, doEncoderB, CHANGE);Serial.begin (230400); // initialize serail monitorpinMode(10,OUTPUT);// declaration of PWM pinspinMode(11,OUTPUT);for (int k = 0; k < n; k++)//calculating Sin wave values{ t = (float)k / Fs;samples[k] = (byte) (127.0/1 * sin(2 * 3.142*t*1) + 127.0/1);}}void loop() {char value; //declaraing input variableif(Serial.available()>0){value = Serial.read();if(value == 'a'){for(int m = 0; m < elements;m++){// loop for changing the frequenciessamling_interval = (float)1000000/(freq_arra[m]*n);no_samples = (float)(freq_arra[m]*n/(50));digitalWrite(10 , HIGH); // All motor pin are '0'to avoid inertia for 1 seconddigitalWrite(11 , HIGH);delay(1000); // wait for 1 secondencoder0Pos = 0; // to reset the position of encoderfor (int p = 0; p < 5*freq_arra[m]; p++){// to have 5sec signal starting from 0 degreefor(int j = 0; j ){;j++if(j>=n/4 && j<3*n/4){ //signal between 90-270 degreeanalogWrite(10 , samples[j]);digitalWrite(11 , LOW);}else{ //signal between 270->90 degreeanalogWrite(11 , samples[j]);digitalWrite(10 , LOW);}Serial.print (encoder0Pos);Serial.write(13);Serial.write(10);if(freq_arra[m]<=1){delay(samling_interval/1000);// delay for sampling 2/frequency ms}else{delayMicroseconds(samling_interval); // delay for sampling 2000/frequency microseconds}}}}Serial.print ('b'); // When sweep process complete 'b' is sent and motor stops motionSerial.write(13);Serial.write(10);digitalWrite(10 , LOW);digitalWrite(11 , LOW);}}}
댓글 2
조회수 1,513master님의 댓글
master 작성일
for(int j = 0; j ){;j++
깨진 곳이 있네요?
sdwdjangwon님의 댓글
sdwdjangwon
댓글이 달리면 수정이 안되는군요 for(int j=0; j<n; j++)입니다