Skip to content

Commit

Permalink
Issue #1656: adopt simulation to new camera configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
bjost2s committed Aug 14, 2024
1 parent 29a953d commit d47bc36
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package de.fhg.iais.roberta.components;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -19,11 +21,11 @@
import de.fhg.iais.roberta.bean.IProjectBean;
import de.fhg.iais.roberta.bean.NNBean;
import de.fhg.iais.roberta.blockly.generated.BlockSet;
import de.fhg.iais.roberta.blockly.generated.Instance;
import de.fhg.iais.roberta.factory.RobotFactory;
import de.fhg.iais.roberta.inter.mode.action.ILanguage;
import de.fhg.iais.roberta.robotCommunication.RobotCommunicator;
import de.fhg.iais.roberta.syntax.configuration.ConfigurationComponent;
import de.fhg.iais.roberta.syntax.configuration.ConfigurationComponentNode;
import de.fhg.iais.roberta.transformer.Jaxb2ConfigurationAst;
import de.fhg.iais.roberta.transformer.Jaxb2ProgramAst;
import de.fhg.iais.roberta.typecheck.NepoInfo;
Expand Down Expand Up @@ -328,35 +330,46 @@ public Builder setConfigurationXml(String configurationXml) {
public Builder setConfigurationAst(ConfigurationAst configurationAst) {
this.project.configuration = configurationAst;
this.project.configurationJSON = new JSONObject();
JSONObject sensorsJSON = new JSONObject();
JSONObject actuatorsJSON = new JSONObject();
try {
this.project.configurationJSON.put("TRACKWIDTH", configurationAst.getTrackWidth());
this.project.configurationJSON.put("WHEELDIAMETER", configurationAst.getWheelDiameter());
for ( ConfigurationComponent sensor : configurationAst.getSensors() ) {
JSONObject propJSON = new JSONObject();
propJSON.put("TYPE", sensor.componentType);
for ( String prop : sensor.getComponentProperties().keySet() ) {
propJSON.put(prop, sensor.getComponentProperties().get(prop));
}
sensorsJSON.put(sensor.userDefinedPortName, propJSON);
}
for ( ConfigurationComponent actuator : configurationAst.getActors() ) {
JSONObject propJSON = new JSONObject();
propJSON.put("TYPE", actuator.componentType);
for ( String prop : actuator.getComponentProperties().keySet() ) {
propJSON.put(prop, actuator.getComponentProperties().get(prop));
}
actuatorsJSON.put(actuator.userDefinedPortName, propJSON);
}
this.project.configurationJSON.put("SENSORS", sensorsJSON);
this.project.configurationJSON.put("ACTUATORS", actuatorsJSON);
this.project.configurationJSON.put("SENSORS", configurationAst2JSON(configurationAst.getSensors()));
this.project.configurationJSON.put("ACTUATORS", configurationAst2JSON(configurationAst.getActors()));
} catch ( JSONException e ) {
throw new DbcException("exception when generating the configuration ", e);
}
return this;
}

private static JSONObject configurationAst2JSON(Collection<ConfigurationComponent> configurationAst) {
JSONObject compJSON = new JSONObject();
for ( ConfigurationComponent component : configurationAst ) {
JSONObject propJSON = new JSONObject();
propJSON.put("TYPE", component.componentType);
for ( String prop : component.getComponentProperties().keySet() ) {
propJSON.put(prop, component.getComponentProperties().get(prop));
}
if ( component instanceof ConfigurationComponentNode ) {
LinkedHashMap<String, List<ConfigurationComponent>> subComponentLists = component.getSubComponents();
for ( String subComponentList : subComponentLists.keySet() ) {
JSONObject sub = new JSONObject();
List<ConfigurationComponent> subList = subComponentLists.get(subComponentList);
for ( ConfigurationComponent subComp : subList ) {
JSONObject subPropJSON = new JSONObject();
subPropJSON.put("TYPE", subComp.componentType);
for ( String prop : subComp.getComponentProperties().keySet() ) {
subPropJSON.put(prop, subComp.getComponentProperties().get(prop));
}
sub.put(subComp.userDefinedPortName, subPropJSON);
}
propJSON.put("SUBCOMPONENTS", sub);
}
}
compJSON.put(component.userDefinedPortName, propJSON);
}
return compJSON;
}

public Builder setProgramAst(ProgramAst programAst) {
this.project.program = programAst;
return this;
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 36 additions & 25 deletions OpenRobertaWeb/src/app/simulation/simulationLogic/robot.sensors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2248,8 +2248,8 @@ export class CameraSensor implements ISensor, IUpdateAction, IDrawable, ILabel,
readonly MAX_BLOB_DIST_SQR = this.MAX_MARKER_DIST_SQR;
readonly LINE_RADIUS: number = 60;
readonly markerEnabled: boolean = true;
readonly lineEnabled: boolean = true;
readonly colorEnabled: boolean = true;
lineEnabled: boolean = true;
colorEnabled: boolean = true;
x: number;
y: number;
h: number;
Expand Down Expand Up @@ -2674,27 +2674,35 @@ export class Txt4CameraSensor extends CameraSensor {
p: PointRobotWorld = { x: 0, y: 0, rx: 0, ry: 0 };
rect: Point4Rectangle = { p1: this.p, p2: this.p, p3: this.p, p4: this.p };

constructor(pose: Pose, aov: number, blobSize: number) {
constructor(pose: Pose, aov: number, blobSize: number, lineWidth: number) {
super(pose, aov);
this.BLOBSIZE = blobSize;
if (this.BLOBSIZE === 0) {
this.colorEnabled = false;
}
if (lineWidth === 0) {
this.lineEnabled = false;
}
}

override draw(rCtx: CanvasRenderingContext2D, myRobot: RobotBase): void {
super.draw(rCtx, myRobot);
rCtx.save();
//***********
rCtx.rotate(-(myRobot as RobotBaseMobile).pose.theta);
rCtx.translate(-(myRobot as RobotBaseMobile).pose.x, -(myRobot as RobotBaseMobile).pose.y);
rCtx.beginPath();
rCtx.strokeStyle = '#00ffff';
rCtx.moveTo(this.rect.p1.rx, this.rect.p1.ry);
rCtx.lineTo(this.rect.p2.rx, this.rect.p2.ry);
rCtx.lineTo(this.rect.p3.rx, this.rect.p3.ry);
rCtx.lineTo(this.rect.p4.rx, this.rect.p4.ry);
rCtx.lineTo(this.rect.p1.rx, this.rect.p1.ry);
rCtx.stroke();
//*******************
rCtx.restore();
if (this.colorEnabled || this.lineEnabled) {
super.draw(rCtx, myRobot);
}
if (this.colorEnabled) {
rCtx.save();
rCtx.rotate(-(myRobot as RobotBaseMobile).pose.theta);
rCtx.translate(-(myRobot as RobotBaseMobile).pose.x, -(myRobot as RobotBaseMobile).pose.y);
rCtx.beginPath();
rCtx.strokeStyle = '#00ffff';
rCtx.moveTo(this.rect.p1.rx, this.rect.p1.ry);
rCtx.lineTo(this.rect.p2.rx, this.rect.p2.ry);
rCtx.lineTo(this.rect.p3.rx, this.rect.p3.ry);
rCtx.lineTo(this.rect.p4.rx, this.rect.p4.ry);
rCtx.lineTo(this.rect.p1.rx, this.rect.p1.ry);
rCtx.stroke();
rCtx.restore();
}
}

override updateSensor(
Expand Down Expand Up @@ -2974,6 +2982,16 @@ export class Txt4CameraSensor extends CameraSensor {

override getLabel(): string {
let myLabel: string = '';
if (this.colorEnabled) {
myLabel +=
'<div><label>' +
Blockly.Msg.SENSOR_CAMERA +
' ' +
Blockly.Msg.MODE_COLOUR +
'</label><span style="margin-left:6px; width: 20px; border-style:solid; border-width:thin; background-color:' +
(this.color.length > 0 ? SIMATH.rgbToHex(this.color[0], this.color[1], this.color[2]) : '#fff') +
'">&nbsp;</span></div>';
}
if (this.lineEnabled) {
myLabel +=
'<div><label>' +
Expand All @@ -2995,13 +3013,6 @@ export class Txt4CameraSensor extends CameraSensor {
Blockly.Msg.MODE_COLOUR +
'</label><span style="margin-left:6px; width: 20px; border-style:solid; border-width:thin; background-color:' +
(this.lineColor.length > 0 ? SIMATH.rgbToHex(this.lineColor[0], this.lineColor[1], this.lineColor[2]) : '#fff') +
'">&nbsp;</span></div>' +
'<div><label>' +
Blockly.Msg.SENSOR_CAMERA +
' ' +
Blockly.Msg.MODE_COLOUR +
'</label><span style="margin-left:6px; width: 20px; border-style:solid; border-width:thin; background-color:' +
(this.color.length > 0 ? SIMATH.rgbToHex(this.color[0], this.color[1], this.color[2]) : '#fff') +
'">&nbsp;</span></div>';
}
return myLabel;
Expand Down
Loading

0 comments on commit d47bc36

Please sign in to comment.