servo.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /**
  2. * @license Licensed under the Apache License, Version 2.0 (the "License"):
  3. * http://www.apache.org/licenses/LICENSE-2.0
  4. */
  5. /**
  6. * @fileoverview Arduino code generator for the Servo library blocks.
  7. * The Arduino Servo library docs: http://arduino.cc/en/reference/servo
  8. *
  9. * TODO: If angle selector added to blocks edit code here.
  10. */
  11. 'use strict';
  12. goog.provide('Blockly.Python.servo');
  13. goog.require('Blockly.Python');
  14. /**
  15. * Code generator to setup a servo.
  16. * Arduino code: #include <Servo.h>
  17. * Servo myServoX;
  18. * @param {!Blockly.Block} block Block to generate the code from.
  19. * @return {string} Completed code.
  20. */
  21. Blockly.Python['servo_setup'] = function(block) {
  22. //var pinKey = block.getFieldValue('SERVO_PIN');
  23. //var servoName = 'myServo' + pinKey;
  24. Blockly.Python.addInclude('servo', '#include <Servo.h>');
  25. //Blockly.Python.addDeclaration('servo_' + pinKey, 'Servo ' + servoName + ';');
  26. var code = '';
  27. return code;
  28. };
  29. /**
  30. * Code generator to set an angle (Y) value to a servo pin (X).
  31. * Arduino code: #include <Servo.h>
  32. * Servo myServoX;
  33. * setup { myServoX.attach(X); }
  34. * loop { myServoX.write(Y); }
  35. * @param {!Blockly.Block} block Block to generate the code from.
  36. * @return {string} Completed code.
  37. */
  38. Blockly.Python['servo_write'] = function(block) {
  39. var pinKey = block.getFieldValue('SERVO_PIN');
  40. var servoAngle = Blockly.Python.valueToCode(
  41. block, 'SERVO_ANGLE', Blockly.Python.ORDER_ATOMIC) || '90';
  42. var servoName = 'myServo' + pinKey;
  43. Blockly.Python.reservePin(
  44. block, pinKey, Blockly.Python.PinTypes.SERVO, 'Servo Write');
  45. Blockly.Python.addDeclaration('servo_' + pinKey, 'Servo ' + servoName + ';');
  46. var setupCode = servoName + '.attach(' + pinKey + ');';
  47. Blockly.Python.addSetup('servo_' + pinKey, setupCode, true);
  48. var code = servoName + '.write(' + servoAngle + ');\n';
  49. return code;
  50. };
  51. /**
  52. * Code generator to read an angle value from a servo pin (X).
  53. * Arduino code: #include <Servo.h>
  54. * Servo myServoX;
  55. * setup { myServoX.attach(X); }
  56. * loop { myServoX.read(); }
  57. * @param {!Blockly.Block} block Block to generate the code from.
  58. * @return {array} Completed code with order of operation.
  59. */
  60. Blockly.Python['servo_read'] = function(block) {
  61. var pinKey = block.getFieldValue('SERVO_PIN');
  62. var servoName = 'myServo' + pinKey;
  63. Blockly.Python.reservePin(
  64. block, pinKey, Blockly.Python.PinTypes.SERVO, 'Servo Read');
  65. Blockly.Python.addDeclaration('servo_' + pinKey, 'Servo ' + servoName + ';');
  66. var setupCode = servoName + '.attach(' + pinKey + ');';
  67. Blockly.Python.addSetup('servo_' + pinKey, setupCode, true);
  68. var code = servoName + '.read()';
  69. return [code, Blockly.Python.ORDER_ATOMIC];
  70. };
  71. Blockly.Python['robot_setup'] = function(block) {
  72. var YR_pin = block.getFieldValue('YR_PIN');
  73. var YL_pin = block.getFieldValue('YL_PIN');
  74. var RR_pin = block.getFieldValue('RR_PIN');
  75. var RL_pin = block.getFieldValue('RL_PIN');
  76. Blockly.Python.reservePin(
  77. block, YR_pin, Blockly.Python.PinTypes.OUTPUT, 'YR_pin');
  78. Blockly.Python.reservePin(
  79. block, YL_pin, Blockly.Python.PinTypes.OUTPUT, 'YL_pin');
  80. Blockly.Python.reservePin(
  81. block, RR_pin, Blockly.Python.PinTypes.OUTPUT, 'RR_pin');
  82. Blockly.Python.reservePin(
  83. block, RL_pin, Blockly.Python.PinTypes.OUTPUT, 'RL_pin');
  84. Blockly.Python.addInclude('servo', '#include <Servo.h>');
  85. Blockly.Python.addInclude('oscillator', '#include <Oscillator.h>');
  86. Blockly.Python.addInclude('EEPROM', '#include <EEPROM.h>');
  87. Blockly.Python.addInclude('SERVO_QUANT_AMOUNTS', '#define N_SERVOS 4');
  88. Blockly.Python.addInclude('EEPROM_TRIM', '#define EEPROM_TRIM false');
  89. Blockly.Python.addInclude('SERVO_TRIMRR', '#define TRIM_RR 7');
  90. Blockly.Python.addInclude('SERVO_TRIMRL', '#define TRIM_RL 4');
  91. Blockly.Python.addInclude('SERVO_TRIMYR', '#define TRIM_YR 4');
  92. Blockly.Python.addInclude('SERVO_TRIMYL', '#define TRIM_YL 7\n');
  93. var robot_def = '#define PIN_YR ' + YR_pin + '\n' +
  94. '#define PIN_YL ' + YL_pin + '\n' +
  95. '#define PIN_RR ' + RR_pin + '\n' +
  96. '#define PIN_RL ' + RL_pin + '\n' +
  97. '#define INTERVALTIME 10.0\n\n' +
  98. 'Oscillator servo[N_SERVOS];\n\n' +
  99. 'void goingUp(int tempo);\n' +
  100. 'void drunk (int tempo);\n' +
  101. 'void noGravity(int tempo);\n' +
  102. 'void kickLeft(int tempo);\n' +
  103. 'void kickRight(int tempo);\n' +
  104. 'void run(int steps, int T = 500);\n' +
  105. 'void walk(int steps, int T = 1000);\n' +
  106. 'void backyard(int steps, int T = 3000);\n' +
  107. 'void backyardSlow(int steps, int T = 5000);\n' +
  108. 'void turnLeft(int steps, int T = 3000);\n' +
  109. 'void turnRight(int steps, int T = 3000);\n' +
  110. 'void moonWalkLeft(int steps, int T = 1000);\n' +
  111. 'void moonWalkRight(int steps, int T = 1000);\n' +
  112. 'void crusaito(int steps, int T = 1000);\n' +
  113. 'void swing(int steps, int T = 1000);\n' +
  114. 'void upDown(int steps, int T = 1000);\n' +
  115. 'void flapping(int steps, int T = 1000);\n';
  116. Blockly.Python.addDeclaration('robot_def', robot_def);
  117. var servo_setup = 'servo[0].attach(PIN_RR);\n' +
  118. ' servo[1].attach(PIN_RL);\n' +
  119. ' servo[2].attach(PIN_YR);\n' +
  120. ' servo[3].attach(PIN_YL);';
  121. var trim_setup = 'int trim;';
  122. var eeprom_setup = 'if (EEPROM_TRIM) {\n' +
  123. ' for (int x = 0; x < 4; x++) {\n' +
  124. ' trim = EEPROM.read(x);\n' +
  125. ' if (trim > 128)trim = trim - 256;\n' +
  126. ' Serial.print("TRIM ");\n' +
  127. ' Serial.print(x);\n' +
  128. ' Serial.print(" en ");\n' +
  129. ' Serial.println(trim);\n' +
  130. ' servo[x].SetTrim(trim);\n }\n }\n' +
  131. ' else {\n' +
  132. ' servo[0].SetTrim(TRIM_RR);\n' +
  133. ' servo[1].SetTrim(TRIM_RL);\n' +
  134. ' servo[2].SetTrim(TRIM_YR);\n' +
  135. ' servo[3].SetTrim(TRIM_YL);\n }';
  136. Blockly.Python.addSetup('servo_setup', servo_setup);
  137. Blockly.Python.addSetup('trim_setup', trim_setup);
  138. Blockly.Python.addSetup('eeprom_setup', eeprom_setup);
  139. var dance = 'void dance() {' +
  140. '\n for ( int i = 0; i < 4; i++ ) {' +
  141. '\n servo[i].SetPosition(90);\n }' +
  142. '\n primera_parte();' +
  143. '\n segunda_parte();' +
  144. '\n moonWalkLeft(4, t * 2);' +
  145. '\n moonWalkRight(4, t * 2);' +
  146. '\n moonWalkLeft(4, t * 2);' +
  147. '\n moonWalkRight(4, t * 2);' +
  148. '\n primera_parte();' +
  149. '\n crusaito(1, t * 8);' +
  150. '\n crusaito(1, t * 7);' +
  151. '\n for (int i = 0; i < 16; i++) {' +
  152. '\n flapping(1, t / 4);' +
  153. '\n delay(3 * t / 4);\n }' +
  154. '\n moonWalkRight(4, t * 2);' +
  155. '\n moonWalkLeft(4, t * 2);' +
  156. '\n moonWalkRight(4, t * 2);' +
  157. '\n moonWalkLeft(4, t * 2);' +
  158. '\n drunk(t * 4);' +
  159. '\n drunk(t * 4);' +
  160. '\n kickLeft(t);' +
  161. '\n kickRight(t);' +
  162. '\n delay(t * 4);' +
  163. '\n drunk(t / 2);' +
  164. '\n delay(t * 4);' +
  165. '\n walk(2, t * 2);' +
  166. '\n backyard(2, t * 2);' +
  167. '\n goingUp(t * 2);' +
  168. '\n goingUp(t * 1);' +
  169. '\n noGravity(t * 2);' +
  170. '\n crusaito(1, t * 2);' +
  171. '\n crusaito(1, t * 8);' +
  172. '\n crusaito(1, t * 2);' +
  173. '\n crusaito(1, t * 8);' +
  174. '\n crusaito(1, t * 2);' +
  175. '\n crusaito(1, t * 3);' +
  176. '\n delay(t);' +
  177. '\n primera_parte();' +
  178. '\n for (int i = 0; i < 32; i++) {' +
  179. '\n flapping(1, t / 2);' +
  180. '\n delay(t / 2);\n }' +
  181. '\n for ( int i = 0; i < 4; i++ ) {' +
  182. '\n servo[i].SetPosition(90);\n }\n}\n';
  183. Blockly.Python.addFunction('dance', dance);
  184. var oscillate = 'void oscillate(int A[N_SERVOS], int O[N_SERVOS], int T, double phase_diff[N_SERVOS]) {' +
  185. '\n for (int i = 0; i < 4; i++) {' +
  186. '\n servo[i].SetO(O[i]);' +
  187. '\n servo[i].SetA(A[i]);' +
  188. '\n servo[i].SetT(T);' +
  189. '\n servo[i].SetPh(phase_diff[i]);\n }' +
  190. '\n double ref = millis();' +
  191. '\n for (double x = ref; x < T + ref; x = millis()) {' +
  192. '\n for (int i = 0; i < 4; i++) {' +
  193. '\n servo[i].refresh();' +
  194. '\n }' +
  195. '\n }\n}';
  196. Blockly.Python.addFunction('oscillate', oscillate);
  197. var moveNServos = 'unsigned long final_time;' +
  198. '\nunsigned long interval_time;' +
  199. '\nint oneTime;' +
  200. '\nint iteration;' +
  201. '\nfloat increment[N_SERVOS];' +
  202. '\nint oldPosition[] = {90, 90, 90, 90};\n\n' +
  203. 'void moveNServos(int time, int newPosition[]) {' +
  204. '\n for (int i = 0; i < N_SERVOS; i++) increment[i] = ((newPosition[i]) - oldPosition[i]) / (time / INTERVALTIME);' +
  205. '\n final_time = millis() + time;' +
  206. '\n iteration = 1;' +
  207. '\n while (millis() < final_time) { //Javi del futuro cambia esto' +
  208. '\n interval_time = millis() + INTERVALTIME;' +
  209. '\n oneTime = 0;' +
  210. '\n while (millis() < interval_time) {' +
  211. '\n if (oneTime < 1) {' +
  212. '\n for (int i = 0; i < N_SERVOS; i++) {' +
  213. '\n servo[i].SetPosition(oldPosition[i] + (iteration * increment[i]));' +
  214. '\n }' +
  215. '\n iteration++;' +
  216. '\n oneTime++;' +
  217. '\n }' +
  218. '\n }' +
  219. '\n }' +
  220. '\n for (int i = 0; i < N_SERVOS; i++) {' +
  221. '\n oldPosition[i] = newPosition[i];' +
  222. '\n }\n}';
  223. Blockly.Python.addFunction('moveNServos', moveNServos);
  224. var goingUp = 'void goingUp(int tempo) {' +
  225. '\n pause = millis();' +
  226. '\n for (int i = 0; i < 4; i++) servo[i].SetPosition(90);' +
  227. '\n delay(tempo);' +
  228. '\n servo[0].SetPosition(80);' +
  229. '\n servo[1].SetPosition(100);' +
  230. '\n delay(tempo);' +
  231. '\n servo[0].SetPosition(70);' +
  232. '\n servo[1].SetPosition(110);' +
  233. '\n delay(tempo);' +
  234. '\n servo[0].SetPosition(60);' +
  235. '\n servo[1].SetPosition(120);' +
  236. '\n delay(tempo);' +
  237. '\n servo[0].SetPosition(50);' +
  238. '\n servo[1].SetPosition(130);' +
  239. '\n delay(tempo);' +
  240. '\n servo[0].SetPosition(40);' +
  241. '\n servo[1].SetPosition(140);' +
  242. '\n delay(tempo);' +
  243. '\n servo[0].SetPosition(30);' +
  244. '\n servo[1].SetPosition(150);' +
  245. '\n delay(tempo);' +
  246. '\n servo[0].SetPosition(20);' +
  247. '\n servo[1].SetPosition(160);' +
  248. '\n delay(tempo);' +
  249. '\n while (millis() < pause + 8 * t);\n}';
  250. Blockly.Python.addFunction('goingUp', goingUp);
  251. var primera_parte = 'void primera_parte() {' +
  252. '\n int move1[4] = {60, 120, 90, 90};' +
  253. '\n int move2[4] = {90, 90, 90, 90};' +
  254. '\n int move3[4] = {40, 140, 90, 90};' +
  255. '\n for (int x = 0; x < 3; x++) {' +
  256. '\n for (int i = 0; i < 3; i++) {' +
  257. '\n lateral_fuerte(1, t / 2);' +
  258. '\n lateral_fuerte(0, t / 4);' +
  259. '\n lateral_fuerte(1, t / 4);' +
  260. '\n delay(t);\n }' +
  261. '\n pause = millis();' +
  262. '\n for (int i = 0; i < 4; i++) servo[i].SetPosition(90);' +
  263. '\n moveNServos(t * 0.4, move1);' +
  264. '\n moveNServos(t * 0.4, move2);' +
  265. '\n while (millis() < (pause + t * 2));\n }' +
  266. '\n for (int i = 0; i < 2; i++) {' +
  267. '\n lateral_fuerte(1, t / 2);' +
  268. '\n lateral_fuerte(0, t / 4);' +
  269. '\n lateral_fuerte(1, t / 4);' +
  270. '\n delay(t);\n }' +
  271. '\n pause = millis();' +
  272. '\n for (int i = 0; i < 4; i++) servo[i].SetPosition(90);' +
  273. '\n crusaito(1, t * 1.4);' +
  274. '\n moveNServos(t * 1, move3);' +
  275. '\n for (int i = 0; i < 4; i++) servo[i].SetPosition(90);' +
  276. '\n while (millis() < (pause + t * 4));\n}';
  277. Blockly.Python.addFunction('primera_parte', primera_parte);
  278. var segunda_parte = 'void segunda_parte() {' +
  279. '\n int move1[4] = {90, 90, 80, 100};' +
  280. '\n int move2[4] = {90, 90, 100, 80};' +
  281. '\n int move3[4] = {90, 90, 80, 100};' +
  282. '\n int move4[4] = {90, 90, 100, 80};' +
  283. '\n int move5[4] = {40, 140, 80, 100};' +
  284. '\n int move6[4] = {40, 140, 100, 80};' +
  285. '\n int move7[4] = {90, 90, 80, 100};' +
  286. '\n int move8[4] = {90, 90, 100, 80};' +
  287. '\n int move9[4] = {40, 140, 80, 100};' +
  288. '\n int move10[4] = {40, 140, 100, 80};' +
  289. '\n int move11[4] = {90, 90, 80, 100};' +
  290. '\n int move12[4] = {90, 90, 100, 80};' +
  291. '\n for (int x = 0; x < 7; x++) {' +
  292. '\n for (int i = 0; i < 3; i++) {' +
  293. '\n pause = millis();' +
  294. '\n moveNServos(t * 0.15, move1);' +
  295. '\n moveNServos(t * 0.15, move2);' +
  296. '\n moveNServos(t * 0.15, move3);' +
  297. '\n moveNServos(t * 0.15, move4);' +
  298. '\n while (millis() < (pause + t));\n }' +
  299. '\n pause = millis();' +
  300. '\n moveNServos(t * 0.15, move5);' +
  301. '\n moveNServos(t * 0.15, move6);' +
  302. '\n moveNServos(t * 0.15, move7);' +
  303. '\n moveNServos(t * 0.15, move8);' +
  304. '\n while (millis() < (pause + t));\n }' +
  305. '\n for (int i = 0; i < 3; i++) {' +
  306. '\n pause = millis();' +
  307. '\n moveNServos(t * 0.15, move9);' +
  308. '\n moveNServos(t * 0.15, move10);' +
  309. '\n moveNServos(t * 0.15, move11);' +
  310. '\n moveNServos(t * 0.15, move12);' +
  311. '\n while (millis() < (pause + t));' +
  312. '\n }\n}';
  313. Blockly.Python.addFunction('segunda_parte', segunda_parte);
  314. var lateral_fuerte = 'void lateral_fuerte(boolean side, int tempo) {' +
  315. '\n for (int i = 0; i < 4; i++) servo[i].SetPosition(90);' +
  316. '\n if (side) servo[0].SetPosition(40);' +
  317. '\n else servo[1].SetPosition(140);' +
  318. '\n delay(tempo / 2);' +
  319. '\n servo[0].SetPosition(90);' +
  320. '\n servo[1].SetPosition(90);' +
  321. '\n delay(tempo / 2);\n}';
  322. Blockly.Python.addFunction('lateral_fuerte', lateral_fuerte);
  323. var drunk = 'void drunk (int tempo) {' +
  324. '\n pause = millis();' +
  325. '\n int move1[] = {60, 70, 90, 90};' +
  326. '\n int move2[] = {110, 120, 90, 90};' +
  327. '\n int move3[] = {60, 70, 90, 90};' +
  328. '\n int move4[] = {110, 120, 90, 90};' +
  329. '\n moveNServos(tempo * 0.235, move1);' +
  330. '\n moveNServos(tempo * 0.235, move2);' +
  331. '\n moveNServos(tempo * 0.235, move3);' +
  332. '\n moveNServos(tempo * 0.235, move4);' +
  333. '\n while (millis() < (pause + tempo));\n}';
  334. Blockly.Python.addFunction('drunk', drunk);
  335. var noGravity = 'void noGravity(int tempo) {' +
  336. '\n int move1[4] = {120, 140, 90, 90};' +
  337. '\n int move2[4] = {140, 140, 90, 90};' +
  338. '\n int move3[4] = {120, 140, 90, 90};' +
  339. '\n int move4[4] = {90, 90, 90, 90};' +
  340. '\n for (int i = 0; i < 4; i++) servo[i].SetPosition(90);' +
  341. '\n for (int i = 0; i < N_SERVOS; i++) oldPosition[i] = 90;' +
  342. '\n moveNServos(tempo * 2, move1);' +
  343. '\n moveNServos(tempo * 2, move2);' +
  344. '\n delay(tempo * 2);' +
  345. '\n moveNServos(tempo * 2, move3);' +
  346. '\n moveNServos(tempo * 2, move4);\n}';
  347. Blockly.Python.addFunction('noGravity', noGravity);
  348. var kickLeft = 'void kickLeft(int tempo) {' +
  349. '\n for (int i = 0; i < 4; i++) servo[i].SetPosition(90);' +
  350. '\n delay(tempo);' +
  351. '\n servo[0].SetPosition(50);' +
  352. '\n servo[1].SetPosition(70);' +
  353. '\n delay(tempo);' +
  354. '\n servo[0].SetPosition(80);' +
  355. '\n servo[1].SetPosition(70);' +
  356. '\n delay(tempo / 4);' +
  357. '\n servo[0].SetPosition(30);' +
  358. '\n servo[1].SetPosition(70);' +
  359. '\n delay(tempo / 4);' +
  360. '\n servo[0].SetPosition(80);' +
  361. '\n servo[1].SetPosition(70);' +
  362. '\n delay(tempo / 4);' +
  363. '\n servo[0].SetPosition(30);' +
  364. '\n servo[1].SetPosition(70);' +
  365. '\n delay(tempo / 4);' +
  366. '\n servo[0].SetPosition(80);' +
  367. '\n servo[1].SetPosition(70);' +
  368. '\n delay(tempo);\n}';
  369. Blockly.Python.addFunction('kickLeft', kickLeft);
  370. var kickRight = 'void kickRight(int tempo) {' +
  371. '\n for (int i = 0; i < 4; i++) servo[i].SetPosition(90);' +
  372. '\n delay(tempo);' +
  373. '\n servo[0].SetPosition(110);' +
  374. '\n servo[1].SetPosition(130);' +
  375. '\n delay(tempo);' +
  376. '\n servo[0].SetPosition(110);' +
  377. '\n servo[1].SetPosition(100);' +
  378. '\n delay(tempo / 4);' +
  379. '\n servo[0].SetPosition(110);' +
  380. '\n servo[1].SetPosition(150);' +
  381. '\n delay(tempo / 4);' +
  382. '\n servo[0].SetPosition(110);' +
  383. '\n servo[1].SetPosition(80);' +
  384. '\n delay(tempo / 4);' +
  385. '\n servo[0].SetPosition(110);' +
  386. '\n servo[1].SetPosition(150);' +
  387. '\n delay(tempo / 4);' +
  388. '\n servo[0].SetPosition(110);' +
  389. '\n servo[1].SetPosition(100);' +
  390. '\n delay(tempo);\n}';
  391. Blockly.Python.addFunction('kickRight', kickRight);
  392. var walk = 'void walk(int steps, int T) {' +
  393. '\n int A[4] = {15, 15, 30, 30};' +
  394. '\n int O[4] = {0, 0, 0, 0};' +
  395. '\n double phase_diff[4] = {DEG2RAD(0), DEG2RAD(0), DEG2RAD(90), DEG2RAD(90)};' +
  396. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  397. Blockly.Python.addFunction('walk', walk);
  398. var run = 'void run(int steps, int T) {' +
  399. '\n int A[4] = {10, 10, 10, 10};' +
  400. '\n int O[4] = {0, 0, 0, 0};' +
  401. '\n double phase_diff[4] = {DEG2RAD(0), DEG2RAD(0), DEG2RAD(90), DEG2RAD(90)};' +
  402. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  403. Blockly.Python.addFunction('run', run);
  404. var backyard = 'void backyard(int steps, int T) {' +
  405. '\n int A[4] = {15, 15, 30, 30};' +
  406. '\n int O[4] = {0, 0, 0, 0};' +
  407. '\n double phase_diff[4] = {DEG2RAD(0), DEG2RAD(0), DEG2RAD(-90), DEG2RAD(-90)};' +
  408. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  409. Blockly.Python.addFunction('backyard', backyard);
  410. var backyardSlow = 'void backyardSlow(int steps, int T) {' +
  411. '\n int A[4] = {15, 15, 30, 30};' +
  412. '\n int O[4] = {0, 0, 0, 0};' +
  413. '\n double phase_diff[4] = {DEG2RAD(0), DEG2RAD(0), DEG2RAD(-90), DEG2RAD(-90)};' +
  414. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  415. Blockly.Python.addFunction('backyardSlow', backyardSlow);
  416. var turnLeft = 'void turnLeft(int steps, int T) {' +
  417. '\n int A[4] = {20, 20, 10, 30};' +
  418. '\n int O[4] = {0, 0, 0, 0};' +
  419. '\n double phase_diff[4] = {DEG2RAD(0), DEG2RAD(0), DEG2RAD(90), DEG2RAD(90)};' +
  420. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  421. Blockly.Python.addFunction('turnLeft', turnLeft);
  422. var turnRight = 'void turnRight(int steps, int T) {' +
  423. '\n int A[4] = {20, 20, 30, 10};' +
  424. '\n int O[4] = {0, 0, 0, 0};' +
  425. '\n double phase_diff[4] = {DEG2RAD(0), DEG2RAD(0), DEG2RAD(90), DEG2RAD(90)};' +
  426. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  427. Blockly.Python.addFunction('turnRight', turnRight);
  428. var moonWalkRight = 'void moonWalkRight(int steps, int T) {' +
  429. '\n int A[4] = {25, 25, 0, 0};' +
  430. '\n int O[4] = { -15 , 15, 0, 0};' +
  431. '\n double phase_diff[4] = {DEG2RAD(0), DEG2RAD(180 + 120), DEG2RAD(90), DEG2RAD(90)};' +
  432. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  433. Blockly.Python.addFunction('turnRight', turnRight);
  434. var moonWalkLeft = 'void moonWalkLeft(int steps, int T) {' +
  435. '\n int A[4] = {25, 25, 0, 0};' +
  436. '\n int O[4] = { -15, 15, 0, 0};' +
  437. '\n double phase_diff[4] = {DEG2RAD(0), DEG2RAD(180 - 120), DEG2RAD(90), DEG2RAD(90)};' +
  438. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  439. Blockly.Python.addFunction('turnRight', turnRight);
  440. var crusaito = 'void crusaito(int steps, int T) {' +
  441. '\n int A[4] = {25, 25, 30, 30};' +
  442. '\n int O[4] = { - 15, 15, 0, 0};' +
  443. '\n double phase_diff[4] = {DEG2RAD(0), DEG2RAD(180 + 120), DEG2RAD(90), DEG2RAD(90)};' +
  444. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  445. Blockly.Python.addFunction('crusaito', crusaito);
  446. var swing = 'void swing(int steps, int T) {' +
  447. '\n int A[4] = {25, 25, 0, 0};' +
  448. '\n int O[4] = { -15, 15, 0, 0};' +
  449. '\n double phase_diff[4] = {DEG2RAD(0), DEG2RAD(0), DEG2RAD(90), DEG2RAD(90)};' +
  450. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  451. Blockly.Python.addFunction('swing', swing);
  452. var upDown = 'void upDown(int steps, int T) {' +
  453. '\n int A[4] = {25, 25, 0, 0};' +
  454. '\n int O[4] = { -15, 15, 0, 0};' +
  455. '\n double phase_diff[4] = {DEG2RAD(180), DEG2RAD(0), DEG2RAD(270), DEG2RAD(270)};' +
  456. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  457. Blockly.Python.addFunction('upDown', upDown);
  458. var flapping = 'void flapping(int steps, int T) {' +
  459. '\n int A[4] = {15, 15, 8, 8};' +
  460. '\n int O[4] = { -A[0], A[1], 0, 0};' +
  461. '\n double phase_diff[4] = {DEG2RAD(0), DEG2RAD(180), DEG2RAD(90), DEG2RAD(-90)};' +
  462. '\n for (int i = 0; i < steps; i++)oscillate(A, O, T, phase_diff);\n}';
  463. Blockly.Python.addFunction('flapping', flapping);
  464. }
  465. Blockly.Python['robot_set_movement'] = function(block) {
  466. var value_movement = Blockly.Python.valueToCode(block, 'MOVEMENT', Blockly.Python.ORDER_ATOMIC);
  467. var value_tempo = Blockly.Python.valueToCode(block, 'TEMPO', Blockly.Python.ORDER_ATOMIC);
  468. var robot_setmovement_def = 'int t = ' + value_tempo + ';\n' +
  469. 'double pause = 0;\n';
  470. Blockly.Python.addDeclaration('robot_setmovement_def', robot_setmovement_def);
  471. var steps = "";
  472. switch (value_movement) {
  473. case "goingUp":
  474. case "drunk":
  475. case "kickLeft":
  476. case "kickRight":
  477. steps = '(';
  478. break;
  479. default:
  480. steps = '(1, ';
  481. break;
  482. }
  483. var code = value_movement + steps + ' 2*t);\n';
  484. return code;
  485. };
  486. Blockly.Python['robot_movement'] = function(block) {
  487. var dropdown_movement = block.getFieldValue('MOVEMENT');
  488. var code = dropdown_movement;
  489. return [code, Blockly.Python.ORDER_ATOMIC];
  490. };
  491. Blockly.Python['robot_tempo'] = function(block) {
  492. var number_tempo = block.getFieldValue('TEMPO');
  493. var code = number_tempo;
  494. return [code, Blockly.Python.ORDER_ATOMIC];
  495. };