-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockly-turtlebot.min.js
1 lines (1 loc) · 7.98 KB
/
blockly-turtlebot.min.js
1
var blockly_mario;!function(){function a(a,b,c){return Blockly.JavaScript.valueToCode(a,b,c)}Blockly.Language.agent_colorAt={init:function(){this.setColour(230),this.appendDummyInput().appendTitle(new Blockly.FieldDropdown([["red","RED"],["green","GREEN"],["blue","BLUE"]]),"CHANNEL"),this.appendValueInput("X").setCheck(Number).appendTitle("at x"),this.appendValueInput("Y").setCheck(Number).appendTitle("y"),this.setInputsInline(!0),this.setOutput(!0,Number),this.setTooltip("Get the red, green, or blue color value for the given pixel.\nColors are between 0 and 255.\nX coordinates are between 1 and 320, y between 1 and 240,\ncounting from the lower-left corner.")}},Blockly.Language.agent_drive={init:function(){this.setColour(290),this.appendValueInput("AMOUNT").setCheck(Number).appendTitle("drive"),this.setPreviousStatement(!0),this.setNextStatement(!0),this.setTooltip("Positive drives forward, and negative back.\nUnits are in meters / second, with a max magnitude of 0.2 m/s.")}},Blockly.Language.agent_turn={init:function(){this.setColour(290),this.appendValueInput("AMOUNT").setCheck(Number).appendTitle("turn"),this.setPreviousStatement(!0),this.setNextStatement(!0),this.setTooltip("Positive turns right, and negative left.\nUnits are in degrees / second, with a max magnitude of 30 deg/s.")}},Blockly.JavaScript.agent_colorAt=function(){var b=this.getTitleValue("CHANNEL"),c=a(this,"X",Blockly.JavaScript.ORDER_COMMA),d=a(this,"Y",Blockly.JavaScript.ORDER_COMMA),e="$$support.colorAt("+['"'+b+'"',c,d].join(", ")+")";return[e,Blockly.JavaScript.ORDER_MEMBER]},Blockly.JavaScript.agent_drive=function(){var b=a(this,"AMOUNT",Blockly.JavaScript.ORDER_ASSIGNMENT),c=["$$actions.drive = ",b,";\n"].join("");return c},Blockly.JavaScript.agent_turn=function(){var b=a(this,"AMOUNT",Blockly.JavaScript.ORDER_ASSIGNMENT),c=["$$actions.turn = ",b,";\n"].join("");return c},Blockly.JavaScript.text_print=function(){var b=a(this,"TEXT",Blockly.JavaScript.ORDER_NONE)||'""';return"blockly_turtlebot.log("+b+");\n"}}(blockly_mario||(blockly_mario={}));var blockly_turtlebot;!function(a){var b={BLUE:2,GREEN:1,OPACITY:3,RED:0},c={x:320,y:240},d=function(){function a(){}return a.prototype.colorAt=function(a,d,e){if(1>d||c.x<d||1>e||c.y<e)return null;d--,e=c.y-e;var f=4*(c.x*e+d)+b[a];return this.pixels[f]},a}();a.Support=d}(blockly_turtlebot||(blockly_turtlebot={}));var blockly_turtlebot;!function(blockly_turtlebot){function log(a){var b=$("console"),c=String(a).replace(/&/g,"&").replace(/</g,"<");if(c===lastMessage){var d=b.lastChild,e=d.firstChild;e instanceof Element||(d.innerHTML='<span class="log-count">1</span>'+d.innerHTML,e=d.firstChild);var f=Number(e.innerHTML);e.innerHTML=String(f+1)}else lastMessage=c,b.innerHTML+="<div>"+c+"</div>";for(;b.childNodes.length>100;)b.removeChild(b.firstChild);b.scrollTop=b.scrollHeight}function $(a){return document.getElementById(a)}function $a(a){return $(a)}function $any(a){return $(a)}function $img(a){return $(a)}function $input(a){return $(a)}function buildVideoBuffer(){var a=document.createElement("canvas");return a.width=320,a.height=240,a}function defineFinishCode(a){return function(b){return a(["var $$actions;","return function() {","$$actions = {};",b,"return $$actions;","};"].join("\n"))}}function getRobotHostname(){var a=storageKey("robot.hostname"),b=localStorage.getItem(a);return b||(b=document.location.hostname||"localhost",localStorage.setItem(a,b)),b}function handleFileChosen(a){var b=a.target.files;if(b.length){var c=new FileReader;c.onload=function(a){loadBlocksXml(a.target.result,loadClears)},c.readAsText(b[0])}}function handleExport(a){Blockly.selected||(alert("You must first select a block to export."),a.preventDefault())}function handleImport(){loadClears=!1,$("file-chooser").click()}function handleOpen(){loadClears=!0,$("file-chooser").click()}function handlePause(){paused=$input("pause").checked?!0:!1}function hasFocus(){return document.activeElement==$("display")}function keyDown(a){var b=keyNames[a.keyCode];if(b&&(keysDown[b]=!0,hasFocus())){switch(b){case"enter":var c=$input("ai");c.checked=!c.checked;break;case"space":var d=$input("pause");d.checked=!d.checked,handlePause();break;default:return}a.preventDefault()}}function keyUp(a){var b=keyNames[a.keyCode];b&&(keysDown[b]=!1)}function limitValue(a,b){return a=a||0,-b>a?a=-b:a>b&&(a=b),a}function loadBlocksXml(a,b){try{var c=Blockly.Xml.textToDom(a);b&&Blockly.mainWorkspace.clear(),Blockly.Xml.domToWorkspace(Blockly.mainWorkspace,c)}catch(d){throw alert("Failed to open Blockly program."),d}}function preload(){ros.on("error",function(a){console.log(a)}),ros.on("connection",function(){console.log("Connected!")});var a="ws://"+getRobotHostname()+":9090";console.log("Connecting to '"+a+"'."),ros.connect(a);for(var b in keyCodes){var c=keyCodes[b];keyNames[c]=b,keysDown[b]=!1}}function redefine(a,b,c){a[b]=c(a[b])}function selectionChanged(){var a="#";if(Blockly.selected){var b=Blockly.Xml.blockToDom_(Blockly.selected),c="<xml>"+Blockly.Xml.domToText(b)+"</xml>";a="data:text/plain,"+encodeURIComponent(c)}$a("export").href=a}function storageKey(a){return window.location.href.split("#")[0]+"#"+a}function updateCode(){var code=Blockly.Generator.workspaceToCode("JavaScript");code=["(function($$support) {",code,"})"].join("\n");try{aiFunction=eval(code)(support),$input("ai").disabled=!1,$input("update").disabled=!0}catch(e){throw alert("Error building code."),aiFunction=null,$input("ai").checked=!1,$input("ai").disabled=!0,e}}function updateControl(){var a={linear:{x:0,y:0,z:0},angular:{x:0,y:0,z:0}},b=$input("ai").checked&&Boolean(aiFunction);if(b){var c=aiFunction(),d=limitValue(c.drive,.2),e=limitValue(c.turn,30);e=-e*Math.PI/180,a.linear.x=d,a.angular.z=e}else keysDown.up?a.linear.x=1:keysDown.down&&(a.linear.x=-1),a.linear.x*=.2,keysDown.left?a.angular.z=1:keysDown.right&&(a.angular.z=-1),a.angular.z*=.5;cmdVel.publish(new ROSLIB.Message(a))}function updateImageData(){var a=$img("display"),b=videoBuffer.getContext("2d");b.drawImage(a,0,0);var c=videoBuffer.width,d=videoBuffer.height;support.pixels=b.getImageData(0,0,c,d).data,a.src=a.src.replace(/&i=.*/,"&i="+Math.random())}function updateRobot(){paused||(updateImageData(),updateControl())}function windowResized(){var a=$("console");a.style.height=$("app").clientHeight-a.offsetTop-12+"px"}function workspaceChanged(){$("update").disabled=!1;var a=Blockly.Xml.domToText(Blockly.Xml.workspaceToDom(Blockly.mainWorkspace));window.localStorage.setItem(storageKey("blocks"),a),$a("save").href="data:text/plain,"+encodeURIComponent(a)}var aiFunction,loadClears=!1,paused=!1,support=new blockly_turtlebot.Support,ros=new ROSLIB.Ros,videoBuffer=buildVideoBuffer(),lastMessage=null;blockly_turtlebot.log=log;var cmdVel=new ROSLIB.Topic({ros:ros,name:"/cmd_vel",messageType:"geometry_msgs/Twist"}),rgbd,keyCodes={down:40,enter:13,left:37,right:39,space:32,up:38},keyNames={},keysDown={};preload(),window.addEventListener("load",function(){Blockly.inject($("blockly"),{path:"../blockly/",toolbox:$("toolbox")}),redefine(Blockly.JavaScript,"finish",defineFinishCode),Blockly.addChangeListener(workspaceChanged),$img("display").src=["http://",getRobotHostname(),":8081/snapshot","?topic=/camera/rgb/image_raw?quality=50?width=320?height=240&i=0"].join(""),document.addEventListener("keydown",keyDown),document.addEventListener("keyup",keyUp),$("ai").onclick=function(){$input("ai").checked||$("display").focus()},["ai","pause"].forEach(function(a){$input(a).checked=!1}),$("pause").onclick=handlePause,$("update").onclick=updateCode,$("import").addEventListener("click",handleImport,!1),$("open").addEventListener("click",handleOpen,!1),$("file-chooser").addEventListener("change",handleFileChosen,!1),Blockly.bindEvent_(Blockly.mainWorkspace.getCanvas(),"blocklySelectChange",null,selectionChanged),$("export").addEventListener("click",handleExport,!1),window.addEventListener("resize",windowResized,!1),windowResized();var a=localStorage.getItem(storageKey("blocks"));a&&loadBlocksXml(a,!0),setInterval(updateRobot,100),setTimeout(function(){updateCode()},0),$("display").focus()})}(blockly_turtlebot||(blockly_turtlebot={}));