注釈
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
参加する理由は?
エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。
独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。
特別割引:最新製品の独占割引をお楽しみください。
祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[ここ]をクリックして今すぐ参加しましょう!
1.3.2 サーボ¶
はじめに¶
このプロジェクトでは、サーボを回転させる方法を学びます。
必要な部品¶
このプロジェクトには、次のコンポーネントが必要です。
一式を購入するのが便利です、こちらがリンクです:
名前 |
このキットのアイテム |
リンク |
---|---|---|
Raphael Kit |
337 |
以下のリンクから別々に購入することもできます。
コンポーネントの紹介 |
購入リンク |
---|---|
回路図¶
実験手順¶
ステップ1: 回路を組み立てます。
ステップ2: コードのフォルダに移動します。
cd ~/raphael-kit/python-pi5
ステップ3: 実行可能ファイルを実行します。
sudo python3 1.3.2_Servo_zero.py
プログラムが実行されると、サーボは0度から90度、180度まで回転し、次に180度から90度、0度まで回転します。これが繰り返されます。
コード
注釈
以下のコードは変更/リセット/コピー/実行/停止ができます。ただし、コードを変更する前に raphael-kit/python_5
のようなソースコードのパスに移動する必要があります。コードを変更した後、直接実行して効果を確認できます。
#!/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
コードの説明
これらのインポート文は、サーボ制御のための
Servo
クラスとタイミングのためのsleep
関数を取り込みます。#!/usr/bin/env python3 from gpiozero import Servo from time import sleep
サーボモーターを接続するためのGPIOピン番号を18に設定します。
# 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
ループが含まれています。サーボは中間位置、最小位置、最大位置に配置され、各位置が表示され、指定された期間保持されます。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