Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
acfc06e
Adding in mechanisms
alan412 Jan 30, 2025
423ed15
Simple mechanism
alan412 Feb 5, 2025
c249ff8
remove add mechanism
alan412 Feb 5, 2025
57b9d7e
Move to use extended python
alan412 Feb 12, 2025
b09335a
Fix typo
alan412 Feb 12, 2025
6fc7202
remove init and loop from toolbox
alan412 Feb 12, 2025
f2a6d76
Change to use json for start state
alan412 Feb 12, 2025
a7ca6f5
Put holder for mechanisms in generated code
alan412 Feb 12, 2025
ae82b2a
add extra state for imported module
alan412 Feb 12, 2025
624c453
changed starting x, y
alan412 Feb 12, 2025
ea7fc4d
Added import module to mechanism
alan412 Feb 12, 2025
aff65d9
Support parameters for mechanisms
alan412 Feb 14, 2025
51d6d45
fixed imports in wrong place
alan412 Feb 14, 2025
520821a
Changed OpMode and Mechanism to be PascalCase
alan412 Feb 14, 2025
d2ec25e
add known issues
alan412 Feb 14, 2025
a734a67
Remove unused field minus and plus
alan412 Feb 14, 2025
0b3f5fb
removed custom canDelete to use blockly deletable
alan412 Feb 14, 2025
7d242b7
Fix failure of CI
alan412 Feb 14, 2025
8d5daed
Added mechanisms to the app (new mechanism, show mechanism in the tre…
lizlooney Feb 14, 2025
7c1ae9a
Respond to PR comments
alan412 Feb 14, 2025
25ce1fd
change back from const to let
alan412 Feb 14, 2025
596893f
Changed module types to be all lowercase
alan412 Feb 14, 2025
b63e3a0
Merge pull request #45 from lizlooney/pr_mechanism_files
lizlooney Feb 15, 2025
18a1d22
Merge branch 'mechanisms' of github.com:alan412/systemcore-blocks-int…
lizlooney Feb 15, 2025
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@
5. Once all dependencies have been installed, run ```npm run start```
6. Navigate to localhost:3000 in the browser to open the frontend
7. Make sure any python backends are running on your PC.

## Known Issues
1. Mechanisms aren't limited to init
2. Mechanisms aren't limited to only Robot or Mechanism class
3. There is no default Robot class
4. No way to specify whether an opmode is auto or teleop
5. Workspace can have blocks
6. No Robot workspace yet
7. No Mechanism workspace yet
8. Something weird is going on with currentModule getting unset
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I fixed this in PR #45 !

14 changes: 6 additions & 8 deletions src/blocks/mrc_class_method_def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export type Parameter = {
type ClassMethodDefBlock = Blockly.Block & ClassMethodDefMixin & Blockly.BlockSvg;
interface ClassMethodDefMixin extends ClassMethodDefMixinType {
mrcCanChangeSignature: boolean,
mrcCanDelete: boolean,
mrcReturnType: string,
mrcParameters: Parameter[],
mrcPythonMethodName: string,
Expand All @@ -58,10 +57,6 @@ type ClassMethodDefExtraState = {
* Can change name and parameters and return type
*/
canChangeSignature: boolean,
/**
* Can delete from class
*/
canDelete: boolean,
/**
* The return type of the function.
* Use 'None' for no return value.
Expand Down Expand Up @@ -100,7 +95,6 @@ const CLASS_METHOD_DEF = {
this: ClassMethodDefBlock): ClassMethodDefExtraState {
const extraState: ClassMethodDefExtraState = {
canChangeSignature: this.mrcCanChangeSignature,
canDelete: this.mrcCanDelete,
returnType: this.mrcReturnType,
params: [],
};
Expand All @@ -124,7 +118,6 @@ const CLASS_METHOD_DEF = {
extraState: ClassMethodDefExtraState
): void {
this.mrcCanChangeSignature = extraState.canChangeSignature;
this.mrcCanDelete = extraState.canDelete;
this.mrcPythonMethodName = extraState.pythonMethodName ? extraState.pythonMethodName : '';
this.mrcReturnType = extraState.returnType;
this.mrcParameters = [];
Expand Down Expand Up @@ -157,7 +150,6 @@ const CLASS_METHOD_DEF = {
//Case because a current bug in blockly where it won't allow passing null to Blockly.Block.setMutator makes it necessary.
(this as Blockly.BlockSvg).setMutator( null );
}
this.setDeletable(this.mrcCanDelete);
this.mrcUpdateParams();
},
compose: function (this: ClassMethodDefBlock, containerBlock: any) {
Expand Down Expand Up @@ -347,6 +339,7 @@ export const setup = function() {

import { Order, PythonGenerator } from 'blockly/python';


export const pythonFromBlock = function (
block: ClassMethodDefBlock,
generator: PythonGenerator,
Expand Down Expand Up @@ -404,6 +397,11 @@ export const pythonFromBlock = function (
'(' +
paramString +
'):\n';

if(block.mrcPythonMethodName == '__init__'){
code += generator.INDENT + "self.mechanisms = [];\n"

}

code +=
xfix1 +
Expand Down
143 changes: 143 additions & 0 deletions src/blocks/mrc_mechanism.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* @license
* Copyright 2025 Porpoiseful LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @fileoverview Create a mechanism with a name of a certain type
* @author [email protected] (Alan Smith)
*/
import * as Blockly from 'blockly';
import { Order, PythonGenerator } from 'blockly/python';

import { MRC_STYLE_FUNCTIONS } from '../themes/styles'
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
import { getAllowedTypesForSetCheck, getOutputCheck } from './utils/python';

export const BLOCK_NAME = 'mrc_mechanism';
export const OUTPUT_NAME = 'mrc_mechansim';

export type ConstructorArg = {
name: string,
type: string,
};

type MechanismExtraState = {
importModule?: string,
params?: ConstructorArg[],
}

type MechanismBlock = Blockly.Block & MechanismMixin;
interface MechanismMixin extends MechanismMixinType {
mrcArgs: ConstructorArg[],
mrcImportModule: string,

}
type MechanismMixinType = typeof MECHANISM_FUNCTION;

const MECHANISM_FUNCTION = {
/**
* Block initialization.
*/
init: function (this: MechanismBlock): void {
this.setStyle(MRC_STYLE_FUNCTIONS);
this.appendDummyInput()
.appendField(new Blockly.FieldTextInput('my_mech'), 'NAME')
.appendField('of type')
.appendField(createFieldNonEditableText(''), 'TYPE');
this.setPreviousStatement(true);
this.setNextStatement(true);
},

/**
* Returns the state of this block as a JSON serializable object.
*/
saveExtraState: function (this: MechanismBlock): MechanismExtraState {
let extraState: MechanismExtraState = {
};
extraState.params = [];
this.mrcArgs.forEach((arg) => {
extraState.params!.push({
'name': arg.name,
'type': arg.type,
});
});
if (this.mrcImportModule) {
extraState.importModule = this.mrcImportModule;
}
return extraState;
},
/**
* Applies the given state to this block.
*/
loadExtraState: function (this: MechanismBlock, extraState: MechanismExtraState): void {
this.mrcImportModule = extraState.importModule ? extraState.importModule : '';
this.mrcArgs = [];

if(extraState.params){
extraState.params.forEach((arg) => {
this.mrcArgs.push({
'name': arg.name,
'type': arg.type,
});
});
}
this.mrcArgs = extraState.params ? extraState.params : [];
this.updateBlock_();
},
/**
* Update the block to reflect the newly loaded extra state.
*/
updateBlock_: function(this: MechanismBlock): void {
// Add input sockets for the arguments.
for (let i = 0; i < this.mrcArgs.length; i++) {
const input = this.appendValueInput('ARG' + i)
.setAlign(Blockly.inputs.Align.RIGHT)
.appendField(this.mrcArgs[i].name);
if (this.mrcArgs[i].type) {
input.setCheck(getAllowedTypesForSetCheck(this.mrcArgs[i].type));
}
}
}
}

export const setup = function () {
Blockly.Blocks[BLOCK_NAME] = MECHANISM_FUNCTION;
}

//TODO: This needs to cause our own init to create the mechanisms line
export const pythonFromBlock = function (
mechanismBlock: MechanismBlock,
generator: ExtendedPythonGenerator,
) {
if (mechanismBlock.mrcImportModule) {
generator.addImport(mechanismBlock.mrcImportModule);
}
let code = 'self.mechanisms["' + mechanismBlock.getFieldValue('NAME') + '"] = '
+ mechanismBlock.getFieldValue('TYPE') + '('

for (let i = 0; i < mechanismBlock.mrcArgs.length; i++) {
let fieldName = 'ARG' + i;
if(i != 0){
code += ', '
}
code += mechanismBlock.mrcArgs[i].name + ' = ' + generator.valueToCode(mechanismBlock, fieldName, Order.NONE);
}

code += ')' + "\n"

return code
}
2 changes: 2 additions & 0 deletions src/blocks/setup_custom_blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as MiscComment from './mrc_misc_comment';
import * as MiscEvaluateButIgnoreResult from './mrc_misc_evaluate_but_ignore_result';
import * as SetPythonVariable from './mrc_set_python_variable';
import * as ClassMethodDef from './mrc_class_method_def';
import * as Mechanism from './mrc_mechanism';

const customBlocks = [
CallPythonFunction,
Expand All @@ -20,6 +21,7 @@ const customBlocks = [
MiscComment,
MiscEvaluateButIgnoreResult,
SetPythonVariable,
Mechanism,
];

export const setup = function(forBlock: any) {
Expand Down
33 changes: 20 additions & 13 deletions src/editor/extended_python_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,30 +159,37 @@ export class ExtendedPythonGenerator extends PythonGenerator {
this.definitions_['import_' + importModule] = 'import ' + importModule;
}

workspaceToCode(workspace?: Blockly.Workspace): string {
let code = super.workspaceToCode(workspace);
finish(code: string): string {
if (!this.currentModule) {
return code;
}

return super.finish(code);
}
let className = this.currentModule.moduleName;
let classType = this.currentModule.moduleType;

this.addImport(classType);

let prefix = "";
for (let key in this.definitions_) {
prefix += this.definitions_[key] + "\n";
}
if (prefix) {
prefix += "\n";
// Convert the definitions dictionary into a list.
const imports = [];
const definitions = [];
for (let name in this.definitions_) {
const def = this.definitions_[name];
if (def.match(/^(from\s+\S+\s+)?import\s+\S+/)) {
imports.push(def);
} else {
definitions.push(def);
}
}
// Call Blockly.CodeGenerator's finish.
code = Blockly.CodeGenerator.prototype.finish(code);
this.isInitialized = false;

let class_def = "class " + className + "(" + classType + "):\n";
if (!code) {
code = "pass";
}
return prefix + class_def + this.prefixLines(code, this.INDENT);

this.nameDB_!.reset();
const allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n');
return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + class_def + this.prefixLines(code, this.INDENT);
}
}

Expand Down
21 changes: 0 additions & 21 deletions src/modules/mrc_module_opmode.ts

This file was deleted.

38 changes: 38 additions & 0 deletions src/modules/opmode_start.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"blocks": {
"languageVersion": 0,
"blocks": [
{
"type": "mrc_class_method_def",
"id": "ElbONc{)s(,UprTW(|1C",
"x": 10,
"y": 10,
"deletable": false,
"extraState": {
"canChangeSignature": false,
"returnType": "None",
"params": [],
"pythonMethodName": "__init__"
},
"fields": {
"NAME": "init"
}
},
{
"type": "mrc_class_method_def",
"id": "wxFAh6eaR1|3fTuV:UAd",
"x": 10,
"y": 200,
"deletable": false,
"extraState": {
"canChangeSignature": false,
"returnType": "None",
"params": []
},
"fields": {
"NAME": "loop"
}
}
]
}
}
11 changes: 6 additions & 5 deletions src/storage/common_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import * as Blockly from 'blockly/core';

import {Block} from "../toolbox/items";
import {create as createOpMode} from '../modules/mrc_module_opmode'
import startingOpModeBlocks from '../modules/opmode_start.json';

import {extendedPythonGenerator} from '../editor/extended_python_generator';

// Types, constants, and functions related to modules, regardless of where the modules are stored.
Expand All @@ -44,8 +45,8 @@ export type Workspace = Module & {
};

export const MODULE_TYPE_WORKSPACE = 'workspace';
export const MODULE_TYPE_OPMODE = 'opmode';
export const MODULE_TYPE_MECHANISM = 'mechanism';
export const MODULE_TYPE_OPMODE = 'OpMode';
export const MODULE_TYPE_MECHANISM = 'Mechanism';

export const MODULE_NAME_PLACEHOLDER = '%module_name%';

Expand Down Expand Up @@ -157,8 +158,8 @@ export function newOpModeContent(workspaceName: string, opModeName: string): str
// Create a headless blockly workspace.
const headlessBlocklyWorkspace = new Blockly.Workspace();
headlessBlocklyWorkspace.options.oneBasedIndex = false;
createOpMode(headlessBlocklyWorkspace, false);

Blockly.serialization.workspaces.load(startingOpModeBlocks, headlessBlocklyWorkspace);
extendedPythonGenerator.setCurrentModule(module);
const pythonCode = extendedPythonGenerator.workspaceToCode(headlessBlocklyWorkspace);
const exportedBlocks = JSON.stringify(extendedPythonGenerator.getExportedBlocks(headlessBlocklyWorkspace));
Expand Down
Loading