1   package hybrid;
2   
3   import java.io.*;
4   import josx.rcxcomm.*;
5   import josx.platform.rcx.*;
6   
7   /**
8   * Runs onboard the RCX listening for controller commands from the PC. 
9   * Talks to the HybridRCX class.
10  *
11  * @author Graham Ritchie
12  */
13  public class HybridOnBoard implements SensorConstants, HybridCommandConstants
14  {
15      public static void main(String args[])
16      {
17          // set robot up
18          int MOTOR_POWER=3;
19          Motor.A.setPower(MOTOR_POWER);
20          Motor.C.setPower(MOTOR_POWER);
21          
22          // currently only support light sensors
23          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              // set up the data ports
35              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              // just keep on listeneing for the PC command
43              while (true) 
44              {
45                  // get PC command
46                  int command = dis.readInt();
47              
48                  if(command==HybridCommandConstants.MOVE_FORWARD)
49                  {
50                      // move forward
51                      Motor.A.forward();
52                      Motor.C.forward();
53                  }
54                  else if(command==HybridCommandConstants.MOVE_BACKWARD)
55                  {
56                      // move backward
57                      Motor.A.backward();
58                      Motor.C.backward();
59                  }
60                  else if(command==HybridCommandConstants.MOVE_RIGHT)
61                  {
62                      // move right
63                      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                      // move left
71                      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                      // stop
79                      Motor.A.stop();
80                      Motor.C.stop();
81                  }
82                  else if(command==HybridCommandConstants.BEEP)
83                  {
84                      // beep
85                      Sound.beep();
86                  }   
87                  else if(command==HybridCommandConstants.GET_S1)
88                  {
89                      // get value of sensor 1 & write value back to the PC
90                      dos.writeInt(Sensor.S1.readValue());
91                      dos.flush();
92                  }
93                  else if(command==HybridCommandConstants.GET_S2)
94                  {
95                      // get value of sensor 2 & write value back to the PC
96                      dos.writeInt(Sensor.S2.readValue());
97                      dos.flush();
98                  }
99                  else if(command==HybridCommandConstants.GET_S3)
100                 {
101                     // get value of sensor 3 & write value back to the PC
102                     dos.writeInt(Sensor.S3.readValue());
103                     dos.flush();
104                 }
105             }
106         }
107         // if an exception occurs just ignore it - there's not much we can do    
108         catch (Exception e){}
109     }
110 }
111