EV3 MicroPython の使い方
開発環境の構築で,プロジェクトの作成から EV3 インテリジェントブロックへのプログラムの転送・実行方法は説明しているので,手順を失念した方は再度振り返ってください.
プロジェクトについて
アクティビティバーの EV3 Micro Python
から,Create a new project
をクリックして新規にプロジェクトを作成してください
プロジェクト名は,ev3first
にします.どんなプロジェクト名でも構わないのですが,ここでは,同じプロジェクト名にしてください.
自動的にプロジェクト名 (ev3first
) のフォルダと関連するファイルも同時に生成されます.基本的には,main.py
を編集してプログラムを作成します.
ev3first
├── .gitignore
├── .vscode
│ ├── extensions.json
│ ├── launch.json
│ └── settings.json
└── main.py
main.py
は,下記のようになっています.#
から始まる行は,コメント(説明)なので削除しても構いませんが,1 行目 の #!/usr/bin/env pybricks-micropython
は削除してはいけません.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import ( Motor , TouchSensor , ColorSensor ,
InfraredSensor , UltrasonicSensor , GyroSensor )
from pybricks.parameters import Port , Stop , Direction , Button , Color
from pybricks.tools import wait , StopWatch , DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile , ImageFile
# This program requires LEGO EV3 MicroPython v2.0 or higher.
# Click "Open user guide" on the EV3 extension tab for more information.
# Create your objects here.
ev3 = EV3Brick ()
# Write your program here.
ev3 . speaker . beep ()
EV3 の前進の NXT のセンサを使うには以下の設定が必要になります.ここでは,NXT Sound Sensor
を使うときに必要になります.main.py
の 9 行目あたりに追加します.
from pybricks.nxtdevices import SoundSensor
16 行目までは,基本的にはそのままで,19 行以降にプログラムを作成していきます.
ここでは,ev3.speaker.beep()
は,ビープ音を鳴らすメソッドになります.
メソッドは関数の 1 種で詳しく,プログラミングの授業を復習してください.
プログラミング演習
詳細は,指導書を見てください.
また,適宜,オンラインヘルプを参照して,クラス,関数などの使い方を確認してください.
EV3 MicroPython
については,VS Code の拡張機能の中に同封されているので,
アクティビティバーの EV3 Micro Python
から,Open user guide
をクリックするとブラウザーに表示されます.
モータ
PWM 信号のデューティ比によりモータ速度を調整する方法
MicroPython Robot C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import ( Motor , TouchSensor , ColorSensor ,
InfraredSensor , UltrasonicSensor , GyroSensor )
from pybricks.parameters import Port , Stop , Direction , Button , Color
from pybricks.tools import wait , StopWatch , DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile , ImageFile
ev3 = EV3Brick ()
motorB = Motor ( Port . B )
motorC = Motor ( Port . C )
motorB . dc ( 50 )
motorC . dc ( 50 )
wait ( 3000 )
task main ()
{
setMotorSpeed ( motorB , 50 ); // ポートBのモータを50%のデューティ比で正転
setMotorSpeed ( motorC , 50 ); // ポートCのモータを50%のデューティ比で正転
wait1Msec ( 3000 ); // 3000ミリ秒間 (3秒間)待機
}
モータの回転速度と回転度を指定して動かす方法
MicroPython Robot C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import ( Motor , TouchSensor , ColorSensor ,
InfraredSensor , UltrasonicSensor , GyroSensor )
from pybricks.parameters import Port , Stop , Direction , Button , Color
from pybricks.tools import wait , StopWatch , DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile , ImageFile
ev3 = EV3Brick ()
motorB = Motor ( Port . B )
motorC = Motor ( Port . C )
motorB . run_angle ( 3600 , 360 , Stop . HOLD , False ) # 第4引数をFalseにすると指定角度到達まで待たない
motorC . run_angle ( 3600 , - 360 ) # 第4引数はデフォルトでTrueなので指定角度到達まで待つ
task main ()
{
moveMotorTarget ( motorB , 360 , 50 ); // ポートBのモータを+50%で360度回転させる
moveMotorTarget ( motorC , 360 , -50 ); // ポートCのモータを-50%で360度回転させる
waitUntilMotorStop ( motorB ); // ポートBのモータが停止するまで保持
waitUntilMotorStop ( motorC ); // ポートCのモータが停止するまで保持
}
各種センサ
センサの種類
MicroPython Robot C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 #!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import ( Motor , TouchSensor , ColorSensor ,
InfraredSensor , UltrasonicSensor , GyroSensor )
from pybricks.parameters import Port , Stop , Direction , Button , Color
from pybricks.tools import wait , StopWatch , DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile , ImageFile
from pybricks.nxtdevices import SoundSensor
ev3 = EV3Brick ()
def displayTextLine ( y , s ): # Robot C 同様の関数を定義
ev3 . screen . draw_text ( 0 , y * 16 , s )
ts = TouchSensor ( Port . S1 )
ss = SoundSensor ( Port . S2 )
cs = ColorSensor ( Port . S3 )
us = UltrasonicSensor ( Port . S4 )
while True :
ev3 . screen . clear ()
displayTextLine ( 0 , 'touch: ' + str ( ts . pressed ()))
displayTextLine ( 1 , 'sound: ' + str ( ss . intensity ()))
displayTextLine ( 2 , 'light: ' + str ( cs . reflection ()))
displayTextLine ( 3 , 'sonar: ' + str ( us . distance ()))
wait ( 200 )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 task main ()
{
SensorType [ S1 ] = sensorEV3_Touch ; // センサのタイプを宣言
SensorType [ S2 ] = sensorSoundDB ;
SensorType [ S3 ] = sensorEV3_Color ;
SensorType [ S4 ] = sensorEV3_Ultrasonic ;
while ( true ) {
// 0 or 1
displayTextLine ( 0 , "touch: %d " , SensorValue ( S1 )); // 液晶の0行目にポート1のセンサ(タッチ)の値を表示
// 0 (silent) to 100 (noisy)
displayTextLine ( 1 , "sound: %d " , SensorValue ( S2 )); // 液晶の1行目にポート2のセンサ(サウンド)の値を表示
// 0 (dark) to 100 (light)
displayTextLine ( 2 , "light: %d " , SensorValue ( S3 )); // 液晶の2行目にポート3のセンサ(カラー)の値を表示
// 0[cm] to 255[cm]
displayTextLine ( 3 , "sonar: %d " , SensorValue ( S4 )); // 液晶の3行目にポート4のセンサ(超音波)の値を表示
wait1Msec ( 200 );
}
}
超音波センサ
MicroPython Robot C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 #!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import ( Motor , TouchSensor , ColorSensor ,
InfraredSensor , UltrasonicSensor , GyroSensor )
from pybricks.parameters import Port , Stop , Direction , Button , Color
from pybricks.tools import wait , StopWatch , DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile , ImageFile
ev3 = EV3Brick ()
motorB = Motor ( Port . B )
motorC = Motor ( Port . C )
us = UltrasonicSensor ( Port . S4 )
while us . distance () > 100 : # distance() が 100mmより大きい間,繰り返し
motorB . dc ( 50 )
motorC . dc ( 50 )
motorB . stop () # モータ停止
motorC . stop ()
ev3 . speaker . set_volume ( 75 )
ev3 . speaker . play_file ( SoundFile . OKAY )
1
2
3
4
5
6
7
8
9
10
11
12
13
14 task main ()
{
repeatUntil ( getUSDistance ( S4 ) < 10 ) // ポート4のセンサ(超音波)の値が10以下に なるまで
{
setMotorSpeed ( motorB , 50 ); // モータB,Cの速度を50%に指定
setMotorSpeed ( motorC , 50 );
}
setMotorSpeed ( motorB , 0 ); // モータB,Cの速度を0%に指定
setMotorSpeed ( motorC , 0 );
setSoundVolume ( 75 ); // 音量を75に設定
playSoundFile ( "Okay" ); // サウンドファイルOkayを再生
sleep ( 1000 ); // 1000msecスリープ
}
ジャイロセンサ
MicroPython Robot C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import ( Motor , TouchSensor , ColorSensor ,
InfraredSensor , UltrasonicSensor , GyroSensor )
from pybricks.parameters import Port , Stop , Direction , Button , Color
from pybricks.tools import wait , StopWatch , DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile , ImageFile
ev3 = EV3Brick ()
motorB = Motor ( Port . B )
motorC = Motor ( Port . C )
gs = GyroSensor ( Port . S2 )
gs . reset_angle ( 0 )
while gs . angle () < 90 : # angle()が90度未満の間,繰り返し
motorB . dc ( 50 )
motorC . dc ( - 50 )
motorB . brake () # ブレーキ
motorC . brake ()
gyro . py ( END )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 task main ()
{
//Resets the gyro on port 2 to 0 degrees
resetGyro ( S2 );
//Keep looping until the gyro sensor reads greater
//than 90 degrees
repeatUntil ( getGyroDegrees ( S2 ) > 90 )
{
//Point turn to the left
setMotorSpeed ( motorC , -50 );
setMotorSpeed ( motorB , 50 );
}
//Stop the motors at the end of the turn
setMotorSpeed ( motorB , 0 );
setMotorSpeed ( motorC , 0 );
}