K210.js 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  1. Blockly.Blocks['robot_dog_phonetic_to_pinyin_recognition_setup'] = {
  2. init: function () {
  3. this.appendDummyInput()
  4. .appendField(new Blockly.FieldImage("blockly/media/robot_dog_pinyin.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
  5. this.appendDummyInput()
  6. .appendField(Blockly.Msg.robot_dog_movement);
  7. this.setPreviousStatement(true, null);
  8. this.setNextStatement(true, null);
  9. this.setColour("#5c42bd");
  10. this.setTooltip(Blockly.Msg.robot_dog_movement_action_TOOLTIP);
  11. this.setHelpUrl("");
  12. }
  13. };
  14. Blockly.Python['robot_dog_phonetic_to_pinyin_recognition_setup'] = function (block) {
  15. Blockly.Python.definitions_.robot_dog_phonetic_to_pinyin_recognition_setup_def = "" +
  16. "try:\n" +
  17. " from cocorobo import firmware_info\n" +
  18. "except BaseException as e:\n" +
  19. " print(str(e))\n" +
  20. " pass\n" +
  21. "\n" +
  22. "asr = None\n" +
  23. "tim = None\n" +
  24. "t = None\n" +
  25. "\n" +
  26. "if str(firmware_info.ai()) == \"2020-12-26\" or str(firmware_info.ai()) == \"2021-04-16\" or str(firmware_info.ai()) == \"2021-05-20\" or str(firmware_info.ai()) == \"2021-06-11\" or str(firmware_info.ai()) == \"2021-07-09\" or str(firmware_info.ai()) == \"2021-08-04\":\n" +
  27. " from Maix import GPIO, I2S\n" +
  28. " from fpioa_manager import fm\n" +
  29. " fm.register(20,fm.fpioa.I2S0_IN_D0, force=True)\n" +
  30. " fm.register(18,fm.fpioa.I2S0_SCLK, force=True) # dock 32\n" +
  31. " fm.register(19,fm.fpioa.I2S0_WS, force=True) # dock 30\n" +
  32. "\n" +
  33. " rx = I2S(I2S.DEVICE_0)\n" +
  34. " rx.channel_config(rx.CHANNEL_0, rx.RECEIVER, align_mode=I2S.STANDARD_MODE)\n" +
  35. " rx.set_sample_rate(16000)\n" +
  36. "\n" +
  37. " from speech_recognizer import asr\n" +
  38. "\n" +
  39. " class maix_asr_(asr):\n" +
  40. "\n" +
  41. " asr_vocab = ['lv', 'shi', 'yang', 'chun', 'yan', 'jing', 'da', 'kuai', 'wen', 'zhang', 'de', 'di', 'se', 'si', 'yue', 'lin', 'luan', 'geng', 'xian', 'huo', 'xiu', 'mei', 'yi', 'ang', 'ran', 'ta', 'jin', 'ping', 'yao', 'bu', 'li', 'liang', 'zai', 'yong', 'dao', 'shang', 'xia', 'fan', 'teng', 'dong', 'she', 'xing', 'zhuang', 'ru', 'hai', 'tun', 'zhi', 'tou', 'you', 'ling', 'pao', 'hao', 'le', 'zha', 'zen', 'me', 'zheng', 'cai', 'ya', 'shu', 'tuo', 'qu', 'fu', 'guang', 'bang', 'zi', 'chong', 'shui', 'cuan', 'ke', 'shei', 'wan', 'hou', 'zhao', 'jian', 'zuo', 'cu', 'hei', 'yu', 'ce', 'ming', 'dui', 'cheng', 'men', 'wo', 'bei', 'dai', 'zhe', 'hu', 'jiao', 'pang', 'ji', 'lao', 'nong', 'kang', 'yuan', 'chao', 'hui', 'xiang', 'bing', 'qi', 'chang', 'nian', 'jia', 'tu', 'bi', 'pin', 'xi', 'zou', 'chu', 'cun', 'wang', 'na', 'ge', 'an', 'ning', 'tian', 'xiao', 'zhong', 'shen', 'nan', 'er', 'ri', 'zhu', 'xin', 'wai', 'luo', 'gang', 'qing', 'xun', 'te', 'cong', 'gan', 'lai', 'he', 'dan', 'wei', 'die', 'kai', 'ci', 'gu', 'neng', 'ba', 'bao', 'xue', 'shuai', 'dou', 'cao', 'mao', 'bo', 'zhou', 'lie', 'qie', 'ju', 'chuan', 'guo', 'lan', 'ni', 'tang', 'ban', 'su', 'quan', 'huan', 'ying', 'a', 'min', 'meng', 'wu', 'tai', 'hua', 'xie', 'pai', 'huang', 'gua', 'jiang', 'pian', 'ma', 'jie', 'wa', 'san', 'ka', 'zong', 'nv', 'gao', 'ye', 'biao', 'bie', 'zui', 'ren', 'jun', 'duo', 'ze', 'tan', 'mu', 'gui', 'qiu', 'bai', 'sang', 'jiu', 'yin', 'huai', 'rang', 'zan', 'shuo', 'sha', 'ben', 'yun', 'la', 'cuo', 'hang', 'ha', 'tuan', 'gong', 'shan', 'ai', 'kou', 'zhen', 'qiong', 'ding', 'dang', 'que', 'weng', 'qian', 'feng', 'jue', 'zhuan', 'ceng', 'zu', 'bian', 'nei', 'sheng', 'chan', 'zao', 'fang', 'qin', 'e', 'lian', 'fa', 'lu', 'sun', 'xu', 'deng', 'guan', 'shou', 'mo', 'zhan', 'po', 'pi', 'gun', 'shuang', 'qiang', 'kao', 'hong', 'kan', 'dian', 'kong', 'pei', 'tong', 'ting', 'zang', 'kuang', 'reng', 'ti', 'pan', 'heng', 'chi', 'lun', 'kun', 'han', 'lei', 'zuan', 'man', 'sen', 'duan', 'leng', 'sui', 'gai', 'ga', 'fou', 'kuo', 'ou', 'suo', 'sou', 'nu', 'du', 'mian', 'chou', 'hen', 'kua', 'shao', 'rou', 'xuan', 'can', 'sai', 'dun', 'niao', 'chui', 'chen', 'hun', 'peng', 'fen', 'cang', 'gen', 'shua', 'chuo', 'shun', 'cha', 'gou', 'mai', 'liu', 'diao', 'tao', 'niu', 'mi', 'chai', 'long', 'guai', 'xiong', 'mou', 'rong', 'ku', 'song', 'che', 'sao', 'piao', 'pu', 'tui', 'lang', 'chuang', 'keng', 'liao', 'miao', 'zhui', 'nai', 'lou', 'bin', 'juan', 'zhua', 'run', 'zeng', 'ao', 're', 'pa', 'qun', 'lia', 'cou', 'tie', 'zhai', 'kuan', 'kui', 'cui', 'mie', 'fei', 'tiao', 'nuo', 'gei', 'ca', 'zhun', 'nie', 'mang', 'zhuo', 'pen', 'zun', 'niang', 'suan', 'nao', 'ruan', 'qiao', 'fo', 'rui', 'rao', 'ruo', 'zei', 'en', 'za', 'diu', 'nve', 'sa', 'nin', 'shai', 'nen', 'ken', 'chuai', 'shuan', 'beng', 'ne', 'lve', 'qia', 'jiong', 'pie', 'seng', 'nuan', 'nang', 'miu', 'pou', 'cen', 'dia', 'o', 'zhuai', 'yo', 'dei', 'n', 'ei', 'nou', 'bia', 'eng', 'den', '_']\n" +
  42. "\n" +
  43. " def get_asr_list(string='xiao-ai-fas-tong-xue'):\n" +
  44. " return [__class__.asr_vocab.index(t) for t in string.split('-') if t in __class__.asr_vocab]\n" +
  45. "\n" +
  46. " def get_asr_string(listobj=[117, 214, 257, 144]):\n" +
  47. " return '-'.join([__class__.asr_vocab[t] for t in listobj if t < len(__class__.asr_vocab)])\n" +
  48. "\n" +
  49. " def unit_test():\n" +
  50. " print(__class__.get_asr_list('xiao-ai'))\n" +
  51. " print(__class__.get_asr_string(__class__.get_asr_list('xiao-ai-fas-tong-xue')))\n" +
  52. "\n" +
  53. " def config(self, sets):\n" +
  54. " self.set([(sets[key], __class__.get_asr_list(key)) for key in sets])\n" +
  55. "\n" +
  56. " def recognize(self):\n" +
  57. " res = self.result()\n" +
  58. " # print(tmp)\n" +
  59. " if res != None:\n" +
  60. " #print(res)\n" +
  61. " sets = {}\n" +
  62. " for tmp in res:\n" +
  63. " #print(tmp)\n" +
  64. " sets[__class__.get_asr_string(tmp[1])] = tmp[0]\n" +
  65. " #print(tmp[0], get_asr_string(tmp[1]))\n" +
  66. " return sets\n" +
  67. " return None\n" +
  68. "\n";
  69. var code = "";
  70. return code;
  71. };
  72. Blockly.Blocks['robot_dog_phonetic_to_pinyin_recognition_set_param'] = {
  73. init: function () {
  74. this.appendDummyInput()
  75. .appendField(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_set_param);
  76. this.appendValueInput("data")
  77. .setCheck(null)
  78. .appendField(Blockly.Msg.ai_speech_recognition_setup_pinyin);
  79. this.appendDummyInput()
  80. .appendField(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_set_param_1)
  81. .appendField(new Blockly.FieldDropdown([
  82. ["1", "1"],
  83. ["2", "2"],
  84. ["3", "3"],
  85. ["4", "4"],
  86. ["5", "5"],
  87. ["6", "6"],
  88. ["7", "7"]
  89. ]), "type");
  90. this.setPreviousStatement(true, null);
  91. this.setNextStatement(true, null);
  92. this.setInputsInline(false);
  93. this.setColour("#5c42bd");
  94. this.setTooltip(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_set_param_TOOLTIP);
  95. this.setHelpUrl("");
  96. }
  97. };
  98. Blockly.Python['robot_dog_phonetic_to_pinyin_recognition_set_param'] = function (block) {
  99. var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
  100. var _type = block.getFieldValue('type');
  101. Blockly.Python.definitions_.robot_dog_phonetic_to_pinyin_recognition_set_param_def = "" +
  102. "if str(firmware_info.ai()) == \"2020-12-26\" or str(firmware_info.ai()) == \"2021-04-16\" or str(firmware_info.ai()) == \"2021-05-20\" or str(firmware_info.ai()) == \"2021-06-11\" or str(firmware_info.ai()) == \"2021-07-09\" or str(firmware_info.ai()) == \"2021-08-04\":\n" +
  103. " from machine import Timer\n" +
  104. "\n" +
  105. " def on_timer(timer):\n" +
  106. " #print('time up:',timer)\n" +
  107. " #print('param:',timer.callback_arg())\n" +
  108. " timer.callback_arg().state()\n" +
  109. "\n" +
  110. // " t = None\n" +
  111. " if str(firmware_info.ai()) == \"2021-05-20\":\n" +
  112. " t = maix_asr_(0x790000, I2S.DEVICE_0, 3, shift=0) # maix bit set shift=1\n" +
  113. " elif str(firmware_info.ai()) == \"2021-06-11\" or str(firmware_info.ai()) == \"2021-07-09\" or str(firmware_info.ai()) == \"2021-08-04\":\n" +
  114. " t = maix_asr_(0x681000, I2S.DEVICE_0, 3, shift=0) # maix bit set shift=1\n" +
  115. " else:\n" +
  116. " t = maix_asr_(0x500000, I2S.DEVICE_0, 3, shift=0)\n" +
  117. "\n" +
  118. " t.config(" + data + ")\n" +
  119. "\n" +
  120. " tim = Timer(Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=32, callback=on_timer, arg=t,start=False,priority=" + _type + ")\n" +
  121. " def getRecognizeResult():\n" +
  122. " try:\n" +
  123. " global t\n" +
  124. " tmp = t.recognize()\n" +
  125. " if tmp != None:\n" +
  126. " print(tmp)\n" +
  127. " for key in tmp.keys():\n" +
  128. " return key\n" +
  129. " except:\n" +
  130. ' pass\n' +
  131. "\n" +
  132. "else:\n" +
  133. " from cocorobo import mandarin_asr\n" +
  134. " asr = mandarin_asr(\n" +
  135. " config = " + data + "\n" +
  136. " )\n" +
  137. "\n";
  138. var code = "";
  139. return code;
  140. };
  141. Blockly.Blocks['robot_dog_phonetic_to_pinyin_start_recognition'] = {
  142. init: function () {
  143. this.appendDummyInput()
  144. .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_start_recognition);
  145. this.setPreviousStatement(true, null);
  146. this.setNextStatement(true, null);
  147. this.setColour("#5c42bd");
  148. this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_start_recognition_TOOLTIP);
  149. this.setHelpUrl("");
  150. }
  151. };
  152. Blockly.Python['robot_dog_phonetic_to_pinyin_start_recognition'] = function (block) {
  153. var code = "tim.start()\n";
  154. return code;
  155. };
  156. Blockly.Blocks['robot_dog_phonetic_to_pinyin_stop_recognition'] = {
  157. init: function () {
  158. this.appendDummyInput()
  159. .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_stop_recognition);
  160. this.setPreviousStatement(true, null);
  161. this.setNextStatement(true, null);
  162. this.setColour("#5c42bd");
  163. this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_stop_recognition_TOOLTIP);
  164. this.setHelpUrl("");
  165. }
  166. };
  167. Blockly.Python['robot_dog_phonetic_to_pinyin_stop_recognition'] = function (block) {
  168. var code = "tim.stop()\n" +
  169. "t.__del__()\n" +
  170. "del t\n";
  171. return code;
  172. };
  173. Blockly.Blocks['robot_dog_phonetic_to_pinyin_pause_recognition'] = {
  174. init: function () {
  175. this.appendDummyInput()
  176. .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_pause_recognition);
  177. this.setPreviousStatement(true, null);
  178. this.setNextStatement(true, null);
  179. this.setColour("#5c42bd");
  180. this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_pause_recognition_TOOLTIP);
  181. this.setHelpUrl("");
  182. }
  183. };
  184. Blockly.Python['robot_dog_phonetic_to_pinyin_pause_recognition'] = function (block) {
  185. var code = "tim.stop()\n";
  186. return code;
  187. };
  188. Blockly.Blocks['robot_dog_phonetic_to_pinyin_get_result'] = {
  189. init: function () {
  190. this.appendDummyInput()
  191. .appendField(Blockly.Msg.robot_dog_phonetic_to_pinyin_get_result);
  192. this.setOutput(true, null);
  193. this.setColour("#5c42bd");
  194. this.setTooltip(Blockly.Msg.robot_dog_phonetic_to_pinyin_get_result_TOOLTIP);
  195. this.setHelpUrl("");
  196. }
  197. };
  198. Blockly.Python['robot_dog_phonetic_to_pinyin_get_result'] = function (block) {
  199. Blockly.Python.definitions_.robot_dog_phonetic_to_pinyin_start_recognition_def = "" +
  200. "def getRecoResult():\n" +
  201. " global asr\n" +
  202. " if str(firmware_info.ai()) == \"2020-12-26\" or str(firmware_info.ai()) == \"2021-04-16\" or str(firmware_info.ai()) == \"2021-05-20\" or str(firmware_info.ai()) == \"2021-06-11\" or str(firmware_info.ai()) == \"2021-07-09\" or str(firmware_info.ai()) == \"2021-08-04\":\n" +
  203. " return getRecognizeResult()\n" +
  204. " else:\n" +
  205. " return asr.getRecognizeResult()\n" +
  206. "\n";
  207. var code = 'getRecoResult()';
  208. return [code, Blockly.Python.ORDER_NONE];
  209. };
  210. Blockly.Blocks['robot_dog_phonetic_to_pinyin_action'] = {
  211. init: function () {
  212. this.appendDummyInput()
  213. .appendField(Blockly.Msg.robot_dog_execution)
  214. .appendField(new Blockly.FieldDropdown([
  215. [Blockly.Msg.robot_dog_movement_action_0, "51"],
  216. [Blockly.Msg.robot_dog_movement_action_1, "52"],
  217. [Blockly.Msg.robot_dog_movement_action_3, "54"],
  218. [Blockly.Msg.robot_dog_movement_action_5, "56"],
  219. [Blockly.Msg.robot_dog_movement_action_17, "68"],
  220. [Blockly.Msg.robot_dog_movement_action_18, "69"],
  221. ]), "type");
  222. this.appendDummyInput()
  223. .appendField(Blockly.Msg.robot_dog_action);
  224. this.setInputsInline(true);
  225. this.setPreviousStatement(true, null);
  226. this.setNextStatement(true, null);
  227. this.setColour("#5c42bd");
  228. this.setHelpUrl("");
  229. var thisBlock = this;
  230. this.setTooltip(function () {
  231. var mode = thisBlock.getFieldValue('type');
  232. var TOOLTIPS = {
  233. '51': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_0),
  234. '52': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_1),
  235. '54': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_3),
  236. '56': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_5),
  237. '68': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_17),
  238. '69': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_18)
  239. };
  240. return TOOLTIPS[mode];
  241. });
  242. }
  243. }
  244. Blockly.Python['robot_dog_phonetic_to_pinyin_action'] = function (block) {
  245. var dropdown_type = block.getFieldValue('type');
  246. var code = '' +
  247. 'ai_uart.write(bytes([' + dropdown_type + ']))\n' +
  248. 'time.sleep_ms(400)\n' +
  249. '';
  250. return code;
  251. }
  252. Blockly.Blocks['robot_dog_setup'] = {
  253. init: function () {
  254. this.appendDummyInput()
  255. .appendField(new Blockly.FieldImage("blockly/media/robot_dog.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
  256. this.appendDummyInput()
  257. .appendField(Blockly.Msg.robot_dog_setup);
  258. this.setInputsInline(false);
  259. this.setPreviousStatement(true, null);
  260. this.setNextStatement(true, null);
  261. this.setColour("#5c42bd");
  262. this.setTooltip(Blockly.Msg.robot_dog_setup_TOOLTIP);
  263. this.setHelpUrl("");
  264. }
  265. }
  266. Blockly.Python['robot_dog_setup'] = function (block) {
  267. Blockly.Python.definitions_['import_robot_dog_setup'] = 'import machine, time\n' +
  268. 'from fpioa_manager import fm\n' +
  269. 'try:\n' +
  270. // ' from cocorobo import firmware_info\n' +
  271. ' from xgo import XGO\n' +
  272. 'except BaseException as e:\n' +
  273. ' print(str(e))\n' +
  274. ' pass\n' +
  275. '\n';
  276. // Blockly.Python.addVariable('uart_data', 'uart_data = ""', true);
  277. Blockly.Python.definitions_['robot_dog_setup'] = '' +
  278. // 'fm.register(13,fm.fpioa.UART2_TX)\n' +
  279. // 'fm.register(14,fm.fpioa.UART2_RX)\n' +
  280. // 'dog = None\n' +
  281. // 'robot_dog_setup_uart = None\n' +
  282. // '\n' +
  283. // 'if (int(str(firmware_info.ai()).split(\"-\")[0]) == 2021 and int(str(firmware_info.ai()).split(\"-\")[1]) >= 5) or (int(str(firmware_info.ai()).split(\"-\")[0]) > 2021):\n' +
  284. // ' dog = XGO(machine.UART.UART2)\n' +
  285. // 'else:\n' +
  286. // ' robot_dog_setup_uart = machine.UART(machine.UART.UART2,115200,bits=8,parity=None,stop=1)\n' +
  287. // ' def add(a,b):\n' +
  288. // ' num1 = a ^ b\n' +
  289. // ' num2 = (a & b) << 1\n' +
  290. // ' while num2 != 0:\n' +
  291. // ' temp = num1 ^ num2\n' +
  292. // ' num2 = (num1 & num2) << 1\n' +
  293. // ' num1 = temp\n' +
  294. // ' return num1\n' +
  295. // '\n' +
  296. // ' def calculate_sum(a,b):\n' +
  297. // ' bytearr = [9, 1, a, b]\n' +
  298. // ' sum = 0\n' +
  299. // ' for i in bytearr:\n' +
  300. // ' sum = add(sum,i)\n' +
  301. // ' calculated_cksum = bin(sum).replace("0b","")\n' +
  302. // ' while len(calculated_cksum) < 8:\n' +
  303. // ' calculated_cksum = "0" + calculated_cksum\n' +
  304. // ' ReturningChecksum = ""\n' +
  305. // ' for index in range(len(calculated_cksum)):\n' +
  306. // ' if calculated_cksum[index] == "1":\n' +
  307. // ' ReturningChecksum += "0"\n' +
  308. // ' elif calculated_cksum[index] == "0":\n' +
  309. // ' ReturningChecksum += "1"\n' +
  310. // ' return bytes([int(hex(int(ReturningChecksum,2)),16)])\n' +
  311. // '\n' +
  312. // ' def mapping(input_value,i_min,i_max,o_min,o_max):\n' +
  313. // ' if input_value < i_min:\n' +
  314. // ' input_value = i_min\n' +
  315. // ' if input_value > i_max:\n' +
  316. // ' input_value = i_max\n' +
  317. // ' dat=(input_value-i_min)/(i_max-i_min)*(o_max-o_min)+o_min\n' +
  318. // ' return round(dat)\n' +
  319. // 'if (int(str(firmware_info.ai()).split(\"-\")[0]) == 2021 and int(str(firmware_info.ai()).split(\"-\")[1]) >= 5) or (int(str(firmware_info.ai()).split(\"-\")[0]) > 2021):\n' +
  320. 'fm.register(13,fm.fpioa.UART2_TX)\n' +
  321. 'fm.register(14,fm.fpioa.UART2_RX)\n' +
  322. 'dog = XGO(machine.UART.UART2)\n' +
  323. '';
  324. var code = '';
  325. return code;
  326. }
  327. Blockly.Blocks['robot_dog_action'] = {
  328. init: function () {
  329. this.appendDummyInput()
  330. .appendField(Blockly.Msg.robot_dog_execution)
  331. .appendField(new Blockly.FieldDropdown([
  332. [Blockly.Msg.robot_dog_movement_action_19, "0"],
  333. // [Blockly.Msg.robot_dog_movement_action_20, "1"],
  334. // [Blockly.Msg.robot_dog_movement_action_21, "2"],
  335. // [Blockly.Msg.robot_dog_movement_action_22, "3"],
  336. // [Blockly.Msg.robot_dog_movement_action_23, "4"],
  337. // [Blockly.Msg.robot_dog_movement_action_24, "12"],
  338. // [Blockly.Msg.robot_dog_movement_action_25, "13"],
  339. [Blockly.Msg.robot_dog_movement_action_0, "1"],
  340. [Blockly.Msg.robot_dog_movement_action_1, "2"],
  341. [Blockly.Msg.robot_dog_movement_action_2, "3"],
  342. [Blockly.Msg.robot_dog_movement_action_3, "4"],
  343. [Blockly.Msg.robot_dog_movement_action_4, "5"],
  344. [Blockly.Msg.robot_dog_movement_action_5, "6"],
  345. [Blockly.Msg.robot_dog_movement_action_6, "7"],
  346. [Blockly.Msg.robot_dog_movement_action_7, "8"],
  347. [Blockly.Msg.robot_dog_movement_action_8, "9"],
  348. [Blockly.Msg.robot_dog_movement_action_9, "10"],
  349. [Blockly.Msg.robot_dog_movement_action_10, "11"],
  350. [Blockly.Msg.robot_dog_movement_action_11, "12"],
  351. [Blockly.Msg.robot_dog_movement_action_12, "13"],
  352. [Blockly.Msg.robot_dog_movement_action_13, "14"],
  353. [Blockly.Msg.robot_dog_movement_action_14, "15"],
  354. [Blockly.Msg.robot_dog_movement_action_15, "16"],
  355. [Blockly.Msg.robot_dog_movement_action_16, "17"],
  356. [Blockly.Msg.robot_dog_movement_action_17, "18"],
  357. [Blockly.Msg.robot_dog_movement_action_18, "19"],
  358. [Blockly.Msg.robot_dog_movement_action_30, "20"]
  359. ]), "type");
  360. this.appendDummyInput()
  361. .appendField(Blockly.Msg.robot_dog_action);
  362. this.setInputsInline(true);
  363. this.setPreviousStatement(true, null);
  364. this.setNextStatement(true, null);
  365. this.setColour("#5c42bd");
  366. this.setHelpUrl("");
  367. var thisBlock = this;
  368. this.setTooltip(function () {
  369. var mode = thisBlock.getFieldValue('type');
  370. var TOOLTIPS = {
  371. '0': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_19),
  372. // '1': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_20),
  373. // '2': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_21),
  374. // '3': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_22),
  375. // '4': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_23),
  376. // '12': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_24),
  377. // '13': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_25),
  378. '1': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_0),
  379. '2': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_1),
  380. '3': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_2),
  381. '4': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_3),
  382. '5': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_4),
  383. '6': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_5),
  384. '7': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_6),
  385. '8': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_7),
  386. '9': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_8),
  387. '10': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_9),
  388. '11': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_10),
  389. '12': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_11),
  390. '13': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_12),
  391. '14': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_13),
  392. '15': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_14),
  393. '16': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_15),
  394. '17': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_16),
  395. '18': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_17),
  396. '19': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_18),
  397. '20': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_30)
  398. };
  399. return TOOLTIPS[mode];
  400. });
  401. }
  402. }
  403. Blockly.Python['robot_dog_action'] = function (block) {
  404. var dropdown_type = block.getFieldValue('type');
  405. var code = '';
  406. if (dropdown_type == "0") {
  407. code =
  408. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  409. 'dog.reset()\n' +
  410. '';
  411. }
  412. else {
  413. code =
  414. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  415. 'dog.action(' + dropdown_type + ')\n' +
  416. '';
  417. }
  418. return code;
  419. }
  420. Blockly.Blocks['robot_dog_servo_movement'] = {
  421. init: function () {
  422. this.appendDummyInput()
  423. .appendField(Blockly.Msg.robot_dog_servo_execution)
  424. .appendField(new Blockly.FieldDropdown([
  425. [Blockly.Msg.robot_dog_front_left_leg, "Front left"],
  426. [Blockly.Msg.robot_dog_back_left_leg, "Back left"],
  427. [Blockly.Msg.robot_dog_front_right_leg, "Right front"],
  428. [Blockly.Msg.robot_dog_back_right_leg, "Right back"]
  429. ]), "leg");
  430. this.appendDummyInput()
  431. .appendField(Blockly.Msg.robot_dog_leg)
  432. .appendField(new Blockly.FieldDropdown([
  433. [Blockly.Msg.robot_dog_up, "up"],
  434. [Blockly.Msg.robot_dog_middle, "middle"],
  435. [Blockly.Msg.robot_dog_down, "down"]
  436. ]), "joint");
  437. this.appendValueInput("degree")
  438. .setCheck(null)
  439. .appendField(Blockly.Msg.robot_dog_servo_rotate);
  440. this.appendDummyInput()
  441. .appendField(Blockly.Msg.robot_dog_ag);
  442. this.setInputsInline(true);
  443. this.setPreviousStatement(true, null);
  444. this.setNextStatement(true, null);
  445. this.setColour("#5c42bd");
  446. this.setTooltip(Blockly.Msg.robot_dog_servo_movement_TOOLTIP);
  447. this.setHelpUrl("");
  448. },
  449. onchange: function (event) {
  450. var joint = this.getFieldValue('joint');
  451. if (event.blockId == this.id && event.name == 'joint' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
  452. if (joint == "up") {
  453. this.getChildren()[0].getField("NUM").setValue("0");
  454. }
  455. else if (joint == "middle") {
  456. this.getChildren()[0].getField("NUM").setValue("42.5");
  457. }
  458. else {
  459. this.getChildren()[0].getField("NUM").setValue("20");
  460. }
  461. }
  462. }
  463. }
  464. Blockly.Python['robot_dog_servo_movement'] = function (block) {
  465. var leg = block.getFieldValue('leg');
  466. var joint = block.getFieldValue('joint');
  467. var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
  468. var code = '';
  469. var leg_sign = "";
  470. if (leg == "Front left") {
  471. if (joint == "up") {
  472. leg_sign = "b\'\\x52\'"
  473. code =
  474. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  475. 'dog.motor(13,' + value_degree + ')\n' +
  476. // 'else:\n' +
  477. // ' degree = mapping(' + value_degree + ',-31,31,0,255)\n' +
  478. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  479. '';
  480. }
  481. else if (joint == "middle") {
  482. leg_sign = "b\'\\x51\'"
  483. code =
  484. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  485. 'dog.motor(12,' + value_degree + ')\n' +
  486. // 'else:\n' +
  487. // ' degree = mapping(' + value_degree + ',-66,93,0,255)\n' +
  488. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  489. '';
  490. }
  491. else {
  492. leg_sign = "b\'\\x50\'"
  493. code =
  494. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  495. 'dog.motor(11,' + value_degree + ')\n' +
  496. // 'else:\n' +
  497. // ' degree = mapping(' + value_degree + ',-65,73,0,255)\n' +
  498. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  499. '';
  500. }
  501. }
  502. else if (leg == "Back left") {
  503. if (joint == "up") {
  504. leg_sign = "b\'\\x5B\'"
  505. code =
  506. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  507. 'dog.motor(43,' + value_degree + ')\n' +
  508. // 'else:\n' +
  509. // ' degree = mapping(' + value_degree + ',-31,31,0,255)\n' +
  510. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  511. '';
  512. }
  513. else if (joint == "middle") {
  514. leg_sign = "b\'\\x5A\'"
  515. code =
  516. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  517. 'dog.motor(42,' + value_degree + ')\n' +
  518. // 'else:\n' +
  519. // ' degree = mapping(' + value_degree + ',-66,93,0,255)\n' +
  520. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  521. '';
  522. }
  523. else {
  524. leg_sign = "b\'\\x59\'"
  525. code =
  526. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  527. 'dog.motor(41,' + value_degree + ')\n' +
  528. // 'else:\n' +
  529. // ' degree = mapping(' + value_degree + ',-65,73,0,255)\n' +
  530. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  531. '';
  532. }
  533. }
  534. else if (leg == "Right front") {
  535. if (joint == "up") {
  536. leg_sign = "b\'\\x55\'"
  537. code =
  538. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  539. 'dog.motor(23,' + value_degree + ')\n' +
  540. // 'else:\n' +
  541. // ' degree = mapping(' + value_degree + ',-31,31,0,255)\n' +
  542. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  543. '';
  544. }
  545. else if (joint == "middle") {
  546. leg_sign = "b\'\\x54\'"
  547. code =
  548. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  549. 'dog.motor(22,' + value_degree + ')\n' +
  550. // 'else:\n' +
  551. // ' degree = mapping(' + value_degree + ',-66,93,0,255)\n' +
  552. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  553. '';
  554. }
  555. else {
  556. leg_sign = "b\'\\x53\'"
  557. code =
  558. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  559. 'dog.motor(21,' + value_degree + ')\n' +
  560. // 'else:\n' +
  561. // ' degree = mapping(' + value_degree + ',-65,73,0,255)\n' +
  562. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  563. '';
  564. }
  565. }
  566. else {
  567. if (joint == "up") {
  568. leg_sign = "b\'\\x58\'"
  569. code =
  570. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  571. 'dog.motor(33,' + value_degree + ')\n' +
  572. // 'else:\n' +
  573. // ' degree = mapping(' + value_degree + ',-31,31,0,255)\n' +
  574. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  575. '';
  576. }
  577. else if (joint == "middle") {
  578. leg_sign = "b\'\\x57\'"
  579. code =
  580. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  581. 'dog.motor(32,' + value_degree + ')\n' +
  582. // 'else:\n' +
  583. // ' degree = mapping(' + value_degree + ',-66,93,0,255)\n' +
  584. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  585. '';
  586. }
  587. else {
  588. leg_sign = "b\'\\x56\'"
  589. code =
  590. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  591. 'dog.motor(31,' + value_degree + ')\n' +
  592. // 'else:\n' +
  593. // ' degree = mapping(' + value_degree + ',-65,73,0,255)\n' +
  594. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
  595. '';
  596. }
  597. }
  598. return code;
  599. }
  600. Blockly.Blocks['robot_dog_servo_speed'] = {
  601. init: function () {
  602. this.appendDummyInput()
  603. .appendField(Blockly.Msg.robot_dog_servo_set_speed)
  604. .appendField(new Blockly.FieldDropdown([
  605. [Blockly.Msg.robot_dog_servo_speed, "servo"],
  606. [Blockly.Msg.robot_dog_movement_speed, "movement"]
  607. ]), "servoOrMovement")
  608. .appendField(Blockly.Msg.robot_dog_servo_to)
  609. .appendField(new Blockly.FieldDropdown([
  610. [Blockly.Msg.robot_dog_servo_speed_1, "fast"],
  611. [Blockly.Msg.robot_dog_servo_speed_2, "normal"],
  612. [Blockly.Msg.robot_dog_servo_speed_3, "slow"]
  613. ]), "speed");
  614. this.setInputsInline(true);
  615. this.setPreviousStatement(true, null);
  616. this.setNextStatement(true, null);
  617. this.setColour("#5c42bd");
  618. this.setHelpUrl("");
  619. var thisBlock = this;
  620. this.setTooltip(function () {
  621. var mode = thisBlock.getFieldValue('speed');
  622. var TOOLTIPS = {
  623. 'fast': Blockly.Msg.robot_dog_servo_speed_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_servo_speed_1),
  624. 'normal': Blockly.Msg.robot_dog_servo_speed_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_servo_speed_2),
  625. 'slow': Blockly.Msg.robot_dog_servo_speed_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_servo_speed_3),
  626. };
  627. return TOOLTIPS[mode];
  628. });
  629. }
  630. }
  631. Blockly.Python['robot_dog_servo_speed'] = function (block) {
  632. var speed = block.getFieldValue('speed');
  633. var servoOrMovement = block.getFieldValue('servoOrMovement');
  634. var code = '';
  635. if (servoOrMovement == "servo") {
  636. if (speed == "fast") {
  637. code =
  638. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  639. 'dog.motor_speed(255)\n' +
  640. // 'else:\n' +
  641. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x5c\\x00\\x99\\x00\\xaa\')\n' +
  642. '';
  643. }
  644. else if (speed == "normal") {
  645. code =
  646. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  647. 'dog.motor_speed(128)\n' +
  648. // 'else:\n' +
  649. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x5c\\x80\\x19\\x00\\xaa\')\n' +
  650. '';
  651. }
  652. else {
  653. code =
  654. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  655. 'dog.motor_speed(1)\n' +
  656. // 'else:\n' +
  657. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x5c\\xff\\x9a\\x00\\xaa\')\n' +
  658. '';
  659. }
  660. }
  661. else if (servoOrMovement == "movement") {
  662. if (speed == "fast") {
  663. code =
  664. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  665. 'dog.pace(\'high\')\n' +
  666. // 'else:\n' +
  667. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3d\\x02\\xb6\\x00\\xaa\')\n' +
  668. '';
  669. }
  670. else if (speed == "normal") {
  671. code =
  672. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  673. 'dog.pace(\'normal\')\n' +
  674. // 'else:\n' +
  675. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3d\\x00\\xb8\\x00\\xaa\')\n' +
  676. '';
  677. }
  678. else {
  679. code =
  680. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  681. 'dog.pace(\'slow\')\n' +
  682. // 'else:\n' +
  683. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3d\\x01\\xb7\\x00\\xaa\')\n' +
  684. '';
  685. }
  686. }
  687. return code;
  688. }
  689. Blockly.Blocks['robot_dog_servo_movement_location'] = {
  690. init: function () {
  691. this.appendDummyInput()
  692. .appendField(Blockly.Msg.robot_dog_servo_execution)
  693. .appendField(new Blockly.FieldDropdown([
  694. [Blockly.Msg.robot_dog_front_left_leg, "Front left"],
  695. [Blockly.Msg.robot_dog_back_left_leg, "Back left"],
  696. [Blockly.Msg.robot_dog_front_right_leg, "Right front"],
  697. [Blockly.Msg.robot_dog_back_right_leg, "Right back"]
  698. ]), "leg")
  699. .appendField(Blockly.Msg.robot_dog_leg_f);
  700. this.appendValueInput("distance_x")
  701. .setCheck(null)
  702. .appendField("X (-35mm~35mm)")
  703. .appendField(Blockly.Msg.robot_dog_servo_movement_location)
  704. // .appendField(Blockly.Msg.robot_dog_angle);
  705. this.appendValueInput("distance_y")
  706. .setCheck(null)
  707. .appendField("Y (-18mm~18mm)")
  708. .appendField(Blockly.Msg.robot_dog_servo_movement_location)
  709. // .appendField(Blockly.Msg.robot_dog_angle);
  710. this.appendValueInput("distance_z")
  711. .setCheck(null)
  712. .appendField("Z (75mm~115mm)")
  713. .appendField(Blockly.Msg.robot_dog_servo_movement_location)
  714. // .appendField(Blockly.Msg.robot_dog_angle);
  715. this.setInputsInline(false);
  716. this.setPreviousStatement(true, null);
  717. this.setNextStatement(true, null);
  718. this.setColour("#5c42bd");
  719. this.setTooltip(Blockly.Msg.robot_dog_servo_movement_location_TOOLTIP);
  720. this.setHelpUrl("");
  721. }
  722. }
  723. Blockly.Python['robot_dog_servo_movement_location'] = function (block) {
  724. var leg = block.getFieldValue('leg');
  725. // var direction = block.getFieldValue('direction');
  726. var distance_x = Blockly.Python.valueToCode(block, 'distance_x', Blockly.Python.ORDER_ATOMIC);
  727. var distance_y = Blockly.Python.valueToCode(block, 'distance_y', Blockly.Python.ORDER_ATOMIC);
  728. var distance_z = Blockly.Python.valueToCode(block, 'distance_z', Blockly.Python.ORDER_ATOMIC);
  729. var code = '';
  730. // var leg_sign = "";
  731. if (leg == "Front left") {
  732. code = 'dog.leg(1,[' + distance_x + ',' + distance_y + ',' + distance_z + '])\n';
  733. // if (direction == "X") {
  734. // leg_sign = "b\'\\x40\'"
  735. // code =
  736. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  737. // 'dog.leg(1,[' + distance + ', 0, 105])\n' +
  738. // // 'else:\n' +
  739. // // ' distance = mapping(' + distance + ',-35,35,0,255)\n' +
  740. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  741. // '';
  742. // }
  743. // else if (direction == "Y") {
  744. // leg_sign = "b\'\\x41\'"
  745. // code =
  746. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  747. // 'dog.leg(1,[0, ' + distance + ', 105])\n' +
  748. // // 'else:\n' +
  749. // // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
  750. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  751. // '';
  752. // }
  753. // else {
  754. // leg_sign = "b\'\\x42\'"
  755. // code =
  756. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  757. // 'dog.leg(1,[0, 0, ' + distance + '])\n' +
  758. // // 'else:\n' +
  759. // // ' distance = mapping(' + distance + ',75,115,0,255)\n' +
  760. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  761. // '';
  762. // }
  763. }
  764. else if (leg == "Back left") {
  765. code = 'dog.leg(4,[' + distance_x + ',' + distance_y + ',' + distance_z + '])\n';
  766. // if (direction == "X") {
  767. // leg_sign = "b\'\\x49\'"
  768. // code =
  769. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  770. // 'dog.leg(4,[' + distance + ', 0, 105])\n' +
  771. // // 'else:\n' +
  772. // // ' distance = mapping(' + distance + ',-35,35,0,255)\n' +
  773. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  774. // '';
  775. // }
  776. // else if (direction == "Y") {
  777. // leg_sign = "b\'\\x4a\'"
  778. // code =
  779. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  780. // 'dog.leg(4,[0, ' + distance + ', 105])\n' +
  781. // // 'else:\n' +
  782. // // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
  783. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  784. // '';
  785. // }
  786. // else {
  787. // leg_sign = "b\'\\x4b\'"
  788. // code =
  789. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  790. // 'dog.leg(4,[0, 0, ' + distance + '])\n' +
  791. // // 'else:\n' +
  792. // // ' distance = mapping(' + distance + ',75,115,0,255)\n' +
  793. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  794. // '';
  795. // }
  796. }
  797. else if (leg == "Right front") {
  798. code = 'dog.leg(2,[' + distance_x + ',' + distance_y + ',' + distance_z + '])\n';
  799. // if (direction == "X") {
  800. // leg_sign = "b\'\\x43\'"
  801. // code =
  802. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  803. // 'dog.leg(2,[' + distance + ', 0, 105])\n' +
  804. // // 'else:\n' +
  805. // // ' distance = mapping(' + distance + ',-35,35,0,255)\n' +
  806. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  807. // '';
  808. // }
  809. // else if (direction == "Y") {
  810. // leg_sign = "b\'\\x44\'"
  811. // code =
  812. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  813. // 'dog.leg(2,[0, ' + distance + ', 105])\n' +
  814. // // 'else:\n' +
  815. // // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
  816. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  817. // '';
  818. // }
  819. // else {
  820. // leg_sign = "b\'\\x45\'"
  821. // code =
  822. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  823. // 'dog.leg(2,[0, 0, ' + distance + '])\n' +
  824. // // 'else:\n' +
  825. // // ' distance = mapping(' + distance + ',75,115,0,255)\n' +
  826. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  827. // '';
  828. // }
  829. }
  830. else {
  831. code = 'dog.leg(3,[' + distance_x + ',' + distance_y + ',' + distance_z + '])\n';
  832. // if (direction == "X") {
  833. // leg_sign = "b\'\\x46\'"
  834. // code =
  835. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  836. // 'dog.leg(3,[' + distance + ', 0, 105])\n' +
  837. // // 'else:\n' +
  838. // // ' distance = mapping(' + distance + ',-35,35,0,255)\n' +
  839. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  840. // '';
  841. // }
  842. // else if (direction == "Y") {
  843. // leg_sign = "b\'\\x47\'"
  844. // code =
  845. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  846. // 'dog.leg(3,[0, ' + distance + ', 105])\n' +
  847. // // 'else:\n' +
  848. // // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
  849. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  850. // '';
  851. // }
  852. // else {
  853. // leg_sign = "b\'\\x48\'"
  854. // code =
  855. // // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  856. // 'dog.leg(3,[0, 0, ' + distance + '])\n' +
  857. // // 'else:\n' +
  858. // // ' distance = mapping(' + distance + ',75,115,0,255)\n' +
  859. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
  860. // '';
  861. // }
  862. }
  863. return code;
  864. }
  865. Blockly.Blocks['robot_dog_open_gyroscope'] = {
  866. init: function () {
  867. this.appendDummyInput()
  868. .appendField(new Blockly.FieldDropdown([
  869. [Blockly.Msg.robot_dog_controll_action_1, "open"],
  870. [Blockly.Msg.robot_dog_controll_action_0, "close"]
  871. ]), "action")
  872. .appendField(Blockly.Msg.robot_dog_gyroscope);
  873. this.setInputsInline(true);
  874. this.setPreviousStatement(true, null);
  875. this.setNextStatement(true, null);
  876. this.setColour("#5c42bd");
  877. this.setHelpUrl("");
  878. var thisBlock = this;
  879. this.setTooltip(function () {
  880. var mode = thisBlock.getFieldValue('action');
  881. var TOOLTIPS = {
  882. 'open': Blockly.Msg.robot_dog_open_gyroscope_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_1),
  883. 'close': Blockly.Msg.robot_dog_open_gyroscope_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_0)
  884. };
  885. return TOOLTIPS[mode];
  886. });
  887. }
  888. }
  889. Blockly.Python['robot_dog_open_gyroscope'] = function (block) {
  890. var action = block.getFieldValue('action');
  891. var code = '';
  892. if (action == "open") {
  893. code =
  894. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  895. 'dog.imu(1)\n' +
  896. // 'else:\n' +
  897. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x61\\x01\\x93\\x00\\xaa\')\n' +
  898. '';
  899. }
  900. else {
  901. code =
  902. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  903. 'dog.imu(0)\n' +
  904. // 'else:\n' +
  905. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x61\\x00\\x94\\x00\\xaa\')\n' +
  906. '';
  907. }
  908. return code;
  909. }
  910. Blockly.Blocks['robot_dog_open_performance_mode'] = {
  911. init: function () {
  912. this.appendDummyInput()
  913. .appendField(new Blockly.FieldDropdown([
  914. [Blockly.Msg.robot_dog_controll_action_1, "open"],
  915. [Blockly.Msg.robot_dog_controll_action_0, "close"]
  916. ]), "action")
  917. .appendField(Blockly.Msg.robot_dog_open_performance_mode);
  918. this.setInputsInline(true);
  919. this.setPreviousStatement(true, null);
  920. this.setNextStatement(true, null);
  921. this.setColour("#5c42bd");
  922. this.setHelpUrl("");
  923. var thisBlock = this;
  924. this.setTooltip(function () {
  925. var mode = thisBlock.getFieldValue('action');
  926. var TOOLTIPS = {
  927. 'open': Blockly.Msg.robot_dog_open_performance_mode_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_1),
  928. 'close': Blockly.Msg.robot_dog_open_performance_mode_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_0)
  929. };
  930. return TOOLTIPS[mode];
  931. });
  932. }
  933. }
  934. Blockly.Python['robot_dog_open_performance_mode'] = function (block) {
  935. var action = block.getFieldValue('action');
  936. var code = '';
  937. if (action == "open") {
  938. code =
  939. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  940. 'dog.perform(1)\n' +
  941. // 'else:\n' +
  942. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x03\\x01\\xf1\\x00\\xaa\')\n' +
  943. '';
  944. }
  945. else {
  946. code =
  947. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  948. 'dog.perform(0)\n' +
  949. // 'else:\n' +
  950. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x03\\x00\\xf2\\x00\\xaa\')\n' +
  951. '';
  952. }
  953. return code;
  954. }
  955. Blockly.Blocks['robot_dog_open_calibration_mode'] = {
  956. init: function () {
  957. this.appendDummyInput()
  958. .appendField(new Blockly.FieldDropdown([
  959. [Blockly.Msg.robot_dog_controll_action_1, "open"],
  960. [Blockly.Msg.robot_dog_controll_action_0, "close"]
  961. ]), "action")
  962. .appendField(Blockly.Msg.robot_dog_open_calibration_mode);
  963. this.setInputsInline(true);
  964. this.setPreviousStatement(true, null);
  965. this.setNextStatement(true, null);
  966. this.setColour("#5c42bd");
  967. this.setHelpUrl("");
  968. var thisBlock = this;
  969. this.setTooltip(function () {
  970. var mode = thisBlock.getFieldValue('action');
  971. var TOOLTIPS = {
  972. 'open': Blockly.Msg.robot_dog_open_calibration_mode_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_1),
  973. 'close': Blockly.Msg.robot_dog_open_calibration_mode_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_0)
  974. };
  975. return TOOLTIPS[mode];
  976. });
  977. }
  978. }
  979. Blockly.Python['robot_dog_open_calibration_mode'] = function (block) {
  980. var action = block.getFieldValue('action');
  981. var code = '';
  982. if (action == "open") {
  983. code = 'robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x04\\x01\\xf0\\x00\\xaa\')\n' +
  984. '';
  985. }
  986. else {
  987. code = 'robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x04\\x00\\xf1\\x00\\xaa\')\n' +
  988. '';
  989. }
  990. return code;
  991. }
  992. Blockly.Blocks['robot_dog_get_power'] = {
  993. init: function () {
  994. this.appendDummyInput()
  995. .appendField(Blockly.Msg.robot_dog_get_power);
  996. this.setOutput(true, null);
  997. this.setInputsInline(true);
  998. this.setColour("#5c42bd");
  999. this.setHelpUrl("");
  1000. this.setTooltip(Blockly.Msg.robot_dog_get_power_TOOLTIP);
  1001. }
  1002. }
  1003. Blockly.Python['robot_dog_get_power'] = function (block) {
  1004. Blockly.Python.definitions_['import_robot_dog_get_power'] = '' +
  1005. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1006. 'def robot_dog_get_power():\n' +
  1007. ' global dog\n' +
  1008. ' return dog.read_battery()\n' +
  1009. // 'else:\n' +
  1010. // ' def _read_serial_data():\n' +
  1011. // ' global robot_dog_setup_uart\n' +
  1012. // ' if robot_dog_setup_uart.any():\n' +
  1013. // ' read_data = robot_dog_setup_uart.read()\n' +
  1014. // ' return read_data\n' +
  1015. // '\n' +
  1016. // ' def robot_dog_get_power():\n' +
  1017. // // ' global robot_dog_setup_uart\n' +
  1018. // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x02\\x01\\x01\\xF2\\x00\\xAA\')\n' +
  1019. // // ' time.sleep_ms(10)\n' +
  1020. // // ' a = _read_serial_data()\n' +
  1021. // ' return None\n' +
  1022. '\n';
  1023. var code = 'robot_dog_get_power()';
  1024. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1025. };
  1026. Blockly.Blocks['robot_dog_servo_movement_distance'] = {
  1027. init: function () {
  1028. this.appendDummyInput()
  1029. .appendField(Blockly.Msg.robot_dog_servo_movement_distance)
  1030. .appendField(new Blockly.FieldDropdown([
  1031. [Blockly.Msg.robot_dog_movement_action_28, "0"],
  1032. [Blockly.Msg.robot_dog_movement_action_29, "1"],
  1033. [Blockly.Msg.robot_dog_movement_action_26, "2"],
  1034. [Blockly.Msg.robot_dog_movement_action_27, "3"]
  1035. ]), "direction");
  1036. this.appendValueInput("distance")
  1037. .setCheck(null)
  1038. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_location);
  1039. this.appendDummyInput()
  1040. .appendField(Blockly.Msg.robot_dog_angle);
  1041. this.setInputsInline(true);
  1042. this.setPreviousStatement(true, null);
  1043. this.setNextStatement(true, null);
  1044. this.setColour("#5c42bd");
  1045. this.setHelpUrl("");
  1046. var thisBlock = this;
  1047. this.setTooltip(function () {
  1048. var mode = thisBlock.getFieldValue('direction');
  1049. var TOOLTIPS = {
  1050. '0': Blockly.Msg.robot_dog_servo_movement_distance_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_28),
  1051. '1': Blockly.Msg.robot_dog_servo_movement_distance_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_29),
  1052. '2': Blockly.Msg.robot_dog_servo_movement_distance_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_26),
  1053. '3': Blockly.Msg.robot_dog_servo_movement_distance_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_27)
  1054. };
  1055. return TOOLTIPS[mode];
  1056. });
  1057. },
  1058. onchange: function (event) {
  1059. var direction = this.getFieldValue('direction');
  1060. if (event.blockId == this.id && event.name == 'direction' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
  1061. if (direction == "0") {
  1062. this.getChildren()[0].getField("NUM").setValue("10");
  1063. }
  1064. else if (direction == "1") {
  1065. this.getChildren()[0].getField("NUM").setValue("-8");
  1066. }
  1067. else if (direction == "2") {
  1068. this.getChildren()[0].getField("NUM").setValue("6");
  1069. }
  1070. else {
  1071. this.getChildren()[0].getField("NUM").setValue("-6");
  1072. }
  1073. }
  1074. }
  1075. }
  1076. Blockly.Python['robot_dog_servo_movement_distance'] = function (block) {
  1077. var direction = block.getFieldValue('direction');
  1078. var distance = Blockly.Python.valueToCode(block, 'distance', Blockly.Python.ORDER_ATOMIC);
  1079. var code = '';
  1080. if (direction == "0" || direction == "1") {
  1081. code =
  1082. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1083. 'dog.move_x(' + distance + ')\n' +
  1084. // 'else:\n' +
  1085. // ' distance = mapping(' + distance + ',-25,25,0,255)\n' +
  1086. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x30\' + bytes([distance]) + calculate_sum(ord(b\'\\x30\'),distance) + b\'\\x00\\xaa\')\n' +
  1087. '';
  1088. }
  1089. else {
  1090. code =
  1091. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1092. 'dog.move_y(' + distance + ')\n' +
  1093. // 'else:\n' +
  1094. // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
  1095. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x31\' + bytes([distance]) + calculate_sum(ord(b\'\\x31\'),distance) + b\'\\x00\\xaa\')\n' +
  1096. ''
  1097. }
  1098. return code;
  1099. }
  1100. Blockly.Blocks['robot_dog_servo_movement_rotate'] = {
  1101. init: function () {
  1102. this.appendDummyInput()
  1103. .appendField(Blockly.Msg.robot_dog_servo_movement_rotate)
  1104. .appendField(new Blockly.FieldDropdown([
  1105. [Blockly.Msg.MOTION_ONDIRECTION_CLOCKWISE1, "0"],
  1106. [Blockly.Msg.MOTION_ONDIRECTION_COUNTERCLOCKWISE1, "1"]
  1107. ]), "direction");
  1108. this.appendValueInput("rotate_speed")
  1109. .setCheck(null)
  1110. .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_location);
  1111. this.appendDummyInput()
  1112. .appendField(Blockly.Msg.robot_dog_angle_speed);
  1113. this.setInputsInline(true);
  1114. this.setPreviousStatement(true, null);
  1115. this.setNextStatement(true, null);
  1116. this.setColour("#5c42bd");
  1117. this.setHelpUrl("");
  1118. var thisBlock = this;
  1119. this.setTooltip(function () {
  1120. var mode = thisBlock.getFieldValue('direction');
  1121. var TOOLTIPS = {
  1122. '0': Blockly.Msg.robot_dog_servo_movement_rotate_TOOLTIP.replace('%1', Blockly.Msg.MOTION_ONDIRECTION_CLOCKWISE1),
  1123. '1': Blockly.Msg.robot_dog_servo_movement_rotate_TOOLTIP.replace('%1', Blockly.Msg.MOTION_ONDIRECTION_COUNTERCLOCKWISE1)
  1124. };
  1125. return TOOLTIPS[mode];
  1126. });
  1127. },
  1128. onchange: function (event) {
  1129. var direction = this.getFieldValue('direction');
  1130. if (event.blockId == this.id && event.name == 'direction' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
  1131. if (direction == "0") {
  1132. this.getChildren()[0].getField("NUM").setValue("60");
  1133. }
  1134. else {
  1135. this.getChildren()[0].getField("NUM").setValue("-60");
  1136. }
  1137. }
  1138. }
  1139. }
  1140. Blockly.Python['robot_dog_servo_movement_rotate'] = function (block) {
  1141. var rotate_speed = Blockly.Python.valueToCode(block, 'rotate_speed', Blockly.Python.ORDER_ATOMIC);
  1142. var code = '';
  1143. code =
  1144. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1145. 'dog.turn(' + rotate_speed + ')\n' +
  1146. // 'else:\n' +
  1147. // ' rotate_speed = mapping(' + rotate_speed + ',-150,150,0,255)\n' +
  1148. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x32\' + bytes([rotate_speed]) + calculate_sum(ord(b\'\\x32\'),rotate_speed) + b\'\\x00\\xaa\')\n' +
  1149. '';
  1150. return code;
  1151. }
  1152. Blockly.Blocks['robot_dog_servo_movement_standing_still'] = {
  1153. init: function () {
  1154. this.appendValueInput("leg_lift_height")
  1155. .setCheck(null)
  1156. .appendField(Blockly.Msg.robot_dog_servo_movement_standing_still);
  1157. this.appendDummyInput()
  1158. .appendField(Blockly.Msg.robot_dog_angle)
  1159. .appendField(Blockly.Msg.robot_dog_servo_movement_standing_still_1);
  1160. this.setInputsInline(true);
  1161. this.setPreviousStatement(true, null);
  1162. this.setNextStatement(true, null);
  1163. this.setColour("#5c42bd");
  1164. this.setHelpUrl("");
  1165. this.setTooltip(Blockly.Msg.robot_dog_servo_movement_standing_still_TOOLTIP);
  1166. }
  1167. }
  1168. Blockly.Python['robot_dog_servo_movement_standing_still'] = function (block) {
  1169. var leg_lift_height = Blockly.Python.valueToCode(block, 'leg_lift_height', Blockly.Python.ORDER_ATOMIC);
  1170. var code = '';
  1171. code =
  1172. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1173. 'dog.mark_time(' + leg_lift_height + ')\n' +
  1174. // 'else:\n' +
  1175. // ' leg_lift_height = mapping(' + leg_lift_height + ',10,35,1,255)\n' +
  1176. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3c\' + bytes([leg_lift_height]) + calculate_sum(ord(b\'\\x3c\'),leg_lift_height) + b\'\\x00\\xaa\')\n' +
  1177. '';
  1178. return code;
  1179. }
  1180. Blockly.Blocks['robot_dog_servo_movement_distance_cycle'] = {
  1181. init: function () {
  1182. this.appendDummyInput()
  1183. .appendField(Blockly.Msg.robot_dog_servo_movement_distance)
  1184. .appendField(new Blockly.FieldDropdown([
  1185. [Blockly.Msg.robot_dog_movement_action_28, "0"],
  1186. [Blockly.Msg.robot_dog_movement_action_29, "1"],
  1187. [Blockly.Msg.robot_dog_movement_action_26, "2"],
  1188. [Blockly.Msg.robot_dog_movement_action_27, "3"]
  1189. ]), "direction");
  1190. this.appendValueInput("distance")
  1191. .setCheck(null)
  1192. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_location);
  1193. this.appendDummyInput()
  1194. .appendField(Blockly.Msg.robot_dog_angle);
  1195. this.appendValueInput("second")
  1196. .setCheck(null)
  1197. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_1);
  1198. this.appendDummyInput()
  1199. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_2);
  1200. this.setInputsInline(true);
  1201. this.setPreviousStatement(true, null);
  1202. this.setNextStatement(true, null);
  1203. this.setColour("#5c42bd");
  1204. this.setHelpUrl("");
  1205. var thisBlock = this;
  1206. this.setTooltip(function () {
  1207. var mode = thisBlock.getFieldValue('direction');
  1208. var TOOLTIPS = {
  1209. '0': Blockly.Msg.robot_dog_servo_movement_distance_cycle_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_28),
  1210. '1': Blockly.Msg.robot_dog_servo_movement_distance_cycle_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_29),
  1211. '2': Blockly.Msg.robot_dog_servo_movement_distance_cycle_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_26),
  1212. '3': Blockly.Msg.robot_dog_servo_movement_distance_cycle_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_27)
  1213. };
  1214. return TOOLTIPS[mode];
  1215. });
  1216. },
  1217. onchange: function (event) {
  1218. var direction = this.getFieldValue('direction');
  1219. if (event.blockId == this.id && event.name == 'direction' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
  1220. if (direction == "0") {
  1221. this.getChildren()[0].getField("NUM").setValue("10");
  1222. }
  1223. else if (direction == "1") {
  1224. this.getChildren()[0].getField("NUM").setValue("-8");
  1225. }
  1226. else if (direction == "2") {
  1227. this.getChildren()[0].getField("NUM").setValue("6");
  1228. }
  1229. else {
  1230. this.getChildren()[0].getField("NUM").setValue("-6");
  1231. }
  1232. }
  1233. }
  1234. }
  1235. Blockly.Python['robot_dog_servo_movement_distance_cycle'] = function (block) {
  1236. var direction = block.getFieldValue('direction');
  1237. var distance = Blockly.Python.valueToCode(block, 'distance', Blockly.Python.ORDER_ATOMIC);
  1238. var second = Blockly.Python.valueToCode(block, 'second', Blockly.Python.ORDER_ATOMIC);
  1239. var code = '';
  1240. if (direction == "0" || direction == "1") {
  1241. code =
  1242. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1243. 'dog.move_x(' + distance + ')\n' +
  1244. 'time.sleep(' + second + ')\n' +
  1245. 'dog.move_x(0)\n' +
  1246. // 'else:\n' +
  1247. // ' distance = mapping(' + distance + ',-25,25,0,255)\n' +
  1248. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x30\' + bytes([distance]) + calculate_sum(ord(b\'\\x30\'),distance) + b\'\\x00\\xaa\')\n' +
  1249. // ' time.sleep(' + second + ')\n' +
  1250. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x30\\x80\\x45\\x00\\xaa\')\n' +
  1251. '';
  1252. }
  1253. else {
  1254. code =
  1255. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1256. 'dog.move_y(' + distance + ')\n' +
  1257. 'time.sleep(' + second + ')\n' +
  1258. 'dog.move_y(0)\n' +
  1259. // 'else:\n' +
  1260. // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
  1261. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x31\' + bytes([distance]) + calculate_sum(ord(b\'\\x31\'),distance) + b\'\\x00\\xaa\')\n' +
  1262. // ' time.sleep(' + second + ')\n' +
  1263. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x31\\x80\\x44\\x00\\xaa\')\n' +
  1264. '';
  1265. }
  1266. return code;
  1267. }
  1268. Blockly.Blocks['robot_dog_servo_movement_rotate_cycle'] = {
  1269. init: function () {
  1270. this.appendDummyInput()
  1271. .appendField(Blockly.Msg.robot_dog_servo_movement_rotate)
  1272. .appendField(new Blockly.FieldDropdown([
  1273. [Blockly.Msg.MOTION_ONDIRECTION_CLOCKWISE1, "0"],
  1274. [Blockly.Msg.MOTION_ONDIRECTION_COUNTERCLOCKWISE1, "1"]
  1275. ]), "direction");
  1276. this.appendValueInput("rotate_speed")
  1277. .setCheck(null)
  1278. .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_location);
  1279. this.appendDummyInput()
  1280. .appendField(Blockly.Msg.robot_dog_angle_speed);
  1281. this.appendValueInput("second")
  1282. .setCheck(null)
  1283. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_1);
  1284. this.appendDummyInput()
  1285. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_2);
  1286. this.setInputsInline(true);
  1287. this.setPreviousStatement(true, null);
  1288. this.setNextStatement(true, null);
  1289. this.setColour("#5c42bd");
  1290. this.setHelpUrl("");
  1291. var thisBlock = this;
  1292. this.setTooltip(function () {
  1293. var mode = thisBlock.getFieldValue('direction');
  1294. var TOOLTIPS = {
  1295. '0': Blockly.Msg.robot_dog_servo_movement_rotate_cycle_TOOLTIP.replace('%1', Blockly.Msg.MOTION_ONDIRECTION_CLOCKWISE1),
  1296. '1': Blockly.Msg.robot_dog_servo_movement_rotate_cycle_TOOLTIP.replace('%1', Blockly.Msg.MOTION_ONDIRECTION_COUNTERCLOCKWISE1)
  1297. };
  1298. return TOOLTIPS[mode];
  1299. });
  1300. },
  1301. onchange: function (event) {
  1302. var direction = this.getFieldValue('direction');
  1303. if (event.blockId == this.id && event.name == 'direction' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
  1304. if (direction == "0") {
  1305. this.getChildren()[0].getField("NUM").setValue("60");
  1306. }
  1307. else {
  1308. this.getChildren()[0].getField("NUM").setValue("-60");
  1309. }
  1310. }
  1311. }
  1312. }
  1313. Blockly.Python['robot_dog_servo_movement_rotate_cycle'] = function (block) {
  1314. var rotate_speed = Blockly.Python.valueToCode(block, 'rotate_speed', Blockly.Python.ORDER_ATOMIC);
  1315. var second = Blockly.Python.valueToCode(block, 'second', Blockly.Python.ORDER_ATOMIC);
  1316. var code = '';
  1317. code =
  1318. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1319. 'dog.turn(' + rotate_speed + ')\n' +
  1320. 'time.sleep(' + second + ')\n' +
  1321. 'dog.turn(0)\n' +
  1322. // 'else:\n' +
  1323. // ' rotate_speed = mapping(' + rotate_speed + ',-150,150,0,255)\n' +
  1324. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x32\' + bytes([rotate_speed]) + calculate_sum(ord(b\'\\x32\'),rotate_speed) + b\'\\x00\\xaa\')\n' +
  1325. // ' time.sleep(' + second + ')\n' +
  1326. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x32\\x80\\x43\\x00\\xaa\')\n' +
  1327. '';
  1328. return code;
  1329. }
  1330. Blockly.Blocks['robot_dog_servo_movement_standing_still_cycle'] = {
  1331. init: function () {
  1332. this.appendValueInput("leg_lift_height")
  1333. .setCheck(null)
  1334. .appendField(Blockly.Msg.robot_dog_servo_movement_standing_still);
  1335. this.appendDummyInput()
  1336. .appendField(Blockly.Msg.robot_dog_angle)
  1337. .appendField(Blockly.Msg.robot_dog_servo_movement_standing_still_1);
  1338. this.appendValueInput("second")
  1339. .setCheck(null)
  1340. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_1);
  1341. this.appendDummyInput()
  1342. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_2);
  1343. this.setInputsInline(true);
  1344. this.setPreviousStatement(true, null);
  1345. this.setNextStatement(true, null);
  1346. this.setColour("#5c42bd");
  1347. this.setHelpUrl("");
  1348. this.setTooltip(Blockly.Msg.robot_dog_servo_movement_standing_still_cycle_TOOLTIP);
  1349. }
  1350. }
  1351. Blockly.Python['robot_dog_servo_movement_standing_still_cycle'] = function (block) {
  1352. var leg_lift_height = Blockly.Python.valueToCode(block, 'leg_lift_height', Blockly.Python.ORDER_ATOMIC);
  1353. var second = Blockly.Python.valueToCode(block, 'second', Blockly.Python.ORDER_ATOMIC);
  1354. var code = '';
  1355. code =
  1356. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1357. 'dog.mark_time(' + leg_lift_height + ')\n' +
  1358. 'time.sleep(' + second + ')\n' +
  1359. 'dog.mark_time(0)\n' +
  1360. // 'else:\n' +
  1361. // ' leg_lift_height = mapping(' + leg_lift_height + ',10,35,1,255)\n' +
  1362. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3c\' + bytes([leg_lift_height]) + calculate_sum(ord(b\'\\x3c\'),leg_lift_height) + b\'\\x00\\xaa\')\n' +
  1363. // ' time.sleep(' + second + ')\n' +
  1364. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3c\\x00\\xb9\\x00\\xaa\')\n' +
  1365. '';
  1366. return code;
  1367. }
  1368. Blockly.Blocks['robot_dog_servo_movement_distance_still'] = {
  1369. init: function () {
  1370. this.appendDummyInput()
  1371. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still)
  1372. .appendField(new Blockly.FieldDropdown([
  1373. ["X (-35mm~35mm)", "X"],
  1374. ["Y (-18mm~18mm)", "Y"],
  1375. ["Z (75mm~115mm)", "Z"]
  1376. ]), "direction");
  1377. this.appendValueInput("distance")
  1378. .setCheck(null)
  1379. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_1);
  1380. this.appendDummyInput()
  1381. .appendField(Blockly.Msg.robot_dog_angle);
  1382. this.setInputsInline(true);
  1383. this.setPreviousStatement(true, null);
  1384. this.setNextStatement(true, null);
  1385. this.setColour("#5c42bd");
  1386. this.setHelpUrl("");
  1387. var thisBlock = this;
  1388. this.setTooltip(function () {
  1389. var mode = thisBlock.getFieldValue('direction');
  1390. var TOOLTIPS = {
  1391. "X": Blockly.Msg.robot_dog_servo_movement_distance_still_TOOLTIP.replace('%1', "X"),
  1392. "Y": Blockly.Msg.robot_dog_servo_movement_distance_still_TOOLTIP.replace('%1', "Y"),
  1393. "Z": Blockly.Msg.robot_dog_servo_movement_distance_still_TOOLTIP.replace('%1', "Z")
  1394. };
  1395. return TOOLTIPS[mode];
  1396. });
  1397. },
  1398. onchange: function (event) {
  1399. var direction = this.getFieldValue('direction');
  1400. if (event.blockId == this.id && event.name == 'direction' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
  1401. if (direction == "X") {
  1402. this.getChildren()[0].getField("NUM").setValue("0");
  1403. }
  1404. else if (direction == "Y") {
  1405. this.getChildren()[0].getField("NUM").setValue("0");
  1406. }
  1407. else {
  1408. this.getChildren()[0].getField("NUM").setValue("105");
  1409. }
  1410. }
  1411. }
  1412. }
  1413. Blockly.Python['robot_dog_servo_movement_distance_still'] = function (block) {
  1414. var direction = block.getFieldValue('direction');
  1415. var distance = Blockly.Python.valueToCode(block, 'distance', Blockly.Python.ORDER_ATOMIC);
  1416. var code = '';
  1417. if (direction == "X") {
  1418. code =
  1419. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1420. 'dog.translation(\'x\', ' + distance + ')\n' +
  1421. // 'else:\n' +
  1422. // ' distance = mapping(' + distance + ',-35,35,0,255)\n' +
  1423. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x33\' + bytes([distance]) + calculate_sum(ord(b\'\\x33\'),distance) + b\'\\x00\\xaa\')\n' +
  1424. '';
  1425. }
  1426. else if (direction == "Y") {
  1427. code =
  1428. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1429. 'dog.translation(\'y\', ' + distance + ')\n' +
  1430. // 'else:\n' +
  1431. // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
  1432. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x34\' + bytes([distance]) + calculate_sum(ord(b\'\\x34\'),distance) + b\'\\x00\\xaa\')\n' +
  1433. '';
  1434. }
  1435. else {
  1436. code =
  1437. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1438. 'dog.translation(\'z\', ' + distance + ')\n' +
  1439. // 'else:\n' +
  1440. // ' distance = mapping(' + distance + ',75,115,0,255)\n' +
  1441. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x35\' + bytes([distance]) + calculate_sum(ord(b\'\\x35\'),distance) + b\'\\x00\\xaa\')\n' +
  1442. '';
  1443. }
  1444. return code;
  1445. }
  1446. Blockly.Blocks['robot_dog_servo_movement_distance_still_cycle'] = {
  1447. init: function () {
  1448. this.appendValueInput("cycle")
  1449. .setCheck(null)
  1450. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle);
  1451. this.appendDummyInput()
  1452. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_1)
  1453. .appendField(new Blockly.FieldDropdown([
  1454. ["X", "X"],
  1455. ["Y", "Y"],
  1456. ["Z", "Z"]
  1457. ]), "direction");
  1458. this.appendDummyInput()
  1459. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_2);
  1460. this.setInputsInline(true);
  1461. this.setPreviousStatement(true, null);
  1462. this.setNextStatement(true, null);
  1463. this.setColour("#5c42bd");
  1464. this.setHelpUrl("");
  1465. var thisBlock = this;
  1466. this.setTooltip(function () {
  1467. var mode = thisBlock.getFieldValue('direction');
  1468. var TOOLTIPS = {
  1469. "X": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_TOOLTIP.replace('%1', "X"),
  1470. "Y": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_TOOLTIP.replace('%1', "Y"),
  1471. "Z": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_TOOLTIP.replace('%1', "Z")
  1472. };
  1473. return TOOLTIPS[mode];
  1474. });
  1475. }
  1476. }
  1477. Blockly.Python['robot_dog_servo_movement_distance_still_cycle'] = function (block) {
  1478. var direction = block.getFieldValue('direction');
  1479. var cycle = Blockly.Python.valueToCode(block, 'cycle', Blockly.Python.ORDER_ATOMIC);
  1480. var code = '';
  1481. if (direction == "X") {
  1482. code =
  1483. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1484. 'dog.periodic_tran(\'x\', ' + cycle + ')\n' +
  1485. // 'else:\n' +
  1486. // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
  1487. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x80\' + bytes([cycle]) + calculate_sum(ord(b\'\\x80\'),cycle) + b\'\\x00\\xaa\')\n' +
  1488. '';
  1489. }
  1490. else if (direction == "Y") {
  1491. code =
  1492. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1493. 'dog.periodic_tran(\'y\', ' + cycle + ')\n' +
  1494. // 'else:\n' +
  1495. // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
  1496. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x81\' + bytes([cycle]) + calculate_sum(ord(b\'\\x81\'),cycle) + b\'\\x00\\xaa\')\n' +
  1497. '';
  1498. }
  1499. else {
  1500. code =
  1501. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1502. 'dog.periodic_tran(\'z\', ' + cycle + ')\n' +
  1503. // 'else:\n' +
  1504. // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
  1505. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x82\' + bytes([cycle]) + calculate_sum(ord(b\'\\x82\'),cycle) + b\'\\x00\\xaa\')\n' +
  1506. '';
  1507. }
  1508. return code;
  1509. }
  1510. Blockly.Blocks['robot_dog_servo_movement_distance_still_cycle_stop'] = {
  1511. init: function () {
  1512. this.appendDummyInput()
  1513. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_stop)
  1514. .appendField(new Blockly.FieldDropdown([
  1515. ["X", "X"],
  1516. ["Y", "Y"],
  1517. ["Z", "Z"]
  1518. ]), "direction");
  1519. this.appendDummyInput()
  1520. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_2);
  1521. this.setInputsInline(true);
  1522. this.setPreviousStatement(true, null);
  1523. this.setNextStatement(true, null);
  1524. this.setColour("#5c42bd");
  1525. this.setHelpUrl("");
  1526. var thisBlock = this;
  1527. this.setTooltip(function () {
  1528. var mode = thisBlock.getFieldValue('direction');
  1529. var TOOLTIPS = {
  1530. "X": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_stop_TOOLTIP.replace('%1', "X"),
  1531. "Y": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_stop_TOOLTIP.replace('%1', "Y"),
  1532. "Z": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_stop_TOOLTIP.replace('%1', "Z")
  1533. };
  1534. return TOOLTIPS[mode];
  1535. });
  1536. }
  1537. }
  1538. Blockly.Python['robot_dog_servo_movement_distance_still_cycle_stop'] = function (block) {
  1539. var direction = block.getFieldValue('direction');
  1540. var code = '';
  1541. if (direction == "X") {
  1542. code =
  1543. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1544. 'dog.periodic_tran(\'x\', 0)\n' +
  1545. // 'else:\n' +
  1546. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x80\\x00\' + calculate_sum(ord(b\'\\x80\'),0) + b\'\\x00\\xaa\')\n' +
  1547. '';
  1548. }
  1549. else if (direction == "Y") {
  1550. code =
  1551. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1552. 'dog.periodic_tran(\'y\', 0)\n' +
  1553. // 'else:\n' +
  1554. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x81\\x00\' + calculate_sum(ord(b\'\\x81\'),0) + b\'\\x00\\xaa\')\n' +
  1555. '';
  1556. }
  1557. else {
  1558. code =
  1559. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1560. 'dog.periodic_tran(\'z\', 0)\n' +
  1561. // 'else:\n' +
  1562. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x82\\x00\' + calculate_sum(ord(b\'\\x82\'),0) + b\'\\x00\\xaa\')\n' +
  1563. '';
  1564. }
  1565. return code;
  1566. }
  1567. Blockly.Blocks['robot_dog_servo_movement_rotate_still'] = {
  1568. init: function () {
  1569. this.appendDummyInput()
  1570. .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still)
  1571. .appendField(new Blockly.FieldDropdown([
  1572. ["X (-15˚~15˚)", "X"],
  1573. ["Y (-20˚~20˚)", "Y"],
  1574. ["Z (-11˚~11˚)", "Z"]
  1575. ]), "direction");
  1576. this.appendValueInput("rotate_speed")
  1577. .setCheck(null)
  1578. .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still1);
  1579. this.appendDummyInput()
  1580. .appendField(Blockly.Msg.robot_dog_ag);
  1581. this.setInputsInline(true);
  1582. this.setPreviousStatement(true, null);
  1583. this.setNextStatement(true, null);
  1584. this.setColour("#5c42bd");
  1585. this.setHelpUrl("");
  1586. var thisBlock = this;
  1587. this.setTooltip(function () {
  1588. var mode = thisBlock.getFieldValue('direction');
  1589. var TOOLTIPS = {
  1590. "X": Blockly.Msg.robot_dog_servo_movement_rotate_still_TOOLTIP.replace('%1', "X"),
  1591. "Y": Blockly.Msg.robot_dog_servo_movement_rotate_still_TOOLTIP.replace('%1', "Y"),
  1592. "Z": Blockly.Msg.robot_dog_servo_movement_rotate_still_TOOLTIP.replace('%1', "Z")
  1593. };
  1594. return TOOLTIPS[mode];
  1595. });
  1596. }
  1597. }
  1598. Blockly.Python['robot_dog_servo_movement_rotate_still'] = function (block) {
  1599. var direction = block.getFieldValue('direction');
  1600. var rotate_speed = Blockly.Python.valueToCode(block, 'rotate_speed', Blockly.Python.ORDER_ATOMIC);
  1601. var code = '';
  1602. if (direction == "X") {
  1603. code =
  1604. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1605. 'dog.attitude(\'r\', ' + rotate_speed + ')\n' +
  1606. // 'else:\n' +
  1607. // ' rotate_speed = mapping(' + rotate_speed + ',-15,15,0,255)\n' +
  1608. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x36\' + bytes([rotate_speed]) + calculate_sum(ord(b\'\\x36\'),rotate_speed) + b\'\\x00\\xaa\')\n' +
  1609. '';
  1610. }
  1611. else if (direction == "Y") {
  1612. code =
  1613. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1614. 'dog.attitude(\'p\', ' + rotate_speed + ')\n' +
  1615. // 'else:\n' +
  1616. // ' rotate_speed = mapping(' + rotate_speed + ',-20,20,0,255)\n' +
  1617. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x37\' + bytes([rotate_speed]) + calculate_sum(ord(b\'\\x37\'),rotate_speed) + b\'\\x00\\xaa\')\n' +
  1618. '';
  1619. }
  1620. else {
  1621. code =
  1622. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1623. 'dog.attitude(\'y\', ' + rotate_speed + ')\n' +
  1624. // 'else:\n' +
  1625. // ' rotate_speed = mapping(' + rotate_speed + ',-11,11,0,255)\n' +
  1626. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x38\' + bytes([rotate_speed]) + calculate_sum(ord(b\'\\x38\'),rotate_speed) + b\'\\x00\\xaa\')\n' +
  1627. '';
  1628. }
  1629. return code;
  1630. }
  1631. Blockly.Blocks['robot_dog_servo_movement_rotate_still_cycle'] = {
  1632. init: function () {
  1633. this.appendValueInput("cycle")
  1634. .setCheck(null)
  1635. .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle);
  1636. this.appendDummyInput()
  1637. .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle)
  1638. .appendField(new Blockly.FieldDropdown([
  1639. ["X", "X"],
  1640. ["Y", "Y"],
  1641. ["Z", "Z"]
  1642. ]), "direction");
  1643. this.appendDummyInput()
  1644. .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_1);
  1645. this.setInputsInline(true);
  1646. this.setPreviousStatement(true, null);
  1647. this.setNextStatement(true, null);
  1648. this.setColour("#5c42bd");
  1649. this.setHelpUrl("");
  1650. var thisBlock = this;
  1651. this.setTooltip(function () {
  1652. var mode = thisBlock.getFieldValue('direction');
  1653. var TOOLTIPS = {
  1654. "X": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_TOOLTIP.replace('%1', "X"),
  1655. "Y": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_TOOLTIP.replace('%1', "Y"),
  1656. "Z": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_TOOLTIP.replace('%1', "Z")
  1657. };
  1658. return TOOLTIPS[mode];
  1659. });
  1660. }
  1661. }
  1662. Blockly.Python['robot_dog_servo_movement_rotate_still_cycle'] = function (block) {
  1663. var direction = block.getFieldValue('direction');
  1664. var cycle = Blockly.Python.valueToCode(block, 'cycle', Blockly.Python.ORDER_ATOMIC);
  1665. var code = '';
  1666. if (direction == "X") {
  1667. code =
  1668. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1669. 'dog.periodic_rot(\'r\', ' + cycle + ')\n' +
  1670. // 'else:\n' +
  1671. // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
  1672. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3a\' + bytes([cycle]) + calculate_sum(ord(b\'\\x3a\'),cycle) + b\'\\x00\\xaa\')\n' +
  1673. '';
  1674. }
  1675. else if (direction == "Y") {
  1676. code =
  1677. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1678. 'dog.periodic_rot(\'p\', ' + cycle + ')\n' +
  1679. // 'else:\n' +
  1680. // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
  1681. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x39\' + bytes([cycle]) + calculate_sum(ord(b\'\\x39\'),cycle) + b\'\\x00\\xaa\')\n' +
  1682. '';
  1683. }
  1684. else {
  1685. code =
  1686. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1687. 'dog.periodic_rot(\'y\', ' + cycle + ')\n' +
  1688. // 'else:\n' +
  1689. // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
  1690. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3b\' + bytes([cycle]) + calculate_sum(ord(b\'\\x3b\'),cycle) + b\'\\x00\\xaa\')\n' +
  1691. '';
  1692. }
  1693. return code;
  1694. }
  1695. Blockly.Blocks['robot_dog_servo_movement_rotate_still_cycle_stop'] = {
  1696. init: function () {
  1697. this.appendDummyInput()
  1698. .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_stop)
  1699. .appendField(new Blockly.FieldDropdown([
  1700. ["X", "X"],
  1701. ["Y", "Y"],
  1702. ["Z", "Z"]
  1703. ]), "direction");
  1704. this.appendDummyInput()
  1705. .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_1);
  1706. this.setInputsInline(true);
  1707. this.setPreviousStatement(true, null);
  1708. this.setNextStatement(true, null);
  1709. this.setColour("#5c42bd");
  1710. this.setHelpUrl("");
  1711. var thisBlock = this;
  1712. this.setTooltip(function () {
  1713. var mode = thisBlock.getFieldValue('direction');
  1714. var TOOLTIPS = {
  1715. "X": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_stop_TOOLTIP.replace('%1', "X"),
  1716. "Y": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_stop_TOOLTIP.replace('%1', "Y"),
  1717. "Z": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_stop_TOOLTIP.replace('%1', "Z")
  1718. };
  1719. return TOOLTIPS[mode];
  1720. });
  1721. }
  1722. }
  1723. Blockly.Python['robot_dog_servo_movement_rotate_still_cycle_stop'] = function (block) {
  1724. var direction = block.getFieldValue('direction');
  1725. var code = '';
  1726. if (direction == "X") {
  1727. code =
  1728. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1729. 'dog.periodic_rot(\'r\', 0)\n' +
  1730. // 'else:\n' +
  1731. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3a\\x00\' + calculate_sum(ord(b\'\\x3a\'),0) + b\'\\x00\\xaa\')\n' +
  1732. '';
  1733. }
  1734. else if (direction == "Y") {
  1735. code =
  1736. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1737. 'dog.periodic_rot(\'p\', 0)\n' +
  1738. // 'else:\n' +
  1739. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x39\\x00\' + calculate_sum(ord(b\'\\x39\'),0) + b\'\\x00\\xaa\')\n' +
  1740. '';
  1741. }
  1742. else {
  1743. code =
  1744. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1745. 'dog.periodic_rot(\'y\', 0)\n' +
  1746. // 'else:\n' +
  1747. // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3b\\x00\' + calculate_sum(ord(b\'\\x3b\'),0) + b\'\\x00\\xaa\')\n' +
  1748. '';
  1749. }
  1750. return code;
  1751. }
  1752. Blockly.Blocks['robot_dog_get_servo_angle'] = {
  1753. init: function () {
  1754. this.appendDummyInput()
  1755. .appendField(Blockly.Msg.COCOCLOUD_TIME_GETDATE)
  1756. .appendField(new Blockly.FieldDropdown([
  1757. [Blockly.Msg.robot_dog_front_left_leg, "Front left"],
  1758. [Blockly.Msg.robot_dog_back_left_leg, "Back left"],
  1759. [Blockly.Msg.robot_dog_front_right_leg, "Right front"],
  1760. [Blockly.Msg.robot_dog_back_right_leg, "Right back"]
  1761. ]), "leg");
  1762. this.appendDummyInput()
  1763. .appendField(Blockly.Msg.robot_dog_leg)
  1764. .appendField(new Blockly.FieldDropdown([
  1765. [Blockly.Msg.robot_dog_up_1, "up"],
  1766. [Blockly.Msg.robot_dog_middle_1, "middle"],
  1767. [Blockly.Msg.robot_dog_down_1, "down"]
  1768. ]), "joint");
  1769. this.appendDummyInput()
  1770. .appendField(Blockly.Msg.robot_dog_get_servo_angle);
  1771. this.setInputsInline(true);
  1772. this.setOutput(true, null);
  1773. this.setColour("#5c42bd");
  1774. this.setTooltip(Blockly.Msg.robot_dog_get_servo_angle_TOOLTIP);
  1775. this.setHelpUrl("");
  1776. }
  1777. }
  1778. Blockly.Python['robot_dog_get_servo_angle'] = function (block) {
  1779. var leg = block.getFieldValue('leg');
  1780. var joint = block.getFieldValue('joint');
  1781. var code = '';
  1782. Blockly.Python.definitions_['import_robot_dog_get_servo_angle'] = '' +
  1783. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1784. 'def robot_dog_get_servo_angle(index):\n' +
  1785. ' global dog\n' +
  1786. ' xgoAngle = dog.read_motor()\n' +
  1787. ' return xgoAngle[index]\n' +
  1788. // 'else:\n' +
  1789. // ' def robot_dog_get_servo_angle(index):\n' +
  1790. // ' return None\n' +
  1791. '\n';
  1792. if (leg == "Front left") {
  1793. if (joint == "up") {
  1794. code = 'robot_dog_get_servo_angle(2)';
  1795. }
  1796. else if (joint == "middle") {
  1797. code = 'robot_dog_get_servo_angle(1)';
  1798. }
  1799. else {
  1800. code = 'robot_dog_get_servo_angle(0)';
  1801. }
  1802. }
  1803. else if (leg == "Back left") {
  1804. if (joint == "up") {
  1805. code = 'robot_dog_get_servo_angle(11)';
  1806. }
  1807. else if (joint == "middle") {
  1808. code = 'robot_dog_get_servo_angle(10)';
  1809. }
  1810. else {
  1811. code = 'robot_dog_get_servo_angle(9)';
  1812. }
  1813. }
  1814. else if (leg == "Right front") {
  1815. if (joint == "up") {
  1816. code = 'robot_dog_get_servo_angle(5)';
  1817. }
  1818. else if (joint == "middle") {
  1819. code = 'robot_dog_get_servo_angle(4)';
  1820. }
  1821. else {
  1822. code = 'robot_dog_get_servo_angle(3)';
  1823. }
  1824. }
  1825. else {
  1826. if (joint == "up") {
  1827. code = 'robot_dog_get_servo_angle(8)';
  1828. }
  1829. else if (joint == "middle") {
  1830. code = 'robot_dog_get_servo_angle(7)';
  1831. }
  1832. else {
  1833. code = 'robot_dog_get_servo_angle(6)';
  1834. }
  1835. }
  1836. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1837. }
  1838. Blockly.Blocks['robot_dog_get_rotate_angle'] = {
  1839. init: function () {
  1840. this.appendDummyInput()
  1841. .appendField(Blockly.Msg.robot_dog_get_rotate_angle_1)
  1842. .appendField(new Blockly.FieldDropdown([
  1843. ["X", "X"],
  1844. ["Y", "Y"],
  1845. ["Z", "Z"]
  1846. ]), "direction");
  1847. this.appendDummyInput()
  1848. .appendField(Blockly.Msg.robot_dog_get_rotate_angle);
  1849. this.setInputsInline(true);
  1850. this.setOutput(true, null);
  1851. this.setColour("#5c42bd");
  1852. this.setHelpUrl("");
  1853. var thisBlock = this;
  1854. this.setTooltip(function () {
  1855. var mode = thisBlock.getFieldValue('direction');
  1856. var TOOLTIPS = {
  1857. "X": Blockly.Msg.robot_dog_get_rotate_angle_TOOLTIP.replace('%1', "X"),
  1858. "Y": Blockly.Msg.robot_dog_get_rotate_angle_TOOLTIP.replace('%1', "Y"),
  1859. "Z": Blockly.Msg.robot_dog_get_rotate_angle_TOOLTIP.replace('%1', "Z")
  1860. };
  1861. return TOOLTIPS[mode];
  1862. });
  1863. }
  1864. }
  1865. Blockly.Python['robot_dog_get_rotate_angle'] = function (block) {
  1866. var direction = block.getFieldValue('direction');
  1867. Blockly.Python.definitions_['import_robot_dog_get_rotate_angle'] = '' +
  1868. // 'if (int(str(firmware_info.ai()).split("-")[0]) == 2021 and int(str(firmware_info.ai()).split("-")[1]) >= 5) or (int(str(firmware_info.ai()).split("-")[0]) > 2021):\n' +
  1869. 'def robot_dog_get_rotate_angle(index):\n' +
  1870. ' global dog\n' +
  1871. ' if index == 0:\n' +
  1872. ' return dog.read_roll()\n' +
  1873. ' elif index == 1:\n' +
  1874. ' return dog.read_pitch()\n' +
  1875. ' else:\n' +
  1876. ' return dog.read_yaw()\n' +
  1877. // 'else:\n' +
  1878. // ' def robot_dog_get_rotate_angle(index):\n' +
  1879. // ' return None\n' +
  1880. '\n';
  1881. var code = '';
  1882. if (direction == "X") {
  1883. code = 'robot_dog_get_rotate_angle(0)';
  1884. }
  1885. else if (direction == "Y") {
  1886. code = 'robot_dog_get_rotate_angle(1)';
  1887. }
  1888. else {
  1889. code = 'robot_dog_get_rotate_angle(2)';
  1890. }
  1891. return [code, Blockly.Python.ORDER_CONDITIONAL];
  1892. }
  1893. /*
  1894. __ ___ _____ _
  1895. \ \ / (_) ___(_)
  1896. \ \ /\ / /| | |_ | |
  1897. \ V V / | | _| | |
  1898. \_/\_/ |_|_| |_|
  1899. */
  1900. var K210_WIFI_COLOR = "#3062c1";
  1901. Blockly.Blocks['k210_wifi_enable_disable'] = {
  1902. init: function () {
  1903. this.appendDummyInput()
  1904. .appendField(Blockly.Msg.ai_wifi_set_wifi)
  1905. .appendField(new Blockly.FieldDropdown([
  1906. [Blockly.Msg.ai_wifi_enabled, "1"],
  1907. [Blockly.Msg.ai_wifi_disabled, "0"]
  1908. ]), "de");
  1909. this.setPreviousStatement(true, null);
  1910. this.setNextStatement(true, null);
  1911. this.setColour(K210_WIFI_COLOR);
  1912. var thisBlock = this;
  1913. this.setTooltip(function () {
  1914. var mode = thisBlock.getFieldValue('de');
  1915. var TOOLTIPS = {
  1916. '1': Blockly.Msg.k210_wifi_enable_disable_TOOLTIP.replace('%1', Blockly.Msg.ai_wifi_enabled),
  1917. '0': Blockly.Msg.k210_wifi_enable_disable_TOOLTIP.replace('%1', Blockly.Msg.ai_wifi_disabled)
  1918. };
  1919. return TOOLTIPS[mode];
  1920. });
  1921. this.setHelpUrl("");
  1922. }
  1923. };
  1924. Blockly.Python['k210_wifi_enable_disable'] = function (block) {
  1925. var dropdown_de = block.getFieldValue('de');
  1926. // Blockly.Python.addVariable('k210_wifi_enable_disable', '', true);
  1927. Blockly.Python.definitions_['k210_wifi_enable_disable_def'] = '' +
  1928. 'from machine import UART\n' +
  1929. 'import network, time\n' +
  1930. 'from machine import UART\n' +
  1931. 'from Maix import GPIO\n' +
  1932. 'from fpioa_manager import fm, board_info\n' +
  1933. '\n' +
  1934. '# En SEP8285\n' +
  1935. 'fm.register(8, fm.fpioa.GPIOHS0, force=True)\n' +
  1936. '_k210_wifi_en=GPIO(GPIO.GPIOHS0, GPIO.OUT)\n' +
  1937. '\n' +
  1938. 'fm.register(0, fm.fpioa.GPIOHS1, force=True)\n' +
  1939. '_k210wifi_io0_en=GPIO(GPIO.GPIOHS1, GPIO.OUT)\n' +
  1940. '_k210wifi_io0_en.value(0)\n' +
  1941. '\n' +
  1942. 'fm.register(board_info.WIFI_RX, fm.fpioa.UART2_TX, force=True)\n' +
  1943. 'fm.register(board_info.WIFI_TX, fm.fpioa.UART2_RX, force=True)\n' +
  1944. '\n' +
  1945. '_k210_uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)\n' +
  1946. '\n' +
  1947. 'def _k210_wifi_enable(en):\n' +
  1948. ' global _k210_wifi_en\n' +
  1949. ' _k210_wifi_en.value(en)\n' +
  1950. '';
  1951. // TODO: Assemble Python into code variable.
  1952. if (dropdown_de == "1") {
  1953. var code = '_k210_wifi_enable(' + dropdown_de + ')\n' +
  1954. 'time.sleep(2)\n' +
  1955. '_k210_wifi = network.ESP8285(_k210_uart)\n' +
  1956. '\n';
  1957. } else if (dropdown_de == "0") {
  1958. var code = '_k210_wifi_enable(' + dropdown_de + ')\n' +
  1959. '\n';
  1960. }
  1961. return code;
  1962. };
  1963. Blockly.Blocks['k210_wifi_scan'] = {
  1964. init: function () {
  1965. this.appendDummyInput()
  1966. .appendField(new Blockly.FieldImage("blockly/media/wifi_scan.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1967. this.appendDummyInput()
  1968. .appendField(Blockly.Msg.ai_wifi_start_wifi_scan);
  1969. this.setPreviousStatement(true, null);
  1970. this.setNextStatement(true, null);
  1971. this.setColour(K210_WIFI_COLOR);
  1972. this.setTooltip(Blockly.Msg.k210_wifi_scan_TOOLTIP);
  1973. this.setHelpUrl("");
  1974. }
  1975. };
  1976. Blockly.Python['k210_wifi_scan'] = function (block) {
  1977. // TODO: Assemble Python into code variable.
  1978. Blockly.Python.definitions_['k210_wifi_scan_def'] = '' +
  1979. 'def wifi_deal_ap_info(info):\n' +
  1980. ' res = []\n' +
  1981. ' for ap_str in info:\n' +
  1982. ' ap_str = ap_str.split(",")\n' +
  1983. ' info_one = []\n' +
  1984. ' for node in ap_str:\n' +
  1985. ' if node.startswith(\'\"\'):\n' +
  1986. ' info_one.append(node[1:-1])\n' +
  1987. ' else:\n' +
  1988. ' info_one.append(int(node))\n' +
  1989. ' res.append(info_one)\n' +
  1990. ' return res\n' +
  1991. '';
  1992. var code = '' +
  1993. '_k210_ap_scan_info = _k210_wifi.scan()\n' +
  1994. '_k210_ap_scan_info = wifi_deal_ap_info(_k210_ap_scan_info)\n' +
  1995. '_k210_ap_scan_info.sort(key=lambda x:x[2], reverse=True) # sort by rssi\n' +
  1996. '\n';
  1997. return code;
  1998. };
  1999. Blockly.Blocks['k210_wifi_scan_get'] = {
  2000. init: function () {
  2001. this.appendDummyInput()
  2002. .appendField(Blockly.Msg.ai_wifi_wifi_scan_result);
  2003. this.setOutput(true, null);
  2004. this.setColour(K210_WIFI_COLOR);
  2005. this.setTooltip(Blockly.Msg.k210_wifi_scan_get_TOOLTIP);
  2006. this.setHelpUrl("");
  2007. }
  2008. };
  2009. Blockly.Python['k210_wifi_scan_get'] = function (block) {
  2010. // TODO: Assemble Python into code variable.
  2011. var code = '_k210_ap_scan_info';
  2012. // TODO: Change ORDER_NONE to the correct strength.
  2013. return [code, Blockly.Python.ORDER_NONE];
  2014. };
  2015. Blockly.Blocks['k210_wifi_connect_hotspot'] = {
  2016. init: function () {
  2017. this.appendDummyInput()
  2018. .appendField(new Blockly.FieldImage("blockly/media/wifi_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
  2019. this.appendDummyInput()
  2020. .appendField(Blockly.Msg.ai_wifi_connect_wifi);
  2021. this.appendValueInput("ssid")
  2022. .setCheck(null)
  2023. .appendField(Blockly.Msg.ai_wifi_connect_wifi_ssid);
  2024. this.appendValueInput("pass")
  2025. .setCheck(null)
  2026. .appendField(Blockly.Msg.ai_wifi_connect_wifi_pass);
  2027. this.setPreviousStatement(true, null);
  2028. this.setNextStatement(true, null);
  2029. this.setColour(K210_WIFI_COLOR);
  2030. this.setTooltip(Blockly.Msg.k210_wifi_connect_hotspot_TOOLTIP);
  2031. this.setHelpUrl("");
  2032. }
  2033. };
  2034. Blockly.Python['k210_wifi_connect_hotspot'] = function (block) {
  2035. var value_ssid = Blockly.Python.valueToCode(block, 'ssid', Blockly.Python.ORDER_ATOMIC);
  2036. var value_pass = Blockly.Python.valueToCode(block, 'pass', Blockly.Python.ORDER_ATOMIC);
  2037. // TODO: Assemble Python into code variable.
  2038. Blockly.Python.definitions_['k210_wifi_connect_hotspot_def'] = '' +
  2039. 'def _k210_wifi_reset():\n' +
  2040. ' global _k210_uart\n' +
  2041. ' _k210_wifi_enable(0)\n' +
  2042. ' time.sleep_ms(200)\n' +
  2043. ' _k210_wifi_enable(1)\n' +
  2044. ' time.sleep(2)\n' +
  2045. ' _k210_uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)\n' +
  2046. ' tmp = _k210_uart.read()\n' +
  2047. ' _k210_uart.write("AT+UART_CUR=921600,8,1,0,0\\r\\n")\n' +
  2048. ' print(_k210_uart.read())\n' +
  2049. ' _k210_uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=10240)\n' +
  2050. ' _k210_uart.write("AT\\r\\n")\n' +
  2051. ' tmp = _k210_uart.read()\n' +
  2052. ' print(tmp)\n' +
  2053. ' if not tmp.endswith("OK\\r\\n"):\n' +
  2054. ' print("reset fail")\n' +
  2055. ' return None\n' +
  2056. ' try:\n' +
  2057. ' _k210_wifi = network.ESP8285(_k210_uart)\n' +
  2058. ' except Exception:\n' +
  2059. ' return None\n' +
  2060. ' return _k210_wifi\n' +
  2061. '';
  2062. var code = '' +
  2063. '_k210_wifi = _k210_wifi_reset()\n' +
  2064. 'if not _k210_wifi:\n' +
  2065. ' raise Exception("WiFi init fail")\n' +
  2066. '\n' +
  2067. '_k210_wifi.connect(' + value_ssid + ',' + value_pass + ')\n' +
  2068. '\n';
  2069. return code;
  2070. };
  2071. Blockly.Blocks['k210_wifi_get_connection_status'] = {
  2072. init: function () {
  2073. this.appendDummyInput()
  2074. .appendField(Blockly.Msg.ai_wifi_get_connection_status);
  2075. this.setOutput(true, null);
  2076. this.setColour(K210_WIFI_COLOR);
  2077. this.setTooltip(Blockly.Msg.k210_wifi_get_connection_status_TOOLTIP);
  2078. this.setHelpUrl("");
  2079. }
  2080. };
  2081. Blockly.Python['k210_wifi_get_connection_status'] = function (block) {
  2082. // TODO: Assemble Python into code variable.
  2083. var code = '_k210_wifi.isconnected()';
  2084. // TODO: Change ORDER_NONE to the correct strength.
  2085. return [code, Blockly.Python.ORDER_NONE];
  2086. };
  2087. Blockly.Blocks['k210_wifi_get_config'] = {
  2088. init: function () {
  2089. this.appendDummyInput()
  2090. .appendField(Blockly.Msg.ai_wifi_get_wifi_info)
  2091. .appendField(new Blockly.FieldDropdown([
  2092. [Blockly.Msg.ai_wifi_get_wifi_info_name, "6"],
  2093. [Blockly.Msg.ai_wifi_get_wifi_info_ip, "0"],
  2094. [Blockly.Msg.ai_wifi_get_wifi_info_dns, "2"],
  2095. [Blockly.Msg.ai_wifi_get_wifi_info_mac, "5"]
  2096. ]), "type");
  2097. this.setOutput(true, null);
  2098. this.setColour(K210_WIFI_COLOR);
  2099. this.setTooltip(Blockly.Msg.k210_wifi_get_config_TOOLTIP);
  2100. this.setHelpUrl("");
  2101. }
  2102. };
  2103. Blockly.Python['k210_wifi_get_config'] = function (block) {
  2104. var dropdown_type = block.getFieldValue('type');
  2105. // TODO: Assemble Python into code variable.
  2106. var code = '_k210_wifi.ifconfig()[' + dropdown_type + ']';
  2107. // TODO: Change ORDER_NONE to the correct strength.
  2108. return [code, Blockly.Python.ORDER_NONE];
  2109. };
  2110. Blockly.Blocks['k210_wifi_cococloud'] = {
  2111. init: function () {
  2112. this.appendDummyInput()
  2113. .appendField(new Blockly.FieldImage("blockly/media/cococloud_send.png", 180, 65, { alt: "*", flipRtl: "FALSE" }));
  2114. this.appendDummyInput()
  2115. .appendField(Blockly.Msg.iotservice_cococloud_send_title);
  2116. this.appendDummyInput()
  2117. .appendField(Blockly.Msg.iotservice_cococloud_send_title_api)
  2118. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
  2119. /*this.appendValueInput("first_input")
  2120. .setCheck(null)
  2121. .appendField("Field 0");*/
  2122. this.itemCount_ = 1;
  2123. this.updateShape_();
  2124. this.setMutator(new Blockly.Mutator(['k210_wifi_cococloud_create_with_item']));
  2125. this.setColour(K210_WIFI_COLOR);
  2126. this.setPreviousStatement(true, null);
  2127. this.setNextStatement(true, null);
  2128. this.setTooltip("");
  2129. this.setHelpUrl("");
  2130. },
  2131. mutationToDom: function () {
  2132. var container = document.createElement('mutation');
  2133. container.setAttribute('items', this.itemCount_);
  2134. return container;
  2135. },
  2136. domToMutation: function (xmlElement) {
  2137. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  2138. this.updateShape_();
  2139. },
  2140. decompose: function (workspace) {
  2141. var containerBlock = workspace.newBlock('k210_wifi_cococloud_create_with_container');
  2142. containerBlock.initSvg();
  2143. var connection = containerBlock.getInput('STACK').connection;
  2144. for (var i = 0; i < this.itemCount_; i++) {
  2145. var itemBlock = workspace.newBlock('k210_wifi_cococloud_create_with_item');
  2146. itemBlock.initSvg();
  2147. connection.connect(itemBlock.previousConnection);
  2148. connection = itemBlock.nextConnection;
  2149. }
  2150. return containerBlock;
  2151. },
  2152. compose: function (containerBlock) {
  2153. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2154. // Count number of inputs.
  2155. var connections = [];
  2156. while (itemBlock) {
  2157. connections.push(itemBlock.valueConnection_);
  2158. itemBlock = itemBlock.nextConnection &&
  2159. itemBlock.nextConnection.targetBlock();
  2160. }
  2161. // Disconnect any children that don't belong.
  2162. for (var i = 0; i < this.itemCount_; i++) {
  2163. var connection = this.getInput('ADD' + i).connection.targetConnection;
  2164. if (connection && connections.indexOf(connection) == -1) {
  2165. connection.disconnect();
  2166. }
  2167. }
  2168. this.itemCount_ = connections.length;
  2169. this.updateShape_();
  2170. // Reconnect any child blocks.
  2171. for (var i = 0; i < this.itemCount_; i++) {
  2172. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  2173. }
  2174. },
  2175. saveConnections: function (containerBlock) {
  2176. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  2177. var i = 0;
  2178. while (itemBlock) {
  2179. var input = this.getInput('ADD' + i);
  2180. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  2181. i++;
  2182. itemBlock = itemBlock.nextConnection &&
  2183. itemBlock.nextConnection.targetBlock();
  2184. }
  2185. },
  2186. updateShape_: function () {
  2187. for (var i = 0; i < this.itemCount_; i++) {
  2188. if (!this.getInput('ADD' + i)) {
  2189. var input = this.appendValueInput('ADD' + i);
  2190. input.appendField(Blockly.Msg.iotservice_cococloud_send_property)
  2191. .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
  2192. //input.appendField("資料 " + (i + 1) + ":");
  2193. //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
  2194. // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
  2195. }
  2196. }
  2197. while (this.getInput('ADD' + i)) {
  2198. this.removeInput('ADD' + i);
  2199. i++;
  2200. }
  2201. },
  2202. };
  2203. Blockly.Blocks['k210_wifi_cococloud_create_with_container'] = {
  2204. init: function () {
  2205. this.setColour(K210_WIFI_COLOR);
  2206. this.appendDummyInput()
  2207. .appendField("Items");
  2208. this.appendStatementInput('STACK');
  2209. this.setTooltip('');
  2210. this.contextMenu = false;
  2211. }
  2212. };
  2213. Blockly.Blocks['k210_wifi_cococloud_create_with_item'] = {
  2214. init: function () {
  2215. this.setColour(K210_WIFI_COLOR);
  2216. this.appendDummyInput()
  2217. .appendField("Field");
  2218. this.setPreviousStatement(true);
  2219. this.setNextStatement(true);
  2220. this.setTooltip('');
  2221. this.contextMenu = false;
  2222. }
  2223. };
  2224. Blockly.Python['k210_wifi_cococloud'] = function (block) {
  2225. var api = block.getFieldValue('t_api');
  2226. Blockly.Python.addVariable('_COCOCLOUD_SEND_REQUEST', 'None', true);
  2227. Blockly.Python.definitions_['import_urequests'] = '' +
  2228. 'import urequests\n' +
  2229. 'import ujson as json\n'
  2230. '';
  2231. // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
  2232. var item_field = '',
  2233. item_value = '';
  2234. var cococloud_data = "'{";
  2235. for (var n = 0; n < this.itemCount_; n++) {
  2236. item_field = block.getFieldValue("field" + n);
  2237. item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
  2238. cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
  2239. }
  2240. var cococloud_data_final = cococloud_data + "}'";
  2241. var code = '' +
  2242. '_COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKey/"+ "' + api + '"\n' +
  2243. '_COCOCLOUD_SEND_DATA = ' + cococloud_data_final.slice(0, -3) + '}\'' + '\n' +
  2244. 'try:\n' +
  2245. ' _COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 30)\n' +
  2246. 'except BaseException as e:\n' +
  2247. ' print(e)\n' +
  2248. 'pass\n' +
  2249. '_COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 30)\n'
  2250. '';
  2251. // cococloud_data_final.slice(0, -3) + '}\''
  2252. return code;
  2253. };
  2254. Blockly.Blocks['k210_wifi_cococloud_read'] = {
  2255. init: function () {
  2256. this.appendDummyInput()
  2257. .appendField(new Blockly.FieldImage("blockly/media/cococloud_get.png", 200, 70, { alt: "*", flipRtl: "FALSE" }));
  2258. this.appendDummyInput()
  2259. .appendField(Blockly.Msg.iotservice_cococloud_get_title);
  2260. this.appendDummyInput()
  2261. .appendField(Blockly.Msg.iotservice_cococloud_get_api)
  2262. .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "api");
  2263. this.setInputsInline(false);
  2264. this.setPreviousStatement(true, null);
  2265. this.setNextStatement(true, null);
  2266. this.setColour(K210_WIFI_COLOR);
  2267. this.setTooltip("");
  2268. this.setHelpUrl("");
  2269. }
  2270. };
  2271. Blockly.Python['k210_wifi_cococloud_read'] = function (block) {
  2272. var api = block.getFieldValue('api');
  2273. Blockly.Python.addVariable('_COCOCLOUD_READ_REQUEST', 'None', true);
  2274. Blockly.Python.definitions_['import_urequests'] = '' +
  2275. 'import urequests\n' +
  2276. 'import ujson as json\n'
  2277. '';
  2278. // TODO: Assemble Python into code variable.
  2279. var code = '_COCOCLOUD_READ_REQUEST = urequests.get("http://api.cocorobo.cn/iot/data/eventAPIKey/" + "' + api + '", timeout=60)\n';
  2280. return code;
  2281. };
  2282. Blockly.Blocks['k210_wifi_cococloud_read_data'] = {
  2283. init: function () {
  2284. this.appendDummyInput()
  2285. .appendField(Blockly.Msg.iotservice_cococloud_get_property);
  2286. this.appendValueInput("property")
  2287. .setCheck(null);
  2288. this.appendDummyInput()
  2289. .appendField(Blockly.Msg.iotservice_cococloud_get_data);
  2290. this.setInputsInline(true);
  2291. this.setOutput(true, null);
  2292. this.setColour(K210_WIFI_COLOR);
  2293. this.setTooltip("");
  2294. this.setHelpUrl("");
  2295. }
  2296. };
  2297. Blockly.Python['k210_wifi_cococloud_read_data'] = function (block) {
  2298. var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC);
  2299. // TODO: Assemble Python into code variable.
  2300. var code = '_COCOCLOUD_READ_REQUEST.json()[\'data\'][0][' + property + ']';
  2301. // TODO: Change ORDER_NONE to the correct strength.
  2302. return [code, Blockly.Python.ORDER_NONE];
  2303. };