Untitled

Anonymous
Plain Text
10/10/2025 12:20 AM
2.7 KB
18
Indexable
Never
q1 
// Your program here.
ev3_speak(Hello world);

q2
// Move forward 10 cm using EV3 library functions

// Convert distance to degrees
const DEGREES = 205;

ev3_runToAbsolutePosition(port='B', position=DEGREES, speed=50);
ev3_runToAbsolutePosition(port='C', position=DEGREES, speed=50);

// # Pause so motors have time to reach target
ev3_pause(1500);  // # wait 1.5 seconds

// # Stop motors
ev3_motorStop(port='B');
ev3_motorStop(port='C');

q3
/* To turn 90°, we need to estimate how much each wheel should rotate.

Assume:

Wheel diameter = 5.6 cm → circumference ≈ 17.6 cm

Wheelbase (distance between the two wheels) ≈ 12 cm

The arc length each wheel travels when turning 90° is:

Arc length = 1/4 * (𝜋 * wheelbase) = .25 * 3.14 * 9.42

Now convert that into degrees of wheel rotation:

9.42 / 17.6 * 360 = 193

So, each wheel rotates about 190°, but in opposite directions. */

// # Turn 90° counterclockwise
turn_degrees = 190;

// # Left motor backward, right motor forward
ev3_runToAbsolutePosition(port='B', position=-turn_degrees, speed=50);
ev3_runToAbsolutePosition(port='C', position=turn_degrees, speed=50);

// # Pause to let motors finish turning / can adjust
ev3_pause(1500);

// # Stop both motors
ev3_motorStop('B');
ev3_motorStop('C');

q4
const wheel_diameter = 5.6;
const wheel_circumference = 3.14 * wheel_diameter;
const to_degrees = x => x / wheel_circumference * 360;

function move_foward(distance) {
    ev3_runToAbsolutePosition(port='B', position=to_degrees(distance), speed=50);
    ev3_runToAbsolutePosition(port='C', position=to_degrees(distance), speed=50);
    ev3_pause(1500); // need to programe this to scale with the distance
    ev3_motorStop(port='B');
    ev3_motorStop(port='C');
}

function turn(direction) {
    
    const turn_degrees = 190;

    if (direction === left)  {
        ev3_runToAbsolutePosition(port='B', position=-turn_degrees, speed=50);
        ev3_runToAbsolutePosition(port='C', position=turn_degrees, speed=50);
        ev3_pause(1500);
        ev3_motorStop('B');
        ev3_motorStop('C');
    } else if (direction === right) {
        ev3_runToAbsolutePosition(port='B', position=turn_degrees, speed=50);
        ev3_runToAbsolutePosition(port='C', position=-turn_degrees, speed=50);
        ev3_pause(1500);
        ev3_motorStop('B');
        ev3_motorStop('C');
    } else /* turn 180 */ {
        ev3_runToAbsolutePosition(port='B', position=2 * turn_degrees, speed=50);
        ev3_runToAbsolutePosition(port='C', position=2 * -turn_degrees, speed=50);
        ev3_pause(1500);
        ev3_motorStop('B');
        ev3_motorStop('C');
    }
}

move_foward(10);
turn(left);
move_foward(5);
turn(right);
move_foward(15);
Editor is loading...
Leave a Comment