@@ -59,7 +59,26 @@ const g_shape_values = {
59
59
*/
60
60
61
61
// 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
+ } ;
63
82
Blockly . Blocks [ 'niryo_one_move_joints' ] = {
64
83
init : function ( ) {
65
84
this . appendDummyInput ( ) . appendField ( 'Move Joints' ) ;
@@ -861,7 +880,34 @@ Blockly.Blocks['niryo_one_conveyor_stop'] = {
861
880
* Generators
862
881
*/
863
882
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
+ } ;
865
911
866
912
Blockly . Python [ 'niryo_one_move_joints' ] = function ( block ) {
867
913
var number_joints_1 = block . getFieldValue ( 'JOINTS_1' ) ;
@@ -1921,6 +1967,10 @@ const TOOLBOX = {
1921
1967
colour : '210' ,
1922
1968
name : 'Niryo' ,
1923
1969
contents : [
1970
+ {
1971
+ kind : 'BLOCK' ,
1972
+ type : 'niryo_one_connect'
1973
+ } ,
1924
1974
{
1925
1975
kind : 'BLOCK' ,
1926
1976
type : 'niryo_one_move_joints'
0 commit comments