|
|
@@ -0,0 +1,39 @@
|
|
|
+import pigpio
|
|
|
+
|
|
|
+def main():
|
|
|
+
|
|
|
+ pi = pigpio.pi()
|
|
|
+ servo_pin = 14
|
|
|
+
|
|
|
+ print("Servo Playground")
|
|
|
+ print("Type an angle (0-180) to set the servo, or 'q' to quit.")
|
|
|
+
|
|
|
+ while True:
|
|
|
+ cmd = input("Enter angle: ").strip()
|
|
|
+ if cmd == "q":
|
|
|
+ print("Exiting.")
|
|
|
+ break
|
|
|
+
|
|
|
+ if not pi.connected:
|
|
|
+ raise RuntimeError(f"Failed to connect to pigpio daemon for pin {pin}")
|
|
|
+
|
|
|
+ try:
|
|
|
+ angle = float(cmd)
|
|
|
+ if not (0 <= angle <= 180):
|
|
|
+ print("Angle must be between 0 and 180.")
|
|
|
+ continue
|
|
|
+
|
|
|
+ # Map angle to pulse width
|
|
|
+ pulsewidth = int(500 + (angle / 180.0) * 2000)
|
|
|
+ pi.set_servo_pulsewidth(servo_pin, pulsewidth)
|
|
|
+
|
|
|
+ print(f"Set servo to {angle} degrees.")
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ print(f"Invalid input: {e}")
|
|
|
+ finally:
|
|
|
+ pi.write(servo_pin, 0)
|
|
|
+ pi.stop()
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ main()
|