1 package hybrid;
2
3 import interfaces.*;
4
5 import java.io.*;
6 import josx.rcxcomm.*;
7 import josx.platform.rcx.*;
8
9
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
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
45
46
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
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
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
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
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
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
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
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
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
190 public void beep()
191 {
192 try
193 {
194 dos.writeInt(HybridCommandConstants.BEEP);
195 dos.flush();
196 }
197 catch(Exception e){}
198 }
199
200
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
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
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