.. note::
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
**参加する理由は?**
- **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
- **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。
- **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。
- **特別割引**:最新製品の独占割引をお楽しみください。
- **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう!
.. _1.3.1_py_pi5:
1.3.1 モーター
===================================
はじめに
-----------------
このプロジェクトでは、L293Dを使用してDCモーターを制御し、時計回りと反時計回りに回転させる方法を学びます。 DCモーターはより大きな電流が必要ですので、安全のためにここでは電源供給モジュールを使用してモーターに電力を供給します。
必要な部品
------------------------------
このプロジェクトには、次のコンポーネントが必要です。
.. image:: ../python_pi5/img/1.3.1_motor_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_power_module`
- \-
* - :ref:`cpn_l293d`
- \-
* - :ref:`cpn_motor`
- |link_motor_buy|
回路図
------------------
.. image:: ../python_pi5/img/1.3.1_motor_schematic.png
実験手順
---------------------------
**ステップ1:** 回路を組み立てます。
.. image:: ../python_pi5/img/1.3.1_motor_circuit.png
.. note::
電源モジュールには、キットに含まれている9Vバッテリーと9Vバッテリーバックルを使用できます。 電源モジュールのジャンパーキャップをブレッドボードの5Vバスストリップに挿入してください。
.. image:: ../python_pi5/img/1.3.1_motor_battery.jpeg
**ステップ2**: コードのフォルダに入ります。
.. raw:: html
.. code-block::
cd ~/raphael-kit/python-pi5
**ステップ3: 実行します。**
.. raw:: html
.. code-block::
sudo python3 1.3.1_Motor_zero.py
コードが実行されると、モーターは最初に時計回りに5秒間回転し、5秒間停止し、その後、反時計回りに5秒間回転し、その後、モーターは5秒間停止します。この一連の動作は繰り返し実行されます。
.. warning::
エラー メッセージ ``RuntimeError: Cannot determine SOC peripheral base address`` が表示された場合は、 :ref:`faq_soc` を参照してください。
**コード**
.. note::
以下のコードは変更/リセット/コピー/実行/停止ができます。ただし、コードを変更する前に ``raphael-kit/python_5`` のようなソースコードのパスに移動する必要があります。コードを変更した後、直接実行して効果を確認できます。
.. raw:: html
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import Motor
from time import sleep
# Initialize the Motor with GPIO Zero, specifying GPIO pins for forward (17), backward (27), and enable (22) control
motor = Motor(forward=17, backward=27, enable=22)
try:
# Main function to control the motor's direction and movement.
# Alternates motor rotation between clockwise and counterclockwise with stops in between.
actions = {'CW': motor.forward, 'CCW': motor.backward, 'STOP': motor.stop} # Define motor actions for readability
while True:
# Loop through the defined actions to control motor direction
for action in ['CW', 'STOP', 'CCW', 'STOP']:
actions[action]() # Execute the current action (forward, stop, backward, stop)
print(f"{action}") # Display the current action in the console
sleep(5) # Pause for 5 seconds before proceeding to the next action
except KeyboardInterrupt:
# Gracefully handle a keyboard interrupt (e.g., Ctrl+C) to stop the program
pass
**コードの説明**
1. これらのインポートステートメントは、 ``gpiozero`` ライブラリから ``Motor`` クラスと、 ``time`` モジュールから ``sleep`` 関数を取り込みます。
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import Motor
from time import sleep
2. この行は、前進(17)、後退(27)、および有効(22)制御のためのGPIOピンを指定して ``Motor`` オブジェクトを初期化します。
.. code-block:: python
# Initialize the Motor with GPIO Zero, specifying GPIO pins for forward (17), backward (27), and enable (22) control
motor = Motor(forward=17, backward=27, enable=22)
3. モーター制御のアクションは、可読性のために辞書で定義されています。無限ループ( ``while True`` )がこれらのアクションを繰り返し、各アクションを5秒間実行します。
.. code-block:: python
try:
# Main function to control the motor's direction and movement.
# Alternates motor rotation between clockwise and counterclockwise with stops in between.
actions = {'CW': motor.forward, 'CCW': motor.backward, 'STOP': motor.stop} # Define motor actions for readability
while True:
# Loop through the defined actions to control motor direction
for action in ['CW', 'STOP', 'CCW', 'STOP']:
actions[action]() # Execute the current action (forward, stop, backward, stop)
print(f"{action}") # Display the current action in the console
sleep(5) # Pause for 5 seconds before proceeding to the next action
4. このセグメントは、キーボード割り込み(Ctrl+Cなど)を使用してプログラムを安全に終了できるようにします。
.. code-block:: python
except KeyboardInterrupt:
# Gracefully handle a keyboard interrupt (e.g., Ctrl+C) to stop the program
pass