.. note:: こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。 **参加する理由は?** - **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。 - **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。 - **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。 - **特別割引**:最新製品の独占割引をお楽しみください。 - **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。 👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう! .. _1.3.2_py_pi5: 1.3.2 サーボ ======================= はじめに ----------------- このプロジェクトでは、サーボを回転させる方法を学びます。 必要な部品 ------------------------------ このプロジェクトには、次のコンポーネントが必要です。 .. image:: ../python_pi5/img/1.3.2_servo_list.png 一式を購入するのが便利です、こちらがリンクです: .. list-table:: :widths: 20 20 20 :header-rows: 1 * - 名前 - このキットのアイテム - リンク * - Raphael Kit - 337 - |link_Raphael_kit| 以下のリンクから別々に購入することもできます。 .. list-table:: :widths: 30 20 :header-rows: 1 * - コンポーネントの紹介 - 購入リンク * - :ref:`cpn_gpio_board` - |link_gpio_board_buy| * - :ref:`cpn_breadboard` - |link_breadboard_buy| * - :ref:`cpn_wires` - |link_wires_buy| * - :ref:`cpn_servo` - |link_servo_buy| 回路図 -------------------- .. image:: ../img/image337.png 実験手順 ----------------------- **ステップ1:** 回路を組み立てます。 .. image:: ../img/image125.png **ステップ2**: コードのフォルダに移動します。 .. raw:: html .. code-block:: cd ~/raphael-kit/python-pi5 **ステップ3**: 実行可能ファイルを実行します。 .. raw:: html .. code-block:: sudo python3 1.3.2_Servo_zero.py プログラムが実行されると、サーボは0度から90度、180度まで回転し、次に180度から90度、0度まで回転します。これが繰り返されます。 コード .. note:: 以下のコードは変更/リセット/コピー/実行/停止ができます。ただし、コードを変更する前に ``raphael-kit/python_5`` のようなソースコードのパスに移動する必要があります。コードを変更した後、直接実行して効果を確認できます。 .. raw:: html .. code-block:: python #!/usr/bin/env python3 from gpiozero import Servo from time import sleep # Set the GPIO pin number where the servo motor is connected myGPIO = 18 # Define a correction factor to fine-tune servo pulse width myCorrection = 0.45 maxPW = (2.0 + myCorrection) / 1000 # Calculate maximum pulse width minPW = (1.0 - myCorrection) / 1000 # Calculate minimum pulse width # Initialize the Servo object with custom pulse widths servo = Servo(myGPIO, min_pulse_width=minPW, max_pulse_width=maxPW) try: while True: # Position the servo at the middle and wait servo.mid() print("mid") # Indicate current position sleep(0.5) # Brief pause for 0.5 seconds # Move the servo to its minimum position and wait servo.min() print("min") # Indicate current position sleep(1) # Hold position for 1 second # Return the servo to the middle position and wait servo.mid() print("mid") # Indicate current position sleep(0.5) # Brief pause for 0.5 seconds # Move the servo to its maximum position and wait servo.max() print("max") # Indicate current position sleep(1) # Hold position for 1 second except KeyboardInterrupt: # Gracefully terminate the script on a keyboard interrupt (Ctrl+C) pass **コードの説明** 1. これらのインポート文は、サーボ制御のための ``Servo`` クラスとタイミングのための ``sleep`` 関数を取り込みます。 .. code-block:: python #!/usr/bin/env python3 from gpiozero import Servo from time import sleep 2. サーボモーターを接続するためのGPIOピン番号を18に設定します。 .. code-block:: python # Set the GPIO pin number where the servo motor is connected myGPIO = 18 3. これらの行は、補正係数を定義し、それを使用してサーボの最大および最小パルス幅を計算します。これにより、サーボの動きの範囲を微調整できます。 .. code-block:: python # Define a correction factor to fine-tune servo pulse width myCorrection = 0.45 maxPW = (2.0 + myCorrection) / 1000 # Calculate maximum pulse width minPW = (1.0 - myCorrection) / 1000 # Calculate minimum pulse width 4. カスタムパルス幅でサーボオブジェクトを初期化します。 .. code-block:: python # Initialize the Servo object with custom pulse widths servo = Servo(myGPIO, min_pulse_width=minPW, max_pulse_width=maxPW) 5. ``try`` ブロックには、サーボを連続的に動かすための ``while True`` ループが含まれています。サーボは中間位置、最小位置、最大位置に配置され、各位置が表示され、指定された期間保持されます。 .. code-block:: python try: while True: # Position the servo at the middle and wait servo.mid() print("mid") # Indicate current position sleep(0.5) # Brief pause for 0.5 seconds # Move the servo to its minimum position and wait servo.min() print("min") # Indicate current position sleep(1) # Hold position for 1 second # Return the servo to the middle position and wait servo.mid() print("mid") # Indicate current position sleep(0.5) # Brief pause for 0.5 seconds # Move the servo to its maximum position and wait servo.max() print("max") # Indicate current position sleep(1) # Hold position for 1 second except KeyboardInterrupt: # Gracefully terminate the script on a keyboard interrupt (Ctrl+C) pass