◆ Lego Mindstoms NXT - leJOS - (15) タイマーとストップウォッチ ================================================================================ NXTボタンを使ったリスナー、センサーを使ったリスナーは、それぞれ、 (8) ボタン・リスナー 2008-08-26 http://blog.goo.ne.jp/marunomarunogoo/d/20080826 (9) リスナー 2008-09-02 http://blog.goo.ne.jp/marunomarunogoo/d/20080902 で行いました。今度は、タイマーを使ったリスナーを学習しましょう。 ■時間がきたらプログラムを終える タイマーを使って、時間がきたらプログラムを終了させます。 ついでに、今までのEscボタン押下でもプログラムが止まるように、ButtonListener も合 わせて実装しましょう。 □リスナーを利用するクラス まずは、その場で10秒間回転するプログラムです。 ただし、mainメソッドの中では、20秒間回転するようになっています。 タイマーリスナーを使い、10秒で終了するようにしています。 TimerListener01.java ------------------------------------------------------ import jp.marunomaruno.nxt.timer.s01.ExitListener; import lejos.nxt.Motor; import lejos.util.Timer; import lejos.util.TimerListener; /** * 「トライボット」使用 * その場で10秒間回転する。 * * @author marunomaruno * @version 1.0, 2010/03/08 * @since 1.0 */ public class TimerListener01 { public static void main(String[] args) throws Exception { // 10秒でプログラムを終了するタイマーを設定する。 TimerListener timerListener = new ExitListener(); // (1) Timer timer = new Timer(10000, timerListener); // (2) timer.start(); // (3) // その場で20秒回転する。 Motor.B.forward(); Motor.C.backward(); Thread.sleep(20000); timer.stop(); // (4) } } ------------------------------------------------------ □TimerListener timerListener = new ExitListener(); // (1) タイマーリスナーのオブジェクトを生成します。 これは、TimerListenerインターフェースを実装したオブジェクトになります。 今回は、ExitListenerクラスとして作りました。 □Timer timer = new Timer(10000, timerListener); // (2) タイマーリスナーは、このタイマーオブジェクトに設定します。 コンストラクター --- Timer(int theDelay, TimerListener el) イベント遅延の時間をtheDelayミリ秒とし、リスナーelとするタイマーオブジェクト を生成するとコンストラクターです。 --- メソッド --- int getDelay() theDelayミリ秒経過するごとに、 timedOut()メソッドにメッセージを送ります。 void start() タイマーを起動します。このとき、timedOut()メソッドにメッセージを送ります。 void stop() タイマーを停止します。 --- □timer.start(); // (3) タイマーを起動します。 □timer.stop(); // (4) タイマーを停止します。 ■ExitListener プログラムを終わらせるリスナーです。 このリスナーは、いずれかのボタン(想定はESCAPEボタン)押下または、指定された時間に なったらプログラムを終了するリスナーとして作ります。 ------------------------------------------------------ package jp.marunomaruno.nxt.timer.s01; import lejos.nxt.Button; import lejos.nxt.ButtonListener; import lejos.nxt.Sound; import lejos.util.TimerListener; /** * Escボタン押下または、指定された時間になったらプログラムを終了するリスナー。 * * @author marunomaruno * @version 1.0, 2010/03/08 * @since 1.0 */ public class ExitListener implements ButtonListener, TimerListener { // (1) /* (non-Javadoc) * @see lejos.nxt.ButtonListener#buttonPressed(lejos.nxt.Button) */ public void buttonPressed(Button button) { exit(262); // C } /* (non-Javadoc) * @see lejos.nxt.ButtonListener#buttonReleased(lejos.nxt.Button) */ public void buttonReleased(Button button) { exit(277); // C# } /* (non-Javadoc) * @see lejos.util.TimerListener#timedOut() */ public void timedOut() { // (2) exit(294); // D } /** * トーンを鳴らしてプログラムを終了する。 * @param freq 周波数 */ private void exit(int freq) { // (3) Sound.playTone(freq, 200); Sound.pause(300); System.exit(0); } } ------------------------------------------------------ □public class ExitListener implements ButtonListener, TimerListener { // (1) このクラスには、ButtonListenerインターフェースと、TimerListenerインターフェース を実装しています。 ButtonListenerインターフェースは、以下のメソッドがあります。詳しくは、 (8) ボタン・リスナー を参照してください。 public void buttonPressed(Button button) public void buttonReleased(Button button) また、TimerListenerインターフェースは、以下のメソッドを持ちます。 public void timedOut() □public void timedOut() { // (2) このtimedOut()に、タイムアウトしたときの処理を記述します。今回は、このクラスのpr ivateメソッドであるexit()メソッドを呼び出して、そこでプログラムを終了させます。 □private void exit(int freq) { // (3) これは、指定された周波数のトーンを鳴らしてプログラムを終了します。 ■ストップウォッチ 今度は、ストップウォッチのように、NXT画面に動かしてからの経過秒数を表示するプロ グラムを作りましょう。 □TimerListener02.java ------------------------------------------------------ import jp.marunomaruno.nxt.timer.s01.StopwatchListener; import lejos.nxt.Button; import lejos.nxt.Motor; import lejos.util.Timer; import lejos.util.TimerListener; /** * 「トライボット」使用 * その場で回転し、1秒ごとに経過時間を画面に表示する。 * * @author marunomaruno * @version 1.0, 2010/03/08 * @since 1.0 */ public class TimerListener02 { public static void main(String[] args) throws Exception { // 1秒ごとに画面表示するタイマーを設定する。 TimerListener timerListener = new StopwatchListener(); // (1) Timer timer = new Timer(1000, timerListener); // 開始する。 System.out.println("Press any button to start stopwatch."); Button.waitForPress(); timer.start(); // (2) // その場で回転する。 Motor.B.forward(); Motor.C.backward(); // 終了する。 System.out.println("Press any button to stop stopwatch."); Button.waitForPress(); timer.stop(); // (3) Motor.B.stop(); Motor.C.stop(); // プログラムを終了する。 System.out.println("Press any button to exit program."); Button.waitForPress(); } } ------------------------------------------------------ □TimerListener timerListener = new StopwatchListener(); // (1) つぎで作る、ストップウォッチを実装するリスナーのオブジェクトを生成します。 □timer.start(); // (2) タイマーを起動します。 □timer.stop(); // (3) タイマーを停止します。 ■ストップウォッチ・リスナー 呼び出されるごとに経過時間を画面に表示するリスナーです。 □StopwatchListener.java ------------------------------------------------------ package jp.marunomaruno.nxt.timer.s01; import lejos.util.Stopwatch; import lejos.util.TimerListener; /** * 呼び出されるごとに経過時間を画面に表示する。 * * @author marunomaruno * @version 1.0, 2010/03/08 * @since 1.0 */ public class StopwatchListener implements TimerListener { // (1) private Stopwatch stopwatch; // (2) ストップウォッチ /** * コンストラクター。 */ public StopwatchListener() { this.stopwatch = new Stopwatch(); // (3) this.stopwatch.reset(); // (4) } /* (non-Javadoc) * @see lejos.util.TimerListener#timedOut() */ @Override public void timedOut() { // (5) System.out.println(stopwatch.elapsed() / 1000); // (6) } } ------------------------------------------------------ □public class StopwatchListener implements TimerListener { // (1) TimerListenerを実装します。 □private Stopwatch stopwatch; // (2) ストップウォッチ ストップウォッチ・オブジェクトを入れるフィールドを宣言します。 コンストラクター --- Stopwatch() --- メソッド --- int elapsed() 経過時間をミリ秒単位で返します。 void reset() 時間をリセットします。 --- □this.stopwatch = new Stopwatch(); // (3) ストップウォッチ・オブジェクトを生成します。 コンストラクターは、この引数のないものだけです。 □this.stopwatch.reset(); // (4) ストップウォッチをリセットします。 □public void timedOut() { // (5) タイムアウトしたときのメソッドを定義します。 □System.out.println(stopwatch.elapsed() / 1000); // (6) ストップウォッチから経過時間を取得して表示します。elapsed()メソッドは、ミリ秒で 取得するので、秒単にするため、1000で割っています。 以上