OSDN Git Service

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