1   package real;
2   
3   import interfaces.*;
4   
5   import josx.platform.rcx.*;
6   
7   /**
8   * This class implements the simple commands provided by AbstractRobot with
9   * real lejos commands, allowing the simple commands to be run on the 
10  * real RCX.
11  *
12  * @author Graham Ritchie
13  */
14  public class RealRCX extends Thread implements AbstractRobot, SensorConstants
15  {
16      public static int MOTOR_POWER;
17      public void run(){}
18      
19      /**
20      * Initialises necessary variables, and sets up sensors, currently fixed to:
21      *
22      * @param controller the Controller associated with this RCX 
23      */
24      public RealRCX(Controller controller)
25      {
26          MOTOR_POWER=3;
27          Motor.A.setPower(MOTOR_POWER);
28          Motor.B.setPower(MOTOR_POWER);
29          Motor.C.setPower(MOTOR_POWER);
30          
31          // set up sensors as specified in controller
32          int[] sensors=controller.getSensors();
33          
34          if(sensors[0]==Controller.SENSOR_TYPE_LIGHT)
35          {
36              Sensor.S1.setTypeAndMode (SensorConstants.SENSOR_TYPE_LIGHT, SensorConstants.SENSOR_MODE_PCT);
37              Sensor.S1.activate();
38          }
39          else if(sensors[0]==Controller.SENSOR_TYPE_TOUCH)
40          {
41              Sensor.S1.setTypeAndMode (SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_PCT);
42              Sensor.S1.activate();
43          }
44          
45          if(sensors[1]==Controller.SENSOR_TYPE_LIGHT)
46          {
47              Sensor.S2.setTypeAndMode (SensorConstants.SENSOR_TYPE_LIGHT, SensorConstants.SENSOR_MODE_PCT);
48              Sensor.S2.activate();
49          }
50          else if(sensors[1]==Controller.SENSOR_TYPE_TOUCH)
51          {
52              Sensor.S2.setTypeAndMode (SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_PCT);
53              Sensor.S2.activate();
54          }
55          
56          if(sensors[2]==Controller.SENSOR_TYPE_LIGHT)
57          {
58              Sensor.S3.setTypeAndMode (SensorConstants.SENSOR_TYPE_LIGHT, SensorConstants.SENSOR_MODE_PCT);
59              Sensor.S3.activate();
60          }
61          else if(sensors[2]==Controller.SENSOR_TYPE_TOUCH)
62          {
63              Sensor.S3.setTypeAndMode (SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_PCT);
64              Sensor.S3.activate();
65          }
66      }
67      
68      
69      /*******************************************************************
70                      Methods required by AbstractRobot
71      *******************************************************************/
72      
73      /**
74      * Sets the robot moving forwards, this will continue until some other 
75      * method is called to stop it.
76      */
77      public void forward()
78      {
79          Motor.A.forward();
80          Motor.C.forward();
81      }
82      
83      /**
84      * Makes the robot move forwards for the given amount of time
85      *
86      * @param time the time in milliseconds
87      */
88      public void forward(int time)
89      {
90          Motor.A.forward();
91          Motor.C.forward();
92          try{sleep(time);}catch(Exception e){}
93          Motor.A.stop();
94          Motor.C.stop();
95      }
96      
97      /**
98      * Sets the robot moving backwards, this will continue until some other 
99      * method is called to stop it.
100     */
101     public void backward()
102     {
103         Motor.A.backward();
104         Motor.C.backward();
105     }
106     
107     /**
108     * Makes the robot move backwards for the given amount of time
109     *
110     * @param time the time in milliseconds
111     */
112     public void backward(int time)
113     {
114         Motor.A.backward();
115         Motor.C.backward();
116         try{sleep(time);}catch(Exception e){}
117         Motor.A.stop();
118         Motor.C.stop();
119     }
120     
121     /**
122     * Sets the robot spinning right, this will continue until some other 
123     * method is called to stop it.
124     */
125     public void right()
126     {
127         Motor.A.stop();
128         Motor.C.stop();
129         Motor.A.forward();
130         Motor.C.backward();
131     }
132     
133     /**
134     * Spins the robot right for the given amount of time
135     *
136     * @param time the time in milliseconds
137     */
138     public void right(int time)
139     {
140         Motor.A.stop();
141         Motor.C.stop();
142         Motor.A.forward();
143         Motor.C.backward();
144         try{sleep(time);}catch(Exception e){}
145         Motor.A.stop();
146         Motor.C.stop();
147     }
148     
149     /**
150     * Sets the robot spinning left, this will continue until some other 
151     * method is called to stop it.
152     */
153     public void left()
154     {
155         Motor.A.stop();
156         Motor.C.stop();
157         Motor.A.backward();
158         Motor.C.forward();
159     }
160     
161     /**
162     * Spins the robot left for the given amount of time
163     *
164     * @param time the time in milliseconds
165     */
166     public void left(int time)
167     {
168         Motor.A.stop();
169         Motor.C.stop();
170         Motor.A.backward();
171         Motor.C.forward();
172         try{sleep(time);}catch(Exception e){}
173         Motor.A.stop();
174         Motor.C.stop();
175     }
176     
177     /**
178     * Stops all motors immediately
179     */
180     public void stopMoving()
181     {
182         Motor.A.stop();
183         Motor.C.stop();
184     }
185     
186     /**
187     * Makes the robot beep
188     */
189     public void beep()
190     {
191         Sound.beep();
192     }
193     
194     /**
195     * Get the current reading of this sensor
196     *
197     * @return the current value
198     */
199     public int getSensor1()
200     {
201         return Sensor.S1.readValue();
202     }
203     
204     /**
205     * Get the current reading of this sensor
206     *
207     * @return the current value
208     */
209     public int getSensor2()
210     {
211         return Sensor.S2.readValue();
212     }
213     
214     /**
215     * Get the current reading of this sensor
216     *
217     * @return the current value
218     */
219     public int getSensor3()
220     {
221         return Sensor.S3.readValue();
222     }
223 }
224