Skip to content

Commit

Permalink
Update exports and add JBC 10 and 11 with a couple bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tcorbly committed Oct 18, 2024
1 parent c75dae9 commit d032840
Show file tree
Hide file tree
Showing 30 changed files with 813 additions and 158 deletions.
27 changes: 14 additions & 13 deletions src/simulator/ScriptManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface ScriptSceneBinding {
readonly geometry: Dict<Geometry>;
addGeometry(geometry: Geometry, id?: string): string;
removeGeometry(id: string): void;

gravity: Vector3wUnits;

camera: Camera;
Expand Down Expand Up @@ -148,7 +148,7 @@ class ScriptManager {
refCounts.set(filterId, 1);
}
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
if (this[onChangedKey]) this[onChangedKey](listener.nodeId, new Set(refCounts.keys()));
};
Expand Down Expand Up @@ -209,7 +209,7 @@ namespace ScriptManager {
}

export const collision = construct<Collision>(Type.Collision);

export interface IntersectionStart {
type: Type.IntersectionStart;
nodeId: string;
Expand Down Expand Up @@ -256,7 +256,7 @@ namespace ScriptManager {
}

export const render = construct<Render>(Type.Render);

export interface Collision {
type: Type.Collision;
nodeId: string;
Expand All @@ -265,7 +265,7 @@ namespace ScriptManager {
}

export const collision = construct<Collision>(Type.Collision);

export interface Intersection {
type: Type.Intersection;
nodeId: string;
Expand Down Expand Up @@ -330,7 +330,7 @@ namespace ScriptManager {
constructor(script: Script, manager: ScriptManager) {
this.script_ = script;
this.manager_ = manager;

this.spawnFunc_({
scene: this,
RotationwUnits,
Expand All @@ -342,6 +342,7 @@ namespace ScriptManager {
Distance,
Mass,
Angle,
Date,
}, this.script_.code);
}

Expand Down Expand Up @@ -415,7 +416,7 @@ namespace ScriptManager {
if (this.onUnbind) this.onUnbind(nodeId);
this.boundNodeIds_.delete(nodeId);
}

dispose() {
// TODO: This code could be more efficient. We need to unregister the collision and intersection listeners
// from the parent, but doing it as a single operation would be better.
Expand All @@ -439,7 +440,7 @@ namespace ScriptManager {
if (resolvedId in this.manager_.scene.nodes) {
throw new Error(`Node with id ${resolvedId} already exists`);
}

onNodeAdd(resolvedId, node);

return resolvedId;
Expand All @@ -455,7 +456,7 @@ namespace ScriptManager {

onNodeRemove(id);
}

setNode(id: string, node: Node): void {
const { onNodeChange } = this.manager_;
if (!onNodeChange) return;
Expand Down Expand Up @@ -496,7 +497,7 @@ namespace ScriptManager {

onGeometryRemove(id);
}

get gravity(): Vector3wUnits {
return this.manager_.scene.gravity;
}
Expand Down Expand Up @@ -568,7 +569,7 @@ namespace ScriptManager {
this.manager_.removeIntersectionRefCounts_(listener);
break;
}

delete this.listeners_[handle];
}

Expand All @@ -584,13 +585,13 @@ namespace ScriptManager {
setChallengeEventValue(eventId: string, value: boolean) {
if (!this.manager_.onChallengeSetEventValue) return;
this.manager_.onChallengeSetEventValue(eventId, value);
this.getChallengeEventValue(eventId,value);
this.getChallengeEventValue(eventId, value);
}

getChallengeEventValue(eventId: string, value: boolean) {
if (!this.manager_.onChallengeGetEventValue) return;
return value;
this.getChallengeEventValue(eventId,value);
this.getChallengeEventValue(eventId, value);
}
}

Expand Down
53 changes: 27 additions & 26 deletions src/simulator/babylonBindings/RobotBinding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import { Scene as babylonScene, TransformNode, PhysicsViewer, Vector3, IPhysicsEnabledObject,
Mesh, PhysicsJoint, IPhysicsEnginePluginV2, PhysicsConstraintAxis, Physics6DoFConstraint,
PhysicsConstraintMotorType, PhysicsConstraintAxisLimitMode,
import {
Scene as babylonScene, TransformNode, PhysicsViewer, Vector3, IPhysicsEnabledObject,
Mesh, PhysicsJoint, IPhysicsEnginePluginV2, PhysicsConstraintAxis, Physics6DoFConstraint,
PhysicsConstraintMotorType, PhysicsConstraintAxisLimitMode,
} from '@babylonjs/core';

import '@babylonjs/core/Physics/physicsEngineComponent';
Expand Down Expand Up @@ -101,7 +102,7 @@ class RobotBinding {

private createSensor_ = <T extends Node.FrameLike, O, S extends SensorObject<T, O>>(s: { new(parameters: SensorParameters<T>): S }) => (id: string, definition: T): S => {
const parent = this.links_[definition.parentId];

return new s({
id,
definition,
Expand Down Expand Up @@ -138,7 +139,7 @@ class RobotBinding {

private bParentChild_ = (id: string, parentId: string): { bParent: Mesh; bChild: Mesh; childId: string; } => {
if (!parentId) throw new Error(`Missing parent: "${parentId}" for node "${id}"`);

const children = this.childrenNodeIds_[id];
if (children.length !== 1) throw new Error(`"${id}" must have exactly one child`);

Expand Down Expand Up @@ -184,7 +185,7 @@ class RobotBinding {
const maxForce = 50000;

bMotor.setAxisFriction(PhysicsConstraintAxis.ANGULAR_Z, 0);
bMotor.setAxisMotorMaxForce(PhysicsConstraintAxis.ANGULAR_Z, maxForce);
bMotor.setAxisMotorMaxForce(PhysicsConstraintAxis.ANGULAR_Z, maxForce);
bMotor.setAxisMode(PhysicsConstraintAxis.ANGULAR_Z, PhysicsConstraintAxisLimitMode.FREE);
bMotor.setAxisMotorTarget(PhysicsConstraintAxis.ANGULAR_Z, velocity);

Expand Down Expand Up @@ -227,7 +228,7 @@ class RobotBinding {
*/
for (let port = 0; port < 4; ++port) {
const motorId = this.motorPorts_[port];

// If no motor is bound to the port, skip it.
if (!motorId) continue;

Expand Down Expand Up @@ -286,9 +287,9 @@ class RobotBinding {
// If the motor is in pwm mode and the direction is idle, set the motor target to 0.
if (mode === Motor.Mode.Pwm && (direction === Motor.Direction.Idle || direction === Motor.Direction.Brake)) {
this.setMotorVelocity_(bMotor, 0);
if (Math.abs(velocity) < 10) {
bMotor.setAxisMode(PhysicsConstraintAxis.ANGULAR_Z, PhysicsConstraintAxisLimitMode.LOCKED);
}
// if (Math.abs(velocity) < 10) {
// bMotor.setAxisMode(PhysicsConstraintAxis.ANGULAR_Z, PhysicsConstraintAxisLimitMode.LOCKED);
// }
continue;
}

Expand Down Expand Up @@ -319,11 +320,11 @@ class RobotBinding {
// this.brakeAt_[port] = undefined;
// }


let pwm_adj = 0;
// If the motor is not in pwm mode, and we are not done, calculate the pwm value.
if (mode !== Motor.Mode.Pwm && !done) {

// This code is taken from Wombat-Firmware for parity.
const pErr = speedGoal - velocity;
const dErr = pErr - this.lastPErrs_[port];
Expand Down Expand Up @@ -353,7 +354,7 @@ class RobotBinding {
pwm = plug * clamp(-400, pwm, 400);

if (writePwm) writeCommands.push(WriteCommand.motorPwm({ port, pwm }));

const normalizedPwm = pwm / 400;
const nextAngularVelocity = normalizedPwm * velocityMax * 2 * Math.PI / ticksPerRevolution;
// console.log("nextAngularVelocity", nextAngularVelocity);
Expand Down Expand Up @@ -400,7 +401,7 @@ class RobotBinding {
}
}

const currentAngle = this.hingeAngle_(servoId, servo.parentId);
const currentAngle = this.hingeAngle_(servoId, servo.parentId);
const targetAangle = this.lastServoEnabledAngle_[i];

let cur_angle = 0;
Expand All @@ -413,7 +414,7 @@ class RobotBinding {

if (cur_angle.toFixed(5) !== targetAangle.toFixed(5)) {
if (cur_angle < targetAangle) {
bServo.setAxisMaxLimit(PhysicsConstraintAxis.ANGULAR_Z, targetAangle);
bServo.setAxisMaxLimit(PhysicsConstraintAxis.ANGULAR_Z, targetAangle);
bServo.setAxisMotorTarget(PhysicsConstraintAxis.ANGULAR_Z, Math.PI * .4);
}
if (cur_angle > targetAangle) {
Expand Down Expand Up @@ -559,10 +560,10 @@ class RobotBinding {
weight.physicsBody.setAngularVelocity(Vector3.Zero());
weight.physicsBody.setLinearVelocity(Vector3.Zero());
}

rootTransformNode.position = RawVector3.toBabylon(rawOrigin.position || RawVector3.ZERO)
.add(RawVector3.toBabylon(rawInternalOrigin.position || RawVector3.ZERO));

rootTransformNode.rotationQuaternion = RawQuaternion.toBabylon(RawEuler.toQuaternion(UpdatedEulerOrigin));

for (const link of Object.values(this.links_)) {
Expand Down Expand Up @@ -618,7 +619,7 @@ class RobotBinding {
if (node.type !== Node.Type.Link) continue;

const bNode = await createLink(nodeId, node, this.bScene_, this.robot_, this.colliders_);
if (this.physicsViewer_ && bNode.physicsBody) this.physicsViewer_.showBody(bNode.physicsBody);
if (this.physicsViewer_ && bNode.physicsBody) this.physicsViewer_.showBody(bNode.physicsBody);

bNode.metadata = { id: this.robotSceneId_, selected: false } as SceneMeshMetadata;
this.links_[nodeId] = bNode;
Expand All @@ -628,7 +629,7 @@ class RobotBinding {
for (const nodeId of nodeIds) {
const node = robot.nodes[nodeId];
if (node.type === Node.Type.Link || node.type === Node.Type.IRobotCreate) continue;

switch (node.type) {
case Node.Type.Weight: {
const bNode = createWeight(nodeId, node, this.bScene_, this.robot_, this.links_);
Expand All @@ -639,7 +640,7 @@ class RobotBinding {
const { bParent, bChild } = this.bParentChild_(nodeId, node.parentId);
const bJoint = createHinge(nodeId, node, this.bScene_, bParent, bChild);

bJoint.setAxisMotorMaxForce(PhysicsConstraintAxis.ANGULAR_Z, 1000000000);
bJoint.setAxisMotorMaxForce(PhysicsConstraintAxis.ANGULAR_Z, 1000000000);
bJoint.setAxisMotorType(PhysicsConstraintAxis.ANGULAR_Z, PhysicsConstraintMotorType.VELOCITY);
// Start motor in locked position so the wheels don't slide
bJoint.setAxisMode(PhysicsConstraintAxis.ANGULAR_Z, PhysicsConstraintAxisLimitMode.LOCKED);
Expand All @@ -657,10 +658,10 @@ class RobotBinding {
// -90 is upright and closed; 0 is forward and open
const { bParent, bChild } = this.bParentChild_(nodeId, node.parentId);
const bJoint = createHinge(nodeId, node, this.bScene_, bParent, bChild);
bJoint.setAxisMotorMaxForce(PhysicsConstraintAxis.ANGULAR_Z, 10000000);
bJoint.setAxisMotorType(PhysicsConstraintAxis.ANGULAR_Z, PhysicsConstraintMotorType.VELOCITY);
bJoint.setAxisMotorMaxForce(PhysicsConstraintAxis.ANGULAR_Z, 10000000);
bJoint.setAxisMotorType(PhysicsConstraintAxis.ANGULAR_Z, PhysicsConstraintMotorType.VELOCITY);
// Start the servos at 0
bJoint.setAxisMaxLimit(PhysicsConstraintAxis.ANGULAR_Z, Angle.toRadiansValue(Angle.degrees(0)));
bJoint.setAxisMaxLimit(PhysicsConstraintAxis.ANGULAR_Z, Angle.toRadiansValue(Angle.degrees(0)));
bJoint.setAxisMinLimit(PhysicsConstraintAxis.ANGULAR_Z, Angle.toRadiansValue(Angle.degrees(-1)));

this.servos_[nodeId] = bJoint;
Expand Down Expand Up @@ -715,9 +716,9 @@ class RobotBinding {
const node = robot.nodes[nodeId];
if (node.type !== Node.Type.IRobotCreate) continue;
this.createBinding_ = new CreateBinding(
workerInstance.createSerial,
this.createMotors_,
this.createAnalogSensors_,
workerInstance.createSerial,
this.createMotors_,
this.createAnalogSensors_,
this.createDigitalSensors_
);
}
Expand Down
79 changes: 79 additions & 0 deletions src/simulator/definitions/challenges/jbc10-Chopped-New.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import Author from "../../../db/Author";
import Challenge from "../../../state/State/Challenge";
import Expr from "../../../state/State/Challenge/Expr";
import LocalizedString from "../../../util/LocalizedString";
import ProgrammingLanguage from "../../../programming/compiler/ProgrammingLanguage";

export default {
name: { [LocalizedString.EN_US]: "JBC Challenge 10" },
description: {
[LocalizedString.EN_US]: `Junior Botball Challenge 10: Chopped`,
},
author: {
type: Author.Type.Organization,
id: "kipr",
},
code: {
'c': ProgrammingLanguage.DEFAULT_CODE.c,
'cpp': ProgrammingLanguage.DEFAULT_CODE.cpp,
'python': ProgrammingLanguage.DEFAULT_CODE.python,
},
defaultLanguage: "c",
events: {
notInStartBox: {
name: { [LocalizedString.EN_US]: "Robot not in Start Box" },
description: { [LocalizedString.EN_US]: "Robot not in start box" },
},
waitedToChop: {
name: { [LocalizedString.EN_US]: "Waited to Chop" },
description: { [LocalizedString.EN_US]: "Robot waited to chop" },
},
can7Upright: {
name: { [LocalizedString.EN_US]: "Can 7 Upright" },
description: { [LocalizedString.EN_US]: "Can 7 upright" },
},
},
success: {
exprs: {
// Start Box Events
notInStartBox: {
type: Expr.Type.Event,
eventId: "notInStartBox",
},
inStartBox: {
type: Expr.Type.Not,
argId: "notInStartBox",
},
inStartBoxOnce: {
type: Expr.Type.Once,
argId: "inStartBox",
},

//Chop Events
waitedToChop: {
type: Expr.Type.Event,
eventId: "waitedToChop",
},
can7Upright: {
type: Expr.Type.Event,
eventId: "can7Upright",
},
can7NotUpright: {
type: Expr.Type.Not,
argId: "can7Upright",
},

// Success Logic = Can 7 not upright, waited to chop, and began in start box
completion: {
type: Expr.Type.And,
argIds: [
"inStartBoxOnce",
"waitedToChop",
"can7NotUpright",
],
},
},
rootId: "completion",
},
sceneId: "jbc10",
} as Challenge;
Loading

0 comments on commit d032840

Please sign in to comment.