# box_walker.py
# Copyright, 2026, Garth Zeglin.
# Sample Webots controller file for driving a rectilinear robot with two hinged faces used as 'legs'.

print("box_walker.py waking up.")

# Import standard Python libraries.
import math

# Import the Webots simulator API.
from controller import Robot

# Define the time step in milliseconds between controller updates.
EVENT_LOOP_DT = 20

# Request a proxy object representing the robot to control.
robot = Robot()
robot_name = robot.getName()
print("%s: controller connected." % (robot_name))

# Fetch handles for the motors.
hinge1 = robot.getDevice('hinge1')
hinge2 = robot.getDevice('hinge2')

# Set time constants for the overall oscillation.
period = 3.0

# run loop to execute a periodic script until the simulation quits.
# If the controller returns -1, the simulator is quitting.
while robot.step(EVENT_LOOP_DT) != -1:
    # Read simulator clock time.
    t = robot.getTime()

    # Oscillate the hinge angles.  Each hinge swings 180 degrees, but out of phase.
    # The phase variable advances by 360 degrees each period.
    phase = t * (2*math.pi/period)

    hinge1.setPosition(math.pi * max(0, math.sin(phase)))
    hinge2.setPosition(math.pi * max(0, math.sin(phase + math.pi)))
