Mbed bordje 1 -af

Dependencies:   Encoder HIDScope MODSERIAL Matrix MatrixMath biquad-master mbed

Fork of dsjklafjaslkjdfalkjfdaslkasdjklajadsflkjdasflkjdasflkadsflkasd by Dion de Greef

Committer:
RoyvZ
Date:
Fri Oct 13 08:59:23 2017 +0000
Revision:
0:b6c8d56842ce
Child:
2:293665548183
Oke

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RoyvZ 0:b6c8d56842ce 1 /**
RoyvZ 0:b6c8d56842ce 2 * Demo program for BiQuad and BiQuadChain classes
RoyvZ 0:b6c8d56842ce 3 * author: T.J.W. Lankhorst <t.j.w.lankhorst@student.utwente.nl> and Matthijs and Roy and Dion
RoyvZ 0:b6c8d56842ce 4 */
RoyvZ 0:b6c8d56842ce 5 #include "mbed.h"
RoyvZ 0:b6c8d56842ce 6 #include "HIDScope.h"
RoyvZ 0:b6c8d56842ce 7 #include <stdlib.h>
RoyvZ 0:b6c8d56842ce 8 #include <iostream>
RoyvZ 0:b6c8d56842ce 9 #include <iomanip>
RoyvZ 0:b6c8d56842ce 10 #include <complex>
RoyvZ 0:b6c8d56842ce 11 #include "BiQuad.h"
RoyvZ 0:b6c8d56842ce 12
RoyvZ 0:b6c8d56842ce 13 AnalogIn emg0( A0 );
RoyvZ 0:b6c8d56842ce 14 AnalogIn emg1( A1 );
RoyvZ 0:b6c8d56842ce 15
RoyvZ 0:b6c8d56842ce 16 Ticker sample_timer;
RoyvZ 0:b6c8d56842ce 17 HIDScope scope( 2 );
RoyvZ 0:b6c8d56842ce 18 DigitalOut led(LED1);
RoyvZ 0:b6c8d56842ce 19 DigitalOut led2(LED_GREEN);
RoyvZ 0:b6c8d56842ce 20
RoyvZ 0:b6c8d56842ce 21
RoyvZ 0:b6c8d56842ce 22
RoyvZ 0:b6c8d56842ce 23 // Example: 3th order Butterworth LP (w_c = 0.1*f_nyquist)
RoyvZ 0:b6c8d56842ce 24
RoyvZ 0:b6c8d56842ce 25 BiQuadChain bqc;
RoyvZ 0:b6c8d56842ce 26 BiQuad bq1( 0.9645651759199596, -1.5606992390030165, 0.9645651759199596, -1.5606992390030165, 0.9291303518399192 );
RoyvZ 0:b6c8d56842ce 27 BiQuad bq2( 1, 1, 1, 1, 1 );
RoyvZ 0:b6c8d56842ce 28 //BiQuad bq3( 0, 0, 0, 0, 0 );
RoyvZ 0:b6c8d56842ce 29
RoyvZ 0:b6c8d56842ce 30 void sample()
RoyvZ 0:b6c8d56842ce 31 {
RoyvZ 0:b6c8d56842ce 32 /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
RoyvZ 0:b6c8d56842ce 33 scope.set(0, bq1.step(emg0.read()) );
RoyvZ 0:b6c8d56842ce 34 scope.set(1, bq1.step(emg1.read()) );
RoyvZ 0:b6c8d56842ce 35 /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels)
RoyvZ 0:b6c8d56842ce 36 * Ensure that enough channels are available (HIDScope scope( 2 ))
RoyvZ 0:b6c8d56842ce 37 * Finally, send all channels to the PC at once */
RoyvZ 0:b6c8d56842ce 38 scope.send();
RoyvZ 0:b6c8d56842ce 39 /* To indicate that the function is working, the LED is toggled */
RoyvZ 0:b6c8d56842ce 40 led = !led;
RoyvZ 0:b6c8d56842ce 41 }
RoyvZ 0:b6c8d56842ce 42
RoyvZ 0:b6c8d56842ce 43 int main()
RoyvZ 0:b6c8d56842ce 44 {
RoyvZ 0:b6c8d56842ce 45 led2 = 0;
RoyvZ 0:b6c8d56842ce 46 bqc = bq1 * bq2; //* bq3;
RoyvZ 0:b6c8d56842ce 47 /**Attach the 'sample' function to the timer 'sample_timer'.
RoyvZ 0:b6c8d56842ce 48 * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
RoyvZ 0:b6c8d56842ce 49 */
RoyvZ 0:b6c8d56842ce 50 sample_timer.attach(&sample, 0.002);
RoyvZ 0:b6c8d56842ce 51
RoyvZ 0:b6c8d56842ce 52 /*empty loop, sample() is executed periodically*/
RoyvZ 0:b6c8d56842ce 53 while(true) {
RoyvZ 0:b6c8d56842ce 54
RoyvZ 0:b6c8d56842ce 55 // Find the poles of the filter
RoyvZ 0:b6c8d56842ce 56 std::cout << "Filter poles" << std::endl;
RoyvZ 0:b6c8d56842ce 57 std::vector< std::complex<double> > poles = bqc.poles();
RoyvZ 0:b6c8d56842ce 58 for( size_t i = 0; i < poles.size(); i++ )
RoyvZ 0:b6c8d56842ce 59 std::cout << "\t" << poles[i] << std::endl;
RoyvZ 0:b6c8d56842ce 60
RoyvZ 0:b6c8d56842ce 61 // Find the zeros of the filter
RoyvZ 0:b6c8d56842ce 62 std::cout << "Filter zeros" << std::endl;
RoyvZ 0:b6c8d56842ce 63 std::vector< std::complex<double> > zeros = bqc.zeros();
RoyvZ 0:b6c8d56842ce 64 for( size_t i = 0; i < poles.size(); i++ )
RoyvZ 0:b6c8d56842ce 65 std::cout << "\t" << zeros[i] << std::endl;
RoyvZ 0:b6c8d56842ce 66
RoyvZ 0:b6c8d56842ce 67 // Is the filter stable?
RoyvZ 0:b6c8d56842ce 68 std::cout << "This filter is " << (bqc.stable() ? "stable" : "instable") << std::endl;
RoyvZ 0:b6c8d56842ce 69
RoyvZ 0:b6c8d56842ce 70 // Output the step-response of 20 samples
RoyvZ 0:b6c8d56842ce 71 std::cout << "Step response 20 samples" << std::endl;
RoyvZ 0:b6c8d56842ce 72 for( int i = 0; i < 20; i++ )
RoyvZ 0:b6c8d56842ce 73 std::cout << "\t" << bqc.step( 1.0 ) << std::endl;
RoyvZ 0:b6c8d56842ce 74
RoyvZ 0:b6c8d56842ce 75 // Done0
RoyvZ 0:b6c8d56842ce 76 }
RoyvZ 0:b6c8d56842ce 77
RoyvZ 0:b6c8d56842ce 78 }