Skip to content

Commit

Permalink
Issue #1712: improve movable obstacle behaviour, regenerate victims …
Browse files Browse the repository at this point in the history
…when scoring run starts
  • Loading branch information
bjost2s committed Dec 16, 2024
1 parent 8c61090 commit 519ceac
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 21 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ export class CircleSimulationObject extends BaseSimulationObject implements IMov
corners: Corner[] = [];
private collisions: Point[] = [];
private lastMoveBumped: boolean = false;
private lastMoveBumpedOld: boolean = false;
private lastMove: Point;
private observers: IObserver[] = [];
inEvacuationZone: boolean = false;
Expand Down Expand Up @@ -870,6 +871,10 @@ export class CircleSimulationObject extends BaseSimulationObject implements IMov
this.x += dx;
this.y += dy;
this.updateCorners();
if (this.movable) {
this.lastMoveBumpedOld = false;
this.lastMoveBumped = false;
}
//TODO redraw
}
}
Expand Down Expand Up @@ -912,8 +917,7 @@ export class CircleSimulationObject extends BaseSimulationObject implements IMov
return false;
}
this.collisions.push(dist);
if (this.lastMoveBumped) {
//this.lastMoveBumped = false;
if (this.lastMoveBumpedOld) {
return false;
}
return true;
Expand All @@ -923,6 +927,8 @@ export class CircleSimulationObject extends BaseSimulationObject implements IMov
if (!this.movable) {
return;
}
this.lastMoveBumpedOld = this.lastMoveBumped;
this.lastMoveBumped = false;
let locationColor = uCtx.getImageData(this.x, this.y, 1, 1).data;
this.inEvacuationZone = locationColor.toString() === [0, 0, 0, 255].toString();

Expand Down Expand Up @@ -981,7 +987,6 @@ export class CircleSimulationObject extends BaseSimulationObject implements IMov
this.moveTo({ x: this.x + diffP.x, y: this.y + diffP.y });
}
} else {
this.lastMoveBumped = false;
this.moveTo({ x: this.x + diffP.x, y: this.y + diffP.y });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as HUEBEE from 'huebee';
// @ts-ignore
import * as Blockly from 'blockly';
import * as NN_CTRL from 'nn.controller';
import { SimulationScene } from 'simulation.scene';
import { IObservableSimulationObject, SimulationScene } from 'simulation.scene';
import { SelectionListener } from 'robot.base';
import {
BaseSimulationObject,
Expand All @@ -30,6 +30,7 @@ import {
} from 'simulation.objects';
import { Pose } from 'robot.base.mobile';
import { Simulation } from 'progSim.controller';
import * as SIMATH from 'simulation.math';

export class SimulationRoberta implements Simulation {
private static _instance: SimulationRoberta;
Expand Down Expand Up @@ -1011,15 +1012,25 @@ export class SimulationRoberta implements Simulation {
let victims = configData.victims;
let rcjVictimsList = [];
let zone: Rectangle = {
x: Math.min(evacuationZone[0], evacuationZone[2]) * sim.TILE_SIZE,
y: Math.min(evacuationZone[1], evacuationZone[3]) * sim.TILE_SIZE,
w: (Math.max(evacuationZone[0], evacuationZone[2]) - Math.min(evacuationZone[0], evacuationZone[2]) + 1) * sim.TILE_SIZE,
h: (Math.max(evacuationZone[1], evacuationZone[3]) - Math.min(evacuationZone[1], evacuationZone[3]) + 1) * sim.TILE_SIZE,
x: Math.min(evacuationZone[0], evacuationZone[2]) * sim.TILE_SIZE - sim.TILE_SIZE / 2,
y: Math.min(evacuationZone[1], evacuationZone[3]) * sim.TILE_SIZE - sim.TILE_SIZE / 2,
w: (Math.max(evacuationZone[0], evacuationZone[2]) - Math.min(evacuationZone[0], evacuationZone[2]) + 1) * sim.TILE_SIZE + sim.TILE_SIZE,
h: (Math.max(evacuationZone[1], evacuationZone[3]) - Math.min(evacuationZone[1], evacuationZone[3]) + 1) * sim.TILE_SIZE + sim.TILE_SIZE,
};
const createVictim = (color) => {
let p = { x: zone.x + Math.random() * zone.w, y: zone.y + Math.random() * zone.h };
let i = 0;
while (i < rcjVictimsList.length) {
if (SIMATH.getDistance(p, rcjVictimsList[i].p) < 200) {
p = { x: zone.x + Math.random() * zone.w, y: zone.y + Math.random() * zone.h };
i = 0;
} else {
i++;
}
}
let victim = {
id: sim.scene.uniqueObjectId,
p: { x: zone.x + Math.random() * zone.w, y: zone.y + Math.random() * zone.h },
p: p,
params: [7, 1], // 7 is the radius, 1 is movable = true
theta: 0,
color: color,
Expand Down Expand Up @@ -1047,6 +1058,24 @@ export class SimulationRoberta implements Simulation {
image.height = canvas.height;
return image;
};
let evacuationVictimsZone = [];
const resetVictims = () => {
let obstaclesToDelete: BaseSimulationObject[] = sim.scene.obstacleList.filter((obstacle) => {
return obstacle.movable;
});
obstaclesToDelete.forEach((obstacle) => {
obstacle.selected = true;
(obstacle as any as IObservableSimulationObject).removeObserver(sim.scene.rcjScoringTool);
sim.scene.deleteSelectedObject();
});
let newVictims = getRcjVictims(evacuationVictimsZone);
sim.scene.addSomeObstacles(newVictims);
sim.scene.obstacleList.forEach((obstacle) => {
if (obstacle['addObserver'] && typeof obstacle['addObserver'] === 'function') {
(obstacle as CircleSimulationObject).addObserver(sim.scene.rcjScoringTool);
}
});
};
preloadAll(imgArray).then(
function (images) {
let ctx = canvas.getContext('2d');
Expand All @@ -1057,14 +1086,13 @@ export class SimulationRoberta implements Simulation {
let evacuationBottom = [];
let evacuationLeft = [];
let importObstacles = [];
let evacuationVctimsZone = [];
imgArray.forEach((img, index) => {
if (img['tileType']['image'].startsWith('ev')) {
let ev = img['tileType']['image'].replace('.png', '');
switch (ev) {
case 'ev1':
evacuationVctimsZone.push(img['x']);
evacuationVctimsZone.push(img['y']);
evacuationVictimsZone.push(img['x']);
evacuationVictimsZone.push(img['y']);
// evacuation zone tile without walls
break;
case 'ev2': // one wall
Expand Down Expand Up @@ -1236,12 +1264,12 @@ export class SimulationRoberta implements Simulation {
let image = createBackgroundImage();
sim.scene.imgBackgroundList.push(image);
sim.setBackground(sim.scene.imgBackgroundList.length - 1);
sim.scene.addImportObstacle(importObstacles.concat(getRcjVictims(evacuationVctimsZone)));
sim.scene.addImportObstacle(importObstacles.concat(getRcjVictims(evacuationVictimsZone)));
sim.scene.addImportRcjLabel(rcjLabel);
sim.scene.drawRcjLabel();
sim.importPoses = [[startPose, startPose]];
sim.scene.setRobotPoses(sim.importPoses);
sim.scene.setRcjScoringTool(sim.scene.robots[0], configData);
sim.scene.setRcjScoringTool(sim.scene.robots[0], configData, resetVictims);
$('#simCompetition').show();
resolve(result);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ export class RcjScoringTool implements IObserver {
private rescueMulti: number;
private lastCheckPointIndex: number;
private wasOnLineOnce: boolean = false;
private resetObstaclesCallback: Function;

constructor(robot: RobotBase, configData: any) {
constructor(robot: RobotBase, configData: any, resetObstaclesCallback: Function) {
this.configData = configData;
this.robot = robot as RobotBaseMobile;
this.resetObstaclesCallback = resetObstaclesCallback;
this.init();
let rcj = this;
$('#rcjStartStop')
Expand All @@ -88,6 +90,7 @@ export class RcjScoringTool implements IObserver {
if ($(this).text().indexOf('Start') >= 0) {
$(this).html('Stop<br>Scoring Run');
rcj.init();
rcj.resetObstaclesCallback();
$('#rcjStartStop').addClass('running');
return false;
} else {
Expand Down Expand Up @@ -427,7 +430,7 @@ export class SimulationScene {
private readonly aCtx: CanvasRenderingContext2D;
private readonly udCanvas: HTMLCanvasElement;
readonly uCanvas: HTMLCanvasElement;
private rcjScoringTool: RcjScoringTool;
rcjScoringTool: RcjScoringTool;
private _scoring: boolean = false;

constructor(sim: SimulationRoberta) {
Expand Down Expand Up @@ -564,6 +567,24 @@ export class SimulationScene {
this.obstacleList = newObstacleList;
}

addSomeObstacles(importObstacleList: any[]) {
let that = this;
importObstacleList.forEach((obj) => {
let newObject = SimObjectFactory.getSimObject(
obj.id,
this,
this.sim.selectionListener,
obj.shape,
SimObjectType.Obstacle,
obj.p,
null,
obj.color,
...obj.params
);
that.obstacleList.push(newObject);
});
}

addImportRcjLabel(importRcjLabelList: any[]) {
let newRcjList = [];
importRcjLabelList.forEach((obj) => {
Expand Down Expand Up @@ -1225,8 +1246,8 @@ export class SimulationScene {
this._redrawMarkers = true;
}

setRcjScoringTool(robot: RobotBase, configData) {
this.rcjScoringTool = new RcjScoringTool(robot, configData);
setRcjScoringTool(robot: RobotBase, configData, resetObstacles: Function) {
this.rcjScoringTool = new RcjScoringTool(robot, configData, resetObstacles);
this.scoring = true;
let scene = this;
$('#simCompetition').show();
Expand Down

0 comments on commit 519ceac

Please sign in to comment.