Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions server_python_scripts/blocks_base_classes/opmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ def __init__(self, robot: RobotBase):
def start(self) -> None:
self.robot.start()
def loop(self) -> None:
# Call steps method if it exists in the derived class
if hasattr(self, 'steps') and callable(self.steps):
self.steps()
self.robot.update()
def stop(self) -> None:
self.robot.stop()
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/mrc_class_method_def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ export const pythonFromBlock = function (
xfix2 = xfix1;
}
if (block.mrcPythonMethodName === '__init__') {
const classSpecific = generator.getClassSpecificForInit();
branch = generator.INDENT + 'super().__init__(' + classSpecific + ')\n' +
const superInitParameters = generator.getSuperInitParameters();
branch = generator.INDENT + 'super().__init__(' + superInitParameters + ')\n' +
generator.generateInitStatements() + branch;
}
else if (generator.inBaseClassMethod(blocklyName)) {
Expand Down
6 changes: 4 additions & 2 deletions src/blocks/mrc_steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import * as toolboxItems from '../toolbox/items';

export const BLOCK_NAME = 'mrc_steps';

export const STEPS_METHOD_NAME = '_steps';

const INPUT_CONDITION_PREFIX = 'CONDITION_';
const INPUT_STATEMENT_PREFIX = 'STATEMENT_';

Expand Down Expand Up @@ -219,7 +221,7 @@ export const pythonFromBlock = function (
block: StepsBlock,
generator: ExtendedPythonGenerator,
) {
let code = 'def steps(self):\n';
let code = 'def ' + STEPS_METHOD_NAME + '(self):\n';
code += generator.INDENT + 'if not hasattr(self, \'_initialized_steps\'):\n';
code += generator.INDENT.repeat(2) + 'self._current_step = \'' + block.mrcStepNames[0] + '\'\n';
code += generator.INDENT.repeat(2) + 'self._initialized_steps = True\n\n';
Expand All @@ -243,7 +245,7 @@ export const pythonFromBlock = function (
}
});

generator.addClassMethodDefinition('steps', code);
generator.addClassMethodDefinition(STEPS_METHOD_NAME, code);

return ''
}
Expand Down
5 changes: 4 additions & 1 deletion src/blocks/utils/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ import * as SetPythonVariable from "../mrc_set_python_variable";

export const MODULE_NAME_BLOCKS_BASE_CLASSES = 'blocks_base_classes';
export const CLASS_NAME_ROBOT_BASE = MODULE_NAME_BLOCKS_BASE_CLASSES + '.RobotBase';
export const CLASS_NAME_OPMODE = MODULE_NAME_BLOCKS_BASE_CLASSES + '.OpMode';
export const CLASS_NAME_MECHANISM = MODULE_NAME_BLOCKS_BASE_CLASSES + '.Mechanism';

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

export const robotPyData = generatedRobotPyData as PythonData;
const externalSamplesData = generatedExternalSamplesData as PythonData
Expand Down
21 changes: 20 additions & 1 deletion src/editor/extended_python_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import { PythonGenerator } from 'blockly/python';
import { createGeneratorContext, GeneratorContext } from './generator_context';
import * as mechanismContainerHolder from '../blocks/mrc_mechanism_component_holder';
import * as eventHandler from '../blocks/mrc_event_handler';
import { STEPS_METHOD_NAME } from '../blocks/mrc_steps';

import {
MODULE_NAME_BLOCKS_BASE_CLASSES,
CLASS_NAME_OPMODE,
PERIODIC_METHOD_NAME,
getClassData,
} from '../blocks/utils/python';
import * as storageModule from '../storage/module';
Expand Down Expand Up @@ -248,6 +251,22 @@ export class ExtendedPythonGenerator extends PythonGenerator {

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

if (this.getModuleType() === storageModule.ModuleType.OPMODE) {
// If the user has a steps method, we need to generate code to call it from the periodic method.
if (STEPS_METHOD_NAME in this.classMethods) {
let periodicCode: string;
if (PERIODIC_METHOD_NAME in this.classMethods) {
periodicCode = this.classMethods[PERIODIC_METHOD_NAME];
} else {
periodicCode = `def ${PERIODIC_METHOD_NAME}(self):\n`;
periodicCode += this.INDENT + `super().${PERIODIC_METHOD_NAME}()\n`;
}
periodicCode += this.INDENT + `if hasattr(self, '${STEPS_METHOD_NAME}') and callable(self.${STEPS_METHOD_NAME}):\n`;
periodicCode += this.INDENT.repeat(2) + `self.${STEPS_METHOD_NAME}()\n`;
this.classMethods[PERIODIC_METHOD_NAME] = periodicCode;
}
}

const classMethods = [];

// Generate the __init__ method first.
Expand Down Expand Up @@ -295,7 +314,7 @@ export class ExtendedPythonGenerator extends PythonGenerator {
this.opModeDetails = opModeDetails;
}

getClassSpecificForInit(): string {
getSuperInitParameters(): string {
if (this.context?.getBaseClassName() == CLASS_NAME_OPMODE) {
return 'robot'
}
Expand Down
1 change: 0 additions & 1 deletion src/modules/opmode_start.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"type": "mrc_class_method_def",
"x": 10,
"y": 190,
"deletable": false,
"editable": false,
"extraState": {
"canChangeSignature": false,
Expand Down