/Player/Test/test_main_webots.lua

http://github.com/UPenn-RoboCup/UPennalizers · Lua · 261 lines · 204 code · 40 blank · 17 comment · 52 complexity · 7eddb1ae996f5e72be7f2c71deb4f128 MD5 · raw file

  1. cwd = cwd or os.getenv('PWD')
  2. package.path = cwd.."/?.lua;"..package.path;
  3. require('init')
  4. require('unix')
  5. require('Config')
  6. require('Speak')
  7. require('shm')
  8. require('vector')
  9. require('gcm')
  10. require('wcm')
  11. require('mcm')
  12. require('getch')
  13. io.stdout:flush();
  14. require('Body')
  15. require('Motion')
  16. require('Team')
  17. require('GameControl')
  18. use_gps_only = Config.use_gps_only or 0;
  19. if use_gps_only>0 then
  20. cognition = require('cognition_webots_gpsonly')
  21. else
  22. cognition = require('cognition_webots')
  23. end
  24. Motion.entry();
  25. Team.entry();
  26. GameControl.entry();
  27. darwin = false;
  28. webots = true
  29. if(Config.platform.name == 'OP') then darwin = true end
  30. init = false;
  31. calibrating = false;
  32. ready = true;
  33. smindex = 0;
  34. initToggle = true;
  35. -- main loop
  36. count = 0;
  37. lcount = 0;
  38. tUpdate = unix.time();
  39. --Webots specific key input
  40. controller.wb_robot_keyboard_enable(500);
  41. penalized_state={0,0,0,0,0};
  42. print("=====================================================")
  43. print("WEBOTS MAIN LOADED")
  44. print("1,2,3,4,5: Initial / Ready / Set/ Playing / Finished")
  45. print("8,9: Blue / Red kickoff")
  46. print("q,w,e,r,t: Penalize player 1/2/3/4/5")
  47. print("=====================================================")
  48. function process_keyinput()
  49. local str = controller.wb_robot_keyboard_get_key();
  50. if str>0 then
  51. byte = str;
  52. -- Webots only return captal letter number
  53. if byte>=65 and byte<=90 then
  54. byte = byte + 32;
  55. end
  56. penalize_player=0;
  57. if byte==string.byte("1") then
  58. Speak.talk('Initial');
  59. gcm.set_game_state(0);
  60. elseif byte==string.byte("2") then
  61. Speak.talk('Ready');
  62. gcm.set_game_state(1);
  63. elseif byte==string.byte("3") then
  64. Speak.talk('Set');
  65. gcm.set_game_state(2);
  66. elseif byte==string.byte("4") then
  67. Speak.talk('Playing');
  68. gcm.set_game_state(3);
  69. elseif byte==string.byte("5") then
  70. Speak.talk('Finished');
  71. gcm.set_game_state(4);
  72. elseif byte==string.byte("8") then
  73. --Blue team kickoff
  74. if gcm.get_team_color()==0 then
  75. gcm.set_game_kickoff(1);
  76. else
  77. gcm.set_game_kickoff(0);
  78. end
  79. Speak.talk('Blue kickoff');
  80. elseif byte==string.byte("9") then
  81. if gcm.get_team_color()==0 then
  82. gcm.set_game_kickoff(0);
  83. else
  84. gcm.set_game_kickoff(1);
  85. end
  86. Speak.talk('Red kickoff');
  87. elseif byte==string.byte("q") then
  88. penalize_player=1;
  89. penalize_team = 0;
  90. elseif byte==string.byte("w") then
  91. penalize_player=2;
  92. penalize_team = 0;
  93. elseif byte==string.byte("e") then
  94. penalize_player=3;
  95. penalize_team = 0;
  96. elseif byte==string.byte("r") then
  97. penalize_player=4;
  98. penalize_team = 0;
  99. elseif byte==string.byte("t") then
  100. penalize_player=5;
  101. penalize_team = 0;
  102. elseif byte==string.byte("z") then
  103. penalize_player=1;
  104. penalize_team = 1;
  105. elseif byte==string.byte("x") then
  106. penalize_player=2;
  107. penalize_team = 1;
  108. elseif byte==string.byte("c") then
  109. penalize_player=3;
  110. penalize_team = 1;
  111. elseif byte==string.byte("v") then
  112. penalize_player=4;
  113. penalize_team = 1;
  114. elseif byte==string.byte("b") then
  115. penalize_player=5;
  116. penalize_team = 1;
  117. end
  118. if penalize_player>0 and penalize_team == gcm.get_team_color() then
  119. penalized_state[penalize_player]=1-penalized_state[penalize_player];
  120. gcm.set_game_penalty(penalized_state) ;
  121. if penalized_state[penalize_player]>0 then
  122. if penalize_team==0 then
  123. Speak.talk(string.format("Red Player %d penalized",penalize_player));
  124. else
  125. Speak.talk(string.format("Blue Player %d penalized",penalize_player));
  126. end
  127. else
  128. if penalize_team==0 then
  129. Speak.talk(string.format("Red Player %d unpenalized",penalize_player));
  130. else
  131. Speak.talk(string.format("Blue Player %d unpenalized",penalize_player));
  132. end
  133. end
  134. end
  135. end
  136. end
  137. function update()
  138. count = count + 1;
  139. --Update battery info
  140. wcm.set_robot_battery_level(Body.get_battery_level());
  141. if (not init) then
  142. if (calibrating) then
  143. if (Body.calibrate(count)) then
  144. Speak.talk('Calibration done');
  145. calibrating = false;
  146. ready = true;
  147. end
  148. elseif (ready) then
  149. -- initialize state machines
  150. package.path = cwd..'/BodyFSM/'..Config.fsm.body[smindex+1]..'/?.lua;'..package.path;
  151. package.path = cwd..'/HeadFSM/'..Config.fsm.head[smindex+1]..'/?.lua;'..package.path;
  152. package.path = cwd..'/GameFSM/'..Config.fsm.game..'/?.lua;'..package.path;
  153. require('BodyFSM')
  154. require('HeadFSM')
  155. require('GameFSM')
  156. BodyFSM.entry();
  157. HeadFSM.entry();
  158. GameFSM.entry();
  159. --[[
  160. if( webots ) then
  161. --BodyFSM.sm:add_event('button');
  162. GameFSM.sm:set_state('gamePlaying');
  163. end
  164. --]]
  165. init = true;
  166. else
  167. if (count % 20 == 0) then
  168. if (Body.get_change_state() == 1) then
  169. Speak.talk('Calibrating');
  170. calibrating = true;
  171. elseif (Body.get_change_role() == 1) then
  172. smindex = (smindex + 1) % #Config.fsm.body;
  173. end
  174. end
  175. -- toggle state indicator
  176. if (count % 100 == 0) then
  177. initToggle = not initToggle;
  178. if (initToggle) then
  179. Body.set_indicator_state({1,1,1});
  180. else
  181. Body.set_indicator_state({0,0,0});
  182. end
  183. end
  184. end
  185. else
  186. -- update state machines
  187. GameFSM.update();
  188. BodyFSM.update();
  189. HeadFSM.update();
  190. Motion.update();
  191. Body.update();
  192. end
  193. local dcount = 50;
  194. if (count % 50 == 0) then
  195. -- print('fps: '..(50 / (unix.time() - tUpdate)));
  196. tUpdate = unix.time();
  197. -- update battery indicator
  198. Body.set_indicator_batteryLevel(Body.get_battery_level());
  199. end
  200. -- check if the last update completed without errors
  201. lcount = lcount + 1;
  202. if (count ~= lcount) then
  203. print('count: '..count)
  204. print('lcount: '..lcount)
  205. Speak.talk('missed cycle');
  206. lcount = count;
  207. end
  208. end
  209. gcm.set_game_state(0);
  210. cognition.entry()
  211. t_cog =Body.get_time()
  212. while true do
  213. process_keyinput();
  214. t= Body.get_time()
  215. if t-t_cog>1/30 then
  216. cognition.update();
  217. t_cog = t
  218. end
  219. update();
  220. io.stdout:flush();
  221. end