Eiffel and Mindstorms

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
boxer41a
Posts: 9
Joined: 13 May 2011, 10:55

Eiffel and Mindstorms

Post by boxer41a »

I would like to write a library for interfacing Eiffel with the NXT Mindstorms but do not have the required expertise for working with Bluetooth/USB and bytecodes. I know Eiffel but do not understand how to send commands to the processor. I have only been able to wrap the example that came with the fantom api in order to play a tone, but I'm not sure that is the way to go.

The objectives is to produce a platform for testing concurrency in Eiffel, test multi-agent systems, have an interesting platform for teaching intro to programming in EIffel, and motivate my daughter to program (its her robot by the way).

Would anyone like to work together on this project? I could teach you Eiffel; you teach me how to work with the byte code.

Thanks and best regards
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Eiffel and Mindstorms

Post by mightor »

I programmed Eiffel in college about 15 years ago. Does Eiffel have the ability to bind with C# or C++ ? If so, you could just use one of the existing frameworks (higher level than Fantom) and write a wrapper class for it in Eiffel.

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
boxer41a
Posts: 9
Joined: 13 May 2011, 10:55

Re: Eiffel and Mindstorms

Post by boxer41a »

yes, easily with c and c++.

Which framework would you recommend?
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Eiffel and Mindstorms

Post by mightor »

I believe the Fantom library does have C++ headers. The direct command interface isn't that scary if you just know what to send. This is from a C++ program I downloaded from the Net to control an NXT motor via the Kinect:

Code: Select all

void motor (ViUInt8 out, ViUInt8 pwr, nFANTOM100::iNXT *ptr)
{
	nFANTOM100::tStatus status;
	ViUInt8 directCommandBuffer[] = {
		0x04, //SETOUTPUTSTATE
		out, // Output port
		pwr, // Power (-100..100)
		pwr?0x05:0x02, // Mode byte (motor on)
		0x01, // Regulation mode idle
		100, // Turn ratio
		pwr?0x20:0x00, // Run state
		0x00, 0x00, 0x00, 0x00, 0x00 // Tacholimit
	};

	ptr->sendDirectCommand (false,
		reinterpret_cast< ViByte* >( directCommandBuffer ), sizeof( directCommandBuffer ),
		NULL, 0, status );
}
As you can see, if you make your own wrappers like this for reading sensor data, controlling motors, etc, you can easily make Eiffel bindings for those. I've only ever used the Mindsqualls C# library, which is probably not going to be of much use to you.

I found this C++ wrapper: http://www.norgesgade14.dk/bluetoothlibrary.php , I have never used it but the project looks familiar.

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
boxer41a
Posts: 9
Joined: 13 May 2011, 10:55

Re: Eiffel and Mindstorms

Post by boxer41a »

Thanks Xander. But I have two questions:

1. What does the c++ code you included do? Was that just to demonstrate how to build the command buffer to send via "send direct command" in fantom? How did you know to include all this? Where is a reference describing how to build the command buffer?

2. The link to the bluetoothlibrary looks promising but I get numorous errors.
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(91) : warning C4005: 'AF_IPX' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(460) : see previous definition of 'AF_IPX'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(127) : warning C4005: 'AF_MAX' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(479) : see previous definition of 'AF_MAX'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(163) : warning C4005: 'SO_DONTLINGER' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(402) : see previous definition of 'SO_DONTLINGER'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(206) : error C2011: 'sockaddr' : 'struct' type redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(485) : see declaration of 'sockaddr'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(384) : error C2143: syntax error : missing '}' before 'constant'

I am 20 years rusty with C++ but it appears I am getting duplicate define errors and perhaps some cascading. I'm using MS Visual C++ 2008 on windows XP. I included all the .h and .cpp files and copied the example to a "main.cpp" file. What am I missing?

thanks
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Eiffel and Mindstorms

Post by mightor »

The code I included moves a motor and I just copied it from some project I found on the Net. This site to be exact:http://rasomuro.org/20110326.html

I've never used the C++ wrapper I linked to you. I use C# when programming for the NXT via a Windows host. What you are seeing are simply warnings, they don't need be bad. Did you take a look at any of the examples that came with the library? I think there are some example programs on the website. Like I said, I just googled for this library and it looked familiar; I've seen it mentioned on the NXTasy forums (RIP) in the past.

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: Eiffel and Mindstorms

Post by timpattinson »

