next up previous
Next: 6.5.9 Wanderクラス Up: 6.5 ロボットの行動プログラム Previous: 6.5.7 左バンパーへの反応行動

6.5.8 右バンパーへの反応行動

/**
 * Defines a finite state machine to
 * avoid an obstacle on the right.
 */                
class RightBumber extends Task
  implements SensorListener {
  public RightBumber() {
    RIGHT_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(RIGHT_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(LEFT_MOTOR,
                   7, FORWARD);
          return 0;
        }
      };

    fsm = new int[3];                
    fsm[0] = 1;
    fsm[1] = 2;
    fsm[2] = END;
    RIGHT_BUMBER.activate();
    RIGHT_BUMBER.addSensorListener(this);
  }

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


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