Skip to content

Commit 4a4e348

Browse files
committed
Let the user delete the loop method in an OpMode.
Removed code from opmode.py that calls the steps method. In mrc_class_method_def.ts: Renamed classSpecific and getClassSpecificForInit to superInitParameters and getSuperInitParameters. In mrc_steps.ts: Added constant STEPS_METHOD_NAME and changed the steps method name to _steps. In python.ts: Added constant PERIODIC_METHOD_NAME. In extended_python_generator.ts: Changed finish method to add the code for calling the steps method to the end of the loop method. Renamed getClassSpecificForInit to getSuperInitParameters.
1 parent 48de2f8 commit 4a4e348

File tree

6 files changed

+30
-10
lines changed

6 files changed

+30
-10
lines changed

server_python_scripts/blocks_base_classes/opmode.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ def __init__(self, robot: RobotBase):
77
def start(self) -> None:
88
self.robot.start()
99
def loop(self) -> None:
10-
# Call steps method if it exists in the derived class
11-
if hasattr(self, 'steps') and callable(self.steps):
12-
self.steps()
1310
self.robot.update()
1411
def stop(self) -> None:
1512
self.robot.stop()

src/blocks/mrc_class_method_def.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ export const pythonFromBlock = function (
457457
xfix2 = xfix1;
458458
}
459459
if (block.mrcPythonMethodName === '__init__') {
460-
const classSpecific = generator.getClassSpecificForInit();
461-
branch = generator.INDENT + 'super().__init__(' + classSpecific + ')\n' +
460+
const superInitParameters = generator.getSuperInitParameters();
461+
branch = generator.INDENT + 'super().__init__(' + superInitParameters + ')\n' +
462462
generator.generateInitStatements() + branch;
463463
}
464464
else if (generator.inBaseClassMethod(blocklyName)) {

src/blocks/mrc_steps.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import * as toolboxItems from '../toolbox/items';
3232

3333
export const BLOCK_NAME = 'mrc_steps';
3434

35+
export const STEPS_METHOD_NAME = '_steps';
36+
3537
const INPUT_CONDITION_PREFIX = 'CONDITION_';
3638
const INPUT_STATEMENT_PREFIX = 'STATEMENT_';
3739

@@ -219,7 +221,7 @@ export const pythonFromBlock = function (
219221
block: StepsBlock,
220222
generator: ExtendedPythonGenerator,
221223
) {
222-
let code = 'def steps(self):\n';
224+
let code = 'def ' + STEPS_METHOD_NAME + '(self):\n';
223225
code += generator.INDENT + 'if not hasattr(self, \'_initialized_steps\'):\n';
224226
code += generator.INDENT.repeat(2) + 'self._current_step = \'' + block.mrcStepNames[0] + '\'\n';
225227
code += generator.INDENT.repeat(2) + 'self._initialized_steps = True\n\n';
@@ -243,7 +245,7 @@ export const pythonFromBlock = function (
243245
}
244246
});
245247

246-
generator.addClassMethodDefinition('steps', code);
248+
generator.addClassMethodDefinition(STEPS_METHOD_NAME, code);
247249

248250
return ''
249251
}

src/blocks/utils/python.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ import * as SetPythonVariable from "../mrc_set_python_variable";
3333

3434
export const MODULE_NAME_BLOCKS_BASE_CLASSES = 'blocks_base_classes';
3535
export const CLASS_NAME_ROBOT_BASE = MODULE_NAME_BLOCKS_BASE_CLASSES + '.RobotBase';
36-
export const CLASS_NAME_OPMODE = MODULE_NAME_BLOCKS_BASE_CLASSES + '.OpMode';
3736
export const CLASS_NAME_MECHANISM = MODULE_NAME_BLOCKS_BASE_CLASSES + '.Mechanism';
3837

38+
// TODO(lizlooney): what about PeriodicOpMode and LinearOpMode?
39+
export const CLASS_NAME_OPMODE = MODULE_NAME_BLOCKS_BASE_CLASSES + '.OpMode';
40+
// TODO(lizlooney): Make sure to update the value of PERIODIC_METHOD_NAME when we update wpilib.
41+
export const PERIODIC_METHOD_NAME = 'loop';
3942

4043
export const robotPyData = generatedRobotPyData as PythonData;
4144
const externalSamplesData = generatedExternalSamplesData as PythonData

src/editor/extended_python_generator.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ import { PythonGenerator } from 'blockly/python';
2424
import { createGeneratorContext, GeneratorContext } from './generator_context';
2525
import * as mechanismContainerHolder from '../blocks/mrc_mechanism_component_holder';
2626
import * as eventHandler from '../blocks/mrc_event_handler';
27+
import { STEPS_METHOD_NAME } from '../blocks/mrc_steps';
28+
2729
import {
2830
MODULE_NAME_BLOCKS_BASE_CLASSES,
2931
CLASS_NAME_OPMODE,
32+
PERIODIC_METHOD_NAME,
3033
getClassData,
3134
} from '../blocks/utils/python';
3235
import * as storageModule from '../storage/module';
@@ -248,6 +251,22 @@ export class ExtendedPythonGenerator extends PythonGenerator {
248251

249252
code = decorators + 'class ' + className + '(' + baseClassName + '):\n';
250253

254+
if (this.getModuleType() === storageModule.ModuleType.OPMODE) {
255+
// If the user has a steps method, we need to generate code to call it from the periodic method.
256+
if (STEPS_METHOD_NAME in this.classMethods) {
257+
let periodicCode: string;
258+
if (PERIODIC_METHOD_NAME in this.classMethods) {
259+
periodicCode = this.classMethods[PERIODIC_METHOD_NAME];
260+
} else {
261+
periodicCode = `def ${PERIODIC_METHOD_NAME}(self):\n`;
262+
periodicCode += this.INDENT + `super().${PERIODIC_METHOD_NAME}()\n`;
263+
}
264+
periodicCode += this.INDENT + `if hasattr(self, '${STEPS_METHOD_NAME}') and callable(self.${STEPS_METHOD_NAME}):\n`;
265+
periodicCode += this.INDENT.repeat(2) + `self.${STEPS_METHOD_NAME}()\n`;
266+
this.classMethods[PERIODIC_METHOD_NAME] = periodicCode;
267+
}
268+
}
269+
251270
const classMethods = [];
252271

253272
// Generate the __init__ method first.
@@ -295,7 +314,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
295314
this.opModeDetails = opModeDetails;
296315
}
297316

298-
getClassSpecificForInit(): string {
317+
getSuperInitParameters(): string {
299318
if (this.context?.getBaseClassName() == CLASS_NAME_OPMODE) {
300319
return 'robot'
301320
}

src/modules/opmode_start.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"type": "mrc_class_method_def",
3838
"x": 10,
3939
"y": 190,
40-
"deletable": false,
4140
"editable": false,
4241
"extraState": {
4342
"canChangeSignature": false,

0 commit comments

Comments
 (0)