Posts with «python» label

Embedded programming gap

It seems that there is a shortage of programmers who can do embedded systems (which is what computer engineering is mostly about these days).

Critics lay much of the blame for the embedded programming gap at the doorstep of university computer science departments that have tended to migrate curricula toward trendy programming languages like Java at the expense of unglamorous tasks such as how to design and analyze algorithms and data structures.
Struggle continues to plug embedded programming gap | EE Times (by George Leopold)

I’m not so sure that Java is at fault here. It seems to me to be perfectly fine second programming language (after a simpler one like Python that does not require all data structures to be declared before any code is written).  The problem is more that the instruction focuses entirely on designing huge complex data structures and using large libraries of complex software components, rather than on fundamentals:

The problems start early in the curriculum. Too often an introductory Computer Science course will fall into one of two extremes: either focusing on the syntactic details of the programming language that is being used—“where the semicolons go”—or else treating programming as a matter of choosing components and plugging them into a GUI.

The basics—how to design and analyze algorithms and data structures—are covered summarily if at all. Software methodologies such as Object Orientation that are best approached in the context of the development life cycle for large applications, are introduced through trivial examples before their benefits can be appreciated: the proverbial cannon that is used to shoot a fly.

The education of embedded systems software engineers: failures and fixes | EE Times (by Robert Dewar)

I’m not so sure that I believe in Robert Dewar’s proposed solution, though, as he suggests having students do more high-level design (software architecture, rather than nuts-and-bolts programming), which is in direct opposition to his claim that students should be getting more training in low-level languages like C.

Robert Dewar also makes an argument for group work at the university level—something that is common in computer engineering programs, but apparently rare in computer science programs.  At UCSC, I know that all computer engineers, electrical engineers, and game design majors are expected to do group senior projects, and some of their other classes (such as mechatronics) are also group projects.

I think that the lack of group projects in many CS courses is not so much tied to Dewar’s idea “a perhaps regrettable staple of the educational process is the need to assess a student’s progress through a grade, and a team project makes this difficult” as it is to the problem of scale—a group project is only reasonable when the project is too big to be done more efficiently by a single person.  Creating and managing such big projects in lower-level classes would be a major undertaking, particularly in the short time frame of a quarter or semester, when a lot of things other than group dynamics need to be learned. Pasting a group structure onto tiny project would make things worse, not better, in terms of training students to be effective in groups (see Group work).

Some entrepreneurs have addressed the problem by starting up “initiatives like Barr’s week-long, hands-on Embedded Software Boot Camp.”  The idea is to take programmers who already have degrees and supposedly know C and train them specifically in the skills needed to do real-time programming. The cost is not small ($3000 for 4.5 days, if you register early).

Some computer scientists have been pointing out problems in the standard CS curriculum for a while:

I started saying this over a decade ago. I even did embedded stuff in my 3rd year data architecture course—my department was uninterested, and the students had a real hard time wrapping their heads around the thought that there are places where resources are limited.

The department fought me when I said that students needed to learn more than one language (Java). The department disagreed when I said that students should learn how to program for environments where bloated OO methods might not work (… But, the ARE no places where efficiency matters!!! It’s all about “Software Engineering”!).

The students had NO idea what it meant to program for a machine that had no disk, only memory.

Part of the reason CS departments are seen as being so out of touch is BECAUSE THEY ARE!!!

University should not be about job training, BUT it is also NOT about teaching only those things the faculty find interesting.

Struggle continues to plug embedded programming gap | The Becker Blog.

I know that there have been struggles between the computer science and computer engineering departments at UCSC about what programming language to teach first, with the computer scientists arguing for Java and the computer engineers arguing for C and assembly language.  Eventually they reached a compromise (which has been stable for about a decade), with the computer science students taught Java first and the computer engineering students taught C first, then both making transitions to the other language.

I think that both approaches work, but the strengths of the resulting programmers are somewhat different.  For device drivers and small embedded systems, I’d bet on the computer engineers, who have a better grasp of low-level details, interrupts, and hardware/software interactions.  For more complicated projects, I’d want one of the computer scientists doing the high-level programming, teamed with computer engineers to do the detail work.

I actually don’t like either C or Java as a first programming language.  I think that students would be better off starting with Scratch, which gets them quickly into multi-threaded code, real time operation, and race conditions, without burdening them with a lot of syntax.  Then they can switch to Python to learn about code syntax, types, and objects, without the burden (or support) of compile-time type checking.  After that they can switch to Java to learn about declaring all their data structures and having very rigid type rules (useful for bigger projects to make interfaces between different design groups more explicit).  In parallel with learning Python and Java, they should learn C and C++ in the context of the Arduino microprocessor (or similar small microprocessor) to control real-time physical systems.

The computer engineers could even skip Java, and learn their rigid type systems strictly in C++ (which they are more likely to use later), though Java is cleaner and makes the learning somewhat easier.

Computer scientists should not stop with this handful of languages, though, as they are essentially all Algol derivatives.  The CS students should also learn a couple of languages that come from different lineages (LISP or Haskell, for example).


Filed under: Uncategorized Tagged: Arduino, C++, computer science, education, higher education, Java, programming, Python, Scratch

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 (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

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

n/a

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