diff --git a/js/xrp_blockly_toolbox.js b/js/xrp_blockly_toolbox.js index c887ed7..a6ced29 100644 --- a/js/xrp_blockly_toolbox.js +++ b/js/xrp_blockly_toolbox.js @@ -415,6 +415,14 @@ var baseToolbox = { { "kind": "BLOCK", "type": "math_random_float" + }, + { + "kind": "BLOCK", + "type": "xrp_deadband", + "inputs":{ + "VALUE": {"shadow": {"type": "math_number", "fields": {"NUM": "1"}}}, + "DEADBAND": {"shadow": {"type": "math_number", "fields": {"NUM": "0.1"}}} + } } ], "name": "Math", diff --git a/js/xrp_blocks.js b/js/xrp_blocks.js index 514e7eb..370d278 100644 --- a/js/xrp_blocks.js +++ b/js/xrp_blocks.js @@ -610,6 +610,24 @@ Blockly.Blocks['xrp_sleep'] = { } }; +// Math +Blockly.Blocks['xrp_deadband'] = { + init: function () { + this.appendDummyInput() + .appendField("set the value:"); + this.appendValueInput("VALUE") + .setCheck("Number"); + this.appendValueInput("DEADBAND") + .setCheck("Number") + .appendField("to 0 if the value is less than:"); + this.setInputsInline(true); + this.setOutput(true, "Number"); + this.setColour(230); // indigo + this.setTooltip("Sets the value to 0 if it is within the deadband range, which is useful for filtering drift from joysticks"); + this.setHelpUrl(""); + } +} + // OTHER BLOCK COLORS - These colors can be found in the xrp_blockly_toolbox1.js file // BLOCK TYPE --> COLOR // Loops --> grass green diff --git a/js/xrp_blocks_python.js b/js/xrp_blocks_python.js index 3b495b3..1b87658 100644 --- a/js/xrp_blocks_python.js +++ b/js/xrp_blocks_python.js @@ -367,6 +367,14 @@ Blockly.Python['xrp_sleep'] = function (block) { return code; }; +//Math +Blockly.Python['xrp_deadband'] = function (block) { + var number_value = Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_ATOMIC); + var number_deadband = Blockly.Python.valueToCode(block, 'DEADBAND', Blockly.Python.ORDER_ATOMIC); + var code = `(${number_value} if abs(${number_value}) > ${number_deadband} else 0)`; + return [code, Blockly.Python.ORDER_ATOMIC]; +} + Blockly.Python['comment'] = function(block) { var text = block.getFieldValue('TEXT'); return '# ' + text + '\n';