1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416 |
- Blockly.Blocks['robot_dog_phonetic_to_pinyin_recognition_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/robot_dog_pinyin.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_movement);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.robot_dog_movement_action_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['robot_dog_phonetic_to_pinyin_recognition_setup'] = function (block) {
- Blockly.Python.definitions_.robot_dog_phonetic_to_pinyin_recognition_setup_def = "" +
- "try:\n" +
- " from cocorobo import firmware_info\n" +
- "except BaseException as e:\n" +
- " print(str(e))\n" +
- " pass\n" +
- "\n" +
- "asr = None\n" +
- "tim = None\n" +
- "t = None\n" +
- "\n" +
- "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" +
- " from Maix import GPIO, I2S\n" +
- " from fpioa_manager import fm\n" +
- " fm.register(20,fm.fpioa.I2S0_IN_D0, force=True)\n" +
- " fm.register(18,fm.fpioa.I2S0_SCLK, force=True) # dock 32\n" +
- " fm.register(19,fm.fpioa.I2S0_WS, force=True) # dock 30\n" +
- "\n" +
- " rx = I2S(I2S.DEVICE_0)\n" +
- " rx.channel_config(rx.CHANNEL_0, rx.RECEIVER, align_mode=I2S.STANDARD_MODE)\n" +
- " rx.set_sample_rate(16000)\n" +
- "\n" +
- " from speech_recognizer import asr\n" +
- "\n" +
- " class maix_asr_(asr):\n" +
- "\n" +
- " 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" +
- "\n" +
- " def get_asr_list(string='xiao-ai-fas-tong-xue'):\n" +
- " return [__class__.asr_vocab.index(t) for t in string.split('-') if t in __class__.asr_vocab]\n" +
- "\n" +
- " def get_asr_string(listobj=[117, 214, 257, 144]):\n" +
- " return '-'.join([__class__.asr_vocab[t] for t in listobj if t < len(__class__.asr_vocab)])\n" +
- "\n" +
- " def unit_test():\n" +
- " print(__class__.get_asr_list('xiao-ai'))\n" +
- " print(__class__.get_asr_string(__class__.get_asr_list('xiao-ai-fas-tong-xue')))\n" +
- "\n" +
- " def config(self, sets):\n" +
- " self.set([(sets[key], __class__.get_asr_list(key)) for key in sets])\n" +
- "\n" +
- " def recognize(self):\n" +
- " res = self.result()\n" +
- " # print(tmp)\n" +
- " if res != None:\n" +
- " #print(res)\n" +
- " sets = {}\n" +
- " for tmp in res:\n" +
- " #print(tmp)\n" +
- " sets[__class__.get_asr_string(tmp[1])] = tmp[0]\n" +
- " #print(tmp[0], get_asr_string(tmp[1]))\n" +
- " return sets\n" +
- " return None\n" +
- "\n";
- var code = "";
- return code;
- };
- Blockly.Blocks['robot_dog_phonetic_to_pinyin_recognition_set_param'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_set_param);
- this.appendValueInput("data")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_speech_recognition_setup_pinyin);
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_set_param_1)
- .appendField(new Blockly.FieldDropdown([
- ["1", "1"],
- ["2", "2"],
- ["3", "3"],
- ["4", "4"],
- ["5", "5"],
- ["6", "6"],
- ["7", "7"]
- ]), "type");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setInputsInline(false);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_set_param_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['robot_dog_phonetic_to_pinyin_recognition_set_param'] = function (block) {
- var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
- var _type = block.getFieldValue('type');
- Blockly.Python.definitions_.robot_dog_phonetic_to_pinyin_recognition_set_param_def = "" +
- "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" +
- " from machine import Timer\n" +
- "\n" +
- " def on_timer(timer):\n" +
- " #print('time up:',timer)\n" +
- " #print('param:',timer.callback_arg())\n" +
- " timer.callback_arg().state()\n" +
- "\n" +
- // " t = None\n" +
- " if str(firmware_info.ai()) == \"2021-05-20\":\n" +
- " t = maix_asr_(0x790000, I2S.DEVICE_0, 3, shift=0) # maix bit set shift=1\n" +
- " 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" +
- " t = maix_asr_(0x681000, I2S.DEVICE_0, 3, shift=0) # maix bit set shift=1\n" +
- " else:\n" +
- " t = maix_asr_(0x500000, I2S.DEVICE_0, 3, shift=0)\n" +
- "\n" +
- " t.config(" + data + ")\n" +
- "\n" +
- " tim = Timer(Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=32, callback=on_timer, arg=t,start=False,priority=" + _type + ")\n" +
- " def getRecognizeResult():\n" +
- " try:\n" +
- " global t\n" +
- " tmp = t.recognize()\n" +
- " if tmp != None:\n" +
- " print(tmp)\n" +
- " for key in tmp.keys():\n" +
- " return key\n" +
- " except:\n" +
- ' pass\n' +
- "\n" +
- "else:\n" +
- " from cocorobo import mandarin_asr\n" +
- " asr = mandarin_asr(\n" +
- " config = " + data + "\n" +
- " )\n" +
- "\n";
- var code = "";
- return code;
- };
- Blockly.Blocks['robot_dog_phonetic_to_pinyin_start_recognition'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_start_recognition);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_start_recognition_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['robot_dog_phonetic_to_pinyin_start_recognition'] = function (block) {
- var code = "tim.start()\n";
- return code;
- };
- Blockly.Blocks['robot_dog_phonetic_to_pinyin_stop_recognition'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_stop_recognition);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_stop_recognition_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['robot_dog_phonetic_to_pinyin_stop_recognition'] = function (block) {
- var code = "tim.stop()\n" +
- "t.__del__()\n" +
- "del t\n";
- return code;
- };
- Blockly.Blocks['robot_dog_phonetic_to_pinyin_pause_recognition'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_pause_recognition);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_pause_recognition_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['robot_dog_phonetic_to_pinyin_pause_recognition'] = function (block) {
- var code = "tim.stop()\n";
- return code;
- };
- Blockly.Blocks['robot_dog_phonetic_to_pinyin_get_result'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_phonetic_to_pinyin_get_result);
- this.setOutput(true, null);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.robot_dog_phonetic_to_pinyin_get_result_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['robot_dog_phonetic_to_pinyin_get_result'] = function (block) {
- Blockly.Python.definitions_.robot_dog_phonetic_to_pinyin_start_recognition_def = "" +
- "def getRecoResult():\n" +
- " global asr\n" +
- " 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" +
- " return getRecognizeResult()\n" +
- " else:\n" +
- " return asr.getRecognizeResult()\n" +
- "\n";
- var code = 'getRecoResult()';
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['robot_dog_phonetic_to_pinyin_action'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_execution)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_movement_action_0, "51"],
- [Blockly.Msg.robot_dog_movement_action_1, "52"],
- [Blockly.Msg.robot_dog_movement_action_3, "54"],
- [Blockly.Msg.robot_dog_movement_action_5, "56"],
- [Blockly.Msg.robot_dog_movement_action_17, "68"],
- [Blockly.Msg.robot_dog_movement_action_18, "69"],
- ]), "type");
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_action);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- '51': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_0),
- '52': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_1),
- '54': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_3),
- '56': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_5),
- '68': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_17),
- '69': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_18)
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_phonetic_to_pinyin_action'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var code = '' +
- 'ai_uart.write(bytes([' + dropdown_type + ']))\n' +
- 'time.sleep_ms(400)\n' +
- '';
- return code;
- }
- Blockly.Blocks['robot_dog_setup'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/robot_dog.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_setup);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.robot_dog_setup_TOOLTIP);
- this.setHelpUrl("");
- }
- }
- Blockly.Python['robot_dog_setup'] = function (block) {
- Blockly.Python.definitions_['import_robot_dog_setup'] = 'import machine, time\n' +
- 'from fpioa_manager import fm\n' +
- 'try:\n' +
- // ' from cocorobo import firmware_info\n' +
- ' from xgo import XGO\n' +
- 'except BaseException as e:\n' +
- ' print(str(e))\n' +
- ' pass\n' +
- '\n';
- // Blockly.Python.addVariable('uart_data', 'uart_data = ""', true);
- Blockly.Python.definitions_['robot_dog_setup'] = '' +
- // 'fm.register(13,fm.fpioa.UART2_TX)\n' +
- // 'fm.register(14,fm.fpioa.UART2_RX)\n' +
- // 'dog = None\n' +
- // 'robot_dog_setup_uart = None\n' +
- // '\n' +
- // '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' +
- // ' dog = XGO(machine.UART.UART2)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart = machine.UART(machine.UART.UART2,115200,bits=8,parity=None,stop=1)\n' +
- // ' def add(a,b):\n' +
- // ' num1 = a ^ b\n' +
- // ' num2 = (a & b) << 1\n' +
- // ' while num2 != 0:\n' +
- // ' temp = num1 ^ num2\n' +
- // ' num2 = (num1 & num2) << 1\n' +
- // ' num1 = temp\n' +
- // ' return num1\n' +
- // '\n' +
- // ' def calculate_sum(a,b):\n' +
- // ' bytearr = [9, 1, a, b]\n' +
- // ' sum = 0\n' +
- // ' for i in bytearr:\n' +
- // ' sum = add(sum,i)\n' +
- // ' calculated_cksum = bin(sum).replace("0b","")\n' +
- // ' while len(calculated_cksum) < 8:\n' +
- // ' calculated_cksum = "0" + calculated_cksum\n' +
- // ' ReturningChecksum = ""\n' +
- // ' for index in range(len(calculated_cksum)):\n' +
- // ' if calculated_cksum[index] == "1":\n' +
- // ' ReturningChecksum += "0"\n' +
- // ' elif calculated_cksum[index] == "0":\n' +
- // ' ReturningChecksum += "1"\n' +
- // ' return bytes([int(hex(int(ReturningChecksum,2)),16)])\n' +
- // '\n' +
- // ' def mapping(input_value,i_min,i_max,o_min,o_max):\n' +
- // ' if input_value < i_min:\n' +
- // ' input_value = i_min\n' +
- // ' if input_value > i_max:\n' +
- // ' input_value = i_max\n' +
- // ' dat=(input_value-i_min)/(i_max-i_min)*(o_max-o_min)+o_min\n' +
- // ' return round(dat)\n' +
- // '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' +
- 'fm.register(13,fm.fpioa.UART2_TX)\n' +
- 'fm.register(14,fm.fpioa.UART2_RX)\n' +
- 'dog = XGO(machine.UART.UART2)\n' +
- '';
- var code = '';
- return code;
- }
- Blockly.Blocks['robot_dog_action'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_execution)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_movement_action_19, "0"],
- // [Blockly.Msg.robot_dog_movement_action_20, "1"],
- // [Blockly.Msg.robot_dog_movement_action_21, "2"],
- // [Blockly.Msg.robot_dog_movement_action_22, "3"],
- // [Blockly.Msg.robot_dog_movement_action_23, "4"],
- // [Blockly.Msg.robot_dog_movement_action_24, "12"],
- // [Blockly.Msg.robot_dog_movement_action_25, "13"],
- [Blockly.Msg.robot_dog_movement_action_0, "1"],
- [Blockly.Msg.robot_dog_movement_action_1, "2"],
- [Blockly.Msg.robot_dog_movement_action_2, "3"],
- [Blockly.Msg.robot_dog_movement_action_3, "4"],
- [Blockly.Msg.robot_dog_movement_action_4, "5"],
- [Blockly.Msg.robot_dog_movement_action_5, "6"],
- [Blockly.Msg.robot_dog_movement_action_6, "7"],
- [Blockly.Msg.robot_dog_movement_action_7, "8"],
- [Blockly.Msg.robot_dog_movement_action_8, "9"],
- [Blockly.Msg.robot_dog_movement_action_9, "10"],
- [Blockly.Msg.robot_dog_movement_action_10, "11"],
- [Blockly.Msg.robot_dog_movement_action_11, "12"],
- [Blockly.Msg.robot_dog_movement_action_12, "13"],
- [Blockly.Msg.robot_dog_movement_action_13, "14"],
- [Blockly.Msg.robot_dog_movement_action_14, "15"],
- [Blockly.Msg.robot_dog_movement_action_15, "16"],
- [Blockly.Msg.robot_dog_movement_action_16, "17"],
- [Blockly.Msg.robot_dog_movement_action_17, "18"],
- [Blockly.Msg.robot_dog_movement_action_18, "19"],
- [Blockly.Msg.robot_dog_movement_action_30, "20"]
- ]), "type");
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_action);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('type');
- var TOOLTIPS = {
- '0': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_19),
- // '1': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_20),
- // '2': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_21),
- // '3': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_22),
- // '4': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_23),
- // '12': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_24),
- // '13': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_25),
- '1': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_0),
- '2': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_1),
- '3': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_2),
- '4': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_3),
- '5': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_4),
- '6': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_5),
- '7': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_6),
- '8': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_7),
- '9': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_8),
- '10': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_9),
- '11': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_10),
- '12': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_11),
- '13': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_12),
- '14': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_13),
- '15': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_14),
- '16': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_15),
- '17': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_16),
- '18': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_17),
- '19': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_18),
- '20': Blockly.Msg.robot_dog_action_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_30)
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_action'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- var code = '';
- if (dropdown_type == "0") {
- code =
- // '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' +
- 'dog.reset()\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.action(' + dropdown_type + ')\n' +
- '';
- }
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_execution)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_front_left_leg, "Front left"],
- [Blockly.Msg.robot_dog_back_left_leg, "Back left"],
- [Blockly.Msg.robot_dog_front_right_leg, "Right front"],
- [Blockly.Msg.robot_dog_back_right_leg, "Right back"]
- ]), "leg");
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_leg)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_up, "up"],
- [Blockly.Msg.robot_dog_middle, "middle"],
- [Blockly.Msg.robot_dog_down, "down"]
- ]), "joint");
- this.appendValueInput("degree")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_rotate);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_ag);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.robot_dog_servo_movement_TOOLTIP);
- this.setHelpUrl("");
- },
- onchange: function (event) {
- var joint = this.getFieldValue('joint');
- if (event.blockId == this.id && event.name == 'joint' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
- if (joint == "up") {
- this.getChildren()[0].getField("NUM").setValue("0");
- }
- else if (joint == "middle") {
- this.getChildren()[0].getField("NUM").setValue("42.5");
- }
- else {
- this.getChildren()[0].getField("NUM").setValue("20");
- }
- }
- }
- }
- Blockly.Python['robot_dog_servo_movement'] = function (block) {
- var leg = block.getFieldValue('leg');
- var joint = block.getFieldValue('joint');
- var value_degree = Blockly.Python.valueToCode(block, 'degree', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- var leg_sign = "";
- if (leg == "Front left") {
- if (joint == "up") {
- leg_sign = "b\'\\x52\'"
- code =
- // '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' +
- 'dog.motor(13,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-31,31,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else if (joint == "middle") {
- leg_sign = "b\'\\x51\'"
- code =
- // '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' +
- 'dog.motor(12,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-66,93,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else {
- leg_sign = "b\'\\x50\'"
- code =
- // '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' +
- 'dog.motor(11,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-65,73,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- }
- else if (leg == "Back left") {
- if (joint == "up") {
- leg_sign = "b\'\\x5B\'"
- code =
- // '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' +
- 'dog.motor(43,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-31,31,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else if (joint == "middle") {
- leg_sign = "b\'\\x5A\'"
- code =
- // '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' +
- 'dog.motor(42,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-66,93,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else {
- leg_sign = "b\'\\x59\'"
- code =
- // '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' +
- 'dog.motor(41,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-65,73,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- }
- else if (leg == "Right front") {
- if (joint == "up") {
- leg_sign = "b\'\\x55\'"
- code =
- // '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' +
- 'dog.motor(23,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-31,31,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else if (joint == "middle") {
- leg_sign = "b\'\\x54\'"
- code =
- // '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' +
- 'dog.motor(22,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-66,93,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else {
- leg_sign = "b\'\\x53\'"
- code =
- // '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' +
- 'dog.motor(21,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-65,73,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- }
- else {
- if (joint == "up") {
- leg_sign = "b\'\\x58\'"
- code =
- // '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' +
- 'dog.motor(33,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-31,31,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else if (joint == "middle") {
- leg_sign = "b\'\\x57\'"
- code =
- // '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' +
- 'dog.motor(32,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-66,93,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else {
- leg_sign = "b\'\\x56\'"
- code =
- // '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' +
- 'dog.motor(31,' + value_degree + ')\n' +
- // 'else:\n' +
- // ' degree = mapping(' + value_degree + ',-65,73,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([degree]) + calculate_sum(ord(' + leg_sign + '),degree) + b\'\\x00\\xaa\')\n' +
- '';
- }
- }
- return code;
- }
- Blockly.Blocks['robot_dog_servo_speed'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_set_speed)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_servo_speed, "servo"],
- [Blockly.Msg.robot_dog_movement_speed, "movement"]
- ]), "servoOrMovement")
- .appendField(Blockly.Msg.robot_dog_servo_to)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_servo_speed_1, "fast"],
- [Blockly.Msg.robot_dog_servo_speed_2, "normal"],
- [Blockly.Msg.robot_dog_servo_speed_3, "slow"]
- ]), "speed");
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('speed');
- var TOOLTIPS = {
- 'fast': Blockly.Msg.robot_dog_servo_speed_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_servo_speed_1),
- 'normal': Blockly.Msg.robot_dog_servo_speed_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_servo_speed_2),
- 'slow': Blockly.Msg.robot_dog_servo_speed_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_servo_speed_3),
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_servo_speed'] = function (block) {
- var speed = block.getFieldValue('speed');
- var servoOrMovement = block.getFieldValue('servoOrMovement');
- var code = '';
- if (servoOrMovement == "servo") {
- if (speed == "fast") {
- code =
- // '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' +
- 'dog.motor_speed(255)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x5c\\x00\\x99\\x00\\xaa\')\n' +
- '';
- }
- else if (speed == "normal") {
- code =
- // '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' +
- 'dog.motor_speed(128)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x5c\\x80\\x19\\x00\\xaa\')\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.motor_speed(1)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x5c\\xff\\x9a\\x00\\xaa\')\n' +
- '';
- }
- }
- else if (servoOrMovement == "movement") {
- if (speed == "fast") {
- code =
- // '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' +
- 'dog.pace(\'high\')\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3d\\x02\\xb6\\x00\\xaa\')\n' +
- '';
- }
- else if (speed == "normal") {
- code =
- // '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' +
- 'dog.pace(\'normal\')\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3d\\x00\\xb8\\x00\\xaa\')\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.pace(\'slow\')\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3d\\x01\\xb7\\x00\\xaa\')\n' +
- '';
- }
- }
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_location'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_execution)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_front_left_leg, "Front left"],
- [Blockly.Msg.robot_dog_back_left_leg, "Back left"],
- [Blockly.Msg.robot_dog_front_right_leg, "Right front"],
- [Blockly.Msg.robot_dog_back_right_leg, "Right back"]
- ]), "leg")
- .appendField(Blockly.Msg.robot_dog_leg_f);
- this.appendValueInput("distance_x")
- .setCheck(null)
- .appendField("X (-35mm~35mm)")
- .appendField(Blockly.Msg.robot_dog_servo_movement_location)
- // .appendField(Blockly.Msg.robot_dog_angle);
- this.appendValueInput("distance_y")
- .setCheck(null)
- .appendField("Y (-18mm~18mm)")
- .appendField(Blockly.Msg.robot_dog_servo_movement_location)
- // .appendField(Blockly.Msg.robot_dog_angle);
- this.appendValueInput("distance_z")
- .setCheck(null)
- .appendField("Z (75mm~115mm)")
- .appendField(Blockly.Msg.robot_dog_servo_movement_location)
- // .appendField(Blockly.Msg.robot_dog_angle);
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.robot_dog_servo_movement_location_TOOLTIP);
- this.setHelpUrl("");
- }
- }
- Blockly.Python['robot_dog_servo_movement_location'] = function (block) {
- var leg = block.getFieldValue('leg');
- // var direction = block.getFieldValue('direction');
- var distance_x = Blockly.Python.valueToCode(block, 'distance_x', Blockly.Python.ORDER_ATOMIC);
- var distance_y = Blockly.Python.valueToCode(block, 'distance_y', Blockly.Python.ORDER_ATOMIC);
- var distance_z = Blockly.Python.valueToCode(block, 'distance_z', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- // var leg_sign = "";
- if (leg == "Front left") {
- code = 'dog.leg(1,[' + distance_x + ',' + distance_y + ',' + distance_z + '])\n';
- // if (direction == "X") {
- // leg_sign = "b\'\\x40\'"
- // code =
- // // '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' +
- // 'dog.leg(1,[' + distance + ', 0, 105])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',-35,35,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- // else if (direction == "Y") {
- // leg_sign = "b\'\\x41\'"
- // code =
- // // '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' +
- // 'dog.leg(1,[0, ' + distance + ', 105])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- // else {
- // leg_sign = "b\'\\x42\'"
- // code =
- // // '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' +
- // 'dog.leg(1,[0, 0, ' + distance + '])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',75,115,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- }
- else if (leg == "Back left") {
- code = 'dog.leg(4,[' + distance_x + ',' + distance_y + ',' + distance_z + '])\n';
- // if (direction == "X") {
- // leg_sign = "b\'\\x49\'"
- // code =
- // // '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' +
- // 'dog.leg(4,[' + distance + ', 0, 105])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',-35,35,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- // else if (direction == "Y") {
- // leg_sign = "b\'\\x4a\'"
- // code =
- // // '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' +
- // 'dog.leg(4,[0, ' + distance + ', 105])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- // else {
- // leg_sign = "b\'\\x4b\'"
- // code =
- // // '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' +
- // 'dog.leg(4,[0, 0, ' + distance + '])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',75,115,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- }
- else if (leg == "Right front") {
- code = 'dog.leg(2,[' + distance_x + ',' + distance_y + ',' + distance_z + '])\n';
- // if (direction == "X") {
- // leg_sign = "b\'\\x43\'"
- // code =
- // // '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' +
- // 'dog.leg(2,[' + distance + ', 0, 105])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',-35,35,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- // else if (direction == "Y") {
- // leg_sign = "b\'\\x44\'"
- // code =
- // // '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' +
- // 'dog.leg(2,[0, ' + distance + ', 105])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- // else {
- // leg_sign = "b\'\\x45\'"
- // code =
- // // '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' +
- // 'dog.leg(2,[0, 0, ' + distance + '])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',75,115,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- }
- else {
- code = 'dog.leg(3,[' + distance_x + ',' + distance_y + ',' + distance_z + '])\n';
- // if (direction == "X") {
- // leg_sign = "b\'\\x46\'"
- // code =
- // // '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' +
- // 'dog.leg(3,[' + distance + ', 0, 105])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',-35,35,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- // else if (direction == "Y") {
- // leg_sign = "b\'\\x47\'"
- // code =
- // // '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' +
- // 'dog.leg(3,[0, ' + distance + ', 105])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- // else {
- // leg_sign = "b\'\\x48\'"
- // code =
- // // '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' +
- // 'dog.leg(3,[0, 0, ' + distance + '])\n' +
- // // 'else:\n' +
- // // ' distance = mapping(' + distance + ',75,115,0,255)\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\' + ' + leg_sign + ' + bytes([distance]) + calculate_sum(ord(' + leg_sign + '),distance) + b\'\\x00\\xaa\')\n' +
- // '';
- // }
- }
- return code;
- }
- Blockly.Blocks['robot_dog_open_gyroscope'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_controll_action_1, "open"],
- [Blockly.Msg.robot_dog_controll_action_0, "close"]
- ]), "action")
- .appendField(Blockly.Msg.robot_dog_gyroscope);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('action');
- var TOOLTIPS = {
- 'open': Blockly.Msg.robot_dog_open_gyroscope_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_1),
- 'close': Blockly.Msg.robot_dog_open_gyroscope_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_0)
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_open_gyroscope'] = function (block) {
- var action = block.getFieldValue('action');
- var code = '';
- if (action == "open") {
- code =
- // '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' +
- 'dog.imu(1)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x61\\x01\\x93\\x00\\xaa\')\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.imu(0)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x61\\x00\\x94\\x00\\xaa\')\n' +
- '';
- }
- return code;
- }
- Blockly.Blocks['robot_dog_open_performance_mode'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_controll_action_1, "open"],
- [Blockly.Msg.robot_dog_controll_action_0, "close"]
- ]), "action")
- .appendField(Blockly.Msg.robot_dog_open_performance_mode);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('action');
- var TOOLTIPS = {
- 'open': Blockly.Msg.robot_dog_open_performance_mode_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_1),
- 'close': Blockly.Msg.robot_dog_open_performance_mode_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_0)
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_open_performance_mode'] = function (block) {
- var action = block.getFieldValue('action');
- var code = '';
- if (action == "open") {
- code =
- // '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' +
- 'dog.perform(1)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x03\\x01\\xf1\\x00\\xaa\')\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.perform(0)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x03\\x00\\xf2\\x00\\xaa\')\n' +
- '';
- }
- return code;
- }
- Blockly.Blocks['robot_dog_open_calibration_mode'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_controll_action_1, "open"],
- [Blockly.Msg.robot_dog_controll_action_0, "close"]
- ]), "action")
- .appendField(Blockly.Msg.robot_dog_open_calibration_mode);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('action');
- var TOOLTIPS = {
- 'open': Blockly.Msg.robot_dog_open_calibration_mode_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_1),
- 'close': Blockly.Msg.robot_dog_open_calibration_mode_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_controll_action_0)
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_open_calibration_mode'] = function (block) {
- var action = block.getFieldValue('action');
- var code = '';
- if (action == "open") {
- code = 'robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x04\\x01\\xf0\\x00\\xaa\')\n' +
- '';
- }
- else {
- code = 'robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x04\\x00\\xf1\\x00\\xaa\')\n' +
- '';
- }
- return code;
- }
- Blockly.Blocks['robot_dog_get_power'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_get_power);
- this.setOutput(true, null);
- this.setInputsInline(true);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.robot_dog_get_power_TOOLTIP);
- }
- }
- Blockly.Python['robot_dog_get_power'] = function (block) {
- Blockly.Python.definitions_['import_robot_dog_get_power'] = '' +
- // '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' +
- 'def robot_dog_get_power():\n' +
- ' global dog\n' +
- ' return dog.read_battery()\n' +
- // 'else:\n' +
- // ' def _read_serial_data():\n' +
- // ' global robot_dog_setup_uart\n' +
- // ' if robot_dog_setup_uart.any():\n' +
- // ' read_data = robot_dog_setup_uart.read()\n' +
- // ' return read_data\n' +
- // '\n' +
- // ' def robot_dog_get_power():\n' +
- // // ' global robot_dog_setup_uart\n' +
- // // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x02\\x01\\x01\\xF2\\x00\\xAA\')\n' +
- // // ' time.sleep_ms(10)\n' +
- // // ' a = _read_serial_data()\n' +
- // ' return None\n' +
- '\n';
- var code = 'robot_dog_get_power()';
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- };
- Blockly.Blocks['robot_dog_servo_movement_distance'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_movement_action_28, "0"],
- [Blockly.Msg.robot_dog_movement_action_29, "1"],
- [Blockly.Msg.robot_dog_movement_action_26, "2"],
- [Blockly.Msg.robot_dog_movement_action_27, "3"]
- ]), "direction");
- this.appendValueInput("distance")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_location);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_angle);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('direction');
- var TOOLTIPS = {
- '0': Blockly.Msg.robot_dog_servo_movement_distance_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_28),
- '1': Blockly.Msg.robot_dog_servo_movement_distance_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_29),
- '2': Blockly.Msg.robot_dog_servo_movement_distance_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_26),
- '3': Blockly.Msg.robot_dog_servo_movement_distance_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_27)
- };
- return TOOLTIPS[mode];
- });
- },
- onchange: function (event) {
- var direction = this.getFieldValue('direction');
- if (event.blockId == this.id && event.name == 'direction' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
- if (direction == "0") {
- this.getChildren()[0].getField("NUM").setValue("10");
- }
- else if (direction == "1") {
- this.getChildren()[0].getField("NUM").setValue("-8");
- }
- else if (direction == "2") {
- this.getChildren()[0].getField("NUM").setValue("6");
- }
- else {
- this.getChildren()[0].getField("NUM").setValue("-6");
- }
- }
- }
- }
- Blockly.Python['robot_dog_servo_movement_distance'] = function (block) {
- var direction = block.getFieldValue('direction');
- var distance = Blockly.Python.valueToCode(block, 'distance', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- if (direction == "0" || direction == "1") {
- code =
- // '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' +
- 'dog.move_x(' + distance + ')\n' +
- // 'else:\n' +
- // ' distance = mapping(' + distance + ',-25,25,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x30\' + bytes([distance]) + calculate_sum(ord(b\'\\x30\'),distance) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.move_y(' + distance + ')\n' +
- // 'else:\n' +
- // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x31\' + bytes([distance]) + calculate_sum(ord(b\'\\x31\'),distance) + b\'\\x00\\xaa\')\n' +
- ''
- }
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_rotate'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_rotate)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.MOTION_ONDIRECTION_CLOCKWISE1, "0"],
- [Blockly.Msg.MOTION_ONDIRECTION_COUNTERCLOCKWISE1, "1"]
- ]), "direction");
- this.appendValueInput("rotate_speed")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_location);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_angle_speed);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('direction');
- var TOOLTIPS = {
- '0': Blockly.Msg.robot_dog_servo_movement_rotate_TOOLTIP.replace('%1', Blockly.Msg.MOTION_ONDIRECTION_CLOCKWISE1),
- '1': Blockly.Msg.robot_dog_servo_movement_rotate_TOOLTIP.replace('%1', Blockly.Msg.MOTION_ONDIRECTION_COUNTERCLOCKWISE1)
- };
- return TOOLTIPS[mode];
- });
- },
- onchange: function (event) {
- var direction = this.getFieldValue('direction');
- if (event.blockId == this.id && event.name == 'direction' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
- if (direction == "0") {
- this.getChildren()[0].getField("NUM").setValue("60");
- }
- else {
- this.getChildren()[0].getField("NUM").setValue("-60");
- }
- }
- }
- }
- Blockly.Python['robot_dog_servo_movement_rotate'] = function (block) {
- var rotate_speed = Blockly.Python.valueToCode(block, 'rotate_speed', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- code =
- // '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' +
- 'dog.turn(' + rotate_speed + ')\n' +
- // 'else:\n' +
- // ' rotate_speed = mapping(' + rotate_speed + ',-150,150,0,255)\n' +
- // ' 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' +
- '';
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_standing_still'] = {
- init: function () {
- this.appendValueInput("leg_lift_height")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_standing_still);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_angle)
- .appendField(Blockly.Msg.robot_dog_servo_movement_standing_still_1);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.robot_dog_servo_movement_standing_still_TOOLTIP);
- }
- }
- Blockly.Python['robot_dog_servo_movement_standing_still'] = function (block) {
- var leg_lift_height = Blockly.Python.valueToCode(block, 'leg_lift_height', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- code =
- // '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' +
- 'dog.mark_time(' + leg_lift_height + ')\n' +
- // 'else:\n' +
- // ' leg_lift_height = mapping(' + leg_lift_height + ',10,35,1,255)\n' +
- // ' 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' +
- '';
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_distance_cycle'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_movement_action_28, "0"],
- [Blockly.Msg.robot_dog_movement_action_29, "1"],
- [Blockly.Msg.robot_dog_movement_action_26, "2"],
- [Blockly.Msg.robot_dog_movement_action_27, "3"]
- ]), "direction");
- this.appendValueInput("distance")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_location);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_angle);
- this.appendValueInput("second")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_2);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('direction');
- var TOOLTIPS = {
- '0': Blockly.Msg.robot_dog_servo_movement_distance_cycle_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_28),
- '1': Blockly.Msg.robot_dog_servo_movement_distance_cycle_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_29),
- '2': Blockly.Msg.robot_dog_servo_movement_distance_cycle_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_26),
- '3': Blockly.Msg.robot_dog_servo_movement_distance_cycle_TOOLTIP.replace('%1', Blockly.Msg.robot_dog_movement_action_27)
- };
- return TOOLTIPS[mode];
- });
- },
- onchange: function (event) {
- var direction = this.getFieldValue('direction');
- if (event.blockId == this.id && event.name == 'direction' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
- if (direction == "0") {
- this.getChildren()[0].getField("NUM").setValue("10");
- }
- else if (direction == "1") {
- this.getChildren()[0].getField("NUM").setValue("-8");
- }
- else if (direction == "2") {
- this.getChildren()[0].getField("NUM").setValue("6");
- }
- else {
- this.getChildren()[0].getField("NUM").setValue("-6");
- }
- }
- }
- }
- Blockly.Python['robot_dog_servo_movement_distance_cycle'] = function (block) {
- var direction = block.getFieldValue('direction');
- var distance = Blockly.Python.valueToCode(block, 'distance', Blockly.Python.ORDER_ATOMIC);
- var second = Blockly.Python.valueToCode(block, 'second', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- if (direction == "0" || direction == "1") {
- code =
- // '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' +
- 'dog.move_x(' + distance + ')\n' +
- 'time.sleep(' + second + ')\n' +
- 'dog.move_x(0)\n' +
- // 'else:\n' +
- // ' distance = mapping(' + distance + ',-25,25,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x30\' + bytes([distance]) + calculate_sum(ord(b\'\\x30\'),distance) + b\'\\x00\\xaa\')\n' +
- // ' time.sleep(' + second + ')\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x30\\x80\\x45\\x00\\xaa\')\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.move_y(' + distance + ')\n' +
- 'time.sleep(' + second + ')\n' +
- 'dog.move_y(0)\n' +
- // 'else:\n' +
- // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x31\' + bytes([distance]) + calculate_sum(ord(b\'\\x31\'),distance) + b\'\\x00\\xaa\')\n' +
- // ' time.sleep(' + second + ')\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x31\\x80\\x44\\x00\\xaa\')\n' +
- '';
- }
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_rotate_cycle'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_rotate)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.MOTION_ONDIRECTION_CLOCKWISE1, "0"],
- [Blockly.Msg.MOTION_ONDIRECTION_COUNTERCLOCKWISE1, "1"]
- ]), "direction");
- this.appendValueInput("rotate_speed")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_location);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_angle_speed);
- this.appendValueInput("second")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_2);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('direction');
- var TOOLTIPS = {
- '0': Blockly.Msg.robot_dog_servo_movement_rotate_cycle_TOOLTIP.replace('%1', Blockly.Msg.MOTION_ONDIRECTION_CLOCKWISE1),
- '1': Blockly.Msg.robot_dog_servo_movement_rotate_cycle_TOOLTIP.replace('%1', Blockly.Msg.MOTION_ONDIRECTION_COUNTERCLOCKWISE1)
- };
- return TOOLTIPS[mode];
- });
- },
- onchange: function (event) {
- var direction = this.getFieldValue('direction');
- if (event.blockId == this.id && event.name == 'direction' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
- if (direction == "0") {
- this.getChildren()[0].getField("NUM").setValue("60");
- }
- else {
- this.getChildren()[0].getField("NUM").setValue("-60");
- }
- }
- }
- }
- Blockly.Python['robot_dog_servo_movement_rotate_cycle'] = function (block) {
- var rotate_speed = Blockly.Python.valueToCode(block, 'rotate_speed', Blockly.Python.ORDER_ATOMIC);
- var second = Blockly.Python.valueToCode(block, 'second', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- code =
- // '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' +
- 'dog.turn(' + rotate_speed + ')\n' +
- 'time.sleep(' + second + ')\n' +
- 'dog.turn(0)\n' +
- // 'else:\n' +
- // ' rotate_speed = mapping(' + rotate_speed + ',-150,150,0,255)\n' +
- // ' 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' +
- // ' time.sleep(' + second + ')\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x32\\x80\\x43\\x00\\xaa\')\n' +
- '';
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_standing_still_cycle'] = {
- init: function () {
- this.appendValueInput("leg_lift_height")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_standing_still);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_angle)
- .appendField(Blockly.Msg.robot_dog_servo_movement_standing_still_1);
- this.appendValueInput("second")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_cycle_2);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- this.setTooltip(Blockly.Msg.robot_dog_servo_movement_standing_still_cycle_TOOLTIP);
- }
- }
- Blockly.Python['robot_dog_servo_movement_standing_still_cycle'] = function (block) {
- var leg_lift_height = Blockly.Python.valueToCode(block, 'leg_lift_height', Blockly.Python.ORDER_ATOMIC);
- var second = Blockly.Python.valueToCode(block, 'second', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- code =
- // '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' +
- 'dog.mark_time(' + leg_lift_height + ')\n' +
- 'time.sleep(' + second + ')\n' +
- 'dog.mark_time(0)\n' +
- // 'else:\n' +
- // ' leg_lift_height = mapping(' + leg_lift_height + ',10,35,1,255)\n' +
- // ' 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' +
- // ' time.sleep(' + second + ')\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3c\\x00\\xb9\\x00\\xaa\')\n' +
- '';
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_distance_still'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still)
- .appendField(new Blockly.FieldDropdown([
- ["X (-35mm~35mm)", "X"],
- ["Y (-18mm~18mm)", "Y"],
- ["Z (75mm~115mm)", "Z"]
- ]), "direction");
- this.appendValueInput("distance")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_angle);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('direction');
- var TOOLTIPS = {
- "X": Blockly.Msg.robot_dog_servo_movement_distance_still_TOOLTIP.replace('%1', "X"),
- "Y": Blockly.Msg.robot_dog_servo_movement_distance_still_TOOLTIP.replace('%1', "Y"),
- "Z": Blockly.Msg.robot_dog_servo_movement_distance_still_TOOLTIP.replace('%1', "Z")
- };
- return TOOLTIPS[mode];
- });
- },
- onchange: function (event) {
- var direction = this.getFieldValue('direction');
- if (event.blockId == this.id && event.name == 'direction' && event.oldValue != event.newValue && event.type === Blockly.Events.CHANGE) {
- if (direction == "X") {
- this.getChildren()[0].getField("NUM").setValue("0");
- }
- else if (direction == "Y") {
- this.getChildren()[0].getField("NUM").setValue("0");
- }
- else {
- this.getChildren()[0].getField("NUM").setValue("105");
- }
- }
- }
- }
- Blockly.Python['robot_dog_servo_movement_distance_still'] = function (block) {
- var direction = block.getFieldValue('direction');
- var distance = Blockly.Python.valueToCode(block, 'distance', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- if (direction == "X") {
- code =
- // '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' +
- 'dog.translation(\'x\', ' + distance + ')\n' +
- // 'else:\n' +
- // ' distance = mapping(' + distance + ',-35,35,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x33\' + bytes([distance]) + calculate_sum(ord(b\'\\x33\'),distance) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else if (direction == "Y") {
- code =
- // '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' +
- 'dog.translation(\'y\', ' + distance + ')\n' +
- // 'else:\n' +
- // ' distance = mapping(' + distance + ',-18,18,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x34\' + bytes([distance]) + calculate_sum(ord(b\'\\x34\'),distance) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.translation(\'z\', ' + distance + ')\n' +
- // 'else:\n' +
- // ' distance = mapping(' + distance + ',75,115,0,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x35\' + bytes([distance]) + calculate_sum(ord(b\'\\x35\'),distance) + b\'\\x00\\xaa\')\n' +
- '';
- }
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_distance_still_cycle'] = {
- init: function () {
- this.appendValueInput("cycle")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_1)
- .appendField(new Blockly.FieldDropdown([
- ["X", "X"],
- ["Y", "Y"],
- ["Z", "Z"]
- ]), "direction");
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_2);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('direction');
- var TOOLTIPS = {
- "X": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_TOOLTIP.replace('%1', "X"),
- "Y": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_TOOLTIP.replace('%1', "Y"),
- "Z": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_TOOLTIP.replace('%1', "Z")
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_servo_movement_distance_still_cycle'] = function (block) {
- var direction = block.getFieldValue('direction');
- var cycle = Blockly.Python.valueToCode(block, 'cycle', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- if (direction == "X") {
- code =
- // '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' +
- 'dog.periodic_tran(\'x\', ' + cycle + ')\n' +
- // 'else:\n' +
- // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x80\' + bytes([cycle]) + calculate_sum(ord(b\'\\x80\'),cycle) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else if (direction == "Y") {
- code =
- // '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' +
- 'dog.periodic_tran(\'y\', ' + cycle + ')\n' +
- // 'else:\n' +
- // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x81\' + bytes([cycle]) + calculate_sum(ord(b\'\\x81\'),cycle) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.periodic_tran(\'z\', ' + cycle + ')\n' +
- // 'else:\n' +
- // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x82\' + bytes([cycle]) + calculate_sum(ord(b\'\\x82\'),cycle) + b\'\\x00\\xaa\')\n' +
- '';
- }
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_distance_still_cycle_stop'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_stop)
- .appendField(new Blockly.FieldDropdown([
- ["X", "X"],
- ["Y", "Y"],
- ["Z", "Z"]
- ]), "direction");
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_2);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('direction');
- var TOOLTIPS = {
- "X": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_stop_TOOLTIP.replace('%1', "X"),
- "Y": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_stop_TOOLTIP.replace('%1', "Y"),
- "Z": Blockly.Msg.robot_dog_servo_movement_distance_still_cycle_stop_TOOLTIP.replace('%1', "Z")
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_servo_movement_distance_still_cycle_stop'] = function (block) {
- var direction = block.getFieldValue('direction');
- var code = '';
- if (direction == "X") {
- code =
- // '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' +
- 'dog.periodic_tran(\'x\', 0)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x80\\x00\' + calculate_sum(ord(b\'\\x80\'),0) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else if (direction == "Y") {
- code =
- // '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' +
- 'dog.periodic_tran(\'y\', 0)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x81\\x00\' + calculate_sum(ord(b\'\\x81\'),0) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.periodic_tran(\'z\', 0)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x82\\x00\' + calculate_sum(ord(b\'\\x82\'),0) + b\'\\x00\\xaa\')\n' +
- '';
- }
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_rotate_still'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still)
- .appendField(new Blockly.FieldDropdown([
- ["X (-15˚~15˚)", "X"],
- ["Y (-20˚~20˚)", "Y"],
- ["Z (-11˚~11˚)", "Z"]
- ]), "direction");
- this.appendValueInput("rotate_speed")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still1);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_ag);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('direction');
- var TOOLTIPS = {
- "X": Blockly.Msg.robot_dog_servo_movement_rotate_still_TOOLTIP.replace('%1', "X"),
- "Y": Blockly.Msg.robot_dog_servo_movement_rotate_still_TOOLTIP.replace('%1', "Y"),
- "Z": Blockly.Msg.robot_dog_servo_movement_rotate_still_TOOLTIP.replace('%1', "Z")
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_servo_movement_rotate_still'] = function (block) {
- var direction = block.getFieldValue('direction');
- var rotate_speed = Blockly.Python.valueToCode(block, 'rotate_speed', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- if (direction == "X") {
- code =
- // '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' +
- 'dog.attitude(\'r\', ' + rotate_speed + ')\n' +
- // 'else:\n' +
- // ' rotate_speed = mapping(' + rotate_speed + ',-15,15,0,255)\n' +
- // ' 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' +
- '';
- }
- else if (direction == "Y") {
- code =
- // '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' +
- 'dog.attitude(\'p\', ' + rotate_speed + ')\n' +
- // 'else:\n' +
- // ' rotate_speed = mapping(' + rotate_speed + ',-20,20,0,255)\n' +
- // ' 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' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.attitude(\'y\', ' + rotate_speed + ')\n' +
- // 'else:\n' +
- // ' rotate_speed = mapping(' + rotate_speed + ',-11,11,0,255)\n' +
- // ' 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' +
- '';
- }
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_rotate_still_cycle'] = {
- init: function () {
- this.appendValueInput("cycle")
- .setCheck(null)
- .appendField(Blockly.Msg.robot_dog_servo_movement_distance_still_cycle);
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle)
- .appendField(new Blockly.FieldDropdown([
- ["X", "X"],
- ["Y", "Y"],
- ["Z", "Z"]
- ]), "direction");
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_1);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('direction');
- var TOOLTIPS = {
- "X": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_TOOLTIP.replace('%1', "X"),
- "Y": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_TOOLTIP.replace('%1', "Y"),
- "Z": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_TOOLTIP.replace('%1', "Z")
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_servo_movement_rotate_still_cycle'] = function (block) {
- var direction = block.getFieldValue('direction');
- var cycle = Blockly.Python.valueToCode(block, 'cycle', Blockly.Python.ORDER_ATOMIC);
- var code = '';
- if (direction == "X") {
- code =
- // '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' +
- 'dog.periodic_rot(\'r\', ' + cycle + ')\n' +
- // 'else:\n' +
- // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3a\' + bytes([cycle]) + calculate_sum(ord(b\'\\x3a\'),cycle) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else if (direction == "Y") {
- code =
- // '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' +
- 'dog.periodic_rot(\'p\', ' + cycle + ')\n' +
- // 'else:\n' +
- // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x39\' + bytes([cycle]) + calculate_sum(ord(b\'\\x39\'),cycle) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.periodic_rot(\'y\', ' + cycle + ')\n' +
- // 'else:\n' +
- // ' cycle = mapping(' + cycle + ',1.5,8,1,255)\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3b\' + bytes([cycle]) + calculate_sum(ord(b\'\\x3b\'),cycle) + b\'\\x00\\xaa\')\n' +
- '';
- }
- return code;
- }
- Blockly.Blocks['robot_dog_servo_movement_rotate_still_cycle_stop'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_stop)
- .appendField(new Blockly.FieldDropdown([
- ["X", "X"],
- ["Y", "Y"],
- ["Z", "Z"]
- ]), "direction");
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_1);
- this.setInputsInline(true);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('direction');
- var TOOLTIPS = {
- "X": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_stop_TOOLTIP.replace('%1', "X"),
- "Y": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_stop_TOOLTIP.replace('%1', "Y"),
- "Z": Blockly.Msg.robot_dog_servo_movement_rotate_still_cycle_stop_TOOLTIP.replace('%1', "Z")
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_servo_movement_rotate_still_cycle_stop'] = function (block) {
- var direction = block.getFieldValue('direction');
- var code = '';
- if (direction == "X") {
- code =
- // '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' +
- 'dog.periodic_rot(\'r\', 0)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3a\\x00\' + calculate_sum(ord(b\'\\x3a\'),0) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else if (direction == "Y") {
- code =
- // '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' +
- 'dog.periodic_rot(\'p\', 0)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x39\\x00\' + calculate_sum(ord(b\'\\x39\'),0) + b\'\\x00\\xaa\')\n' +
- '';
- }
- else {
- code =
- // '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' +
- 'dog.periodic_rot(\'y\', 0)\n' +
- // 'else:\n' +
- // ' robot_dog_setup_uart.write(b\'\\x55\\x00\\x09\\x01\\x3b\\x00\' + calculate_sum(ord(b\'\\x3b\'),0) + b\'\\x00\\xaa\')\n' +
- '';
- }
- return code;
- }
- Blockly.Blocks['robot_dog_get_servo_angle'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.COCOCLOUD_TIME_GETDATE)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_front_left_leg, "Front left"],
- [Blockly.Msg.robot_dog_back_left_leg, "Back left"],
- [Blockly.Msg.robot_dog_front_right_leg, "Right front"],
- [Blockly.Msg.robot_dog_back_right_leg, "Right back"]
- ]), "leg");
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_leg)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.robot_dog_up_1, "up"],
- [Blockly.Msg.robot_dog_middle_1, "middle"],
- [Blockly.Msg.robot_dog_down_1, "down"]
- ]), "joint");
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_get_servo_angle);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#5c42bd");
- this.setTooltip(Blockly.Msg.robot_dog_get_servo_angle_TOOLTIP);
- this.setHelpUrl("");
- }
- }
- Blockly.Python['robot_dog_get_servo_angle'] = function (block) {
- var leg = block.getFieldValue('leg');
- var joint = block.getFieldValue('joint');
- var code = '';
- Blockly.Python.definitions_['import_robot_dog_get_servo_angle'] = '' +
- // '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' +
- 'def robot_dog_get_servo_angle(index):\n' +
- ' global dog\n' +
- ' xgoAngle = dog.read_motor()\n' +
- ' return xgoAngle[index]\n' +
- // 'else:\n' +
- // ' def robot_dog_get_servo_angle(index):\n' +
- // ' return None\n' +
- '\n';
- if (leg == "Front left") {
- if (joint == "up") {
- code = 'robot_dog_get_servo_angle(2)';
- }
- else if (joint == "middle") {
- code = 'robot_dog_get_servo_angle(1)';
- }
- else {
- code = 'robot_dog_get_servo_angle(0)';
- }
- }
- else if (leg == "Back left") {
- if (joint == "up") {
- code = 'robot_dog_get_servo_angle(11)';
- }
- else if (joint == "middle") {
- code = 'robot_dog_get_servo_angle(10)';
- }
- else {
- code = 'robot_dog_get_servo_angle(9)';
- }
- }
- else if (leg == "Right front") {
- if (joint == "up") {
- code = 'robot_dog_get_servo_angle(5)';
- }
- else if (joint == "middle") {
- code = 'robot_dog_get_servo_angle(4)';
- }
- else {
- code = 'robot_dog_get_servo_angle(3)';
- }
- }
- else {
- if (joint == "up") {
- code = 'robot_dog_get_servo_angle(8)';
- }
- else if (joint == "middle") {
- code = 'robot_dog_get_servo_angle(7)';
- }
- else {
- code = 'robot_dog_get_servo_angle(6)';
- }
- }
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- }
- Blockly.Blocks['robot_dog_get_rotate_angle'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_get_rotate_angle_1)
- .appendField(new Blockly.FieldDropdown([
- ["X", "X"],
- ["Y", "Y"],
- ["Z", "Z"]
- ]), "direction");
- this.appendDummyInput()
- .appendField(Blockly.Msg.robot_dog_get_rotate_angle);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour("#5c42bd");
- this.setHelpUrl("");
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('direction');
- var TOOLTIPS = {
- "X": Blockly.Msg.robot_dog_get_rotate_angle_TOOLTIP.replace('%1', "X"),
- "Y": Blockly.Msg.robot_dog_get_rotate_angle_TOOLTIP.replace('%1', "Y"),
- "Z": Blockly.Msg.robot_dog_get_rotate_angle_TOOLTIP.replace('%1', "Z")
- };
- return TOOLTIPS[mode];
- });
- }
- }
- Blockly.Python['robot_dog_get_rotate_angle'] = function (block) {
- var direction = block.getFieldValue('direction');
- Blockly.Python.definitions_['import_robot_dog_get_rotate_angle'] = '' +
- // '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' +
- 'def robot_dog_get_rotate_angle(index):\n' +
- ' global dog\n' +
- ' if index == 0:\n' +
- ' return dog.read_roll()\n' +
- ' elif index == 1:\n' +
- ' return dog.read_pitch()\n' +
- ' else:\n' +
- ' return dog.read_yaw()\n' +
- // 'else:\n' +
- // ' def robot_dog_get_rotate_angle(index):\n' +
- // ' return None\n' +
- '\n';
- var code = '';
- if (direction == "X") {
- code = 'robot_dog_get_rotate_angle(0)';
- }
- else if (direction == "Y") {
- code = 'robot_dog_get_rotate_angle(1)';
- }
- else {
- code = 'robot_dog_get_rotate_angle(2)';
- }
- return [code, Blockly.Python.ORDER_CONDITIONAL];
- }
- /*
- __ ___ _____ _
- \ \ / (_) ___(_)
- \ \ /\ / /| | |_ | |
- \ V V / | | _| | |
- \_/\_/ |_|_| |_|
-
- */
- var K210_WIFI_COLOR = "#3062c1";
- Blockly.Blocks['k210_wifi_enable_disable'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_set_wifi)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_wifi_enabled, "1"],
- [Blockly.Msg.ai_wifi_disabled, "0"]
- ]), "de");
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(K210_WIFI_COLOR);
- var thisBlock = this;
- this.setTooltip(function () {
- var mode = thisBlock.getFieldValue('de');
- var TOOLTIPS = {
- '1': Blockly.Msg.k210_wifi_enable_disable_TOOLTIP.replace('%1', Blockly.Msg.ai_wifi_enabled),
- '0': Blockly.Msg.k210_wifi_enable_disable_TOOLTIP.replace('%1', Blockly.Msg.ai_wifi_disabled)
- };
- return TOOLTIPS[mode];
- });
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_enable_disable'] = function (block) {
- var dropdown_de = block.getFieldValue('de');
- // Blockly.Python.addVariable('k210_wifi_enable_disable', '', true);
- Blockly.Python.definitions_['k210_wifi_enable_disable_def'] = '' +
- 'from machine import UART\n' +
- 'import network, time\n' +
- 'from machine import UART\n' +
- 'from Maix import GPIO\n' +
- 'from fpioa_manager import fm, board_info\n' +
- '\n' +
- '# En SEP8285\n' +
- 'fm.register(8, fm.fpioa.GPIOHS0, force=True)\n' +
- '_k210_wifi_en=GPIO(GPIO.GPIOHS0, GPIO.OUT)\n' +
- '\n' +
- 'fm.register(0, fm.fpioa.GPIOHS1, force=True)\n' +
- '_k210wifi_io0_en=GPIO(GPIO.GPIOHS1, GPIO.OUT)\n' +
- '_k210wifi_io0_en.value(0)\n' +
- '\n' +
- 'fm.register(board_info.WIFI_RX, fm.fpioa.UART2_TX, force=True)\n' +
- 'fm.register(board_info.WIFI_TX, fm.fpioa.UART2_RX, force=True)\n' +
- '\n' +
- '_k210_uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)\n' +
- '\n' +
- 'def _k210_wifi_enable(en):\n' +
- ' global _k210_wifi_en\n' +
- ' _k210_wifi_en.value(en)\n' +
- '';
- // TODO: Assemble Python into code variable.
- if (dropdown_de == "1") {
- var code = '_k210_wifi_enable(' + dropdown_de + ')\n' +
- 'time.sleep(2)\n' +
- '_k210_wifi = network.ESP8285(_k210_uart)\n' +
- '\n';
- } else if (dropdown_de == "0") {
- var code = '_k210_wifi_enable(' + dropdown_de + ')\n' +
- '\n';
- }
- return code;
- };
- Blockly.Blocks['k210_wifi_scan'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/wifi_scan.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_start_wifi_scan);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_scan_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_scan'] = function (block) {
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['k210_wifi_scan_def'] = '' +
- 'def wifi_deal_ap_info(info):\n' +
- ' res = []\n' +
- ' for ap_str in info:\n' +
- ' ap_str = ap_str.split(",")\n' +
- ' info_one = []\n' +
- ' for node in ap_str:\n' +
- ' if node.startswith(\'\"\'):\n' +
- ' info_one.append(node[1:-1])\n' +
- ' else:\n' +
- ' info_one.append(int(node))\n' +
- ' res.append(info_one)\n' +
- ' return res\n' +
- '';
- var code = '' +
- '_k210_ap_scan_info = _k210_wifi.scan()\n' +
- '_k210_ap_scan_info = wifi_deal_ap_info(_k210_ap_scan_info)\n' +
- '_k210_ap_scan_info.sort(key=lambda x:x[2], reverse=True) # sort by rssi\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['k210_wifi_scan_get'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_wifi_scan_result);
- this.setOutput(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_scan_get_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_scan_get'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_k210_ap_scan_info';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['k210_wifi_connect_hotspot'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/wifi_setup.png", 38, 38, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_connect_wifi);
- this.appendValueInput("ssid")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_wifi_connect_wifi_ssid);
- this.appendValueInput("pass")
- .setCheck(null)
- .appendField(Blockly.Msg.ai_wifi_connect_wifi_pass);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_connect_hotspot_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_connect_hotspot'] = function (block) {
- var value_ssid = Blockly.Python.valueToCode(block, 'ssid', Blockly.Python.ORDER_ATOMIC);
- var value_pass = Blockly.Python.valueToCode(block, 'pass', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- Blockly.Python.definitions_['k210_wifi_connect_hotspot_def'] = '' +
- 'def _k210_wifi_reset():\n' +
- ' global _k210_uart\n' +
- ' _k210_wifi_enable(0)\n' +
- ' time.sleep_ms(200)\n' +
- ' _k210_wifi_enable(1)\n' +
- ' time.sleep(2)\n' +
- ' _k210_uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)\n' +
- ' tmp = _k210_uart.read()\n' +
- ' _k210_uart.write("AT+UART_CUR=921600,8,1,0,0\\r\\n")\n' +
- ' print(_k210_uart.read())\n' +
- ' _k210_uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=10240)\n' +
- ' _k210_uart.write("AT\\r\\n")\n' +
- ' tmp = _k210_uart.read()\n' +
- ' print(tmp)\n' +
- ' if not tmp.endswith("OK\\r\\n"):\n' +
- ' print("reset fail")\n' +
- ' return None\n' +
- ' try:\n' +
- ' _k210_wifi = network.ESP8285(_k210_uart)\n' +
- ' except Exception:\n' +
- ' return None\n' +
- ' return _k210_wifi\n' +
- '';
- var code = '' +
- '_k210_wifi = _k210_wifi_reset()\n' +
- 'if not _k210_wifi:\n' +
- ' raise Exception("WiFi init fail")\n' +
- '\n' +
- '_k210_wifi.connect(' + value_ssid + ',' + value_pass + ')\n' +
- '\n';
- return code;
- };
- Blockly.Blocks['k210_wifi_get_connection_status'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_get_connection_status);
- this.setOutput(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_get_connection_status_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_get_connection_status'] = function (block) {
- // TODO: Assemble Python into code variable.
- var code = '_k210_wifi.isconnected()';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['k210_wifi_get_config'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.ai_wifi_get_wifi_info)
- .appendField(new Blockly.FieldDropdown([
- [Blockly.Msg.ai_wifi_get_wifi_info_name, "6"],
- [Blockly.Msg.ai_wifi_get_wifi_info_ip, "0"],
- [Blockly.Msg.ai_wifi_get_wifi_info_dns, "2"],
- [Blockly.Msg.ai_wifi_get_wifi_info_mac, "5"]
- ]), "type");
- this.setOutput(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip(Blockly.Msg.k210_wifi_get_config_TOOLTIP);
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_get_config'] = function (block) {
- var dropdown_type = block.getFieldValue('type');
- // TODO: Assemble Python into code variable.
- var code = '_k210_wifi.ifconfig()[' + dropdown_type + ']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
- Blockly.Blocks['k210_wifi_cococloud'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/cococloud_send.png", 180, 65, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_send_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_send_title_api)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "t_api");
- /*this.appendValueInput("first_input")
- .setCheck(null)
- .appendField("Field 0");*/
- this.itemCount_ = 1;
- this.updateShape_();
- this.setMutator(new Blockly.Mutator(['k210_wifi_cococloud_create_with_item']));
- this.setColour(K210_WIFI_COLOR);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setTooltip("");
- this.setHelpUrl("");
- },
- mutationToDom: function () {
- var container = document.createElement('mutation');
- container.setAttribute('items', this.itemCount_);
- return container;
- },
- domToMutation: function (xmlElement) {
- this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
- this.updateShape_();
- },
- decompose: function (workspace) {
- var containerBlock = workspace.newBlock('k210_wifi_cococloud_create_with_container');
- containerBlock.initSvg();
- var connection = containerBlock.getInput('STACK').connection;
- for (var i = 0; i < this.itemCount_; i++) {
- var itemBlock = workspace.newBlock('k210_wifi_cococloud_create_with_item');
- itemBlock.initSvg();
- connection.connect(itemBlock.previousConnection);
- connection = itemBlock.nextConnection;
- }
- return containerBlock;
- },
- compose: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- // Count number of inputs.
- var connections = [];
- while (itemBlock) {
- connections.push(itemBlock.valueConnection_);
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- // Disconnect any children that don't belong.
- for (var i = 0; i < this.itemCount_; i++) {
- var connection = this.getInput('ADD' + i).connection.targetConnection;
- if (connection && connections.indexOf(connection) == -1) {
- connection.disconnect();
- }
- }
- this.itemCount_ = connections.length;
- this.updateShape_();
- // Reconnect any child blocks.
- for (var i = 0; i < this.itemCount_; i++) {
- Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
- }
- },
- saveConnections: function (containerBlock) {
- var itemBlock = containerBlock.getInputTargetBlock('STACK');
- var i = 0;
- while (itemBlock) {
- var input = this.getInput('ADD' + i);
- itemBlock.valueConnection_ = input && input.connection.targetConnection;
- i++;
- itemBlock = itemBlock.nextConnection &&
- itemBlock.nextConnection.targetBlock();
- }
- },
- updateShape_: function () {
- for (var i = 0; i < this.itemCount_; i++) {
- if (!this.getInput('ADD' + i)) {
- var input = this.appendValueInput('ADD' + i);
- input.appendField(Blockly.Msg.iotservice_cococloud_send_property)
- .appendField(new Blockly.FieldTextInput("Property" + i), "field" + i);
- //input.appendField("資料 " + (i + 1) + ":");
- //input.appendField(new Blockly.FieldLabelSerializable("field" + (i + 1)), 'FIELD' + i);
- // input.appendField(new Blockly.FieldTextInput("資料" + (i + 1)), 'FIELD' + i)
- }
- }
- while (this.getInput('ADD' + i)) {
- this.removeInput('ADD' + i);
- i++;
- }
- },
- };
- Blockly.Blocks['k210_wifi_cococloud_create_with_container'] = {
- init: function () {
- this.setColour(K210_WIFI_COLOR);
- this.appendDummyInput()
- .appendField("Items");
- this.appendStatementInput('STACK');
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Blocks['k210_wifi_cococloud_create_with_item'] = {
- init: function () {
- this.setColour(K210_WIFI_COLOR);
- this.appendDummyInput()
- .appendField("Field");
- this.setPreviousStatement(true);
- this.setNextStatement(true);
- this.setTooltip('');
- this.contextMenu = false;
- }
- };
- Blockly.Python['k210_wifi_cococloud'] = function (block) {
- var api = block.getFieldValue('t_api');
- Blockly.Python.addVariable('_COCOCLOUD_SEND_REQUEST', 'None', true);
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests\n' +
- 'import ujson as json\n'
- '';
- // var key = Blockly.Arduino.valueToCode(block, 'KEY', Blockly.Arduino.ORDER_ATOMIC).replace(/\"/g, '') || "";
- var item_field = '',
- item_value = '';
- var cococloud_data = "'{";
- for (var n = 0; n < this.itemCount_; n++) {
- item_field = block.getFieldValue("field" + n);
- item_value = Blockly.Python.valueToCode(this, 'ADD' + n, Blockly.Python.ORDER_NONE) || '';
- cococloud_data += '"' + item_field + '":"\' + str(' + item_value + ') + \'",';
- }
- var cococloud_data_final = cococloud_data + "}'";
- var code = '' +
- '_COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKey/"+ "' + api + '"\n' +
- '_COCOCLOUD_SEND_DATA = ' + cococloud_data_final.slice(0, -3) + '}\'' + '\n' +
- 'try:\n' +
- ' _COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 30)\n' +
- 'except BaseException as e:\n' +
- ' print(e)\n' +
- 'pass\n' +
- '_COCOCLOUD_SEND_REQUEST = urequests.post(_COCOCLOUD_SEND_ENDPOINT, data = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 30)\n'
- '';
- // cococloud_data_final.slice(0, -3) + '}\''
- return code;
- };
- Blockly.Blocks['k210_wifi_cococloud_read'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(new Blockly.FieldImage("blockly/media/cococloud_get.png", 200, 70, { alt: "*", flipRtl: "FALSE" }));
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_title);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_api)
- .appendField(new Blockly.FieldTextInput("ENTER_YOUR_EVENT_API_KEY"), "api");
- this.setInputsInline(false);
- this.setPreviousStatement(true, null);
- this.setNextStatement(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_cococloud_read'] = function (block) {
- var api = block.getFieldValue('api');
- Blockly.Python.addVariable('_COCOCLOUD_READ_REQUEST', 'None', true);
- Blockly.Python.definitions_['import_urequests'] = '' +
- 'import urequests\n' +
- 'import ujson as json\n'
- '';
- // TODO: Assemble Python into code variable.
- var code = '_COCOCLOUD_READ_REQUEST = urequests.get("http://api.cocorobo.cn/iot/data/eventAPIKey/" + "' + api + '", timeout=60)\n';
- return code;
- };
- Blockly.Blocks['k210_wifi_cococloud_read_data'] = {
- init: function () {
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_property);
- this.appendValueInput("property")
- .setCheck(null);
- this.appendDummyInput()
- .appendField(Blockly.Msg.iotservice_cococloud_get_data);
- this.setInputsInline(true);
- this.setOutput(true, null);
- this.setColour(K210_WIFI_COLOR);
- this.setTooltip("");
- this.setHelpUrl("");
- }
- };
- Blockly.Python['k210_wifi_cococloud_read_data'] = function (block) {
- var property = Blockly.Python.valueToCode(block, 'property', Blockly.Python.ORDER_ATOMIC);
- // TODO: Assemble Python into code variable.
- var code = '_COCOCLOUD_READ_REQUEST.json()[\'data\'][0][' + property + ']';
- // TODO: Change ORDER_NONE to the correct strength.
- return [code, Blockly.Python.ORDER_NONE];
- };
|