OSDN Git Service

a73fd1c4a984c1db0faf56e987f4d8b34b16198b
[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   Motor.A.regulateSpeed(false);
12   Motor.B.regulateSpeed(false);
13   Motor.C.regulateSpeed(false);
14   Motor.B.forward();
15   Motor.C.forward();
16   int iteration = 0;
17   int startTime = (int)System.currentTimeMillis();
18   int totalTime = 0;
19   int tacho = 0;
20   int distVal=0;
21   int lightVal=0;
22   while(totalTime < TOTALTIME) {
23    lightVal = ls.readValue();
24    distVal = us.getDistance();
25    tacho = Motor.B.getTachoCount();
26    int RN = (int)(Math.random() * 100) + 1;
27    
28    LCD.drawInt(tacho,0,0);
29    LCD.drawInt((lightVal + distVal + tacho)*100/RN, 0, 1);
30    LCD.drawInt(A, 0, 2);
31    LCD.drawInt(iteration, 0, 4);
32    if(iteration%10 == 0)
33     LCD.refresh();
34
35    // Set motor speed for B and C to RN (Using Coast)
36
37    Motor.B.setSpeed(RN);
38    Motor.C.setSpeed(RN);
39    if(RN > 50) ++A;
40    if(RN < 50) --A;
41    
42    if(A<0) Motor.A.backward(); else Motor.A.forward();
43
44    totalTime = (int)System.currentTimeMillis() - startTime;
45    iteration++;
46   }
47   LCD.drawInt(iteration, 0, 4);
48   LCD.refresh();
49
50   Thread.sleep(10000);
51  }
52 }
53