Stargaming | 9 Feb 09:02

Python on the NXC

Hey robo folks,

Abstract: I want to run Python on the NXT, not on my PC and connect to 
the NXC remotely. Did I understand the architecture correctly? Any hints 
on translation?

As far as I know, the NXT is so far lacking support for high-level 
languages running *natively*. There seems to be support for some by using 
a custom firmware, for example `leJOS NXJ`_ (Java) and pbLua_ (Lua). [1]_

The common solution seems to be using a live Bluetooth or USB connection 
to the NXT brick, sending bytecode instructions through a socket to 
control the NXT from a high-level (actually, from *any*) language. [2]_

Now, well, I find having an USB cable connection rather sloppy and not 
everyone has a Bluetooth device yet. So I figured I'd make high-level 
languages (I'm mainly targeting Python, but see below) Just Work.

So, first off, let me see if I understood the flow of a NXT program: 
Let's assume I start off with a NXC program. The NXC compiler will 
translate it to NBC assembly code. The NBC compiler, in turn, will 
translate it to bytecode. I can transfer this bytecode to my NXT brick 
and let its virtual machine execute it there (this is much like the 
normal flow of a C program on a PC, isn't it?).

Are my explanations right so far?

How could I let Python programs run on the NXT now? I can image following 
solutions:

* Write a firmware. This might mean reimplementing the *whole* Python 
architecture and generally sounds like a bad idea. The advantage would be 
a more complete package.
* Bundle the Python interpreter (written in C) with the Python program. 
Much like the way one would distribute Python files as Portable 
Executables. Major drawback: Huuuuge Python interpreter on the tiny NXT 
memory. I doubt this will work. One would *have to modify* the Python 
interpreter and could strip off parts that aren't of any use on the NXT 
anyways (for example unicode and all kinds of encodings) but I still 
doubt this would work out so well.
* Write a translator from Python to NXC (as in: Python to C). So, 
traverse a Python syntax token tree and yield NXC instructions (basic 
flow should be the same). This might be the easiest but most error-prone 
way to go. Chances are high someone already translated Python to C (in a 
not-so-sophisticated, high-level way).
* Write a translator from Python bytecode to NXT bytecode. Since I 
haven't looked into NXC bytecode yet, I don't know if this would work. A 
quick glance on the `NXT Bytecode Reference`_ didn't look that promising. 
The NXT bytecode looks closer to PC assembly than Python bytecode...
* Use PyPy to convert RPython (restricted, stripped-down Python) to NXC. 
They already have a nice infrastructure to convert Python (and, as a 
bonus, some other languages) to other backends, so this might work. As 
far as I know, translation is quite expensive, though, thus this might 
not be the best solution. Perhaps the Python interpreter could be 
translated as well (translate interpreter once, read python files often), 
but I guess this would yield the same problems as porting Python to the 
NXT.

Any other ideas I have overlooked? Remarks on my ideas?

Thank you in advance,
Stargaming

.. [1] See "NXT Programming Software" in the `NXT Programming`_ overview
.. [2] See "NXT Off-Brick controllers (Remote Controls)" in the `NXT 
Programming`_ overview.

.. _NXT Programming: http://www.teamhassenplug.org/NXT/NXTSoftware.html
.. _leJOS NXJ: http://www.lejos.org/
.. _pbLua: http://www.hempeldesigngroup.com/lego/pbLua
.. _NXT Bytecode Reference: http://cache.lego.com/upload/
contentTemplating/MindstormsOverview/otherfiles/2057/LEGO%20MINDSTORMS%
20NXT%20Executable%20File%20Specification(1)_9143FED4-3FF8-40B1-
A06F-78B530347A59.zip


Gmane