Home→Forums→MonoBrick Communication Library→Sonar Sensor with C++ Library (NXT)
- This topic has 11 replies, 2 voices, and was last updated 9 years, 7 months ago by Joe Smith.
-
AuthorPosts
-
March 15, 2015 at 20:58 #5308
Joe SmithParticipantHello!
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 9 years, 8 months ago by Joe Smith. Reason: added some more info
March 16, 2015 at 18:51 #5314
Anders SøborgKeymasterHi 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
March 17, 2015 at 17:16 #5317
Joe SmithParticipantThanks 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 fromvoid 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
March 17, 2015 at 18:29 #5318
Joe SmithParticipantAnd 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
JoeMarch 24, 2015 at 21:22 #5336
Anders SøborgKeymasterHi there
So if I understand correctly – it works over BT but not over a network connection using the tunnel?
/Anders
March 26, 2015 at 17:41 #5338
Joe SmithParticipantindeed. 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 ^^
JoeMarch 29, 2015 at 19:25 #5341
Anders SøborgKeymasterHi there
are you able to connect to the brick using USB without the tunnel?
/Anders
March 30, 2015 at 18:22 #5343
Joe SmithParticipantNot 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?
March 30, 2015 at 19:51 #5353
Anders SøborgKeymasterHi 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
March 31, 2015 at 19:47 #5355
Joe SmithParticipantHi 🙂
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
April 5, 2015 at 19:46 #5362
Anders SøborgKeymasterHi there
This would be an excellent opportunity… if you know C++ then it is easy to move to C#.
/Anders
April 6, 2015 at 09:50 #5363
Joe SmithParticipanthmm 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
-
AuthorPosts
You must be logged in to reply to this topic.
Follow