def set_target_one(device, position, servo=1):
"""Sends the target position to a specific servo channel using a control transfer."""
# bmRequestType = 0x40 (Vendor-defined request, host-to-device direction)
bm_request_type = 0x40
b_request = REQUEST_SET_TARGET
# Value parameter requires position in quarter-microseconds (input * 4)
w_value = int(position * 4)
w_index = int(servo)
try:
# usb.core.Device.ctrl_transfer(bmRequestType, bRequest, wValue, wIndex, data_or_wLength, timeout)
device.ctrl_transfer(
bm_request_type, b_request, w_value, w_index, data_or_wLength=0, timeout=5000
)
except usb.core.USBError as e:
print(f"USB Transfer failed: {e}", file=sys.stderr)
Currently, I can only move one servo at a time. This is a blocker for the arm I am producing.
And…
One of the servos controlled via the Maestro 18-Channel module seems to like to spin freely when given commands. It listens to 0 being a stop function but spins in only one direction freely at the same speed no matter if I choose 1 or 180 for its start to stop procedure.
See the below code for this effort:
def main():
print("Searching for Pololu Maestro device...")
device = find_maestro_device()
if device is None:
print("Error: Maestro device not found. Check physical connections or permissions.")
sys.exit(1)
print("Device found and initialized successfully.")
servo_channel = '0'
servo_channel = '1'
servo_channel = '2'
try:
while True:
user_input = input("Enter position (or 'q' to quit): ").strip()
if user_input.lower() == "q":
break
try:
position = int(user_input)
set_target_zero(device, position, servo_channel)
set_target_one(device, position, servo_channel)
set_target_two(device, position, servo_channel)
except ValueError:
print("Invalid input. Please enter an integer number.")
except KeyboardInterrupt:
print("\nExiting program.")
finally:
# PyUSB handles closing the device and recycling handles implicitly on exit,
# but clearing configurations ensures safe detachment.
usb.util.dispose_resources(device)
if __name__ == "__main__":
main()
Does anyone see what I may be doing versus what needs to be done so that I can control the Maestro 18-Channel servo module with more control?
Seth
