NXC Control via USB

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
bblais
Posts: 2
Joined: 06 Sep 2011, 15:12

NXC Control via USB

Post by bblais »

Hello,

I am trying to control the NXT (drive motors and read sensors) through the USB. Since I work in python, I tried nxtpython, but couldn't get the usb communication to work on either Windows 7 or OS X Lion. I can, however, get NXC working and was wondering if there is a way to interact with a NXT brick running an NXC program through the USB. I'm imagining something where the NXC program polls the sensors and updates a block of data, and the computer can either read that data or send motor commands. I don't mind writing both sides of this, but I don't know where to start. Are there some simple examples of using the USB to exchange info between the computer and the brick, in NXC?

thanks!

Brian Blais
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: NXC Control via USB

Post by linusa »

bblais wrote:I can, however, get NXC working and was wondering if there is a way to interact with a NXT brick running an NXC program through the USB.
No, not from within the language NXC. Programs written in NXC are always compiled to an RXE binary and executed by the NXT brick, not a computer.
bblais wrote: I'm imagining something where the NXC program polls the sensors and updates a block of data, and the computer can either read that data or send motor commands. I don't mind writing both sides of this, but I don't know where to start. Are there some simple examples of using the USB to exchange info between the computer and the brick, in NXC?
If you want to do this, you indeed have to write "both sides". But the computer side cannot be in NXC, it has to be in another language, preferrably one that has already sensor and motor commands implemented and some function for USB connection handling. nxt-python is one of those packages for Python.

The topic of how these "two sides" work together and how they can be written is covered here https://sourceforge.net/apps/phpbb/mind ... ?f=3&t=181 and here http://ni.fr.eu.org/lego/messages , for example.

I don't know the exact requirements of your program, but in general, you should try to stick to "one side" first, before you try the two-side approach. I.e. you could program in NXC, and let your program run on the brick. If you want to analyze data on a computer, you could collect data on the brick (data logging) and analyze them later. On the other hand, you could write just a program on the computer, without executing anything on the brick. There are many many possible solutions for those two approaches.

What was your exact problem with nxt-python? Did you follow the installation instructions https://code.google.com/p/nxt-python/wiki/Installation ?
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
bblais
Posts: 2
Joined: 06 Sep 2011, 15:12

Re: NXC Control via USB

Post by bblais »

Thanks for the responses!
don't know the exact requirements of your program, but in general, you should try to stick to "one side" first, before you try the two-side approach.
I've done this for a while. What I wanted to do is to be able to pull in more analysis tools, like numpy, scipy, matplotlib to look at and analyze data from the robot.
What was your exact problem with nxt-python? Did you follow the installation instructions https://code.google.com/p/nxt-python/wiki/Installation ?
Yes, and then I tried to run the spin.py example, and I get a lovely bus error (on the Mac, OS X Lion). :) I can't use bluetooth, because PyBluez only works with 2.6 and I have 2.7. I know I can get nxc working fine, so I figured I'd ask. I'll look at that message passing page. Unfortunately, it seems to use nxtpython, and the bus error occurs on the locator.
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: NXC Control via USB

Post by linusa »

bblais wrote: I've done this for a while. What I wanted to do is to be able to pull in more analysis tools, like numpy, scipy, matplotlib to look at and analyze data from the robot.
Ah, ok. There are two ways, in general:
1) Collect data on the robot (data-logging). Do this by running a NXC program which creates the data files. There are again two possibilities: You can A) write the data directly to files. The problem is, that sometimes the NXT's flash memory "lags", so your program might get interrupted for some period when data is written to disk (I guess between ~50 and 500ms, just a guess). Some time ago you couldn't work around this, even by using multiple tasks in NXC, but I'm not sure about current versions. B) You can log data to memory, and write them to a file when your datalogging is over. This is very time efficient, but memory is limited, obviously. You could take a look at my "data logging to memory" example ( http://www.mindstorms.rwth-aachen.de/tr ... Memory.nxc , download link at the bottom). This produces a CSV-file on the robot.
You then take the data file and analyze it offline on a computer, with all the tools you want.


2) You use a remote-control solution, and log the data on the computer. You don't have problems with limitations of memory or disk-size, and you can do it in nearly real-time. Problem is the latency for NXT-to-PC-communication with Bluetooth connections.
Big advantage of this method: You can request data from the NXT at the same time while it is executing a classic RXE program (made from NXC or NXT-G files). This doesn't influence the performance of the program, but you get live sensor data "on top" and can log them.
bblais wrote: Yes, and then I tried to run the spin.py example, and I get a lovely bus error (on the Mac, OS X Lion). :) I can't use bluetooth, because PyBluez only works with 2.6 and I have 2.7. I know I can get nxc working fine, so I figured I'd ask. I'll look at that message passing page. Unfortunately, it seems to use nxtpython, and the bus error occurs on the locator.
I vaguely remember that nxt-python may have changed their backend from using libusb to using the Fantom driver. Maybe that's the problem? What about using an older Python version? Or try an older nxt-python?

Anyway, you should go ahead and ask the nxt-python guys, or at least start a new thread here and describe your problem. Your choice of tools (numpy, scipy, matplotlib) seems great, but it relies on Python. There should really be ways to get that working.
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
tcwan
Posts: 186
Joined: 30 Sep 2010, 07:39

Re: NXC Control via USB

Post by tcwan »

linusa wrote:
bblais wrote: Yes, and then I tried to run the spin.py example, and I get a lovely bus error (on the Mac, OS X Lion). :) I can't use bluetooth, because PyBluez only works with 2.6 and I have 2.7. I know I can get nxc working fine, so I figured I'd ask. I'll look at that message passing page. Unfortunately, it seems to use nxtpython, and the bus error occurs on the locator.
I vaguely remember that nxt-python may have changed their backend from using libusb to using the Fantom driver. Maybe that's the problem? What about using an older Python version? Or try an older nxt-python?

Anyway, you should go ahead and ask the nxt-python guys, or at least start a new thread here and describe your problem. Your choice of tools (numpy, scipy, matplotlib) seems great, but it relies on Python. There should really be ways to get that working.
nxt-python 2.20 with pyfantom http://pyfantom.ni.fr.eu.org/ supports USB using the Fantom drivers on Snow Leopard (10.6.x) just fine.
Bluetooth is not working due to some issue with the raw message send/reply which I haven't debugged yet.

AFAIK, there is no Fantom support on Lion yet, unless you managed to get the existing Fantom drivers working with Lion in 32-bit mode. I can't confirm since I'm not running Lion.
Also, it would be better to test Fantom support on Lion using John Hansen's NeXT Tool (which is a known quantity) first.

I've never had success with libusb on Mac OSX.
I've only tried lightblue (actually LightAquaBlue), it used to work under 10.6.7, but after upgrading to 10.6.8, lightblue+nxt-python would segfault.
Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests