Guest User

Untitled

a guest
Apr 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.15 KB | None | 0 0
  1.  
  2. #include <MemoryFree.h>
  3.  
  4. #define FASTADC 1
  5. #define SAMPLES 50
  6.  
  7. #include <math.h>
  8.  
  9. #define INPUT_NEURONS 1
  10. #define HIDDEN_NEURONS 1
  11. #define OUTPUT_NEURONS 1
  12.  
  13. // defines for setting and clearing register bits
  14. #ifndef cbi
  15. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  16. #endif
  17. #ifndef sbi
  18. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  19. #endif
  20.  
  21. struct input_neuron{
  22.         double value;
  23. } input_layer[INPUT_NEURONS];
  24.  
  25. struct hidden_neuron{
  26.     double value;
  27.     double weights[INPUT_NEURONS+1]; //+1 bias
  28. } hidden_layer[HIDDEN_NEURONS];
  29.  
  30. struct output_neuron{
  31.     double value;
  32.     double weights[HIDDEN_NEURONS]; //+1 bias
  33. } output_layer[OUTPUT_NEURONS];
  34.  
  35. MemoryFree mf;
  36. // MATLAB FOR 2 MICS SO FREQ IS 31250 Hz
  37. // [bC,aC]=impinvar([62312.8,0],[1,8769.08,4.26601e8,1.78616e12,2.91404e16],31250);
  38. // [bI,aI]=impinvar([0.000560749,2.45862,114218,0],[1,8769.08,4.26601e8,1.78616e12,2.91404e16],31250);
  39. // bC_int = round(bC*1e12);
  40. // aC_int = round(aC*2^12);
  41. // bI_int = round(bI*1e12);
  42. // aI_int = round(aI*2^12);
  43. //long bI_int[] = {17944L, -46322L, 42690L, -14076L};
  44. //long aI_int[] = {4096L, -13753L, 18621L, -11953L, 3094L};
  45. //long bC_int[] = {0L, 898L, -80L, -818L};
  46. //long aC_int[] = {4096L, -13753L, 18621L, -11953L, 3094L};
  47. //float bI_float[] = {4.3809, -11.3091, 10.4224, -3.4365};
  48. //float aI_float[] = {1, -3.3577, 4.5461, -2.9182, 0.7554};
  49. //float bC_float[] = {0, 0.2192, -0.0195, -0.1997};
  50. //float aC_float[] = {1, -3.3577, 4.5461, -2.9182, 0.7554};
  51.  
  52. // FOR 3 MICS SO FREQ IS 20833 Hz
  53. float bI_float[] = {6.5713, -14.4548, 12.4941, -4.2151};
  54. float aI_float[] = {1, -2.8159, 3.5573, -2.2815, 0.6564};
  55. float bC_float[] = {0, 0.6755, -0.0872, -0.5884};
  56. float aC_float[] = {1, -2.8159, 3.5573, -2.2815, 0.6564};
  57. //SOUND SOURCE LEFT
  58. //float mic0[SAMPLES] = {398,359,316,276,231,193,167,151,147,163,184,219,259,305,345,384,415,432,437,430,409,376,339,295,251,210,176,155,147,153,173,204,240,284,327,367,401,423,435,434,417,391,353,313,268,227,188,161,147,147};
  59. //float mic1[SAMPLES] = {368,327,286,246,207,177,158,153,158,176,204,243,281,323,359,391,413,423,420,404,380,345,305,263,223,189,164,152,152,166,192,225,264,303,344,378,404,420,423,412,391,360,323,280,242,204,175,155,151,159};
  60. //float mic0[SAMPLES] = {170,151,146,163,192,213,255,304,341,374,407,423,426,425,399,375,334,297,251,216,184,162,148,154,177,204,238,278,323,359,389,417,431,420,404,381,355,307,271,233,185,159,156,148,159,182,217,254,304,344};
  61. //float mic1[SAMPLES] = {160,159,164,178,224,255,291,327,370,390,405,417,409,387,363,327,291,249,211,184,161,153,161,172,205,240,275,315,344,385,406,407,409,399,376,337,303,265,225,192,171,155,152,163,184,213,248,291,329,368};
  62. //SOUND SOURCE RIGHT
  63. float mic0[SAMPLES];// = {412,419,415,401,371,344,305,268,230,199,172,157,160,167,189,222,262,302,339,361,396,403,398,399,385,355,320,284,242,213,176,162,157,159,175,209,241,281,321,361,382,401,406,411,396,371,339,302,264,224};
  64. float mic1[SAMPLES];// = {371,388,401,400,386,376,339,305,274,241,207,184,172,170,172,190,217,241,283,313,344,365,376,385,384,375,353,324,287,252,220,188,179,167,170,179,199,231,263,302,335,364,375,390,391,377,363,329,310,267};
  65. float mic2[SAMPLES];
  66. float mic1_I[SAMPLES];
  67. float mic1_C[SAMPLES];
  68. float mic0_I[SAMPLES];
  69. float mic0_C[SAMPLES];
  70.  
  71. // SOME VARIABLES TO CONTROL THE SERVO
  72. int servoPin = 2;     // Control pin for servo motor
  73. int minPulse = 900;   // Minimum servo position
  74. int maxPulse = 2100;  // Maximum servo position
  75. int pulse = 1500;        // Amount to pulse the servo, starts in the center
  76. int refreshTime = 20; // the time needed in between pulses
  77.  
  78. // to store 10 values to calculate the standard deviation.
  79. float std_values0[10];
  80. float std_values1[10];
  81. float std_values2[10];
  82. int counter_values0 = 0;
  83. int counter_values1 = 0;
  84. int counter_values2 = 0;
  85.  
  86. void setup() {
  87.  
  88.   #if FASTADC
  89.   // set prescale to 16
  90.   sbi(ADCSRA,ADPS2) ;
  91.   cbi(ADCSRA,ADPS1) ;
  92.   cbi(ADCSRA,ADPS0) ;
  93.   #endif
  94.  
  95.   Serial.begin(9600);
  96.   //Serial.print("memory ini1 ");
  97.   //Serial.println(mf.freeMemory());
  98.  
  99.   double hidden_weights[] = {0.90359398, -0.12097949};
  100.   double output_weights[] = {-1.3631867, 0.3051893};
  101.  
  102.   set_weights(hidden_weights, output_weights);
  103.   Serial.println("ini");
  104.   Serial.println(output_layer[0].weights[0]);
  105. Serial.println(output_layer[0].weights[1]);
  106. }
  107.  
  108.  
  109. void loop() {
  110.   //unsigned long time1, time2;
  111.   //time1= micros();
  112.   for (int i = 0; i<SAMPLES; i++) {
  113.    mic0[i] = 0;
  114.    mic1[i] = 0;
  115.    mic2[i] = 0;
  116.   }
  117.  
  118.   for (int i=0; i<SAMPLES; i++) {
  119.    mic1[i] = analogRead(A1)-280;
  120.    mic0[i] = analogRead(A0)-280;
  121.    mic2[i] = analogRead(A2)-280;
  122.   }
  123.  
  124.   /*Serial.println("mic0");
  125.   for (int i=0; i<SAMPLES; i++) {
  126.     Serial.println(mic0[i]);
  127.   }
  128.   Serial.println("mic1");
  129.   for (int i=0; i<SAMPLES; i++) {
  130.     Serial.println(mic1[i]);
  131.   }
  132.   Serial.println("mic2");
  133.   for (int i=0; i<SAMPLES; i++) {
  134.     Serial.println(mic2[i]);
  135.   }*/
  136.  
  137.   float dir = calculateDirection(mic0,mic1);
  138.   float avg1 = calculateAvgAndStd(dir,0);
  139.  
  140.   dir = calculateDirection(mic0,mic2);
  141.   float avg2 = calculateAvgAndStd(dir,1);
  142.  
  143.   dir = calculateDirection(mic2,mic1);
  144.   float avg3 = calculateAvgAndStd(dir,2);
  145.  
  146.   //if (avg1 > -50) {
  147.     double inputs[] = {1.0};
  148.     feed_forward(inputs);
  149.     for (int i=0; i<OUTPUT_NEURONS; i++) {
  150.       Serial.print("output ");
  151.       Serial.print(output_layer[i].value);
  152.       Serial.print(" ");
  153.     }
  154.     delay(500);
  155.     Serial.println(" ");
  156.   //}
  157.  
  158.   //time2=micros();
  159.   //Serial.println(time2-time1);
  160.  
  161.   //Serial.print("memory2 ");
  162.   //Serial.println(1024*8-mf.freeMemory());
  163. }
  164.  
  165. float calculateAvgAndStd(float dir, int control) {
  166.  
  167.   float ret = -99;
  168.  
  169.   if (control == 0) {
  170.     std_values0[counter_values0] = dir;
  171.     counter_values0++;
  172.   }
  173.   else if (control == 1) {
  174.     std_values1[counter_values1] = dir;
  175.     counter_values1++;
  176.   }
  177.   else if (control == 2) {
  178.     std_values2[counter_values2] = dir;
  179.     counter_values2++;
  180.   }
  181.    
  182.   if (counter_values0 > 9 || counter_values1 > 9 ||
  183.       counter_values2 > 9) {
  184.     float sum0 = 0;
  185.     float sum1 = 0;
  186.     float sum2 = 0;
  187.     for (int i=0; i<10; i++) {
  188.       if (control == 0) sum0 = sum0 + std_values0[i];
  189.       else if (control == 1) sum1 = sum1 + std_values1[i];
  190.       else if (control == 2) sum2 = sum2 + std_values2[i];
  191.     }
  192.     float avg_std0 = sum0 / 10;
  193.     float avg_std1 = sum1 / 10;
  194.     float avg_std2 = sum2 / 10;
  195.     if (control == 0) {
  196.       Serial.print("0 and 1: ");
  197.       ret = avg_std0;
  198.       Serial.print(avg_std0);
  199.       Serial.print("    ");
  200.     }
  201.     else if (control == 1) {
  202.       Serial.print("0 and 2: ");
  203.       Serial.print(avg_std1);
  204.       ret = avg_std1;
  205.       Serial.print("    ");
  206.     }
  207.     else if (control == 2) {
  208.       Serial.print("1 and 2: ");
  209.       Serial.print(avg_std2);
  210.       ret = avg_std2;
  211.       Serial.print("    ");
  212.     }
  213.    
  214.     double var0 = 0;
  215.     double var1 = 0;
  216.     double var2 = 0;
  217.     for (int i=0; i<10; i++) {
  218.       if (control == 0) {
  219.         double temp = std_values0[i]-avg_std0;
  220.         var0 = var0 + temp*temp;
  221.       }
  222.       else if (control == 1) {
  223.         double temp = std_values1[i]-avg_std1;
  224.         var1 = var1 + temp*temp;
  225.       }
  226.       else if (control == 2) {
  227.         double temp = std_values2[i]-avg_std2;
  228.         var2 = var2 + temp*temp;
  229.       }
  230.     }
  231.     float std_dev;
  232.     if (control==0) std_dev = sqrt(var0 / 10);
  233.     else if (control==1) std_dev = sqrt(var1 / 10);
  234.     else if (control==2) std_dev = sqrt(var2 / 10);
  235.    
  236.     Serial.print(std_dev);
  237.     Serial.print("    ");
  238.    
  239.     if (control == 0) {
  240.       counter_values0 = 0;
  241.     }
  242.     if (control == 1) counter_values1 = 0;
  243.     if (control == 2 ) {
  244.       counter_values2 = 0;
  245.       Serial.println();
  246.       delay(1000);
  247.     }
  248.   }
  249.   return ret;
  250. }
  251.  
  252. float calculateDirection(float mic0[], float mic1[]) {
  253.  
  254.   convolution(mic0, bC_float, aC_float, mic0_C);
  255.   convolution(mic0, bI_float, aI_float, mic0_I);
  256.   convolution(mic1, bC_float, aC_float, mic1_C);
  257.   convolution(mic1, bI_float, aC_float, mic1_I);
  258.  
  259.   addSignals(mic0_I, mic1_C);
  260.   addSignals(mic0_C, mic1_I);
  261.  
  262.   absoluteSignal(mic0_I);
  263.   absoluteSignal(mic0_C);
  264.  
  265.   float avg1 = average(mic0_I);
  266.   float avg2 = average(mic0_C);
  267.  
  268.   return avg1 / avg2;
  269. }
  270.  
  271. void moveRobot(float dir) {
  272.   //Using 1.5khz, 86...90 center, lower left, bigger right
  273.   if (dir < 0.79 ) pulse-=150;
  274.   else if (dir > 0.94) pulse+=150;
  275.   else if ((dir>=0.79) && (dir < 0.86)) pulse-=70;
  276.   else if ((dir <= 0.94) && (dir > 0.90)) pulse +=70;
  277.   if (pulse < 900) pulse = 900;
  278.   if (pulse > 2100) pulse = 2100;
  279.  
  280.   //Serial.println(pulse);
  281.   digitalWrite(servoPin, HIGH);
  282.   delayMicroseconds(pulse);
  283.   digitalWrite(servoPin, LOW);
  284.   delay(refreshTime);
  285. }
  286.  
  287. void convolution(float in_signal[], float b[], float a[], float out_signal[])
  288. {
  289.   int bn = 4;
  290.   int an = 5;
  291.   int nT = SAMPLES;
  292.   float pad_in[SAMPLES+8];
  293.   float local_out[SAMPLES+8];
  294.  
  295.   for (int i=0; i<(SAMPLES+8); i++) {
  296.     if (i<4) pad_in[i] = 0;
  297.     else if (i>(SAMPLES+8)-5) pad_in[i] = 0;
  298.     else pad_in[i] = in_signal[i-4];
  299.     local_out[i]=0;
  300.   }
  301.  
  302.   for (int i=4; i<(SAMPLES+8)-5; i++) {
  303.     for (int j=0; j<bn; j++) {
  304.       local_out[i] = local_out[i] + b[j] *pad_in[i-j];
  305.     }
  306.   }
  307.   for (int i=4; i<(SAMPLES+8)-5; i++) {
  308.     for (int j=1; j<an; j++) {
  309.       local_out[i] = local_out[i] - a[j] * local_out[i-j];
  310.     }
  311.   }
  312.   for (int i=4; i<(SAMPLES+8)-5; i++) {
  313.     out_signal[i-4] = local_out[i];
  314.   }
  315. }
  316.  
  317. //s1 in/out, s2 in. saving memory
  318. void addSignals(float s1[], float s2[])
  319. {
  320.   for (int i=0; i<SAMPLES; i++)
  321.   {
  322.     s1[i] = s1[i] + s2[i];
  323.   }
  324. }
  325.  
  326. void absoluteSignal(float in[])
  327. {
  328.   for (int i=0; i<SAMPLES; i++)
  329.   {
  330.     if (in[i] < 0) { in[i] = -1*in[i]; }
  331.   }
  332. }
  333.  
  334. long average(float in[])
  335. {
  336.   float sum = 0;
  337.   for (int i=0; i<SAMPLES; i++)
  338.   {
  339.     sum = sum + in[i];
  340.   }
  341.   return sum / (SAMPLES);
  342. }
  343.  
  344. double sigmoid(double t) {
  345.     return 1 / (1+exp(-t));
  346. }
  347.  
  348. void feed_forward(double inputs[INPUT_NEURONS]) {
  349.  
  350.     int i, j;
  351.    
  352.     for (i=0; i<INPUT_NEURONS; i++) {
  353.         input_layer[i].value = inputs[i];
  354.     }
  355.  
  356.     for (i=0; i<HIDDEN_NEURONS; i++) {
  357.         double sum = 0.0;
  358.         for (j=0; j<INPUT_NEURONS; j++) {
  359.             sum = sum + hidden_layer[i].weights[j] * input_layer[j].value;
  360.         }
  361.         sum = sum + hidden_layer[i].weights[INPUT_NEURONS];
  362.         hidden_layer[i].value = sigmoid(sum);
  363.     }
  364.  
  365.     for (i=0; i<OUTPUT_NEURONS; i++) {
  366.         double sum = 0.0;
  367.         for (j=0; j<HIDDEN_NEURONS; j++) {
  368.             sum = sum + output_layer[i].weights[j] * hidden_layer[j].value;
  369.         }
  370. Serial.println(output_layer[i].weights[0]);
  371. Serial.println(output_layer[i].weights[1]);
  372.         sum = sum + output_layer[i].weights[HIDDEN_NEURONS];
  373.         output_layer[i].value = sum;
  374. Serial.println(output_layer[i].value);
  375.     }
  376.  
  377. }
  378.  
  379. void set_weights(double hidden_weights[INPUT_NEURONS*HIDDEN_NEURONS],
  380.                 double output_weights[HIDDEN_NEURONS*OUTPUT_NEURONS]) {
  381.                    
  382.     int i,j;
  383.  
  384.     for (i=0; i<HIDDEN_NEURONS; i++) {
  385.         for (j=0; j<INPUT_NEURONS+1; j++) {
  386.             hidden_layer[i].weights[j] = hidden_weights[i*(INPUT_NEURONS+1)+j];
  387.         }
  388.     }
  389.  
  390.     for (i=0; i<OUTPUT_NEURONS; i++) {
  391.         for (j=0; j<HIDDEN_NEURONS+1; j++) {
  392.             output_layer[i].weights[j] = output_weights[i*(HIDDEN_NEURONS+1)+j];
  393.         }
  394.     }
  395.  
  396. }
Add Comment
Please, Sign In to add comment