Dependencies:   mbed HIDScope biquadFilter

Committer:
hidde1104
Date:
Mon Oct 21 08:20:40 2019 +0000
Revision:
27:c53dd84c38aa
Parent:
21:48b40b82d195
Child:
28:4eaf5990a7b3
v4;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 0:32bb76391d89 1 #include "mbed.h"
vsluiter 11:ce72ec658a95 2 #include "HIDScope.h"
hidde1104 21:48b40b82d195 3 #include "BiQuad.h"
hidde1104 27:c53dd84c38aa 4 Serial pc(USBTX, USBRX);
hidde1104 27:c53dd84c38aa 5 InterruptIn but1(PTC6);
hidde1104 21:48b40b82d195 6
hidde1104 21:48b40b82d195 7 BiQuad bq1 (0.881889334678067, -1.76377866935613, 0.8818893346780671, -1.77069673005903, 0.797707797506027);
hidde1104 21:48b40b82d195 8
hidde1104 21:48b40b82d195 9 BiQuad bq2 (0.000198358203463849, 0.000396716406927699, 0.000198358203463849, -1.96262073248799, 0.963423352820821);
hidde1104 21:48b40b82d195 10
hidde1104 21:48b40b82d195 11
hidde1104 21:48b40b82d195 12 BiQuadChain bqc;
hidde1104 21:48b40b82d195 13
vsluiter 0:32bb76391d89 14
vsluiter 4:8b298dfada81 15 //Define objects
tomlankhorst 19:2bf824669684 16 AnalogIn emg0( A0 );
tomlankhorst 19:2bf824669684 17 AnalogIn emg1( A1 );
tomlankhorst 19:2bf824669684 18
tomlankhorst 14:f83354387756 19 Ticker sample_timer;
hidde1104 27:c53dd84c38aa 20 Ticker calibration_timer;
hidde1104 21:48b40b82d195 21 HIDScope scope( 3 );
hidde1104 27:c53dd84c38aa 22 DigitalOut led(LED2);
vsluiter 2:e314bb3b2d99 23
tomlankhorst 14:f83354387756 24 /** Sample function
tomlankhorst 14:f83354387756 25 * this function samples the emg and sends it to HIDScope
tomlankhorst 14:f83354387756 26 **/
hidde1104 27:c53dd84c38aa 27 float filter_value;
hidde1104 27:c53dd84c38aa 28
tomlankhorst 14:f83354387756 29 void sample()
vsluiter 2:e314bb3b2d99 30 {
tomlankhorst 19:2bf824669684 31 /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
hidde1104 27:c53dd84c38aa 32 float emg0_value = emg0.read();
hidde1104 21:48b40b82d195 33 float emg1_value = emg1.read();
hidde1104 27:c53dd84c38aa 34
hidde1104 27:c53dd84c38aa 35
hidde1104 21:48b40b82d195 36 float filter_value = fabs(bq2.step(fabs(bq1.step(emg0_value - emg1_value))));
hidde1104 27:c53dd84c38aa 37
hidde1104 27:c53dd84c38aa 38 //scope.set(0, emg0.read() );
hidde1104 27:c53dd84c38aa 39 //scope.set(1, emg1.read() );
hidde1104 27:c53dd84c38aa 40 //scope.set(2, filter_value);
hidde1104 21:48b40b82d195 41
hidde1104 27:c53dd84c38aa 42 /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels)
tomlankhorst 19:2bf824669684 43 * Ensure that enough channels are available (HIDScope scope( 2 ))
tomlankhorst 20:97059009a491 44 * Finally, send all channels to the PC at once */
hidde1104 27:c53dd84c38aa 45
hidde1104 27:c53dd84c38aa 46 //scope.send();
hidde1104 27:c53dd84c38aa 47
tomlankhorst 18:21d8e7a81cf5 48 /* To indicate that the function is working, the LED is toggled */
tomlankhorst 18:21d8e7a81cf5 49 led = !led;
vsluiter 2:e314bb3b2d99 50 }
vsluiter 0:32bb76391d89 51
hidde1104 27:c53dd84c38aa 52 const int length = 1000;
hidde1104 27:c53dd84c38aa 53 int i;
hidde1104 27:c53dd84c38aa 54 float sample_array[length] = {};
hidde1104 27:c53dd84c38aa 55
hidde1104 27:c53dd84c38aa 56 void calibration()
hidde1104 27:c53dd84c38aa 57 {
hidde1104 27:c53dd84c38aa 58 i = 0;
hidde1104 27:c53dd84c38aa 59 while (i < (length-1)) {
hidde1104 27:c53dd84c38aa 60 sample();
hidde1104 27:c53dd84c38aa 61 pc.printf("I can reach this point\r\n");
hidde1104 27:c53dd84c38aa 62 sample_array[i] = filter_value;
hidde1104 27:c53dd84c38aa 63 pc.printf("The filter_value is %f\r\n",filter_value);
hidde1104 27:c53dd84c38aa 64 i++;
hidde1104 27:c53dd84c38aa 65 wait(0.001);
hidde1104 27:c53dd84c38aa 66 }
hidde1104 27:c53dd84c38aa 67 }
hidde1104 27:c53dd84c38aa 68
vsluiter 0:32bb76391d89 69 int main()
hidde1104 27:c53dd84c38aa 70 {
hidde1104 27:c53dd84c38aa 71 pc.baud(115200);
hidde1104 27:c53dd84c38aa 72 pc.printf("Running main");
hidde1104 21:48b40b82d195 73 bqc.add( &bq1);
tomlankhorst 14:f83354387756 74 /**Attach the 'sample' function to the timer 'sample_timer'.
tomlankhorst 19:2bf824669684 75 * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
vsluiter 4:8b298dfada81 76 */
hidde1104 27:c53dd84c38aa 77
hidde1104 27:c53dd84c38aa 78 calibration();
hidde1104 27:c53dd84c38aa 79
hidde1104 27:c53dd84c38aa 80 sample_timer.attach(&sample, 0.001);
tomlankhorst 15:0da764eea774 81
tomlankhorst 14:f83354387756 82 /*empty loop, sample() is executed periodically*/
tomlankhorst 15:0da764eea774 83 while(1) {}
hidde1104 21:48b40b82d195 84 }