Skip to content

Commit c9d6df5

Browse files
authored
Merge pull request #11 from DenisaCG/tiltsensor
Connect to Tilt Sensors
2 parents e9e046c + 00539f8 commit c9d6df5

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

src/lego_boost_python_generators_and_blocks.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,89 @@ Blockly.Blocks['lego_boost_button_state'] = {
505505
}
506506
};
507507

508+
Blockly.Blocks['lego_boost_position_state_detect'] = {
509+
init: function () {
510+
this.appendValueInput('TIME')
511+
.setCheck('Number')
512+
.appendField('Detect the 2-axis position state for');
513+
this.appendDummyInput().appendField('seconds');
514+
this.setInputsInline(true);
515+
this.setPreviousStatement(true, null);
516+
this.setNextStatement(true, null);
517+
this.setColour(lego_boost_color);
518+
this.setTooltip(
519+
'Output the postion of the robot according to a 2-axis simple detect.'
520+
);
521+
this.setHelpUrl('');
522+
}
523+
};
524+
525+
Blockly.Blocks['lego_boost_position_2axis_angle'] = {
526+
init: function () {
527+
this.appendValueInput('TIME')
528+
.setCheck('Number')
529+
.appendField('Detect the 2-axis position state in degrees for');
530+
this.appendDummyInput().appendField('seconds');
531+
this.setInputsInline(true);
532+
this.setPreviousStatement(true, null);
533+
this.setNextStatement(true, null);
534+
this.setColour(lego_boost_color);
535+
this.setTooltip(
536+
'Output the postion of the robot according to a 2-axis detect. Returns 2-axis roll and pitch values.'
537+
);
538+
this.setHelpUrl('');
539+
}
540+
};
541+
542+
Blockly.Blocks['lego_boost_position_3_axis_state_detect'] = {
543+
init: function () {
544+
this.appendValueInput('TIME')
545+
.setCheck('Number')
546+
.appendField('Detect the 3-axis position state for');
547+
this.appendDummyInput().appendField('seconds');
548+
this.setInputsInline(true);
549+
this.setPreviousStatement(true, null);
550+
this.setNextStatement(true, null);
551+
this.setColour(lego_boost_color);
552+
this.setTooltip(
553+
'Output the postion of the robot according to a 3-axis simple detect.'
554+
);
555+
this.setHelpUrl('');
556+
}
557+
};
558+
559+
Blockly.Blocks['lego_boost_position_3_axis_angle'] = {
560+
init: function () {
561+
this.appendValueInput('TIME')
562+
.setCheck('Number')
563+
.appendField('Detect the 3-axis position state in degrees for');
564+
this.appendDummyInput().appendField('seconds');
565+
this.setInputsInline(true);
566+
this.setPreviousStatement(true, null);
567+
this.setNextStatement(true, null);
568+
this.setColour(lego_boost_color);
569+
this.setTooltip(
570+
'Output the postion of the robot according to a 3-axis detect. Returns 3-axis roll, pitch and yaw values.'
571+
);
572+
this.setHelpUrl('');
573+
}
574+
};
575+
576+
Blockly.Blocks['lego_boost_bumps_detect'] = {
577+
init: function () {
578+
this.appendValueInput('TIME')
579+
.setCheck('Number')
580+
.appendField('Detect the bumps for');
581+
this.appendDummyInput().appendField('seconds');
582+
this.setInputsInline(true);
583+
this.setPreviousStatement(true, null);
584+
this.setNextStatement(true, null);
585+
this.setColour(lego_boost_color);
586+
this.setTooltip('Detect the bumps.');
587+
this.setHelpUrl('');
588+
}
589+
};
590+
508591
/*
509592
* Generators
510593
*/
@@ -906,6 +989,56 @@ BlocklyPy['lego_boost_button_state'] = function (block) {
906989
return code;
907990
};
908991

