Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions src/blocks/mrc_call_python_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,17 @@ const CALL_PYTHON_FUNCTION = {
break;
}
case FunctionKind.INSTANCE_COMPONENT: {
let componentNames: string[] = [];
const componentNames: string[] = [];
// Get the list of component names whose type matches this.mrcComponentClassName so we can
// create a dropdown that has the appropriate component names.
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace);
if (editor) {
componentNames = editor.getComponentNames(this.mrcComponentClassName);
const components = editor.getComponentsFromRobot();
components.forEach(component => {
if (component.className === this.mrcComponentClassName) {
componentNames.push(component.name);
}
});
}
const componentName = this.getComponentName();
if (!componentNames.includes(componentName)) {
Expand Down
30 changes: 10 additions & 20 deletions src/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class Editor {
const promises: { [key: string]: Promise<string> } = {}; // key is module path, value is promise of module content.
promises[this.modulePath] = this.storage.fetchModuleContent(this.modulePath);
if (this.robotPath !== this.modulePath) {
// Also fetch the robot module content. It contains components, etc, that can be used.
// Also fetch the robot module content. It contains components, etc, that can be used in OpModes.
promises[this.robotPath] = this.storage.fetchModuleContent(this.robotPath)
}

Expand Down Expand Up @@ -224,12 +224,12 @@ export class Editor {
const exportedBlocks = JSON.stringify(this.generatorContext.getExportedBlocks());
const blocksContent = JSON.stringify(
Blockly.serialization.workspaces.save(this.blocklyWorkspace));
const componentsContent = JSON.stringify(this.getComponents());
const componentsContent = JSON.stringify(this.getComponentsFromWorkspace());
return commonStorage.makeModuleContent(
this.currentModule, pythonCode, blocksContent, exportedBlocks, componentsContent);
}

private getComponents(): commonStorage.Component[] {
public getComponentsFromWorkspace(): commonStorage.Component[] {
const components: commonStorage.Component[] = [];
if (this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT ||
this.currentModule?.moduleType === commonStorage.MODULE_TYPE_MECHANISM) {
Expand Down Expand Up @@ -257,28 +257,18 @@ export class Editor {
}

/**
* Returns the names of components defined in the robot that have the given component class name.
* Returns the components defined in the robot.
*/
// TODO: what about components defined in a mechanism?
public getComponentNames(componentClassName: string): string[] {
public getComponentsFromRobot(): commonStorage.Component[] {
let components: commonStorage.Component[];

if (this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT) {
components = this.getComponents();
} else {
if (!this.robotContent) {
throw new Error('getComponentNames: this.robotContent is null.');
}
components = commonStorage.extractComponents(this.robotContent);
return this.getComponentsFromWorkspace();
}

const componentNames: string[] = [];
components.forEach((component) => {
if (component.className === componentClassName) {
componentNames.push(component.name);
}
});
return componentNames;
if (!this.robotContent) {
throw new Error('getComponentsFromRobot: this.robotContent is null.');
}
return commonStorage.extractComponents(this.robotContent);
}

public static getEditorForBlocklyWorkspace(workspace: Blockly.Workspace): Editor | null {
Expand Down
179 changes: 91 additions & 88 deletions src/toolbox/blocks_mechanisms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,102 +20,105 @@
*/
import * as ToolboxItems from './items';

/**
* TODO: This is all fake right now, it will be generated dynamically
* based on reading things from mechanisms.
*/

export function getAllPossibleMechanisms(): ToolboxItems.ContentsType[] {
return [
const contents: ToolboxItems.ContentsType[] = []

// TODO: Get the blocks for adding mechanisms.

{
kind: 'block',
type: 'mrc_mechanism',
/* // Uncomment this fake code for testing purposes only.
contents.push(
{
kind: 'block',
type: 'mrc_mechanism',
fields: {
NAME: 'claw',
TYPE: 'Claw'
},
extraState: {
importModule: 'claw',
params: [
{ name: 'gripper_port', type: 'int' },
{ name: 'piece_sensor_port', type: 'int' },
]
},
inputs: {
ARG0: {
shadow: {
type: 'mrc_port',
fields: {
TYPE: 'SmartMotor',
PORT_NUM: 1
},
},
},
ARG1: {
shadow: {
type: 'mrc_port',
fields: {
TYPE: 'SmartIO',
PORT_NUM: 1
},
},
},
}
},
{
kind: 'block',
type: 'mrc_mechanism',
fields: {
NAME: 'drive',
TYPE: 'DriveMecanum'
},
extraState: {
importModule: 'drive_mecanum',
params: [
{ name: 'front_left_drive_port', type: 'int' },
{ name: 'front_right_drive_port', type: 'int' },
{ name: 'back_left_drive_port', type: 'int' },
{ name: 'back_right_drive_port', type: 'int' },
]
},
inputs: {
ARG0: {
shadow: {
type: 'mrc_port',
fields: {
NAME: 'claw',
TYPE: 'Claw'
TYPE: 'SmartMotor',
PORT_NUM: 1
},
extraState: {
importModule: 'claw',
params: [{ name: 'gripper_port', type: 'int' },
{ name: 'piece_sensor_port', type: 'int' },
]
},
},
ARG1: {
shadow: {
type: 'mrc_port',
fields: {
TYPE: 'SmartMotor',
PORT_NUM: 2
},
inputs: {
ARG0: {
shadow: {
type: 'mrc_port',
fields: {
TYPE: 'SmartMotor',
PORT_NUM: 1
},
},
},
ARG1: {
shadow: {
type: 'mrc_port',
fields: {
TYPE: 'SmartIO',
PORT_NUM: 1
},
},
},
}
},
},
{
kind: 'block',
type: 'mrc_mechanism',
ARG2: {
shadow: {
type: 'mrc_port',
fields: {
NAME: 'drive',
TYPE: 'DriveMecanum'
TYPE: 'SmartMotor',
PORT_NUM: 3
},
extraState: {
importModule: 'DriveMecanum',
params: [{ name: 'front_left_drive_port', type: 'int' },
{ name: 'front_right_drive_port', type: 'int' },
{ name: 'back_left_drive_port', type: 'int' },
{ name: 'back_right_drive_port', type: 'int' },
]
},
},
ARG3: {
shadow: {
type: 'mrc_port',
fields: {
TYPE: 'SmartMotor',
PORT_NUM: 4
},
inputs: {
ARG0: {
shadow: {
type: 'mrc_port',
fields: {
TYPE: 'SmartMotor',
PORT_NUM: 1
},
},
},
ARG1: {
shadow: {
type: 'mrc_port',
fields: {
TYPE: 'SmartMotor',
PORT_NUM: 2
},
},
},
ARG2: {
shadow: {
type: 'mrc_port',
fields: {
TYPE: 'SmartMotor',
PORT_NUM: 3
},
},
},
ARG3: {
shadow: {
type: 'mrc_port',
fields: {
TYPE: 'SmartMotor',
PORT_NUM: 4
},
},
},
}
},
},
];
}
}
}
);
*/

return contents;
}
Loading