1 package hybrid;
2
3 import java.io.*;
4 import josx.rcxcomm.*;
5 import josx.platform.rcx.*;
6
7
13 public class HybridOnBoard implements SensorConstants, HybridCommandConstants
14 {
15 public static void main(String args[])
16 {
17 int MOTOR_POWER=3;
19 Motor.A.setPower(MOTOR_POWER);
20 Motor.C.setPower(MOTOR_POWER);
21
22 Sensor.S1.setTypeAndMode (SensorConstants.SENSOR_TYPE_LIGHT, SensorConstants.SENSOR_MODE_PCT);
24 Sensor.S1.activate();
25
26 Sensor.S2.setTypeAndMode (SensorConstants.SENSOR_TYPE_LIGHT, SensorConstants.SENSOR_MODE_PCT);
27 Sensor.S2.activate();
28
29 Sensor.S3.setTypeAndMode (SensorConstants.SENSOR_TYPE_LIGHT, SensorConstants.SENSOR_MODE_PCT);
30 Sensor.S3.activate();
31
32 try
33 {
34 RCXPort port = new RCXPort();
36
37 InputStream is = port.getInputStream();
38 OutputStream os = port.getOutputStream();
39 DataInputStream dis = new DataInputStream(is);
40 DataOutputStream dos = new DataOutputStream(os);
41
42 while (true)
44 {
45 int command = dis.readInt();
47
48 if(command==HybridCommandConstants.MOVE_FORWARD)
49 {
50 Motor.A.forward();
52 Motor.C.forward();
53 }
54 else if(command==HybridCommandConstants.MOVE_BACKWARD)
55 {
56 Motor.A.backward();
58 Motor.C.backward();
59 }
60 else if(command==HybridCommandConstants.MOVE_RIGHT)
61 {
62 Motor.A.stop();
64 Motor.C.stop();
65 Motor.A.forward();
66 Motor.C.backward();
67 }
68 else if(command==HybridCommandConstants.MOVE_LEFT)
69 {
70 Motor.A.stop();
72 Motor.C.stop();
73 Motor.A.backward();
74 Motor.C.forward();
75 }
76 else if(command==HybridCommandConstants.STOP_MOVING)
77 {
78 Motor.A.stop();
80 Motor.C.stop();
81 }
82 else if(command==HybridCommandConstants.BEEP)
83 {
84 Sound.beep();
86 }
87 else if(command==HybridCommandConstants.GET_S1)
88 {
89 dos.writeInt(Sensor.S1.readValue());
91 dos.flush();
92 }
93 else if(command==HybridCommandConstants.GET_S2)
94 {
95 dos.writeInt(Sensor.S2.readValue());
97 dos.flush();
98 }
99 else if(command==HybridCommandConstants.GET_S3)
100 {
101 dos.writeInt(Sensor.S3.readValue());
103 dos.flush();
104 }
105 }
106 }
107 catch (Exception e){}
109 }
110 }
111