992+
BlocklyPy['lego_boost_position_state_detect'] = function (block) {
993+
var value_time = BlocklyPy.valueToCode(block, 'TIME', BlocklyPy.ORDER_ATOMIC);
994+
995+
var code =
996+
'def callback(state):\n print("2-axis state: %s" % (state))\nhub.tilt_sensor.subscribe(callback, mode=TiltSensor.MODE_2AXIS_SIMPLE)\ntime.sleep(' +
997+
value_time +
998+
')# play with sensor while it waits\nhub.tilt_sensor.unsubscribe(callback)\n';
999+
return code;
1000+
};
1001+
1002+
BlocklyPy['lego_boost_position_2axis_angle'] = function (block) {
1003+
var value_time = BlocklyPy.valueToCode(block, 'TIME', BlocklyPy.ORDER_ATOMIC);
1004+
1005+
var code =
1006+
'def callback(roll, pitch):\n print("Roll: %s / Pitch: %s" % (roll, pitch))\nhub.tilt_sensor.subscribe(callback, mode=TiltSensor.MODE_2AXIS_ANGLE)\ntime.sleep(' +
1007+
value_time +
1008+
')# play with sensor while it waits\nhub.tilt_sensor.unsubscribe(callback)\n';
1009+
return code;
1010+
};
1011+
1012+
BlocklyPy['lego_boost_position_3_axis_state_detect'] = function (block) {
1013+
var value_time = BlocklyPy.valueToCode(block, 'TIME', BlocklyPy.ORDER_ATOMIC);
1014+
1015+
var code =
1016+
'def callback(state):\n print("3-axis state: %s" % (state))\nhub.tilt_sensor.subscribe(callback, mode=TiltSensor.MODE_3AXIS_SIMPLE)\ntime.sleep(' +
1017+
value_time +
1018+
')# play with sensor while it waits\nhub.tilt_sensor.unsubscribe(callback)\n';
1019+
return code;
1020+
};
1021+
1022+
BlocklyPy['lego_boost_position_3_axis_angle'] = function (block) {
1023+
var value_time = BlocklyPy.valueToCode(block, 'TIME', BlocklyPy.ORDER_ATOMIC);
1024+
1025+
var code =
1026+
'def callback(roll, pitch, yaw):\n print("Roll: %s / Pitch: %s / Yaw: %s" % (roll, pitch, yaw))\nhub.tilt_sensor.subscribe(callback, mode=TiltSensor.MODE_2AXIS_ACCEL)\ntime.sleep(' +
1027+
value_time +
1028+
')# play with sensor while it waits\nhub.tilt_sensor.unsubscribe(callback)\n';
1029+
return code;
1030+
};
1031+
1032+
BlocklyPy['lego_boost_bumps_detect'] = function (block) {
1033+
var value_time = BlocklyPy.valueToCode(block, 'TIME', BlocklyPy.ORDER_ATOMIC);
1034+
1035+
var code =
1036+
'def callback(state):\n print("Bumps state: %s" % (state))\nhub.tilt_sensor.subscribe(callback, mode=TiltSensor.MODE_IMPACT_COUNT)\ntime.sleep(' +
1037+
value_time +
1038+
')# play with sensor while it waits\nhub.tilt_sensor.unsubscribe(callback)\n';
1039+
return code;
1040+
};
1041+
9091042
// Creating a toolbox containing all the main blocks
9101043
// and adding the lego boost catgory.
9111044
const TOOLBOX = {
@@ -1383,6 +1516,26 @@ const TOOLBOX = {
13831516
{
13841517
kind: 'BLOCK',
13851518
type: 'lego_boost_button_state'
1519+
},
1520+
{
1521+
kind: 'BLOCK',
1522+
type: 'lego_boost_position_state_detect'
1523+
},
1524+
{
1525+
kind: 'BLOCK',
1526+
type: 'lego_boost_position_2axis_angle'
1527+
},
1528+
{
1529+
kind: 'BLOCK',
1530+
type: 'lego_boost_position_3_axis_state_detect'
1531+
},
1532+
{
1533+
kind: 'BLOCK',
1534+
type: 'lego_boost_position_3_axis_angle'
1535+
},
1536+
{
1537+
kind: 'BLOCK',
1538+
type: 'lego_boost_bumps_detect'
13861539
}
13871540
// {
13881541
// kind: 'BLOCK',

0 commit comments

Comments
 (0)