OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / samples / SpeedTest / SpeedTest.java
1 import lejos.nxt.*;
2
3 public class SpeedTest {
4
5  static final int TOTALTIME = 60000;
6
7  public static void main(String [] args) throws Exception {
8   byte A = 0;
9   LightSensor ls = new LightSensor(SensorPort.S3);
10   UltrasonicSensor us = new UltrasonicSensor(SensorPort.S1);
11   /* Disable the speed regulation and close down the associated threads.
12    * This test does not require this type of motion control.
13    */
14   Motor.A.regulateSpeed(false);
15   Motor.B.regulateSpeed(false);
16   Motor.C.regulateSpeed(false);
17   Motor.A.shutdown();
18   Motor.B.shutdown();
19   Motor.C.shutdown();
20   
21   Motor.B.forward();
22   Motor.C.forward();
23   int iteration = 0;
24   int startTime = (int)System.currentTimeMillis();
25   int totalTime = 0;
26   int tacho = 0;
27   int distVal=0;
28   int lightVal=0;
29   while(totalTime < TOTALTIME) {
30    lightVal = ls.readValue();
31    distVal = us.getDistance();
32    tacho = Motor.B.getTachoCount();
33    int RN = (int)(Math.random() * 100) + 1;
34
35    // Uncomment the following to produce display output as per the original test.
36    //LCD.drawInt(tacho,0,0);
37    //LCD.drawInt((lightVal + distVal + tacho)*100/RN, 0, 1);
38    //LCD.drawInt(A, 0, 2);
39    //LCD.drawInt(iteration, 0, 4);
40    //LCD.refresh();
41
42    // Set motor speed for B and C to RN (Using Coast)
43
44    Motor.B.setPower(RN);
45    Motor.C.setPower(RN);
46    if(RN > 50) ++A;
47    if(RN < 50) --A;
48    
49    if(A<0) Motor.A.backward(); else Motor.A.forward();
50
51    totalTime = (int)System.currentTimeMillis() - startTime;
52    iteration++;
53   }
54   LCD.drawInt(iteration, 0, 4);
55   LCD.refresh();
56
57   Thread.sleep(10000);
58  }
59 }
60