Sonar Sensor with C++ Library (NXT)

HomeForumsMonoBrick Communication LibrarySonar Sensor with C++ Library (NXT)

This topic contains 11 replies, has 2 voices, and was last updated by Author Image Joe Smith 2 years, 8 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #5308
    Author Image
    Joe Smith
    Participant

    Hello!

    I’m at a loss here, maybe someone can help:

    I try to control my NXT via PC over USB connection.

    After i got the C++ Library to finally run in Visual Express 2010, i modified the Provided Example to run via the MonoBrick Tunnel.
    And all was well, pushing the touchy-sensor made the motor go round.

    Then i tried to get the Sonar Sensor to work, which failed completely:

    Everytime it should print the result of read(), it throws an exception: “Bytes/answer from I2C sensor not ready which resulted in a timeout”

    Code below, nothing in the other files was changed (except that i threw the bluetooth.h and bluetooth.cpp out, which seemed fine, since i don’t use BT)

    Code:

    
    #include "stdafx.h"  // precompiled headers
    #include "nxt.h"     // monobrick library
          
    using namespace std;
          
    //set up the NXT
    Connection *connection = new Nxt_network();
    
    Sensor *sensor1 = new Touch(IN_1, connection);
    Sensor *sensor2 = new Touch(IN_2, connection);
    
    Sonar *sensor3 = new Sonar(IN_3, connection,METRIC);
    
    Motor *motorB = new Motor(OUT_B, connection);
    
    int main()
    {
    	try
    	{  
    		cout << "Try to connect to the NXT" << endl;  
    		//Network connection
    		connection->connect(1500, "127.0.0.1");
    		cout << "Connected" << endl;  
    		cout << "Press touch sensor - hit any key to end" << endl;  
    		cout << "initialising..." << endl;
    		sensor3->init();
    
    		while(!_kbhit())	//hit a key to end
    		{
    			if(sensor2->read())
    			{
    				cout << "Read: " << sensor3->read() << endl;
    				int i = 0;
    				i = i + 1;
    			}
    
    			if(sensor1->read())
    			{
    				motorB->on(75);
    			}
    			else
    			{
    				motorB->stop();
    			}
    		}
    		connection->disconnect();
    	}  
    	catch (Nxt_exception& e)
    	{  
    		//some error occurred - print it out
    		cout << e.what() << endl;
    		cout << "error code: " << e.error_code() << endl;
    		cout << "error type: " << e.error_type() << endl;
    		cout << e.who() << endl;
    		connection->disconnect();
    	}
    	return 0;
    }
    

    … which is pretty much the example, plus some code to scan the sonar when the touch2 is pushed

    any help would be dearly appreciated

    best regards,
    Joe

    • This topic was modified 2 years, 9 months ago by Author Image Joe Smith. Reason: added some more info
    #5314
    Author Image
    Anders Søborg
    Key Master

    Hi there.

    Try to call init on the sensor once it has been constructed. Another thing to try would be to run the code without the Tunnel. Are you able to do that?

    /Anders

    #5317
    Author Image
    Joe Smith
    Participant

    Thanks for the reply.

    Sadly, it didn’t help.

    a) i moved the construction of the sensors and motor inside the try-brackets, with the sensor3 directly followed by its init(), which just resulted in the same error occuring after the 500 ms delay defined in the i2c.h

    b) i tried both setups without the tunnel, which runs, but complains:

    E
    error code 352
    error type 1
    in Function “send” in class “Nxt_network”

    when the program tries to execute the line:

    "connection->connect(1500, "127.0.0.1");"

    which i guess means it can’t find the nxt without the tunnel. Sadly i can’t try it with bluetooth, as i don’t have a bluetooth connecting-thingy anywhere.

    interesting sidenote:
    the problem seems to be created by the Read()-commands, as the init() command runs fine, when i change the init()function in sonar.cpp from

    void Sonar::init(bool reply){
      I2c::init(reply);
      read();
      read();
      off();
      this->has_init=true;
    }

    to

    void Sonar::init(bool reply){
      I2c::init(reply);
      //read();
      //read();
      off();
      this->has_init=true;
    }

    after this change, the error occurs after the touch-sensor press again, when the read-command is executed.
    (i might be wrong, however, since, maybe, the init() fails without anyone noticing, preventing the read() from working afterwards, simply because the sensor is not initialised?

    anyway, it doesn’t work with init(true) either, same error message, which i guess means that the initialisation works fine, as the ‘true’ would mean that the nxt responds?

    sorry for the wordyness and thanks again

    Joe

    #5318
    Author Image
    Joe Smith
    Participant

    And me again.
    I have some results to share:

    I borrowed a bluetooth dongle from a coworker and it works quite fine, no problems whatsoever.

    so, in conclusion, it could be either the tunnel or the network-part thats acting up.

    since that bluetooth isn’t mine, though, i still could do with a solution :)

    thanks in advance, etc.

    best regards
    Joe

    #5336
    Author Image
    Anders Søborg
    Key Master

    Hi there

    So if I understand correctly – it works over BT but not over a network connection using the tunnel?

    /Anders

    #5338
    Author Image
    Joe Smith
    Participant

    indeed. it works via Bluetooth, but not in this setting:

    the nxt is connected via usb to the pc.
    the pc runs both the program and the tunnel, with the program using the ip 127.0.0.1 to connect
    so its… over a network connection, without a ‘real’ network, so to speak.

    thanks again for the helping hand ^^
    Joe

    #5341
    Author Image
    Anders Søborg
    Key Master

    Hi there

    are you able to connect to the brick using USB without the tunnel?

    /Anders

    #5343
    Author Image
    Joe Smith
    Participant

    Not sure how that would work.
    i mean, the original software connects over usb just fine, but in the c++ library i only see network or bluetooth as available connection types.

    is there a way to connect via usb without the tunnel?

    #5353
    Author Image
    Anders Søborg
    Key Master

    Hi there

    Sorry I did the C++ library a long time ago – and you are correct the C++ library does not support USB. But are you able to control the brick over USB using MonoBrick communication library?

    /Anders

    #5355
    Author Image
    Joe Smith
    Participant

    Hi :)

    Funny thing is, i don’t really know enough about how to use that.
    from my (rather amateurish) point of view, thats c# and not useable by someone like me who pretty much knows nothing about programming languages other than c++. :)

    and since i’m really nothing more than a dabbling hobbyist, i have no real clue about how to use dll’s or anything, which is bothersome, but i hardly find time to further my knowledge about programming.

    sorry for all the trouble

    Joe

    #5362
    Author Image
    Anders Søborg
    Key Master

    Hi there

    This would be an excellent opportunity… if you know C++ then it is easy to move to C#.

    /Anders

    #5363
    Author Image
    Joe Smith
    Participant

    hmm i guess i’ll just stick with c++ and get myself a bluetooth dongle instead ^^
    i doubt it’ll be good to mix another ‘seemingly similar’ language into my very slowly growing c++ knowledge.

    but thanks for the help, anyway.

    (and should i ever delve deeper into the code and find out whats not working with the network thingy, i’ll drop you a note :) )

    thanks again

    Joe

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.

Posted in

Make a donation