SensorListeners don't work right

This is where you talk about the RCX software itself, installation issues, and programming talk.

Moderator: roger

SensorListeners don't work right

Postby Saxman » Tue Apr 24, 2007 6:52 pm

Hi,

I wrote a programm that tells my standard roverbot to
1) move forward until he hits a wall.
2) Then he should go backward and turn right.
3) back to 1)

It works fine, but he is doing point 2) twice :) .

I can't imagine why, because I wrote for each touch sensor a seperated sensor listener. So they can't be activated both at the same time (he hits the wall only with one touch sensor). :?

Does someone have a similar problem.

I may post my code tomorrow. :D
Saxman
Saxman
New User
 
Posts: 5
Joined: Tue Apr 24, 2007 6:38 pm

Postby Saxman » Wed Apr 25, 2007 12:48 pm

Hi,

Here my code:

Code: Select all
import josx.platform.rcx.*;

public class RoverBot{

  protected Motor LeftWheel = Motor.A;
  protected Motor RightWheel = Motor.C;
  protected Motor Light = Motor.B;
 
  protected Sensor TouchSensorRight = Sensor.S3;
  protected Sensor TouchSensorLeft = Sensor.S1;
  protected Sensor LightSensor = Sensor.S2;
 
  protected SensorListener SLTouchSensorRight;
  protected SensorListener SLTouchSensorLeft;
  protected SensorListener SLLightSensor;
 
  protected int motorPowerLeftWheel, motorPowerRightWheel;
 
  protected boolean isLightOn;
 
  public RoverBot(){
    initMotors(7);
    initSensors();
  }
 
  public RoverBot(int motorPowerWheels){
    initMotors(motorPowerWheels);
    initSensors();
  }
 
  public RoverBot(double b){
    initMotors(7);
  }
 
  private void initSensors(){
    TouchSensorRight.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_BOOL);
    TouchSensorLeft.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH, SensorConstants.SENSOR_MODE_BOOL);
    LightSensor.setTypeAndMode(SensorConstants.SENSOR_TYPE_LIGHT, SensorConstants.SENSOR_MODE_PCT);

    TouchSensorRight.passivate();
    TouchSensorLeft.passivate();
    LightSensor.activate();

    SLTouchSensorRight = new SensorListenerTouchSensorRight();
    SLTouchSensorLeft = new SensorListenerTouchSensorLeft();
    SLLightSensor = new SensorListenerLightSensor();

    TouchSensorRight.addSensorListener(SLTouchSensorRight);
    TouchSensorLeft.addSensorListener(SLTouchSensorLeft);
    LightSensor.addSensorListener(SLLightSensor);
  }
 
  private void initMotors(int motorPowerWheels){
    motorPowerLeftWheel = motorPowerWheels;
    motorPowerRightWheel = motorPowerWheels;
  }
 
  public void forward(){
    LeftWheel.setPower(motorPowerLeftWheel);
    RightWheel.setPower(motorPowerRightWheel);
    LeftWheel.forward();
    RightWheel.forward();
  }
 
  public void backward(){
    LeftWheel.setPower(motorPowerLeftWheel);
    RightWheel.setPower(motorPowerRightWheel);
    LeftWheel.backward();
    RightWheel.backward();
  }
 
  public void turnRight(){
    LeftWheel.setPower(motorPowerLeftWheel);
    RightWheel.setPower(motorPowerRightWheel);
    RightWheel.forward();
    LeftWheel.stop();
  }

  public void turnLeft(){
    LeftWheel.setPower(motorPowerLeftWheel);
    RightWheel.setPower(motorPowerRightWheel);
    LeftWheel.forward();
    RightWheel.stop();
  }
 
  public void weicheAus(int backTimeMillis, int turnTimeMillis) throws Exception{
    backward();
    Thread.sleep(backTimeMillis);
    turnRight();
    Thread.sleep(turnTimeMillis);
    forward();
  }
 
  public void stop(){
    LeftWheel.stop();
    RightWheel.stop();
  }
 
  public void switchLightOn(){
    if(!isLightOn){
      Light.setPower(7);
      Light.forward();
      isLightOn = true;
    }
  }
 
  public void switchLightOff(){
    if(isLightOn){
      Light.stop();
      isLightOn = false;
    }
  }
 
  protected boolean isLightOn(){
    return isLightOn;
  }
 
  public void beep(){
    Sound.beep();
  }
 
  public void beep(int beepCount, int interruptionMillis) throws Exception{
    for(int i = 0;i < beepCount;i++){
      beep();
      Thread.sleep(interruptionMillis);
    }
  }
 
  public void lcdPrintString(String text){
    TextLCD.print(text);
  }

  public void lcdPrintNumber(int number){
    LCD.showNumber(number);
  }

  public void lcdClear(){
    LCD.clear();
  }

  public void wait(int time){
    try{
      Thread.sleep(time);
    }catch(InterruptedException iexp){}
  }
 
}
######################################

import josx.platform.rcx.*;

public class SensorListenerLightSensor implements SensorListener{

  protected RoverBot Robo;

  public SensorListenerLightSensor(){
    double a = 1;
    Robo = new RoverBot(a);
  }

  public void stateChanged(Sensor aSource, int aOldValue, int aNewValue){
    //Aktion
  }
}
######################################


import josx.platform.rcx.*;

public class SensorListenerTouchSensorLeft implements SensorListener{

  protected RoverBot Robo;

  public SensorListenerTouchSensorLeft(){
    double a = 1;
    Robo = new RoverBot(a);
  }

  public void stateChanged(Sensor aSource, int aOldValue, int aNewValue){
 
    try{
      Robo.weicheAus(2000,3000);
    } catch(Exception exp){}

  }
}

######################################

import josx.platform.rcx.*;

public class SensorListenerTouchSensorRight implements SensorListener{

  protected RoverBot Robo;

  public SensorListenerTouchSensorRight(){
    double a = 1;
    Robo = new RoverBot(a);
  }
 
  public void stateChanged(Sensor aSource, int aOldValue, int aNewValue){
 
    try{
      Robo.weicheAus(2000,3000);
    } catch(Exception exp){}

  }
}

######################################

import josx.platform.rcx.*;

public class RoverBotDemo1{

  public static void main (String Arg []) throws Exception{
 
    RoverBot Robo = new RoverBot(7);
    Robo.forward();
   
    try {
      Button.RUN.waitForPressAndRelease();
    } catch(InterruptedException e) {}
   
  }
 
}


Please keep in your mind, that the different classes are in seperated files.

I'm looking forward to nice answers. :)[/code]
Saxman
Saxman
New User
 
Posts: 5
Joined: Tue Apr 24, 2007 6:38 pm

Postby ltj » Thu May 08, 2008 6:18 pm

i know its old but maybe someone will step in to the same problem, your robot is doing the task twice because touch sensor generates two event first when the button i pressed (from 0 to 1, when robot hit obstacle) and second when button is released (from 1 to 0), you just have to add something like this
Code: Select all
    try{
        if (aNewValue == 1) {
           Robo.weicheAus(2000,3000);
        }
    } catch(Exception exp){}

and it should be fine
ltj
New User
 
Posts: 4
Joined: Thu Apr 24, 2008 7:01 pm


Return to RCX Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff