The UHF of the film world.
nxtlibc 0.1 - Lego Mindstorms NXT Bluetooth library in C for linux
Tuesday, April 17th, 2007

This is the first release of my library which is specifically aimed at linux (and un*x) systems for bluetooth control. Not all functions have been tested, and in fact I don't know what all the functions do (like poll_command). I also have some problems like how to switch the ultrasonic sensor to cm. So let me know where I'm wrong, what can be fixed, what feature requests you would like, etc..

You might probably notice some calls like draw_pixel are pretty slow, this is because for all calls I have "reply required" turned on. In the next release I will add a function to turn off reply required for all non-necessary functions.

NOTE: You MUST have already paired with the NXT brick from the device you will e using the library with. It does not do the secret pin transaction.

Also I must give credit to Michael Collins, whose ultrasonic and other code from his LEGO::NXT perl module I looked at while writing this.

Here is the API.

If you get an "undefined reference to round" add -lm.
I forgot, you must compile with "-lm -lbluetooth -lnxtlibc"
And the source code is available from: http://sourceforge.net/project/showfiles.php?group_id=192706


avatar

R Delgado (11 years ago) Reply

Hi!
I am just trying to use your library and a I getting the following error when compiling test.c:

/usr/local/lib/libnxtlibc.so: undefined reference to `round'

Is the round function created by you or is
it defined somewhere else?

I'am looking forward for this library cause
I really want to control my NXT from Linux.

Thanks

avatar

R Delgado (11 years ago) Reply

Hi Again!

I've just solved the problem. I forgot to include -lm in gcc.
Now, it seems that the program connects to BT but now I get the following when running:

rfcomm connection to XX:XX:XX:XX:XX:XX good.
nonblock failed: : Bad file descriptor
Failed to initialize bluetooth

Do you know what does that means?
Do you know what could be going on?

Thanks Again!

avatar

quietearth (11 years ago) Reply

Hey sorry I should have mentioned, you must have already paired with the nxt brick from the device you are using. In linux, "hcitool cc XX:XX...." should work.

avatar

Erik (11 years ago) Reply

I keep on having trouble. I cannot get them the pair correctly... anyone else having this same problem?

avatar

ErikDeBruijn (11 years ago) Reply

Hi, I finally got it working. I can now read some simple things (firmware version, battery level) from the NXT. Thanks for the library so far. I have some suggestions. I'm not experienced in C or C++. I saw that I couldn't use the program from C++ without adding extern "C" { #include "nxtlibc.h"
} around it. This may seem a trivial thing, but it was a full day of searching and asking around for me. Perhaps others also want to use it in C++ software. I propose that you add this to the top of the header file:
#ifdef __cplusplus
extern "C" {
#endif

and at the bottom:
#ifdef __cplusplus
}
#endif

Source: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.4

Thanks again for this lib! Keep up the good work!

avatar

R Delgado (11 years ago) Reply

Hi!

I still can't make it work.
I keep receiving the:

rfcomm connection to XX:XX:XX:XX:XX:XX good.
nonblock failed: : Bad file descriptor
Failed to initialize bluetooth

message. I use de hcitool command and it seems to connect but then I get the same error message.

Any suggestions?

Thanks!

avatar

quietearth (11 years ago) Reply

You have to get it to pair with the nxt brick using a pin. Try restarting your bluetooth daemons and re-running the hcitool cc XX.... You might need to do this a few times.

avatar

ErikDeBruijn (11 years ago) Reply

Hi,
I'm building a generic real-time image recognition system that constructs a 3d model of the Lego robot and its world. I wanted to fully control the robot from the same C++/C code as the rest was (also because URBI only runs on windows and I can't connect to a windows box, even with the Windows Firewall turned completely off). Windows lacks the transparency for me (I can't look at the code when I get stuck).

For reading variables, generating beeps the library works fine.

How could I control the servos? Right now I must start a program from each thing I want to do (such as a-up, b-down10).

I tried creating a servoMove C++ function that calls _set_output_state(), but this takes so many parameters that I can hardly make something out of it. When I enter values that I expect that they should do something, I get no behavior out of the servos. It also returns a TRUE, suggesting no (reported) error. Running just the beeper also works (so it seems to be specific to servo control).

The enumerations/defines such as MOTOR_ON are not available in the function I'm programming. Can I use 0x00 instead for the first one, and e.g. 0x10 for the third?

I'll post my function when it works. You may then add it to the nxtlibc package.

avatar

quietearth (11 years ago) Reply

Of course you can use the values in place of the #defines if they are not available, and I'm sorry I don't have more explanation for motor states, this is all the bluetooth api specifies. I wrote this before actually making a robot (which I should be doing soon) and I would be glad to incorporate any new functions (macros) for motor functions you come up with in next releases, as I also plan to do the same when I get around to actually building something.

avatar

ErikDeBruijn (11 years ago) Reply

After a reinstall of Kubuntu I had mayor difficulties (again) with getting pairing right... Last time it was difficult and now the tricks I learned (starting the bluetooth-applet from gnome) was not enough.
I did the following:
dbus-send --system --type=method_call --print-reply --dest=org.bluez /org/bluez/hci0 org.bluez.Adapter.RemoveBonding string:00:16:53:02:xx:xx
dbus-send --system --type=method_call --print-reply --dest=org.bluez /org/bluez/hci0 org.bluez.Adapter.CreateBonding string:00:16:53:02:xx:xx
After that I had to click the bt-applet icon in the system tray.

avatar

juan (11 years ago) Reply

Hi!
I am just trying to use your library and a I getting the following error when execute test

./test: error while loading shared libraries: libnxtlibc.so: cannot open shared object file: No such file or directory

I work in ubuntu

Help!

avatar

quietearth (11 years ago) Reply

Hey Juan, you need to set your LD_LIBRARY_PATH variable to include the dirfectory where libnxtlibc.so is.. then it will be able to find it.

avatar

juan (11 years ago) Reply

Thank you very much quietearth!!

this library is sensational

avatar

Jonathan (11 years ago) Reply

Hey i'm having a problem when i launch test :

./test: error while loading shared libraries: libbluetooth.so.1: cannot open shared object file: No such file or directory

avatar

Elkin (11 years ago) Reply

i have this problem:

rfcomm connection to XX:XX:XX:XX:XX:XX good.
nonblock failed: : Bad file descriptor
Failed to initialize bluetooth
?????

avatar

Nacho (11 years ago) Reply

I also get the error :

rfcomm connection to XX:XX:XX:XX:XX:XX good.
nonblock failed: : Bad file descriptor
Failed to initialize bluetooth

I fixed it adding :

s = nxtsock;

on bluetooth.c:219 and recompiling the library.

Thanks for this library.

avatar

Radek (10 years ago) Reply

Hi.
Jonathan, i´m having de same problem:
./test: error while loading shared libraries: libbluetooth.so.1: cannot open shared object file: No such file or directory
Someone can help us?

avatar

Anonymous (10 years ago) Reply

Hi, I've downloaded the library and I've compiled it with "make all". But while trying to compile test.c (gcc test.c -o test -lm -lbluetooth -lnxtlibc) I get this:


jorcoval@jorcoval-laptop:~/Desktop/nxtlibc-0.1$ gcc test.c -o test -lm -lbluetooth -lnxtlibc
/usr/bin/ld: can't find -lnxtlibc
collect2: ld devolvió el estado de salida 1

Any solutions?

avatar

quietearth (10 years ago) Reply

see the post on LD_LIBRARY_PATH

avatar

Radek (10 years ago) Reply

I have this problem:

rfcomm connection to XX:XX:XX:XX:XX:XX good.
nonblock failed: : Bad file descriptor
Failed to initialize bluetooth

Itry solve it adding:

s = nxtsock;

on bluetooth.c:219 and recompiling the library, but it dont fixed the problem.

How can I fixed it?

avatar

Conor (10 years ago) Reply

This appears to be exactly what I'm looking for, I hope. What firmware does the brick need to have for this to work? Lejos + bluetooth = disaster.

avatar

Manolo (9 years ago) Reply

Hi! This library is great!

But, i have an issue. I need read accelerometer values. From HiThechnic data sheet, says that register for the accelerometer starts at 40H and ends in 47H. My question is: there are a way to read that memory locations?

Greetings!

avatar

Anonymous (9 years ago) Reply

Hey!Great Library and great job!

I'm trying to set up a positioning system including three PCs with Bluetooth for triangularization, measuring the distance between the NXT and each of them via the signal strenght..

It's a research project for the university.

Thanks a lot!

avatar

Epokh (8 years ago) Reply

I'm doing a Qt4 interface with your libraries.
Very nice project.
I just get some minor problems with synchronous calls but is not a big issue.

avatar

Simo (8 years ago) Reply

Hello, I have set up LD_LIBRARY_PATH to LD_LIBRARY_PATH=/opt/nxt/nxtlibc-0.1/ where the library is but compiling with gcc -lm -lbluetooth -lnxtlibc c.c -o c I receive the following error:
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../i486-slackware-linux/bin/ld: cannot find -lnxtlibc
collect2: ld returned 1 exit status
Where I exactly missed something? Thank You for your support

avatar

Simo (8 years ago) Reply

Solved. I didn't executed make install. ;)

avatar

mcoder (8 years ago) Reply

hi, good work :) i want use two brick how to make connection between my laptop and two bricks?

avatar

Yuping SHEN (7 years ago) Reply

The library is perfect! But you will also have to read the above comments carefully to avoid some minor traps...
Thanks all!

avatar

alexg127 (7 years ago) Reply

Very nice site!


Leave a comment