Posts with «python» label

Fitting a sphere

Today I had to write a program to fit a sphere to a bunch of points that were supposedly near the surface of a sphere, but were noisy and sampled in a very biased way.  Since this is obviously not a new problem, I started out doing web research.  but I didn’t look for fitting a sphere, but for fitting a circle, since that is a simpler related problem.

I found a lot of papers, including several review papers, on how to fit a circle to a bunch of points.  The “obvious” method is to  do a least-squares fit to minimize the distance between the points and the circle, minimizing , where is the radius and is the center of the circle.  Unfortunately, that is a difficult problem to solve, and even numerical methods require a lot of iterations to get decent solutions.  What most people do is to change to a slightly different problem that optimizes a different fitness function.  For example, Kåsa’s method minimizes .

There is a very nice, but very formal, presentation of the methods in a paper by Vaughn Pratt from 1987: Direct Least-Squares Fitting of Algebraic Surfaces.  This paper introduced Pratt’s method, which was later slightly improved to make Taubin’s method. I did not read these original papers (other than skimming Pratt’s paper).  Kåsa’s paper (A curve fitting procedure and its error analysis. IEEE Trans. Inst. Meas. 25: 8–14) does not seem to be available on-line.  The IEEE digital library is missing the whole 1976 year.

I did find a recent paper that does careful error analysis of both the geometric approach and several of the algebraic approaches (including the most popular ones: Kåsa, Pratt, and Taubin):

Ali Al-Sharadqah and Nikolai Chernov
Error analysis for circle fitting algorithms
Electronic Journal of Statistics
Vol. 3 (2009) 886–911 ISSN: 1935-7524 DOI: 10.1214/09-EJS419

This paper shows that Taubin’s method is theoretically superior to Pratt’s which is theoretically superior to Kåsa’s (having less essential bias), and gives a very weak example showing it is also tru empirically.  More interestingly, it also gives a “hyperaccurate” algorithm that has less bias even than Taubin’s method.  I did not read the error analysis, but I did read the description of their Hyper algorithm and the implementations of it that Chernov has on his website.

Since I needed Python code, not Matlab code, and I needed spheres rather than circles, I spent a few hours today reimplementing Chernov’s Hyperfit algorithm.  I noticed that the basis suggested by Pratt for spheres, , was a simple modification of the one used in both Pratt’s paper and Chernov’s paper for circles, .  I decided to generalize to dimensions, and use the Numpy package in Python for all the matrix stuff.  I hope I got the generalization right!

From starting to look for papers until getting the code working was about 6 hours, but I had lunch in there as well, so this felt like pretty speedy development.  I’ve released the code with a Creative Commons Attribution-ShareAlike 3.0 Unported License, and would welcome corrections and improvments to it.

Of course, after all this buildup, you are probably wondering why I needed to fit a sphere to points—that is not a common problem for a bioinformatician to have.  Well, it is for the robotics club, of course.  They’ve been having a lot of trouble with the magnetometer calibration and heading code, so we decided to try doing an external calibration of the magnetometer, which has an enormous arbitrary 3D offset.  By waving the magnetometer around in different orientations (which means tumbling the ROV once the magnetometer is installed), we can sample the magnetic field in many orientations, though far from uniformly.  The center of  the sphere fitted to the readings gives us the 3D offset for the magnetometer.

My son and I tested it out with Python code and Arduino code that he had written to get the data from the magnetometer to the laptop, and the magnetometer readings do seem to be nicely centered around (0,0,0) after we do the correction.  We’re still having trouble using the accelerometer to get a tilt correction to give us clean compass headings, but that is a problem for tomorrow morning, I think.


Tagged: Arduino, circle fitting, magnetometer, NumPy, Python, sphere fitting

Python meets arduino

A nice talk by Peter Kropf at PyCon 2012 on using Python to control external devices through an Arduino.

[Via: peterkropf.com]

Arduino Blog 12 Mar 13:55
arduino  python  

Comunikino, an Arduino based communication system


“Social things” have replaced traditional ways of communication like sms and mails. One day i needed a new, fast, mobile and snappy way of communication. So i made Comunikino.

