Skip to content

Commit 2994db3

Browse files
committed
Adds a new block for niryo to connect to the robot
1 parent e1c7b24 commit 2994db3

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/layout.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,7 @@ export class BlocklyLayout extends PanelLayout {
104104

105105
run(): void {
106106
// Serializing our workspace into the chosen language generator.
107-
let code = this._manager.generator.workspaceToCode(this._workspace);
108-
if (this._manager.getToolbox() === 'niryo') {
109-
code =
110-
'from pyniryo import *\nn = NiryoRobot("127.0.0.1")\n' +
111-
code +
112-
'n.close_connection()';
113-
}
107+
const code = this._manager.generator.workspaceToCode(this._workspace);
114108
this._cell.model.sharedModel.setSource(code);
115109
this.addWidget(this._cell);
116110
this._resizeWorkspace();

src/niryo/niryo_one_python_generators.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
};
6382
Blockly.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

866912
Blockly.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

Comments
 (0)