1   package hybrid;
2   
3   import interfaces.*;
4   
5   import java.io.*;
6   import josx.rcxcomm.*;
7   import josx.platform.rcx.*;
8   
9   /**
10  * Acts as an interface between the PC and an RCX running the HybridOnBoard class.
11  * Enables controllers to be run on the PC, and communicate directly with the RCX
12  * over the IR link.
13  *
14  * @author Graham Ritchie
15  */
16  public class HybridRCX extends Thread implements AbstractRobot, HybridCommandConstants
17  {
18      RCXPort port;
19      InputStream is;
20      OutputStream os;
21      DataInputStream dis;
22      DataOutputStream dos;
23      
24      /**
25      * Constructor: sets up the data ports
26      */
27      public HybridRCX(Controller controller)
28      {
29          try
30          {
31              port=new RCXPort();
32              is = port.getInputStream();
33              os = port.getOutputStream();
34              dis = new DataInputStream(is);
35              dos = new DataOutputStream(os);
36          }
37          catch(Exception e){}
38      }
39      
40      public void run(){}
41      
42      /*******************************************************************
43                      Methods required by AbstractRobot
44      *******************************************************************/
45      
46      /**
47      * Sets the robot moving forwards, this will continue until some other 
48      * method is called to stop it.
49      */
50      public void forward()
51      {
52          try
53          {
54              dos.writeInt(HybridCommandConstants.MOVE_FORWARD);
55              dos.flush();
56          }
57          catch(Exception e){}
58      }
59      
60      /**
61      * Makes the robot move forwards for the given amount of time
62      *
63      * @param time the time in milliseconds
64      */
65      public void forward(int time)
66      {
67          try
68          {
69              dos.writeInt(HybridCommandConstants.MOVE_FORWARD);
70              dos.flush();
71              sleep(time);
72              dos.writeInt(HybridCommandConstants.STOP_MOVING);
73              dos.flush();
74          }
75          catch(Exception e){}
76      }
77      
78      /**
79      * Sets the robot moving backwards, this will continue until some other 
80      * method is called to stop it.
81      */
82      public void backward()
83      {
84          try
85          {
86              dos.writeInt(HybridCommandConstants.MOVE_BACKWARD);
87              dos.flush();
88          }
89          catch(Exception e){}
90      }
91      
92      /**
93      * Makes the robot move backwards for the given amount of time
94      *
95      * @param time the time in milliseconds
96      */
97      public void backward(int time)
98      {
99          try
100         {
101             dos.writeInt(HybridCommandConstants.MOVE_BACKWARD);
102             dos.flush();
103             try{sleep(time);}catch(Exception e){}
104             dos.writeInt(HybridCommandConstants.STOP_MOVING);
105             dos.flush();
106         }
107         catch(Exception e){}
108     }
109     
110     /**
111     * Sets the robot spinning right, this will continue until some other 
112     * method is called to stop it.
113     */
114     public void right()
115     {
116         try
117         {
118             dos.writeInt(HybridCommandConstants.MOVE_RIGHT);
119             dos.flush();
120         }
121         catch(Exception e){}
122     }
123     
124     /**
125     * Spins the robot right for the given amount of time
126     *
127     * @param time the time in milliseconds
128     */
129     public void right(int time)
130     {
131         try
132         {
133             dos.writeInt(HybridCommandConstants.MOVE_RIGHT);
134             dos.flush();
135             sleep(time);
136             dos.writeInt(HybridCommandConstants.STOP_MOVING);
137             dos.flush();
138         }
139         catch(Exception e){}
140     }
141     
142     /**
143     * Sets the robot spinning left, this will continue until some other 
144     * method is called to stop it.
145     */
146     public void left()
147     {
148         try
149         {
150             dos.writeInt(HybridCommandConstants.MOVE_LEFT);
151             dos.flush();
152         }
153         catch(Exception e){}
154     }
155     
156     /**
157     * Spins the robot left for the given amount of time
158     *
159     * @param time the time in milliseconds
160     */
161     public void left(int time)
162     {
163         try
164         {
165             dos.writeInt(HybridCommandConstants.MOVE_LEFT);
166             dos.flush();
167             sleep(time);
168             dos.writeInt(HybridCommandConstants.STOP_MOVING);
169             dos.flush();
170         }
171         catch(Exception e){}
172     }
173     
174     /**
175     * Stops all motors immediately
176     */
177     public void stopMoving()
178     {
179         try
180         {
181             dos.writeInt(HybridCommandConstants.STOP_MOVING);
182             dos.flush();
183         }
184         catch(Exception e){}
185     }
186     
187     /**
188     * Makes the robot beep
189     */
190     public void beep()
191     {
192         try
193         {
194             dos.writeInt(HybridCommandConstants.BEEP);
195             dos.flush();
196         }
197         catch(Exception e){}
198     }
199     
200     /**
201     * Get the current reading of this sensor
202     *
203     * @return the current value
204     */
205     public int getSensor1()
206     {
207         try
208         {
209             dos.writeInt(HybridCommandConstants.GET_S1);
210             dos.flush();
211         
212             int x=dis.readInt();
213             
214             return x;
215         }
216         catch(Exception e)
217         {
218             return 0;
219         }
220     }
221     
222     /**
223     * Get the current reading of this sensor
224     *
225     * @return the current value
226     */
227     public int getSensor2()
228     {
229         try
230         {
231             dos.writeInt(HybridCommandConstants.GET_S2);
232             dos.flush();
233         
234             int x=dis.readInt();
235             
236             return x;
237         }
238         catch(Exception e)
239         {
240             return 0;
241         }
242     }
243     
244     /**
245     * Get the current reading of this sensor
246     *
247     * @return the current value
248     */
249     public int getSensor3()
250     {
251         try
252         {
253             dos.writeInt(HybridCommandConstants.GET_S3);
254             dos.flush();
255             
256             int x=dis.readInt();
257             
258             return x;
259         }
260         catch(Exception e)
261         {
262             return 0;
263         }
264     }
265 }
266