この記事はJavaでev3を操作してみたい人のための記事です。
今回はインテリジェントブロックボタンを使っていろいろな操作をしていきたいと思います。
目次
0 . 用意するもの
1 . Buttonクラスを使ったプログラム
2 . Keyインターフェースを使ったプログラム
0.用意するもの
◯ ev3(タンク)
◯ パソコン(VSCode)
◯ bluetooth
◯ microSD
◯ API Documentation(これをみながら進めていくのがオススメです。)
1.Buttonクラスを使ったプログラム
1-0 . 特定のボタンを押したら~するプログラム
◯上のボタンを押したら音がなり、右のボタンを押したらループを終了するプログラムです。
import lejos.hardware.Button;
import lejos.hardware.Sound;
public class Button00
{
public static void main(String[] args)
{
while(true) {
//もし右のボタンが押されたら
if(Button.getButtons() == 8) {
//ループの処理を終了する
break;
//もし上のボタンが押されたら
}else if(Button.getButtons() == 1) {
//音を鳴らす
Sound.beep();
}
}
}
}
Point : Buttonクラス
Point : ボタンの種類
ボタンは数値と紐づけられています。
・上のボタン ID_UP : 1
・下のボタン ID_DOWN : 4
・右のボタン ID_RIGHT : 8
・左のボタン ID_LEFT : 16
・中央のボタン ID_ENTER : 2
・エスケープボタン ID_ESCAPE : 32
1-1 . どれかのボタンを押したら~するプログラム
◯どれかのボタンが押されたらモーターが回転する。ただし、押されたボタンがエスケープボタンだった場合はプログラムを終了する
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.hardware.Button;
public class Button01
{
public static final RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
public static void main(String[] args)
{
while(true) {
//もしどれかのボタンが押されたら
Button.waitForAnyPress();
l_a.forward();
//もしボタンが押されて、それがエスケープボタンだったら
if (Button.waitForAnyPress() == Button.ID_ESCAPE) {
//プログラムを終了する
System.exit(0);
}
}
}
}
1-2 . バンプのプログラム
◯ボタンを押して離したら音を鳴らすプログラムです。
import lejos.hardware.Button;
import lejos.hardware.Sound;
public class Button02
{
public static void main(String[] args)
{
//もしどれかのボタンが押されたら
Button.waitForAnyPress();
//もし何かしらの動作が行われたら
Button.waitForAnyEvent();
Sound.beep();
}
}
1-3 . ボタンの長押しに関するプログラム
◯右ボタンを3秒以上長押しした場合はプログラムを終了し、3秒以内に右ボタンを離した場合は音を鳴らすプログラムです。
import lejos.hardware.Button;
import lejos.hardware.Sound;
public class Button03
{
public static void main(String[] args)
{
while(true) {
//もし右のボタンを押したなら
if(Button.waitForAnyPress() == Button.ID_RIGHT) {
//もし3秒以内にボタンを離したなら
if(Button.waitForAnyEvent(3000) != 0) {
Sound.buzz();
//もし3秒以上ボタンを長押ししたら
}else {
System.exit(0);
}
}
}
}
}
2.Keyインターフェースを使ったプログラム
2-0 . 特定のボタンを押したら~するプログラム
◯それぞれのボタンを押した時に異なる操作をするプログラムです。
import lejos.hardware.Button;
import lejos.hardware.Sound;
public class Key00
{
public static void main(String[] args)
{
while(true) {
//上のボタンを押したら
if(Button.UP.isDown()) {
Sound.beep();
//右のボタンを押したら
}else if(Button.RIGHT.isDown()) {
Sound.buzz();
//エスケープボタンを押したら
}else if(Button.ESCAPE.isDown()) {
System.exit(0);
}
}
}
}
◯Point:Keyインターフェース
2-1 . switch-case文プログラム
◯KeyListenerインターフェースの中にあるメソッドをクラスをつくって実装し、イベント時に呼び出せるようにします。
具体的には、上のボタンを押している間だけ正回転し、下のボタンを押している間だけ負回転するようにします。そしてエスケープキーを押したらプログラムを終了します。
import lejos.hardware.Button;
import lejos.hardware.Key;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.hardware.KeyListener;
public class Key01 {
private static RegulatedMotor l_a = new EV3LargeRegulatedMotor(MotorPort.A);
private static RegulatedMotor l_b = new EV3LargeRegulatedMotor(MotorPort.B);
public static void main(String[] args) {
//イベントがおきた時にメソッドを呼び出せるようにする
Button.UP.addKeyListener(new ButtonEvent(l_a,l_b));
Button.DOWN.addKeyListener(new ButtonEvent(l_a,l_b));
Button.ESCAPE.addKeyListener(new ButtonEvent(l_a,l_b));
while (true) {}
}
}
class ButtonEvent implements KeyListener{ //イベント処理を実装するためにクラスをつくる
//変数を宣言する
private RegulatedMotor lmotor_a;
private RegulatedMotor lmotor_b;
//コンストラクタを設定する
public ButtonEvent(RegulatedMotor Motor_A,RegulatedMotor Motor_B) {
lmotor_a = Motor_A;
lmotor_b = Motor_B;
}
//メソッドをカスタマイズしていく
@Override
public void keyPressed(Key k) { //ボタンを押した時に呼び出されるメソッド
switch (k.getId()){
case Button.ID_UP:
lmotor_a.forward();
lmotor_b.forward();
break;
case Button.ID_DOWN:
lmotor_a.backward();
lmotor_b.backward();
break;
case Button.ID_ESCAPE:
lmotor_a.stop(true);
lmotor_b.stop();
System.exit(0);
}
}
//メソッドをカスタマイズしていく
@Override
public void keyReleased(Key k) { //ボタンを離した時に呼び出されるメソッド
switch (k.getId()){
case Button.ID_UP:
lmotor_a.stop(true);
lmotor_b.stop();
break;
case Button.ID_DOWN:
lmotor_a.stop(true);
lmotor_b.stop();
break;
}
}
}
◯Point:switch-case文
参考資料:switch-case文の使い方総まとめ
最後に
読んで頂きありがとうございました!!
次回はタッチセンサについて書いていきたいと思います!
より良い記事にしていきたいので
◯こうした方がわかりやすい
◯ここがわかりにくい
◯ここが間違っている
◯ここをもっと説明して欲しい
などの御意見、御指摘のほどよろしくお願い致します。