# legwheel.py
# No copyright, 2026 Garth Zeglin.  This file is explicitly placed in the public domain.
# Sample Webots controller file for driving a multi-spoked rimless wheel with telescoping 'legs'.

print("legwheel.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))
print("%s has %d devices." % (robot_name, robot.getNumberOfDevices()))

# Fetch handles for the motors.
legs = []
for i in range(robot.getNumberOfDevices()):
    dev = robot.getDeviceByIndex(i)
    if dev.name.startswith('motor'):
        legs.append(dev)
num_legs = len(legs)

# Set position control mode.
for leg in legs:
    leg.setPosition(0.0)

# Set time constants for the overall oscillation.
period = 4.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()

    # Extend one leg at a time.
    for i, leg in enumerate(legs):
        phase = num_legs * (t / period)
        if (num_legs - 1 - (int(phase) % num_legs) == i):
            leg.setPosition(0.050)
        else:
            leg.setPosition(0.000)
