source-academy / modules

Modules that can be imported by programs in Source Academy, an online experiential environment for computational thinking
Apache License 2.0
8 stars 28 forks source link

Part 2 robot simulator #309

Closed JoelChanZhiYang closed 5 months ago

JoelChanZhiYang commented 6 months ago

Description

The EV3 robot simulator implementation is in PR. This looks like a big PR but about half of it is tests.

image

Type of change

Please delete options that are not relevant.

How Has This Been Tested?

There is 119 tests to test the main aspects of the robot simulation.

image

To try out the robot simulation. You can try out this snippet.

In this snippet, you will observe

import {
  ev3_motorA,
  ev3_motorB,
  ev3_motorC,
  ev3_motorD,
  ev3_runToRelativePosition,
  ev3_colorSensorRed,
  ev3_colorSensorGreen,
  ev3_pause,
  ev3_colorSensorBlue,
  ev3_ultrasonicSensor,
  ev3_ultrasonicSensorDistance,
  init_simulation,
  createRenderer,
  createPhysics,
  createTimer,
  createWorld,
  createPaper,
  createWall,
  createEv3,
  createFloor,
  createCSE,
  createRobotConsole,
  addControllerToWorld,
  saveToContext
} from "robot_simulation";

init_simulation(() => {
    const physics = createPhysics();
    const renderer = createRenderer();
    const timer = createTimer();
    const robot_console = createRobotConsole();

    const world = createWorld(physics, renderer, timer, robot_console);

    const program = createCSE();
    addControllerToWorld(program, world);

    const paper = createPaper(
        renderer, 
        "https://www.shutterstock.com/image-vector/black-white-stripes-260nw-785326606.jpg", 
        1, 
        1);

    addControllerToWorld(paper, world);

    const floor = createFloor(physics, renderer);
    addControllerToWorld(floor, world);

    const wall = createWall(physics, renderer);
    addControllerToWorld(wall, world);

    const ev3 = createEv3(physics, renderer);
    addControllerToWorld(ev3, world);

    saveToContext("world", world);
    saveToContext("ev3", ev3);

    return world;
});

// Student code below

let motorA = ev3_motorA();
let motorB = ev3_motorB();
let ultrasonicSensor = ev3_ultrasonicSensor();

ev3_pause(2000);

let loop = true;

while (loop) {
    let distance = ev3_ultrasonicSensorDistance(ultrasonicSensor);

    if (distance < 50) {
        loop = false;
        continue;
    }

    ev3_runToRelativePosition(motorA, 360, 200);
    ev3_runToRelativePosition(motorB, 360, 200);
    ev3_pause(3000);
}

Checklist: