next up previous
Next: 6.5.8 右バンパーへの反応行動 Up: 6.5 ロボットの行動プログラム Previous: 6.5.6 ユーザ定義の行動クラス

6.5.7 左バンパーへの反応行動


/**
 * Defines a finite state machine
 * to avoid an obstacle on the left.
 */                
class LeftBumber extends Task
  implements SensorListener {
  public LeftBumber() {
    LEFT_BUMBER.setTypeAndMode (SENSOR_TYPE_TOUCH,
                                SENSOR_MODE_BOOL);
    actions = new Action[3];
    actions[0] = new Action() {
        public int act() {
          setMotor(LEFT_MOTOR, 7, BACKWARD);
          setMotor(RIGHT_MOTOR, 7, BACKWARD);
          return 200;
        }
      };
                
    actions[1] = new Action() {
        public int act() {
          setMotor(LEFT_MOTOR,
                   7,
                   FORWARD);
          return 200;
        }
      };

    // Shouldn't really need to do this one,
    // but reset()
    // may not be immediate on lower priority
    // tasks.
    actions[2] = new Action() {
        public int act() {
          setMotor(RIGHT_MOTOR,
                   7,
                   FORWARD);
          return 0;
        }
      };
        
    fsm = new int[3];                
    fsm[0] = 1;
    fsm[1] = 2;
    fsm[2] = END;
    LEFT_BUMBER.activate();
    LEFT_BUMBER.addSensorListener(this);
  }

  /**
   * This is actually executed 
   * in a thread established by
   * LEFT_BUMBER.addSensorListener().
   */        
  public void stateChanged(Sensor bumber,
                           int oldValue,
                           int newValue) {
    Sound.playTone(440, 10);
    if (bumber.readBooleanValue()) {
      Sound.playTone(500, 10);
      execute();
    }
  }
}


generated through LaTeX2HTML. M.Inaba 平成18年5月7日