root/Projects/TrafficLightFinder/src/org/sunspotworld/TrafficLightFinder.java

Revision 67, 4.2 kB (checked in by mcintoke, 1 year ago)

Added copywrite

Line 
1 /*
2  * Title: TrafficLightFinder (Abstract)
3  * Author: Kevin McIntosh
4  * Description:
5  * SPOTBot will search for the SunSpot running TrafficLightFinder. 
6  * The "Traffic Light" will broadcast a radio signal, which a SPOT running this
7  * class will look for.  If the traffic light is green, the SPOT will resume the search
8  * for the traffic light until the LED state changes to yellow.  This is where the
9  * SunSPOT will be thrown off its search, and will attempt to "slow down" by heading
10  * forward for a number of seconds.  Finnaly, when the traffic light state changes to
11  * red, the SunSPOT will stop.
12  *
13  * Pay close attention in keeping settings (port, channel, etc.) in synch with
14  * TrafficLightSender sunSPOT.  Currently, the settings are the default setting of
15  * the BeaconListener class 
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  * A class used to find the location of a SunSPOT running TrafficLightSender
26  *
27  * @author Kevin McIntosh
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      * This is the area where you will write your searching algorythm.
40      * @throws java.io.IOException - Thrown if the TrafficLightSender
41      * stops transmitting
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) { //this while loop is before the SunSPOT begins the search
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) { //main while loop for the rest of the application
85
86             try {
87                 while (true) { //need to be worked on
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 }
111
112 /*
113  
114 Copyright 2008 The SPOTBot Team
115
116 Licensed under the Apache License, Version 2.0 (the "License");
117 you may not use this file except in compliance with the License.
118 You may obtain a copy of the License at
119
120     http://www.apache.org/licenses/LICENSE-2.0
121
122 Unless required by applicable law or agreed to in writing, software
123 distributed under the License is distributed on an "AS IS" BASIS,
124 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125 See the License for the specific language governing permissions and
126 limitations under the License.
127
128  */
129
Note: See TracBrowser for help on using the browser.