ソースを参照

add simple servo playground

Silas Gruen 5 ヶ月 前
コミット
d6e0f39f22
2 ファイル変更40 行追加0 行削除
  1. 1 0
      playgrounds/mag_dist_playground.py
  2. 39 0
      playgrounds/servo.py

+ 1 - 0
playgrounds/mag_dist_playground.py

@@ -9,6 +9,7 @@ def main():
     # You may need to adjust these to your environment
     config = ConfigParser().config
     gpio_config = config.gpio
+    gpio_config.mag_dist_rot_pin = 14
     if gpio_config.debug:
         gpio = MockGPIO()
     else:

+ 39 - 0
playgrounds/servo.py

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