lightServoControl.ino 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. Author: corvin
  3. 1: rosserial Subscriber,function: leds.setPixelColor(i,y) can be used to let number i of your led turn on with color of y
  4. and you can draw your idea easily with this function but never forget function: led.show()
  5. 2:
  6. */
  7. #include <ros.h>
  8. #include <std_msgs/Int32.h>
  9. #include <Adafruit_NeoPixel.h>
  10. ros::NodeHandle nh;
  11. #define PIN 6 //The signal pin connected with Arduino
  12. #define LED_COUNT 60 //the amount of the leds of your strip
  13. String moveTimeStr = " T300"; //move target position need 300ms
  14. String upDownServoStr = "#11 P";
  15. String leftRightSErvoStr = "#15 P";
  16. Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
  17. //show wakeup light effect
  18. void showWakeupLight()
  19. {
  20. for (int i = 0; i < LED_COUNT; i++)
  21. {
  22. rainbow(i);
  23. delay(20); // Delay between rainbow slides
  24. }
  25. clearLEDs();
  26. }
  27. void servo_callback(const std_msgs::Int32& cmd_msg)
  28. {
  29. switch (cmd_msg.data)
  30. {
  31. case 0: //wave arm, greet
  32. Serial.println("PL0 SQ0 SM100 ONCE");
  33. break;
  34. case 1: //hug move
  35. Serial.println("PL0 SQ1 SM100 ONCE");
  36. break;
  37. case 2: //dance1
  38. Serial.println("PL0 SQ2 SM100 ONCE");
  39. break;
  40. case 3: //dance2
  41. Serial.println("PL0 SQ3 SM100 ONCE");
  42. break;
  43. case 4: //only show wakeup light
  44. showWakeupLight(); //show wakeup light
  45. break;
  46. case 5: //wave arm and show light
  47. Serial.println("PL0 SQ0 SM100 ONCE");
  48. showWakeupLight(); //show wakeup light
  49. break;
  50. default:
  51. //Serial.println("PL0"); //stop Operate
  52. moveHeadServo(cmd_msg.data);
  53. break;
  54. }
  55. }
  56. ros::Subscriber<std_msgs::Int32> sub("/sensor/lightServoControl", &servo_callback );
  57. void setup()
  58. {
  59. leds.begin(); // Call this to start up the LED strip.
  60. clearLEDs(); // This function, defined below, turns all LEDs off...
  61. nh.initNode();
  62. nh.subscribe(sub);
  63. Serial.begin(115200);
  64. }
  65. void loop()
  66. {
  67. nh.spinOnce();
  68. delay(1);
  69. }
  70. void moveHeadServo(int input)
  71. {
  72. int pose = 0;
  73. String cmdStr;
  74. if ((input >= 1000) && (input <= 1180)) //up or down move ---11 pin servo control board
  75. {
  76. pose = getServoPose(input - 1000);
  77. cmdStr = upDownServoStr + pose + moveTimeStr;
  78. }
  79. else if ((input >= 2000) && (input <= 2180)) //left or right move --15 pin servo control borad
  80. {
  81. pose = getServoPose(input - 2000);
  82. cmdStr = leftRightSErvoStr + pose + moveTimeStr;
  83. }
  84. else
  85. {
  86. }
  87. Serial.println(cmdStr);
  88. }
  89. int getServoPose(int degree)
  90. {
  91. int pos = map(degree, 0, 180, 500, 2500);
  92. return pos;
  93. }
  94. // Sets all LEDs to off, but DOES NOT update the display;
  95. // call leds.show() to actually turn them off after this.
  96. void clearLEDs()
  97. {
  98. for (int i = 0; i < LED_COUNT; i++)
  99. {
  100. leds.setPixelColor(i, 0);
  101. }
  102. leds.show(); //but the LEDs don't actually update until you call this.
  103. }
  104. // Prints a rainbow on the ENTIRE LED strip.
  105. // The rainbow begins at a specified position.
  106. // ROY G BIV!
  107. void rainbow(byte startPosition)
  108. {
  109. // Need to scale our rainbow. We want a variety of colors, even if there
  110. // are just 10 or so pixels.
  111. int rainbowScale = 192 / LED_COUNT;
  112. // Next we setup each pixel with the right color
  113. for (int i = 0; i < LED_COUNT; i++)
  114. {
  115. // There are 192 total colors we can get out of the rainbowOrder function.
  116. // It'll return a color between red->orange->green->...->violet for 0-191.
  117. leds.setPixelColor(i, rainbowOrder((rainbowScale * (i + startPosition)) % 192));
  118. }
  119. // Finally, actually turn the LEDs on:
  120. leds.show();
  121. }
  122. // Input a value 0 to 191 to get a color value.
  123. // The colors are a transition red->yellow->green->aqua->blue->fuchsia->red...
  124. // Adapted from Wheel function in the Adafruit_NeoPixel library example sketch
  125. uint32_t rainbowOrder(byte position)
  126. {
  127. // 6 total zones of color change:
  128. if (position < 31) // Red -> Yellow (Red = FF, blue = 0, green goes 00-FF)
  129. {
  130. return leds.Color(0xFF, position * 8, 0);
  131. }
  132. else if (position < 63) // Yellow -> Green (Green = FF, blue = 0, red goes FF->00)
  133. {
  134. position -= 31;
  135. return leds.Color(0xFF - position * 8, 0xFF, 0);
  136. }
  137. else if (position < 95) // Green->Aqua (Green = FF, red = 0, blue goes 00->FF)
  138. {
  139. position -= 63;
  140. return leds.Color(0, 0xFF, position * 8);
  141. }
  142. else if (position < 127) // Aqua->Blue (Blue = FF, red = 0, green goes FF->00)
  143. {
  144. position -= 95;
  145. return leds.Color(0, 0xFF - position * 8, 0xFF);
  146. }
  147. else if (position < 159) // Blue->Fuchsia (Blue = FF, green = 0, red goes 00->FF)
  148. {
  149. position -= 127;
  150. return leds.Color(position * 8, 0, 0xFF);
  151. }
  152. else //160 <position< 191 Fuchsia->Red (Red = FF, green = 0, blue goes FF->00)
  153. {
  154. position -= 159;
  155. return leds.Color(0xFF, 0x00, 0xFF - position * 8);
  156. }
  157. }