Announcement

Collapse
No announcement yet.

connection problem in Java

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • connection problem in Java

    I installed RobotinoAPI2 and I want to programm the robotino using java.
    I am able to compile the examples but I am not able to connect to a robotino :neither physically to a robotino nor to robotinoSimDemo.
    It give me a "java.lang.RuntimeException: the connection has been refused"

    Below the core code I used.

    Can someone give me a hint ?

    Thanks

    ------------------------
    import java.util.ArrayList;
    import java.util.List;
    import java.util.TimerTask;
    import java.util.Timer;

    import rec.robotino.api2.Bumper;
    import rec.robotino.api2.Com;
    import rec.robotino.api2.OmniDrive;
    import rec.robotino.api2.DistanceSensor;

    /**
    * Write a description of class Robot here.
    *
    * @author (your name)
    * @version (a version number or a date)
    */
    public class Robot
    {
    // instance variables - replace the example below with your own
    protected final Com _com;
    protected final OmniDrive _omniDrive;
    protected final Bumper _bumper;
    protected final List<DistanceSensor> _distanceSensors;

    protected final float SLOW_VELOCITY = 0.08f;
    protected final float MEDIUM_VELOCITY = 0.16f;
    protected final float VELOCITY = 0.24f;
    protected final float FAST_VELOCITY = 0.32f;
    protected final float ANGULARVELOCITY = 0.02f;

    /**
    * Constructor for objects of class Robot
    */
    public Robot()
    {
    //_com = new MyCom();
    _com = new Com();
    _omniDrive = new OmniDrive();
    _bumper = new Bumper();
    _distanceSensors = new ArrayList<DistanceSensor>();

    init();

    }

    /**
    * An example of a method - replace this comment with your own
    *
    * @param y a sample parameter for a method
    * @return the sum of x and y
    */
    private void init()
    {
    _omniDrive.setComId(_com.id());
    _bumper.setComId(_com.id());
    for(int i=0; i<9; ++i)
    {
    DistanceSensor s = new DistanceSensor();
    s.setSensorNumber(i);
    s.setComId(_com.id());
    _distanceSensors.add(s);
    }
    }

    public boolean isConnected()
    {
    return _com.isConnected();
    }

    public void connect(String hostname, boolean block)
    {
    System.out.println("Connecting...");
    _com.setAddress( hostname );
    _com.connectToServer(block);
    }


    /**
    * The class MyCom derives from rec.robotino.api2.Com and implements some of the virtual event handling methods.
    * This is the standard approach for handling these Events.
    */
    class MyCom extends Com
    {
    Timer _timer;

    public MyCom()
    {
    _timer = new Timer();
    _timer.scheduleAtFixedRate(new OnTimeOut(), 0, 20);
    }

    class OnTimeOut extends TimerTask
    {
    public void run()
    {
    processEvents();
    }
    }

    @Override
    public void connectedEvent()
    {
    System.out.println( "Connected" );
    }

    @Override
    public void errorEvent(String errorStr)
    {
    System.err.println( "Error: " + errorStr );
    }

    @Override
    public void connectionClosedEvent()
    {
    System.out.println( "Disconnected" );
    }
    }


    public static void main(String[] args)
    {

    //String hostname = "172.26.1.6";
    String hostname = "127.0.0.1:8081";
    if( args.length == 1)
    {
    hostname = args[0].toString();
    }

    Robot robotino = new Robot();

    System.out.println("C'est parti");

    try
    {
    robotino.connect(hostname, true);
    // robotino.followWalls();
    // robotino.disconnect();
    }
    catch (Exception e)
    {
    System.out.println(e.toString());
    }


    }
    }


  • #2
    Which operating system do you use? I think we had the same issue with C++ code and Ubuntu and if I remember correctly it was resolved by running the C++ program as super user. Maybe that helps..

    Comment


    • #3
      你用内部类的MyCom对象能连接上吗? Are you able to connect to the MyCom object of the internal class?






      Comment

      Working...
      X