0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LEGOマインドストームEV3のジャイロセンサーで角度取得

0
Posted at

LEGOで作成したロボットなどの角度を取得する方法

EV3_gyro.jpg
EV3のポート1に接続されたジャイロセンサーの値を取得するPythonスクリプト

gyro_test_print.py
#!/usr/bin/env python3
from ev3dev2.sensor.lego import GyroSensor
from ev3dev2.sensor import INPUT_1
import time

# 1. ジャイロセンサーの初期化 (ポート1)
try:
    gyro = GyroSensor(INPUT_1)
    print("Gyro Sensor connected.")
except Exception as e:
    print("Error: Sensor not found on Port 1")
    exit()

# 2. センサー値のリセット
print("Resetting Gyro... Keep the sensor still.")
gyro.mode = 'GYRO-ANG'  # 角度モードに設定
time.sleep(1)           # 安定するまで待機

print("Start reading (Press Ctrl+C to stop)...")

try:
    while True:
        # 3. 角度の取得
        angle = gyro.angle
        
        # 4. 角度の表示
        # Python 2/3 両対応の書き方に変更してエラーを防止
        print("Angle: {0}".format(angle))
        
        # 5. 0.1秒待機
        time.sleep(0.1)

except KeyboardInterrupt:
    # Ctrl+C(またはVSCodeの停止ボタン)で終了
    print("\nStopped.")

<実行手順>
1.接続したev3devに作成したPythonスクリプトをSend to workspace to device
2.送信したファイルtyro_test_print.py上で右クリックして,
  Run in interactive terminalを実行

run出力表示.jpg

すると,ジャイロセンサーの角度値がVSCodeのターミナルウィンドウに表示される.

run出力表示_Angle.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?