It consists of two main parts, a script in python that runs on the PC and a box that goes on desktop connected through an USB cable used for data and power (no external power needed if you use a 500mA capable usb port!). You need to create a mailbox  for Comunikino with pop access, i raccomend to use gmail as many providers don’t offer pop access for free mailboxes.

Using Comunikino is simple, just send a mail to its mailbox and the subject will be printed to its LCD (max 16 chars). Who reads the message can use one of three Comunikino’s buttons to send a mail back to the address setted in python script to say yes, no or readed (this button can also to be used to say: “hey! I’m thinking of you”). Simple, isn’t it?

Schematics

Built instructions

You will need:

Amount Part Type Properties
1 Arduino I’ve used a 2009
1 Basic Servo
1 LCD screen I’ve used a 16×2 LCD based on SPLC780D chip which is totally compatible with Arduino LCD library that is made for HD44780 chip. Its voltage must be 5V!
3 Push-button Momentary normally open push button
1 Rotary Potentiometer Rotary Shaft Potentiometer; see datasheet for maximum resistance
1 Metal or plastic box I used a 13,5×7,5×5,5 cm metal box but you can use anything similar.

First of all the box. You should cut it as images below.

LCD mounting:

Now fix arduino and buttons like this:

Mounting servo:

Putting things together (you can note a recycled hard disk flat cable used to connect LCD, ricycling is good!):

The flag can be made by wood, mdf, or cardboard:

Setting up software

Now download Comunikino's software (122) (Linux and windows version inside) and configure python script with the data of the mailbox you have created for Comunikino (gmail is highly recommended). You can do this just editing the script with a text editor. At the beginning you will find variables that you have to change, read comments for explanation! You need also to download and burn into Arduino the sketch into the zip (no modification needed here).  Remember, before launching python script you have to connect Comunikino to PC or it will quit with an error. I advice you to leave Comunikino linked to the computer and make the script run on start up. Comunikino will inform you that it is ready to communicate with a mail every time computer is turned on.

Comunikino is ready!

Perhaps you may find interesting:

Eraclitux 24 Nov 23:43
arduino  idea  project  projects  python  sketch  

Comunikino, an Arduino based communication system


“Social things” have replaced traditional ways of communication like sms and mails. One day i needed a new, fast, mobile and snappy way of communication. So i made Comunikino.

http://www.youtube.com/watch?v=Y9fqseT9PI0

It consists of two main parts, a script in python that runs on the PC and a box that goes on desktop connected through an USB cable used for data and power (no external power needed if you use a 500mA capable usb port!). You need to create a mailbox  for Comunikino with pop access, i raccomend to use gmail as many providers don’t offer pop access for free mailboxes.

Using Comunikino is simple, just send a mail to its mailbox and the subject will be printed to its LCD (max 16 chars). Who reads the message can use one of three Comunikino’s buttons to send a mail back to the address setted in python script to say yes, no or readed (this button can also to be used to say: “hey! I’m thinking of you”). Simple, isn’t it?

Schematics

Built instructions

You will need:

Amount Part Type Properties
1 Arduino I’ve used a 2009
1 Basic Servo
1 LCD screen I’ve used a 16×2 LCD based on SPLC780D chip which is totally compatible with Arduino LCD library that is made for HD44780 chip. Its voltage must be 5V!
3 Push-button Momentary normally open push button
1 Rotary Potentiometer Rotary Shaft Potentiometer; see datasheet for maximum resistance
1 Metal or plastic box I used a 13,5×7,5×5,5 cm metal box but you can use anything similar.

First of all the box. You should cut it as images below.

LCD mounting:

Now fix arduino and buttons like this:

Mounting servo:

Putting things together (you can note a recycled hard disk flat cable used to connect LCD, ricycling is good!):

The flag can be made by wood, mdf, or cardboard:

Setting up software

