@@ -59,7 +59,26 @@ const g_shape_values = {
5959 */
6060
6161// Movement
62-
62+ Blockly . Blocks [ 'niryo_one_connect' ] = {
63+ init : function ( ) {
64+ this . appendDummyInput ( ) . appendField ( 'IP Address' ) ;
65+ this . appendDummyInput ( )
66+ . appendField ( new Blockly . FieldNumber ( 10 , 0 , 255 , 0 ) , 'ip_0' )
67+ . appendField ( '.' )
68+ . appendField ( new Blockly . FieldNumber ( 10 , 0 , 255 , 0 ) , 'ip_1' )
69+ . appendField ( '.' )
70+ . appendField ( new Blockly . FieldNumber ( 10 , 0 , 255 , 0 ) , 'ip_2' )
71+ . appendField ( '.' )
72+ . appendField ( new Blockly . FieldNumber ( 10 , 0 , 255 , 0 ) , 'ip_3' ) ;
73+ this . appendStatementInput ( 'DO' ) ;
74+ this . setInputsInline ( true ) ;
75+ this . setPreviousStatement ( false , null ) ;
76+ this . setNextStatement ( false , null ) ;
77+ this . setColour ( function_color ) ;
78+ this . setTooltip ( 'Connect to the robot and disconnects after the execution' ) ;
79+ this . setHelpUrl ( '' ) ;
80+ }
81+ } ;
6382Blockly . Blocks [ 'niryo_one_move_joints' ] = {
6483 init : function ( ) {
6584 this . appendDummyInput ( ) . appendField ( 'Move Joints' ) ;
@@ -861,7 +880,34 @@ Blockly.Blocks['niryo_one_conveyor_stop'] = {
861880 * Generators
862881 */
863882
864- // Movement
883+ const connexion = `
884+ from contextlib import contextmanager
885+ from pyniryo import *
886+
887+ @contextmanager
888+ def niryo_connect(ip):
889+ n = NiryoRobot(ip)
890+ try:
891+ yield n
892+ except:
893+ n.close_connection()
894+ raise
895+ else:
896+ n.close_connection()
897+ ` ;
898+
899+ Blockly . Python [ 'niryo_one_connect' ] = function ( block ) {
900+ var ip_0 = block . getFieldValue ( 'ip_0' ) ;
901+ var ip_1 = block . getFieldValue ( 'ip_1' ) ;
902+ var ip_2 = block . getFieldValue ( 'ip_2' ) ;
903+ var ip_3 = block . getFieldValue ( 'ip_3' ) ;
904+
905+ let branch = Blockly . Python . statementToCode ( block , 'DO' ) ;
906+ var ip = ip_0 + '.' + ip_1 + '.' + ip_2 + '.' + ip_3 ;
907+
908+ var code = connexion + '\nwith niryo_connect("' + ip + '") as n:\n' + branch ;
909+ return code ;
910+ } ;
865911
866912Blockly . Python [ 'niryo_one_move_joints' ] = function ( block ) {
867913 var number_joints_1 = block . getFieldValue ( 'JOINTS_1' ) ;
@@ -1921,6 +1967,10 @@ const TOOLBOX = {
19211967 colour : '210' ,
19221968 name : 'Niryo' ,
19231969 contents : [
1970+ {
1971+ kind : 'BLOCK' ,
1972+ type : 'niryo_one_connect'
1973+ } ,
19241974 {
19251975 kind : 'BLOCK' ,
19261976 type : 'niryo_one_move_joints'
0 commit comments