请选择 进入手机版 | 继续访问电脑版

EMG_sensor Applications2---Dry up

[复制链接]

5

主题

9

帖子

47

积分

新手上路

Rank: 1

积分
47
yuyu_morning 发表于 2017-11-13 22:35:48
3517 0
本帖最后由 yuyu_morning 于 2017-11-13 22:42 编辑

Then the second code!(On the basis of first code to modify,through the color changes of the RGB lamp ring reflects the different of the strength !)
Note: The same code does not comment!
  1. #if defined(ARDUINO) && ARDUINO >= 100
  2. #include "Arduino.h"
  3. #else
  4. #include "WProgram.h"
  5. #endif

  6. #include "EMGFilters.h"
  7. //Use the light ring to reflect the signal strength, needes to use mbot RGB underlying processing functions
  8. #include "MakeblockOrion.h"  
  9. #define TIMING_DEBUG 1

  10. // input pin number
  11. #define sensorInputPin A0

  12. //Define RGB data input pin
  13. MeRGBLed led(PORT_3);  

  14. //The numbles of RGB lights
  15. #define RGB_Count 24   
  16.   
  17. /*The enumeration of colors
  18. *
  19. */
  20. enum ColorSet {
  21. RED = 1,
  22. ORANGE,
  23. YELLOW,
  24. GREEN,
  25. CYAN,
  26. BLUE,
  27. PURPLE,
  28. BLACK
  29. };

  30. /*The enumeration of orders of RGB lights,
  31. *corresponding to 1-24 respectively
  32. */
  33. enum RGBNum {
  34. RGB_1 = 1,
  35. RGB_2, RGB_3, RGB_4, RGB_5, RGB_6,
  36. RGB_7, RGB_8, RGB_9, RGB_10, RGB_11,
  37. RGB_12, RGB_13, RGB_14, RGB_15, RGB_16,
  38. RGB_17, RGB_18, RGB_19, RGB_20, RGB_21,
  39. RGB_22, RGB_23, RGB_24
  40. };

  41. enum EMGCalculatMode {
  42. FistTimeMode = 50,
  43. FistNumMode = 200
  44. };

  45. EMGFilters myFilter;

  46. SAMPLE_FREQUENCY sampleRate = SAMPLE_FREQ_500HZ;

  47. NOTCH_FREQUENCY humFreq = NOTCH_FREQ_50HZ;

  48. long threshold = 500, fistNum = 0;
  49. unsigned long timeMillis = 0, timeBeginzero = 0;

  50. /*Add a new variable of powerRatio for the strength of signal
  51. *
  52. */
  53. long integralData = 0, integralDataEve = 0, powerRatio = 0;
  54. int  TimeStandard = (uint8_t)FistNumMode;
  55. bool remainFlag = false;

  56. void setup() {
  57. //Set the numbers of RGB lights
  58. led.setNumber(RGB_Count);
  59. myFilter.init(sampleRate, humFreq, true, true, true);
  60. Serial.begin(115200);
  61. }
  62. void loop() {
  63. int value, dataAfterFilter, envlope;
  64. value = analogRead(sensorInputPin);
  65. dataAfterFilter = myFilter.update(value);
  66. envlope = sq(dataAfterFilter);
  67. envlope = (envlope > threshold) ? envlope : 0;
  68. if (threshold > 0) {
  69. integralDataEve = integralData;
  70. integralData += envlope;
  71. if ((integralDataEve == integralData) && (integralDataEve != 0)) {
  72.      timeMillis = millis();
  73.      if (remainFlag) {
  74.        timeBeginzero = timeMillis;
  75.        remainFlag = false;
  76.      }
  77.       if ((timeMillis - timeBeginzero) > TimeStandard) {
  78.         if (fistNum > 0) {
  79.          powerRatio = integralDataEve / threshold;

  80. //the value of powerRatio equals the ratio of Integral value and the reference value
  81. if (fistNum == 5)
  82.             fistNum = 0;
  83.          if (powerRatio != 0) {
  84.            Serial.print("powerRatio=");   Serial.println(powerRatio);
  85.             /*The display of RGB lights ring,full scale is 700, can be adjusted
  86.              *
  87.              */
  88.             if (powerRatio < 700)
  89.              RGB_LoopLight(RGB_1, RGB_24, (ColorSet)(powerRatio / 100 + 1), 20);
  90.             else
  91.              RGB_LoopLight(RGB_1, RGB_24, BLACK, 20);
  92.           }
  93.          else
  94.             RGB_LoopLight(RGB_1, RGB_24, BLACK, 20);
  95.         }
  96.         integralDataEve = integralData = 0;
  97.         fistNum ++;

  98.       }
  99.     }
  100.     else {
  101.      remainFlag = true;
  102.     }
  103.     if (TIMING_DEBUG) {
  104.       //            Serial.println(envlope);
  105.      //      Serial.print("fistNum=");
  106.       //      Serial.println(fistNum);
  107.     }
  108.   }
  109.   else {
  110.    Serial.println(envlope);
  111.   }
  112. delayMicroseconds(500);
  113. }

  114. /**
  115. * This is a function that light a single RGB light.
  116. *@parameter Num :The number of the light you wanted to light.
  117. *@parameter Color :The color of the light you wanted to light.
  118. *
  119. */
  120. void RGB_SingleLight(RGBNum Num, ColorSet Color)
  121. {
  122. uint8_t num = (uint8_t)Num - 1;
  123. switch (Color) {
  124.   case RED: led.setColorAt(num, 255, 0, 0); //red
  125.       break;
  126.     case ORANGE: led.setColorAt(num, 255, 165, 0); //orange
  127.       break;
  128.    case YELLOW: led.setColorAt(num, 255, 255, 0); //yellow
  129.      break;
  130.     case GREEN: led.setColorAt(num, 0, 255, 0); //green
  131.       break;
  132.    case CYAN: led.setColorAt(num, 0, 255, 255); //cyan
  133.       break;
  134.     case BLUE: led.setColorAt(num, 0, 0, 255); //blue
  135.       break;
  136.     case PURPLE: led.setColorAt(num, 160, 32, 240); //purple
  137.       break;
  138.     case BLACK: led.setColorAt(num, 0, 0, 0); //black
  139.       break;
  140.     default : led.setColorAt(num, 0, 0, 0);
  141.       break;
  142.   }
  143.   led.show();
  144. }

  145. /**
  146. * This is a function that cycle to turn on any of the 1-24 RGB lights.
  147. *@parameter BeginNum :The beginner of the range of the lights you wanted to light.
  148. *@parameter EndNum :The end of the range of the lights you wanted to light.
  149. *@parameter Color :The color of the range of the lights you wanted to light.
  150. *@parameter delayms :Delay microsecond level
  151. *
  152. */
  153. void RGB_LoopLight(RGBNum BeginNum, RGBNum EndNum,
  154.                    ColorSet Color, long delayms)
  155. {
  156. uint8_t num1 = (uint8_t)BeginNum, num2 = (uint8_t)EndNum;
  157.   if (num1 < num2) {
  158.    do {
  159.       delayMicroseconds(delayms);
  160.       RGB_SingleLight((RGBNum)num1, Color);
  161.      num1 ++;
  162.     } while (num1 <= num2);
  163.   }
  164.   else {
  165.    do {
  166.      delayMicroseconds(delayms);
  167.       RGB_SingleLight((RGBNum)num1, Color);
  168.      num1 --;
  169.    } while (num1 >= num2);
  170. }
  171. }
复制代码

By defining a powerRatio to reflect the ratio of integral value and threshold in relaxed state.The greater the ratio, the bigger the energy accumulated by each fist!In fact, there has the code of lights ring,the light ring shows a different color according to the value of powerRatio, (seven rainbow colors from 0 to 700,may be you will pinch it to gameover! )

Well, today's sharing is ended! To be honest this EMG sensor is still quite fun! ANd the two demo is still a little problem!For example: need to re-measure the baseline value when you take off it; EMG sensor is not worn when the integral value will be larger and so on. . .But this requires you to modify it! More good demos also need everyone to work together to activate ours brain! Dry up!


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热帖滚动