boxer41a wrote: 2. The link to the bluetoothlibrary looks promising but I get numorous errors.
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(91) : warning C4005: 'AF_IPX' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(460) : see previous definition of 'AF_IPX'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(127) : warning C4005: 'AF_MAX' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(479) : see previous definition of 'AF_MAX'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(163) : warning C4005: 'SO_DONTLINGER' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(402) : see previous definition of 'SO_DONTLINGER'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(206) : error C2011: 'sockaddr' : 'struct' type redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(485) : see declaration of 'sockaddr'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(384) : error C2143: syntax error : missing '}' before 'constant'
Remove the references to network.h and network.cpp
I am using this library at the moment with VC++ - 2010 and it works fine 4 me
:D
PS.
mightor wrote: I found this C++ wrapper: http://www.norgesgade14.dk/bluetoothlibrary.php ,
I don't think it's a wrapper around the Fantom API as such.
When debugging, I found it uses the Windows API to open a comport and send bytes, no mention of any Fantom stuff
Commit to Lego Mindstorms StackExchange Q&A http://area51.stackexchange.com/proposals/4105
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: Eiffel and Mindstorms

Post by linusa »

timpattinson wrote:
mightor wrote: I found this C++ wrapper: http://www.norgesgade14.dk/bluetoothlibrary.php ,
I don't think it's a wrapper around the Fantom API as such.
When debugging, I found it uses the Windows API to open a comport and send bytes, no mention of any Fantom stuff
It's not a problem, for Bluetooth commands you don't really need the Fantom API. If you've got a virtual serial COM port (or rfcomm device on Linux), you can just send the packets via this erial interface.

I personally found working with the Fantom SDK not as "easy". I remember getting warnings, errors, and missing header files, too (and VISA related problems). You can also try to use libusb for USB connections, but many people (including me) couldn't somehow get the combination "Windows, USB, Mindstorms NXT, libusb" working.

There are other C++ interfaces:
NXT++ http://nxtpp.clustur.com/ , libnxt https://code.google.com/p/libnxt/ , aNXT https://github.com/jgraef/aNXT . Maybe more...

Can you use Python with Eiffel? I think here's an example: http://epolyglot.sourceforge.net/epolyglot/x495.html
If that works, you might be able to use the Python interface nxt-python https://code.google.com/p/nxt-python/


Essentially you have two major options:
1.) You use an existing framework, which creates the byte packets (called "direct commands") for you. Then you just create an interface to this framework. Advantage: You save effort, work, and development time. No need to re-invent the wheel twice.
Disadvantage: You get all the bugs and problems of the framework you use, + additionl overhead, + you don't have the deep understanding "by doing it yourself".

2.) You do everything yourself. You create a "communication layer" that sends the commands via Bluetooth and/or USB, and you create your own byte packets. It's not that complicated actually. By now there are MANY open source implementations you can look at, learn from, or even just port. If you want, I can point you at the "cool ones".
Advantage: Flexibility, performance, "nicer solution".
Disadvantage: More work, bound to repeat other's mistakes or "introduce the same bugs they fixed some time ago".

I'd suggest starting with option 1.). When I was facing the same choice years ago, however, I went for 2.). I think the languages MATLAB and Eiffel are both a bit of "exotic", so this whole thing might be comparable. The big difference: During the years, lots of things have happened, insights where made, tutorials written, and everything tends to be more stable...
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
boxer41a
Posts: 9
Joined: 13 May 2011, 10:55

Re: Eiffel and Mindstorms

Post by boxer41a »

I now have the example from the com library running in Visual Studio 2010
Thanks for all the helpfull replies.

jjj
boxer41a
Posts: 9
Joined: 13 May 2011, 10:55

Re: Eiffel and Mindstorms

Post by boxer41a »

Well, I had the example working and even had some of the eiffel wrapper working to turn a motor on and off.

Now, though, in EiffelStudio I get an "external error" in a C call. Going back to VisualStudio and running the C++ example I get:
'NXT_com.exe': Loaded 'C:\Documents and Settings\jjj\Desktop\NXT_com\Debug\NXT_com.exe', Symbols loaded.
'NXT_com.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', Symbols loaded (source information stripped).
'NXT_com.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', Symbols loaded (source information stripped).
'NXT_com.exe': Loaded 'C:\WINDOWS\system32\msvcp100d.dll', Symbols loaded.
'NXT_com.exe': Loaded 'C:\WINDOWS\system32\msvcr100d.dll', Symbols loaded.
First-chance exception at 0x7c812afb (kernel32.dll) in NXT_com.exe: Microsoft C++ exception: Nxt_exception at memory location 0x0012fba4..
The program '[2972] NXT_com.exe: Native' has exited with code 0 (0x0).

When I try to connect from the NXT I get, "Line is busy." So it work for a while, but now will not. Any ideas?
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests