1   package simworldobjects;
2   
3   import interfaces.*;
4   
5   import java.awt.*;
6   import java.lang.*;
7   import java.awt.event.*;
8   import javax.swing.*;
9   import java.io.*;
10  import java.util.*;
11  import java.awt.geom.*;
12  
13  /**
14  * Simulates an RCX for use in SimWorld
15  *
16  * @author Graham Ritchie
17  */
18  public class SimRCX extends BasicSimObject implements AbstractRobot 
19  {
20      public static final int STATE_STOPPED=1;
21      public static final int STATE_FORWARD=2;
22      public static final int STATE_BACKWARD=3;
23      public static final int STATE_RIGHT=4;
24      public static final int STATE_LEFT=5;
25      
26      private int state;
27      private SimWorld world;
28      
29      private SimSensor S1;
30      private SimSensor S2;
31      private SimSensor S3;
32      
33      private Controller controller;
34      
35      /**
36      * Constructor: sets up the robot with some predefined parameters
37      *
38      * @param w the SimWorld where this SimRCX resides
39      * @param c the Controller of this SimRCX
40      */
41      public SimRCX(SimWorld w, Controller c)
42      {
43          // initialise the SimObject
44          super(30.0,40.0,60.0,"robot",600.0,0.0,200.0,0.0,160.0);
45          
46          // set this robot's world
47          this.world=w;
48          
49          // set this robot's controler
50          this.controller=c;
51          
52          setupRobot();
53      }
54      
55      /**
56      * Constructor: sets up the robot according to the parameters
57      */
58      public SimRCX(SimWorld w, Controller c, double height, double width, double length, String type, double x, double y, double z, double bearingXY, double bearingXZ)
59      {
60          // initialise the SimObject
61          super(height,width,length,type,x,y,z,bearingXY,bearingXZ);
62          
63          // set this robot's world
64          world=w;
65          
66          // set this robot's controler
67          this.controller=c;
68          
69          setupRobot();
70      }
71      
72      /**
73      * Sets up robot: adds sensors etc.
74      */
75      private void setupRobot()
76      {
77          // set the robot's initial state to stopped
78          this.state=SimRCX.STATE_STOPPED;
79          
80          // set up sensors as specified in controller
81          int[] sensors=controller.getSensors();
82          
83          if(sensors[0]==Controller.SENSOR_TYPE_LIGHT)
84          {
85              S1=new SimLightSensor(this,-this.getWidth(),-this.getLength()/4);
86          }
87          else
88          {
89              S1=new SimTouchSensor(this,-this.getWidth(),-this.getLength()/4);
90          }
91          
92          if(sensors[1]==Controller.SENSOR_TYPE_LIGHT)
93          {
94              S2=new SimLightSensor(this,0.0,-(this.getLength()/2)-25); 
95          }
96          else
97          {
98              S2=new SimTouchSensor(this,0.0,-(this.getLength()/2)-25);
99          }
100         
101         if(sensors[2]==Controller.SENSOR_TYPE_LIGHT)
102         {
103             S3=new SimLightSensor(this,this.getWidth(),-this.getLength()/4);
104         }
105         else
106         {
107             S3=new SimTouchSensor(this,this.getWidth(),-this.getLength()/4);
108         }
109         
110         // add sensors to the world
111         world.addObject(S1);
112         world.addObject(S2);
113         world.addObject(S3);
114     }
115     
116     public SimWorld getWorld()
117     {
118         return this.world;
119     }
120     
121     /*******************************************************************
122                      Methods required by AbstractRobot
123     *******************************************************************/
124     
125     /**
126     * Sets the robot moving forwards, this will continue until some other 
127     * method is called to stop it.
128     */
129     public void forward()
130     {
131         stopMoving();
132         this.state=SimRCX.STATE_FORWARD;
133         this.setDesiredVelocity(1);
134     }
135     
136     /**
137     * Makes the robot move forwards for the given amount of time
138     *
139     * @param time the time in milliseconds
140     */
141     public void forward(int time)
142     {
143         stopMoving();
144         this.state=SimRCX.STATE_FORWARD;
145         this.setDesiredVelocity(1);
146         try{Thread.sleep(time);}catch(Exception e){}
147         stopMoving();
148     }
149     
150     /**
151     * Sets the robot moving backwards, this will continue until some other 
152     * method is called to stop it.
153     */
154     public void backward()
155     {
156         stopMoving();
157         this.state=SimRCX.STATE_BACKWARD;
158         this.setDesiredVelocity(-1);
159     }
160     
161     /**
162     * Makes the robot move backwards for the given amount of time
163     *
164     * @param time the time in milliseconds
165     */
166     public void backward(int time)
167     {
168         stopMoving();
169         this.state=SimRCX.STATE_BACKWARD;
170         this.setDesiredVelocity(-1);
171         try{Thread.sleep(time);}catch(Exception e){}
172         stopMoving();
173     }
174     
175     /**
176     * Sets the robot spinning right, this will continue until some other 
177     * method is called to stop it.
178     */
179     public void right()
180     {
181         stopMoving();
182         this.state=SimRCX.STATE_RIGHT;
183         this.setDesiredBearingVelocityXZ(1);
184     }
185     
186     /**
187     * Spins the robot right for the given amount of time
188     *
189     * @param time the time in milliseconds
190     */
191     public void right(int time)
192     {
193         stopMoving();
194         this.state=SimRCX.STATE_RIGHT;
195         this.setDesiredBearingVelocityXZ(1);
196         try{Thread.sleep(time);}catch(Exception e){}
197         this.state=SimRCX.STATE_STOPPED;
198         this.setDesiredBearingVelocityXZ(0);
199     }
200     
201     /**
202     * Sets the robot spinning left, this will continue until some other 
203     * method is called to stop it.
204     */
205     public void left()
206     {
207         stopMoving();
208         this.state=SimRCX.STATE_LEFT;
209         this.setDesiredBearingVelocityXZ(-1);
210     }
211     
212     /**
213     * Spins the robot left for the given amount of time
214     *
215     * @param time the time in milliseconds
216     */
217     public void left(int time)
218     {
219         stopMoving();
220         this.state=SimRCX.STATE_LEFT;
221         this.setDesiredBearingVelocityXZ(-1);
222         try{Thread.sleep(time);}catch(Exception e){}
223         stopMoving();
224     }
225     
226     /**
227     * Stops all motors immediately
228     */
229     public void stopMoving()
230     {
231         this.state=SimRCX.STATE_STOPPED;
232         this.setDesiredVelocity(0);
233         this.setDesiredBearingVelocityXZ(0);
234     }
235     
236     /**
237     * Makes the robot beep
238     */
239     public void beep()
240     {
241         Toolkit.getDefaultToolkit().beep(); 
242     }
243     
244     /**
245     * Get the current reading of this sensor
246     *
247     * @return the current value
248     */
249     public int getSensor1()
250     {
251         return S1.getValue();
252     }
253     
254     /**
255     * Get the current reading of this sensor
256     *
257     * @return the current value
258     */
259     public int getSensor2()
260     {
261         return S2.getValue();
262     }
263     
264     /**
265     * Get the current reading of this sensor
266     *
267     * @return the current value
268     */
269     public int getSensor3()
270     {
271         return S3.getValue();
272     }
273 }
274