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]; };