In the XRP curriculum, there is a suggestion to use machine.Timer in micropython to update the value logged in a webserver. However, there is no equivalent for Blockly.
I feel like adding a periodic function that runs would be a nice solution, even if it would be possible to also use a while loop. The problem with a while loop is that other code can’t be used concurrently: If one wanted to do something like logging values periodically while also traversing a complicated maze directly without sensors and just preprogrammed instructions, they would not be able to use while loops.
When using pestolink, there is also some delay to when servos move if the drivetrain is also being used. This idea should help mitigate this delay, not to mention it is similar to the command based structure of actual FRC.
It is a more elegant solution than while loops, and would be a helpful feature.
Blockly.Python['run_function_periodically'] = function(block) {
// TODO: change Order.ATOMIC to the correct operator precedence strength
PY.definitions_['import_timer'] = 'from machine import Timer \n\n timers=[]';
const value_after = Blockly.Python.valueToCode(block, 'AFTER', Blockly.Python.ORDER_ATOMIC);
const statement_do = Blockly.Python.statementToCode(block, 'DO');
var funcName = getFuncName();
var code = `\ndef ${funcName}():\n${statement_do}\ntimers.append(Timer(-1)); timers[-1].init(period=${value_after}, mode=Timer.PERIODIC, callback=${funcName})`
return code;
}
The above is the python generator implementation of how it could work, using getFunction similar to web servers.
Blockly.Blocks['run_function_periodically'] = {
init: function() {
this.appendValueInput('AFTER')
.appendField('After')
.setCheck('Number');
this.appendDummyInput()
.appendField('seconds,');
this.appendStatementInput('DO')
.appendField('Do');
this.setInputsInline(true)
this.setTooltip('');
this.setHelpUrl('');
this.setColour(120);
}
};
Above is the base generator form. I have tested it, and it generates the correct code. Ideally it would be placed in the loops section.