| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
package org.sunspotworld; |
|---|
| 18 |
|
|---|
| 19 |
import com.sun.spot.util.*; |
|---|
| 20 |
import java.io.*; |
|---|
| 21 |
import javax.microedition.midlet.MIDlet; |
|---|
| 22 |
import javax.microedition.midlet.MIDletStateChangeException; |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
public class TrafficLightFinder extends MIDlet { |
|---|
| 30 |
|
|---|
| 31 |
private static TekBotMotorControl mcu = new TekBotMotorControl(); |
|---|
| 32 |
private static BeaconListener listen = new BeaconListener (); |
|---|
| 33 |
private static int nowPower = 0; |
|---|
| 34 |
private static int newPower = 0; |
|---|
| 35 |
private static boolean ready = false; |
|---|
| 36 |
private static boolean green = true; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
public static void search() throws IOException { |
|---|
| 44 |
int count = 0; |
|---|
| 45 |
System.out.println("nowPower is currently set at: " + nowPower); |
|---|
| 46 |
mcu.forward(); |
|---|
| 47 |
System.out.println("forward"); |
|---|
| 48 |
newPower = listen.getBeacon(1000); |
|---|
| 49 |
System.out.println("The newPower is at: " + newPower); |
|---|
| 50 |
if (newPower < nowPower) { |
|---|
| 51 |
System.out.println("Insufficent power: Backing up"); |
|---|
| 52 |
mcu.backward(1000); |
|---|
| 53 |
mcu.turnLeftDegrees(60); |
|---|
| 54 |
count++; |
|---|
| 55 |
} else { |
|---|
| 56 |
System.out.println("Sufficent Power: moving forward"); |
|---|
| 57 |
mcu.forward(); |
|---|
| 58 |
nowPower = newPower; |
|---|
| 59 |
} |
|---|
| 60 |
if (count == 4){ |
|---|
| 61 |
count = 0; |
|---|
| 62 |
nowPower = -32; |
|---|
| 63 |
} |
|---|
| 64 |
System.out.println("The power being read is: " + nowPower); |
|---|
| 65 |
|
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
protected void startApp() throws MIDletStateChangeException { |
|---|
| 69 |
|
|---|
| 70 |
while (ready == false) { |
|---|
| 71 |
|
|---|
| 72 |
try { |
|---|
| 73 |
|
|---|
| 74 |
nowPower = listen.getBeacon(); |
|---|
| 75 |
System.out.println(nowPower); |
|---|
| 76 |
ready = true; |
|---|
| 77 |
|
|---|
| 78 |
} catch (IOException e) { |
|---|
| 79 |
mcu.stop(); |
|---|
| 80 |
System.out.println("Waiting.."); |
|---|
| 81 |
} |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
while (true) { |
|---|
| 85 |
|
|---|
| 86 |
try { |
|---|
| 87 |
while (true) { |
|---|
| 88 |
|
|---|
| 89 |
search(); |
|---|
| 90 |
System.out.println("Green Light; begin moving"); |
|---|
| 91 |
|
|---|
| 92 |
} |
|---|
| 93 |
} catch (IOException e) { |
|---|
| 94 |
System.out.println("Yellowlight, begin to yield"); |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
mcu.forward(1500); |
|---|
| 98 |
mcu.stop(); |
|---|
| 99 |
System.out.println("At a red light"); |
|---|
| 100 |
Utils.sleep(4000); |
|---|
| 101 |
|
|---|
| 102 |
} |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
protected void pauseApp() { |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { |
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|