59

I have a robotics type project with an Arduino Uno, and to make a long story short, I am experimenting with some AI algorithms. However, I need to implement some high level matrix algorithms that would be quite simple using NumPy/SciPy, but they are an utter nightmare in C or C++. Even with the libraries out there, this is just getting ridiculous.

Is there any way I can do this project in Python? I think I heard something about the Mega having this capability, but I have an Uno, and replacing it is not an option at this point (that would set the project back quite a bit.) Also, I heard somethings about using Python to communicate to the Arduino via USB, but I cannot have the USB cable in while the thing is running. I need to be able to upload the program and be done with it.

Are there any options out there, or have I just reached a dead end?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Alex Eftimiades
  • 2,329
  • 3
  • 22
  • 31
  • 3
    Is this a XY problem? Do you want to know how to use matrices effectively on Arduino (using C++) or do you want to know how to compile python? – Pubby Nov 13 '11 at 21:49
  • 2
    I may be mistaken, but isn't this just a matter of compiling python to avr-c? – danem Nov 13 '11 at 21:56
  • This is more of a linear algebra fitting problem. I am planning on implementing some least squares fitting to determine the relationship between the power sent to the devices and the input it received from the sensors. I have to implement this algorithm for multiple stages--as in stage 1=move there; stage 2=grab something; etc. Each of these stages have a different combination of inputs and outputs, so I the size of the matrices I am working with will not be defined in the usual way in "C." – Alex Eftimiades Nov 13 '11 at 22:09
  • It is probably possible to implement this in C, it is just becoming more trouble than its worth. I was hoping to make this versatile enough to post on sourceforge for other AI projects. – Alex Eftimiades Nov 13 '11 at 22:10
  • Probably a dead end. This would require you to compile not only python, but also numpy and scipy, to avr-c. I strongly suspect that writing your code in C would be faster. – Aaron Dufour Nov 13 '11 at 22:35
  • Pete I wouldn't say "just" a matter of compiling python to avr. Code size of python is about 2M - even if 95% of that isn't relevant to the app it's still going to be a big challenge to fit into avr. @Feynman have you looked at GadgetPC ? could run linux & python. board is more $ than arduino but addons like wifi are cheaper since they are just usb sticks. – greggo Nov 14 '11 at 03:13
  • 1
    Ahh, someone at my local python group just gave a talk about a library for doing python->arduino stuff.. I'll try to dig it up for you. Edit: Found it. Not sure if it's actually useful. http://myhdl.org/doku.php – Daenyth Nov 15 '11 at 04:43
  • If you have to use C++, have a look at [eigen](http://eigen.tuxfamily.org/). Its a very fast high level matrix library. – P3trus Jul 22 '13 at 09:59

4 Answers4

21

There was a talk about using Python with robotics at this years PyConAU called Ah! I see you have the machine that goes 'BING'! by Dr. Graeme Cross.

The only option he recommended for using Python on a microcontroller board was PyMite which I think also goes by the name of Python-On-A-Chip.

It has been ported to a range of boards - specifically he mentions the Arduino Mega which you said is not an option for you, but it is possible it is supported on other Arduino boards.

However, because it is a "batteries not included" version of Python it is more than likely that you will have a real problem getting numpy/scipy etc up and running.

As other posters have suggested, implementing in C might be the path of least resistence.

Update: again, not specifically for Arduino, but pyMCU looks to provide python on a chip. The author states he may look at developing an Arduino version of pyMCU if there is enough interest.

Mark Streatfield
  • 3,019
  • 1
  • 19
  • 18
  • 9
    A new kickstarter project for "Micro Python" might also be of interest to some people. (http://www.kickstarter.com/projects/214379695/micro-python-python-for-microcontrollers) – Mark Streatfield Nov 14 '13 at 22:43
  • I believe the Micro Python project uses a special board, and was not ported to Arduino. They do have Python 3 though.. – Gadi Feb 27 '15 at 03:44
7

I've started work on a "Little Python" to C++ (called Pyxie - a play on Py CC- Pyc-C) compiler, with the specific aim of compiling a sane subset of python to C++ such that it can run on an arduino.

This is far from complete at time of writing (0.0.16), but it can currently compile a very small subset of python - enough for the arduino "blink" example to run. To support this, it has a compilation profile - which essentially means "compile using the arduino toolchain."

A program it can compile looks like this:

led = 13

pinMode(led, OUTPUT)

while True:
  digitalWrite(led, HIGH)
  delay(1000)
  digitalWrite(led, LOW)
  delay(1000)

This parses, performs analysis (like type inference, etc), compiles to C++, which is then compiled to a hex file, which you can load onto your device.

There's a long way to go before it's useful, but it is progressing and does have a roadmap/etc.

In particular a key difference from Micropython (and PyMite) is that it's designed to compile to devices too small to run either implementation. (This also means it's very different from things like ShedSkin which while a Python to C++ compiler target larger execution environments)

Michael Sparks
  • 618
  • 5
  • 9
1

It's going to be difficult to get any kind of Python script running directly on the Arduino uno.Reason is that it is an interpreted language, so you will need a interpreter on-board in addition to the plain text script. There is probably not going to be enough memory for all of thatin arduino uno.

What you can do best is to find a way to compile a Python script to native machine code (this is how C/C++ works). I have seen projects around to do something like that for other platforms, but (as far as I know) none which does it successfully for Arduino uno yet.

you can visit http://www.toptechboy.com/using-python-with-arduino-lessons/ for more.

wish this will help you. thanks!

0

This is not a direct solution but in your circumstances If I were you, I would write the AI program on my computer and the rest of it in Arduino. after that I would write a flask server with my AI program. and then, port forward from my router to the local machine. finally, make requests from Arduino to the server.

Anupama
  • 389
  • 1
  • 9