# trackbase.py
# Copyright, 2026, Garth Zeglin.
# Sample Webots controller file for driving a dual-track base.

print("trackbase.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('left_motor')
hinge2 = robot.getDevice('right_motor')

# velocity control mode
hinge1.setPosition(math.inf)
hinge2.setPosition(math.inf)

# 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()

    # Pulse running motors forward.
    if t % 4.0 < 3.0:
        hinge1.setVelocity(0.1)
        hinge2.setVelocity(0.1)
    else:
        hinge1.setVelocity(0.0)
        hinge2.setVelocity(0.0)