Now download Comunikino's software (430) (Linux and windows version inside) and configure python script with the data of the mailbox you have created for Comunikino (gmail is highly recommended). You can do this just editing the script with a text editor. At the beginning you will find variables that you have to change, read comments for explanation! You need also to download and burn into Arduino the sketch into the zip (no modification needed here).  Remember, before launching python script you have to connect Comunikino to PC or it will quit with an error. I advice you to leave Comunikino linked to the computer and make the script run on start up. Comunikino will inform you that it is ready to communicate with a mail every time computer is turned on.

Comunikino is ready!

I want to upgrade this device with advanced features. I want to create a better software with GUI and a nice looking box. Help me!

Eraclitux 24 Nov 23:43
arduino  idea  project  projects  python  sketch  

Communicate with Arduino using python

Os: linux, windows (not tested), mac osx (not tested)
Difficulty: medium
Knowledge you need: a little bit of python programming (ver 2.x) and Arduino

Let’s see how easy it is to communicate with an Arduino 2009 board and the  pySerial python’s module. What we will do is to use python to send characters serially to an Arduino 2009 which will send them back. Obviously, everything has a demonstration purposes only, since the code proposed here has no specific function but you can easily modify it to get something usable for your projects. What you describe has been tested on Ubuntu 10.04 but should work on other distributions as on various Windows and Mac OSX. If you have problems let me know!

1 Install pySerial

[Linux] Use your favorite packet manager to install python-serial or from command line type the famous command (on Ubuntu &co.):

sudo apt-get install python-serial

Type administration password and you are done!
[Windows] Download ed install pySerial (the file should be pyserial-2.5.win32.exe)

2 Download  test software

Program Arduino with this sketch Serial echo test program (84) and download the  python code Python to Arduino test program (98). Open it with a text editor and see if at line 23 in place of ’/ dev/ttyUSB0′ what you find the editor of the Arduino sketch below themenu Tools -> Serial Port -> xxxxxxx. Remeber, do not forget the single quotes!

conn = serial.Serial('/dev/ttyUSB0', timeout=1)

3 Let’s try

With Arduino programmed with the sketch above and connected to the computer launch communication.py, if it works the board will send back all the ASCII characters that you type on the keyboard. You can verify that the communication is actually taking place looking at the flashing LED TX and RX on the board. Now you can to modify these examples to suit your needs.

Ah! The python’s way…

Eraclitux 20 Feb 11:13

Communicate with Arduino using python

Os: linux, windows (not tested), mac osx (not tested)
Difficulty: medium
Knowledge you need: a little bit of python programming (ver 2.x) and Arduino

Let’s see how easy it is to communicate with an Arduino 2009 board and the  pySerial python’s module. What we will do is to use python to send characters serially to an Arduino 2009 which will send them back. Obviously, everything has a demonstration purposes only, since the code proposed here has no specific function but you can easily modify it to get something usable for your projects. What you describe has been tested on Ubuntu 10.04 but should work on other distributions as on various Windows and Mac OSX. If you have problems let me know!

1 Install pySerial

[Linux] Use your favorite packet manager to install python-serial or from command line type the famous command (on Ubuntu &co.):

sudo apt-get install python-serial

Type administration password and you are done!
[Windows] Download ed install pySerial (the file should be pyserial-2.5.win32.exe)

2 Download  test software

Program Arduino with this sketch Serial echo test program (403) and download the  python code Python to Arduino test program (385). Open it with a text editor and see if at line 23 in place of ’/ dev/ttyUSB0′ what you find the editor of the Arduino sketch below themenu Tools -> Serial Port -> xxxxxxx. Remeber, do not forget the single quotes!

conn = serial.Serial('/dev/ttyUSB0', timeout=1)

3 Let’s try

With Arduino programmed with the sketch above and connected to the computer launch communication.py, if it works the board will send back all the ASCII characters that you type on the keyboard. You can verify that the communication is actually taking place looking at the flashing LED TX and RX on the board. Now you can to modify these examples to suit your needs.

Ah! The python’s way…

Eraclitux 20 Feb 11:13

n/a

n/a
Let's Make Robots 01 Jan 00:00
3d print  arduino  diy  linux  python  robot arm  windows