Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. #include <webots/robot.h>
  2. #include <webots/differential_wheels.h>
  3. #include <webots/accelerometer.h>
  4. #include <webots/keyboard.h>
  5. #include <webots/led.h>
  6.  
  7. // Global defines
  8. #define LEFT 0
  9. #define RIGHT 1
  10. #define TIME_STEP 32 // [ms]
  11.  
  12. // LEDs
  13. #define NB_LEDS 8
  14. WbDeviceTag led[NB_LEDS];
  15.  
  16. //
  17. double ledDelay = 0.20; // 1 second
  18. double lastLedTime = 0.0; // time in seconds
  19. bool isLedOn = false;
  20. double currentTime = 0.0;
  21. //
  22.  
  23. int speed[2]={0,0};
  24.  
  25.  
  26. static void reset(void)
  27. {
  28. // get Leds (from led0 to led7)
  29. int i;
  30. char text[5] = "led0";
  31. for(i = 0; i < NB_LEDS; i++) {
  32. led[i] = wb_robot_get_device(text); //get leds
  33. text[3]++;
  34. }
  35.  
  36. // enable keyboard
  37. wb_keyboard_enable(TIME_STEP);
  38.  
  39. // Switch off leds
  40. for(i = 0; i < NB_LEDS; i++) {
  41. wb_led_set(led[i],0);
  42. }
  43. }
  44.  
  45.  
  46. static void switch_off_all_leds() {
  47. for(int it=0;it<NB_LEDS;it++) {
  48. wb_led_set(led[it],0); // switch off led[it]
  49. }
  50. }
  51.  
  52. static void switch_on_all_leds() {
  53. for(int it=0;it<NB_LEDS;it++) {
  54. wb_led_set(led[it],1); // switch on led[it]
  55. }
  56. }
  57.  
  58. void blinking_led() {
  59. if (currentTime - lastLedTime > ledDelay) {
  60. if (isLedOn)
  61. switch_on_all_leds();
  62. else
  63. switch_off_all_leds();
  64. isLedOn = !isLedOn;
  65. lastLedTime = currentTime; }
  66. }
  67.  
  68. void blinking_right(){
  69. if (currentTime - lastLedTime > ledDelay) {
  70. if (isLedOn) {
  71. for(int it=0;it<NB_LEDS-4;it++) {
  72. wb_led_set(led[it],1); } }
  73. else
  74. switch_off_all_leds();
  75. isLedOn = !isLedOn;
  76. lastLedTime = currentTime; }
  77. }
  78.  
  79. void blinking_left(){
  80. if (currentTime - lastLedTime > ledDelay) {
  81. if (isLedOn) {
  82. for(int it=7;it>NB_LEDS-4;it--) {
  83. wb_led_set(led[it],1); }
  84.  
  85. wb_led_set(led[0],1); }
  86. else
  87. switch_off_all_leds();
  88. isLedOn = !isLedOn;
  89. lastLedTime = currentTime; }
  90. }
  91.  
  92.  
  93. static void run(void) {
  94. currentTime = wb_robot_get_time();
  95.  
  96. int key = wb_keyboard_get_key();
  97. switch (key) {
  98. case WB_KEYBOARD_UP:
  99. speed[LEFT]=150;
  100. speed[RIGHT]=150;
  101. break;
  102. case WB_KEYBOARD_DOWN:
  103. speed[LEFT]=-150;
  104. speed[RIGHT]=-150;
  105. break;
  106. case WB_KEYBOARD_LEFT:
  107. speed[RIGHT]=50;
  108. speed[LEFT]=-50;
  109. break;
  110. case WB_KEYBOARD_RIGHT:
  111. speed[LEFT]=50;
  112. speed[RIGHT]=-50;
  113. break;
  114. case '0':
  115. speed[LEFT]=speed[RIGHT]=0;
  116. break;
  117. default:
  118. break;
  119. }
  120.  
  121. if (speed[LEFT]>0 && speed[RIGHT]<0) blinking_right(); //right
  122. else if (speed[LEFT]<0 && speed[RIGHT]>0) blinking_left(); //left
  123. else if (speed[LEFT]<0 && speed[RIGHT]<0) blinking_led(); //backwards
  124. else switch_off_all_leds();
  125.  
  126. wb_differential_wheels_set_speed(speed[LEFT],speed[RIGHT]);
  127.  
  128. return;
  129. }
  130.  
  131. int main() {
  132. wb_robot_init();
  133. reset();
  134.  
  135. while(wb_robot_step(TIME_STEP) != -1) {
  136. run();
  137. }
  138.  
  139. wb_robot_cleanup();
  140.  
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement