Yet Another (Simple) POV

There are a lot of POV (Persistence of Vision) projects nice and completely open source that could be found in the Web. The POV is an optical illusion due to our eyes that allow to see two lights turned on, when they are not turned on at the same time. This happens because the light is very fast respect to our retina response/perception.

The POV effect has been used initially as the Magic Wand projects (thanks to them for the idea and some resources). These tools could be used moving them very fast in the air allowing to some predefined images or text to compare.
Another POV‘s application is the rotating clock, a string of LEDs that turns in circle showing the current time.
Applying the same reasoning to a bicycle wheel it is possible to show nice writings or pictures when riding a bike, instead of moving your arm.
There are many example of this kind of application some of them are professional ones like the Lady Ada SpokePOV.

One of the last and more innovative usage of the POV effect will be used in the Tokyo Metro to show some advertisments during each travel (in this case people moves while LEDs are static!).

Unforunately the more complex the showed images are the more complex will be the circuit and the program used to show writings.

In the Bike use case, each POV uses a Hall Effect sensor to detect the rotating speed and decide how much time each led must be turned on!
In this case I decided to create a very simple POV using only some LEDS (8 smaller SMD LEDs in this case) and an Attiny2313 as microcontroller that turn on and off very fast the LEDs.
Obviously in this case there will not be any hall sensor effect (for now) and the writings are statically flashed in the EEPROM.

The circuit schematics is ultra simple and essential. The 8 LEDs are connected from the 1 to the 8 pin of the Attiny2313. This pins corresponds to the 0-7 digital output pins (declaring them as it).

I extract and converted the matrix rapresentation for each symbol (numbers, punctuation, lower/upper characters) from the one provided by the Magic Wand project. In particular a 8 by 6 matrix has been defined for each symbol, setting each matrix’s entry to 1 or 0, as defined also here in order to not waste memory space.

So given a string containing a sequence of symbols, the sketch has to:

  •  scan each symbol of the sequence;
  • convert it in a set of ordered columns (from left to right);
  • for each column get the status of each element in a different row and turn on/off the correspondent LED;
  • keep the LEDs in the current status for some delay;
  • when all columns has been considered, turn off all LEDs and wait some delay in order to create a blank space between two consecutive characters;
  • repeat these steps for each symbol in the sequence.

The conversion in the step 2 is provided by the matrix definition of each symbol. All definitions has been inserted in a switch construct that allow to return the correct matrix representation for the provided symbol.

NB: unfortunately there is a big problem introduced by Integreted Circuit’s usage. It is the memory size! There is not enough memory in the Attiny2313 that I used….I hope to find some (valid) solution (without removing no symbol, TIPS & TRICKS will be appreciated so much!!!).

The entire code used is contained in this file: POV_writer.ino.

The needed hardware will be a simple piece of wood, connected to a DC motor in one vertex and the programmed Arduino with the set of LEDs and a battery on the other side.
Because there is no Hall Effect sensor, or any other device that allows to know which is the rotation speed, I have to make some tries to find the correct speed using a potentiometer connected to the DC motor power supply.

The result is not very precise but it has been developed really low cost!!!.

Ping Robot Car 2

In the previous post about the Ping Robot, I created a small hardware platform for the robot recycling an old RC toy car.

For some reasons of simplicity and available electronics I decided to use as controller an Arduino Leonardo and to construct a shield that will contain the ultrasonic sensor and the motors drivers.

So there are two parts which still need to be developed to obtain a ping robot:

  • the hardware to mount on top of the robot, corresponding to an Arduino Shield with all the circuit’s components needed to drive both DC motors;
  • the software that will be used to read the ping sensor’s signal and to drive the DC motors.

In this post I will describe how to realize the hardware part for the Arduino shield, that it will be  mounted on the top of the robot platform.

First problem that I had to solve was the power source, in order to create a small (but completely) autonomous ping robot. So I decided to use a 9V battery to power Arduino Leonardo, ultrasonic sensor and DC motors. I couldn’t use directely 9V on DC motors and ultrasonic sensor, so I had to buy a voltage regulator (L7805CV) that will return a constant 5V power signal from 9V battery.

After I had to decide how to control robot moving using available DC motors.  My robot does not have stearing wheels on the front, so when it must turn on one motor and turn off (or invert the rotation of)  the other one. It will be drived like a tank! 😀
For simplicity this small robot will turn always on left when it will detect an obstacle, turning off the right side motor. When I was making some test I had to solve the second problem: Arduinos (all models!?) are just microcontrollers, they have to be used only to define the logic of a device; electrical values on pins are just to interact with external world. This is why the TOTAL max output current for ALL pins is 500mA.
So I cannot use directly output voltage/current provided by an Arduino board to turn on DC motors, but I have to use another circuit like a rele to provide the current.
Remembering that I want to maintain the project low cost, I decided to use 2 NPN transistors (2N2222) like switches (a well known practice in DIY projects) like explained here.
I used a 330Ohm resistor between each Arduino output pin and the base pin of each transistor. Moreover, due to the fact that I used NPN transistors, each collector pin must be connected to the motor while the emitter pin is connected to ground for a correct switch circuit.

Finally I created a shield for my Arduino using a prototype breadboard like this one and some male pins soldered on it. I disposed, soldered and connected all components on this PCB, including also  the ping (ultrasonic) sensor used for the obstacles detection creation is the HC-SR04 (just few dollars on Ebay) and two SMD LEDs necessary for debug purposes.

Here there are front and rear pictures of the (bad-soldered low-cost DIY-ugly) shield containing all componentes stacked, in a mess of wires and pins 😀

20140512_205133          20140512_205121

At the end it is possibile to connect the shield to my Arduino Leonardo and test if there is any explosion or short circuit 😀 (seriously I always control all connections to don’t damage Arduino board).

That is a picture reporting the entire “system” (Arduino Leonardo + Shield) and the 9V battery.

20140517_180857          20140517_180813

Next post will be about Arduino code necessary to control robot movings.

The curse of the stepper motors

In this last period I am very interested to make a small CNC reusing some hardware that belongs to old CD/DVD/floppy drives. Inside that “trash” there are a lot of components that could be reused, in particular the bipolar stepper motors!

I used other projects as reference for my idea of reuse 🙂

These motors could be used in a variety of projects, using the motors and the structure used to move the drives reading head.
Unfortunately I didn’t have any precedent knowledge about steppers motors and how they work. Googling around I have found there are a lot of materials about on driving bipolar stepper motors using some H-bridge to power on and off the  motor’s coils (four). H-bridges are necessary because the amount of current provided in any Arduino pins is not sufficient to move a bipolar stepper motor. So H-bridges have to be used as amplifiers (and also to avoid damages to the Arduino).

However H-bridges are unuseful if I don’t have a driver: a controller that turns on and off each motor pin in the correct sequence. You can find on eBay many drivers for bipolar stepper motors but I was really interested to understand how stepper motors works and which is the correct sequence of values to apply to each pin.

On many blogs I have found that people use the Stepper.h library included in the Arduino IDE but almost no one report the correct sequence for each coil.

Fortunately I have found a link where Xiang Zhai has described (really good and with and with a fine grain of details) the entire process for the construction of a small CNC (likely to my project 😀 ). He uses a Raspberry Pi to drive a bipolar stepper motor. Moreover he described also some details about stepper motors working and their sequence of pins values!

Simply using information provided by him I wrote the following driver that allow to use any Arduino to control a bipolar stepper motor. Obviously I constructed an H-bridge and I installed it between my Arduino and the motor.

I done some attempts to understand how to correctly cable the stepper motor and my Arduino (there are two couples of coils). I realized this driver simply using a global counter, 4 vectors and module operator.

Here there Arduino sample sketch, that I used to make some simple movings to my motor:

* This sketch has been defined in order to understand how stepper motors works. In particular I was very interested to understand
* which is the correct sequence of values to apply to bipolar stepper motor pins (4 pins in total, two for each coil).
* Unfortunately not many blogs threat this argument, most of them show examples based on the Stepper.h library contained in the Arduino IDE.
* So I write a very small, simple and compact sketch that could be used as a driver for any bipolar stepper motor.

// pins connected (in couple) to an H-bridge
int motorPinA1 = 8;
int motorPinA2 = 9;
int motorPinB1 = 10;
int motorPinB2 = 11;

// delay between two consecutive steps
int delayTime = 100;

// sequence of values for each motor's pin
int pinA1[] = {LOW, LOW, HIGH, HIGH};
int pinA2[] = {HIGH, HIGH, LOW, LOW};
int pinB1[] = {HIGH, LOW, LOW, HIGH};
int pinB2[] = {LOW, HIGH, HIGH, LOW};

// internal counter used to detect the correct sequence
// of values in the motor's pins
int count = 0;

// variable used to save the previous value of the step pin.
int prevStepValue = LOW;

// initialize the pin mode
void setup() {
    // initialize stepper motor pins
    pinMode(motorPinA1, OUTPUT);
    pinMode(motorPinA2, OUTPUT);
    pinMode(motorPinB1, OUTPUT);
    pinMode(motorPinB2, OUTPUT);

void loop() {
    int num_steps = 10;
    // 10 steps forward
    updateMotorPins(HIGH, num_steps);
    // 10 steps backward
    updateMotorPins(LOW, num_steps);

* Function used to set bipolar stepper motor pins due to the chosen
* direction and specified number of steps in that direction.
void updateMotorPins(int dir, int steps) {
    int val = 0;
    // detect if it have to move forward or backward (turning direction)
    if(dir == HIGH)
        val = 1;
    else if(dir == LOW)
        val = -1;

    // execute the specified number of steps
    for(int i=0; i < steps; i++) {
        // update global position
        count += val;
        // compute the motor pins configuration
        int pos = abs(count%4);
        // apply the motor pin configuration
        digitalWrite(motorPinA1, pinA1[pos]);
        digitalWrite(motorPinA2, pinA2[pos]);
        digitalWrite(motorPinB1, pinB1[pos]);
        digitalWrite(motorPinB2, pinB2[pos]);

        // apply a delay between consecutive steps

I load this code in my Arduino Leonardo, I connected its pins to the H-bridge ones and the H-bridge pins to the stepper motor pins in the correct way (I made some tries). After that it was possible to see the bipolar stepper motor (obtained from an old floppy drive) move clockwise and counterclockwise with a very fine grain precision.
PS: I used an external power source for H-bridge because Arduinos are limited in the amount of current that they could provide (500mA). So I used an old ATX power supply for this purpose and it worked smoothly.

You could simply edit my code and create a two pin driver (direction and step pulse) and upload it in a low-cost microcontroller (ATtiny)  obtaining a stepper driver similar to the existing ones 😉

Custom cool key chain

I usually publish most of my software projects in this blog. But in this last period I’m creating a lot of hardware stuff, following the DIY mentality. Some of this projects are not very useful but they are very funny to realize and to complete (I really like start from an idea and arrive to the final ‘product’).

One of my many (unuseful) ideas is the creation fo some cool design, custom and nice key chain. There are a lot of key chains, differing for size, materials and logos drawn on them. But with the new maker tools (like 3D printers, millers, laser cutters) it is possible to realize complete custom and nice objects directly from your notebook and only with modelling software knowledges!

The main purpose of key chains is to find, distinguish and catch keys in a simpler way and they are necessary in most cases (until we use keys to open doors 😀 ). Another useful function could be to have some light attached to the key ring that could be used to find the keyhole, in particular when you come back home in the night and there is no lights near the door…

Most key chains allow just one of the two possibilities as you can see at this link. My idea was to combine both of them, using the light refraction phenomena to display custom pattern on the key chain and use the light when necessary.

There are already a lot of key chains with some light included but they are:

  • objectively ugly! There are big bunches of plastic with another bunch of metal including LEDs and batteries that is attached to the plastic with some glue…
  • they are three-dimensional objects, very cumbersome and not nice to see…
  • they aren’t customizable, in some cases it is possible to draw a custom image inside the plastic but it is an expensive service..

Here there are some images about existing key chains using lights and drawn images:

That’s why I decided to try to create a new type of key chains which uses the light’s refraction to visualize some custom images/drawings/patterns, made by a layer of plexiglass (any shape is possible)  integrating lights and batteries inside the piece of plexiglass.

The process flow necessary to obtain “my” key chains is the following:

  1. select the LED color (any of the existing ones, most common colors are available);
  2. select the key chain shape (usually is a rectangle with rounded vertices);
  3. select a custom image to draw in the plexiglass surface;
  4. mount and connect battery with the LED;
  5. isolate the electronic part (with some tape?);

Steps 2 and 3 can be done in a simple way using a laser cutter, that allows to shape the plexiglass key chain, make holes for the battery, the key ring and the LED, and draw the custom image.

I’m waiting the opening of a near FabLab the week to access to some professionals tools at affordable prices. I hope to get some nice alpha prototypes.  I will upload them as soon as possible and after I will upload also all projects and details (maybe some tutorial or production flow).

Why Leave IT app

I was bored some evenings ago, so I decided to recycle some information scraped from a web site for the creation of a web app that provides one reason to leave Italy for every day (in some pseudo-random way).

The web site is, handled by an Italian emigrated many years ago and now living in Australia. In this site there are a lot of useful information for anyone that decided to leave Italy.
unfortunately in this last years Italy is not a good place for living especially for young people (there are few jobs, salaries are low and corruption is widely diffused…..).

There is a lot of young Italian people who would like to leave Italy and go in other countries like UK, Germany and even USA. That’s why the site administrator created a poll asking exactly “one reason to leave Italy”.

This is why I has got the idea for the creation of this web application! (not very nice…I know.. :-\  )

I extracted the data from this website, parsing the HTML page containing it, and I saved them in a text file, one phrase per row.
The dataset is composed by over 300 “reasons” and so it could be used to propose/suggest about one reason for each day of a year.
The idea is to create a classic web application (3-tier) where the Data Layer is composed by a static .txt file containing the reasons, the Business Logic composed by a Java Servlet (passing data through HTTP’s methods) used to decide which reason return to the user and the Presentation Layer composed by HTML + CSS + JAVASCRIPT used to show the selected reason.
Defining the row number like an identificator of each reason, it is used to determine which reason to return: current_day_number % #reasons.

I also included some share buttons for the main social networks and an about session reporting the principal information for the developing of this small (un)useful web application. I have done it using this wonderful tutorial where there is explained how to introduce some social network capabilities in any web site (composing URL and associating them to elements, instead of using standard icons and javascript code).

The web app has been hosted in the Google App Engine for free and could be reached at this link.

Date Range Generator

Working on databases I usually find very frustrating the differences that there are in the provided DBMS’s packages.

Except for the (not so much) standard guaranteed by the SQL language, there are a lot of differences in the syntax used to declare a function in the procedural language. Things even go worse working on spatial extensions of some famous DBMS. In particular I have found some difficulties passing from postgreSQL to MySQL DBMS, especially when declaring procedures and looping over queries’ results (MySQL is sooooo painful with its cursors!!! :-[  ).

In one task, I had to generate a range of dates. in particular I needed to generate a date for every day in a provided interval/range. This could be done in postgreSQl using a function for the generation of series like described here! Unfortunately there is no equivalent form in MySQL -.-‘  So I decided to create a procedure that generate any date series, of an arbitrary given step size, and store each generated date in a table.

The procedure is called date_range_generator and has 4 parameters:

  • the name of the table to create for storing generated datetime;
  • the start datetime to consider;
  • the enddatetime to consider;
  • the step to use for the series generation (number of seconds between one generated datetime and the next one).

The procedure simply convert the provided timestamps in unix timestamps (convert all in the amount of seconds passed from the Jannuary 1th, 1970 ) and iterate over a loop adding the number of seconds, corresponding to the step size, to a temporary variable.
Convert the value in the temporary variable in a valid datetime, and store it in the previously created table.
This is done until the temporary variable contains an amount of seconds greater that the end datetime.Note that if the end timestamp could not be generated by this procedure, so it will not be stored in the table.

Moreover this solution could be used to generate potentially any sequence of datetime values, simply editing the step size. It could be possibile to generate the datetime for any hour over a year (or over ten years!!! it’s just a matter of parameters’ values).

I will report it here to simplify the job of any other poor it guy in my conditions XD

# script used for the creation of a datetime series
DROP PROCEDURE IF EXISTS date_range_generator //
CREATE PROCEDURE date_range_generator (tablename VARCHAR(20), start_datetime TIMESTAMP, end_datetime TIMESTAMP, size INTEGER)

SET temp = UNIX_TIMESTAMP(start_datetime);

# drop the table if already exists
SET @tablename = tablename;
SET @drop_query = CONCAT("DROP TABLE ", "IF EXISTS ", @tablename);
PREPARE stm FROM @drop_query;

# create the table for storing generated datetime
SET @create_query = CONCAT("CREATE TABLE ", @tablename, " (id INTEGER AUTO_INCREMENT PRIMARY KEY, date_time TIMESTAMP)");
PREPARE stm FROM @create_query;

# compute datetimes and store each in the table
WHILE temp <= UNIX_TIMESTAMP(end_datetime) DO
SET @insert_query = CONCAT(COnCAT("INSERT INTO ", @tablename, " (date_time) VALUES (FROM_UNIXTIME("), temp , "))");
PREPARE stm FROM @insert_query;

SET temp = temp + size;

END //

# example of procedure usage
SET @length = 60*60*24; # seconds in a day
CALL date_range_generator("time_series", STR_TO_DATE("2013-10-01", "%Y-%m-%d"), STR_TO_DATE("2013-10-31", "%Y-%m-%d"), @length);
SELECT * FROM time_series;

Why Leave IT

Many times I thought about emigration. In particular to leave Italy and decide to live and work abroad. It is well known that in Italy there is not a good situation for work and opportunities of young people. So many Italians (usually under 30 and graduated) decided to leave their nation, looking for new opportunities all over the world. Many of them have found a good job and decided to stay permanently abroad and some of them opened a blog with useful information to leave abroad.

One of this blog/sites is a site handled by an Italian emigrated in Australia and now living permanently there. There are many useful information reported directly by the site’s owner but also by contributes reported by other Italians all over the world and job opportunities.
Moreover some times there is a poll and users could comment/respond freely. One of these polls was to provide a reason to not live in Italy, that in my opinion could also be interpreted as “One reason to leaving Italy”. There has been about 290 replies to this poll and the admin decided to publish the responses.

I decided to take this dataset of opinions (from real people) and to make some text analysis using Apache Lucene, a well known Java framework  famous for its capabilities in text indexing, storing and fast retrieving using particular data structures (like inverted list indexes). It is mainly used for the creation of search engines.

I would like to find the most relevant/common words that could provide a generic view/motivations/patterns because Italians will leave or already leaved Italy.

To do that I copied all answers/phrases in a txt file, where they has been disposed one per row, obtaining a dataset of answers. After some preprocessing removing errors, like double quotes or mistyped words, each answer could be processed individually, dividing the answer from the name of the user that provides it or the user’s living place. Then stop words are removed because they aren’t meaningful for this analysis (conjunctions, articles and punctuations are removed).

The next step corresponds to the stemming phase: it corresponds to the reduction of a word to its morphologically root, for example a word that corresponds to a form of a verb is replaced with the truncated version of this verb (e.g. going => go, making => mak). This allows to make effective queries even when there is not a perfect match between query terms and document’s words, because they will be mapped to the same term in the index.
It is not effective like its counterpart, lemmatization, because it doesn’t care about context but its fast and it has been implemented for different languages.

I used the Lucene 4.4 version that includes stemmers for different languages and stop words files. So I created the IndexWriter that uses the ItalianAnalyzer:

Analyzer an =  new ItalianAnalyzer(Version.LUCENE_32);
//Analyzer an =  new ItalianAnalyzer(Version.LUCENE_44);
IndexWriterConfig iwc =  new IndexWriterConfig(Version.LUCENE_44, an);
IndexWriter iw =  new IndexWriter(new SimpleFSDirectory(new File(dir)), iwc);

I used the version 3.6 because it uses the ElisionFilter stemmer as written in the documentation (is seems to give better results).
I would like to consider the top 50 word that appear in the highest number of documents (without considering the frequency inside each document). Googling a little bit I have found the class HighFreqTerms that has got a static method which does exactly that:

TermStats[] ts = HighFreqTerms.getHighFreqTerms(ir, 50, "text");

The top 50 word with the highest term frequency, obtained with the previous process on 303 documents/comments/replies are:
lavor                81         (work/job)
paes                 74
ital                   70
futur                52         (future)
polit                 51         (politics)
viv                    51         (live)
sol                    50
anni                 40
vit                    38         (life)
perc                 37
far                   35
cos                   34
mot                  34
figl                   32         (child/children)
mancanz          31         (missing)
stat                  30
pag                  30
tass                  28         (taxes)
tropp               27
senz                 26
meritocraz      25         (meritocracy)
part                 25
fatt                  25
sempr              25
possibil            24         (possible/possibility?)
trov                 23
esser               22
italian             22         (italians)
ved                 22
me                  21
person           21
molt               21
pens               21
tant                20
null                20
principal        20
ben                20
propr             20
dev                19
rispett           19         (respect)
class              19
fa                  19
può               19
giovan          18         (young)
aver              18
miglior          18
giorn             18
econom         18         (economy)
fin                 17
camb            17         (change)

I highlighted in red the most interesting terms. It could be noticed that most of the main terms corresponds to the classical one usually listened in many newspapers. I divided them by macro categories:

  1. job / work / economy / taxes
  2. future / life / change / missing
  3. children / young
  4. respect / meritocracy / possibilities

Those categories corresponds to the already known/heard (for me) problems existing in Italy and they are reflected in user’s comments.
That small and fast exercise showed how it is possible to extract information from plain text. Of course there are a lot of more accurate and complex analysis that could be done.
Here there is the code that I used in this post and the needed libraries.

PS: I defined the terms as problems because the poll’s question was about a reason to not live in Italy and so negative answers are expected. So highlighted terms has a negative acceptation.

Simple OCR with Neural Network

I has always been fascinated by the OCR systems (Optical Character Recognition applications that are used to translate writings contained in images in real text). This applications are very useful (it is its main role) when it is necessary to digitalize some printed document, instead of type it manually from scratch.

There are many ways to implement it, in this case I decided to develop a simple classifier that given a symbol (upper case letter or number) return the corresponding encoded character.

To do that I decided to obtain symbols like I have done in this previous post. So the application will remove from each captcha image obtained all the noise points and leave just the clusters of points that belong to some character, write it in an image file and save the image in some directory.
This collection will be used to construct some models that will be used to recognize the symbols contained in it. So for each symbol in the collection it is necessary to construct, train and validate a model for its recognition.
PS: the set of considered symbols correspond to te upper case letters and numbers with the exclusion of the zero and O symbols (maybe because it is hard to distinguish them).

I decided to use a neural network as algorithm/structure for the construction of the models because I know them a little bit more than other Machine Learning algorithms. Moreover there are entire frameworks developed to simply the construction and training of neural networks without developing and debugging the needed algorithms and data structures. In this case I used the Encog framework, that has many models and training algorithms available.
Neural network is a supervised learning method, it means that it is necessary to provide previously labelled instances (positive and negative examples) in order to allow the training of the model. It takes different continuos values as input and provides other continuos valus as output.

The programm has been developed using one neural network for each symbol to recognize. This allow to customize the neural network when needed. A Multi Layer Perceptron network structure has been used (900 nodes as Input layer, 2 Hidden layers of 100 and 50 nodes and 1 node as Output Layer).
Each node is connected to all nodes in the upper layer and uses a sigmoid as activation function.
The input layer is composed of 900 nodes because each image has dimension 30 x 30 pixel, so a node for each pixel has been used. The value provided to each node is 1.0 if its pixel is black and 0.0 if its pixel is white.
The output layer is composed only of one node because the net is used to establish if the input image corresponds to the recognized symbol ( so ideal output value will be 1.0 ) or not ( ideal output value will be 0.0 ).
Here there is the code used to create a neural network:

nn =  new BasicNetwork();
nn.addLayer(new BasicLayer(new ActivationSigmoid(), false, inputNodes));
nn.addLayer(new BasicLayer(new ActivationSigmoid(), true, 100));
nn.addLayer(new BasicLayer(new ActivationSigmoid(), true, 50));
nn.addLayer(new BasicLayer(new ActivationSigmoid(), true, outputLayer));

For the learning of the neural network it has been used the Resilient Propagation algorithm.
Here the is the code used to make the training of a neural network:

Train train = new ResilientPropagation(nn, dataset);
EncogUtility.trainToError(train, error);

After that all symbols has been extracted and saved in a file, I obtained a collection of images.
I divided the collection in three ( disjoint ) sets :

  1. training set : it is composed by a set of directories (one for each symbol) where 7115 images has been moved based on the contained symbol. This allows to obtain some labelling about images and to use the images in each directory for the learning (training) of the associated neural network. Most images are contained in each directory and more “knowledge” is provided to the neural network, this is why this set is composed by most collection’s images;
  2. validation set : also in this (and the next) case 196 images have been moved in directories. But this set will be used to evaluate the neural network obtained from the training and establish if it could be used to classify images. Considering one net at time it has been created a dataset where images containing the recognized symbol are positive instances and others are negative instances. This dataset is provided to the net and the obtained result is compared with the ideal one: if values are similar the net classified correctly the instance. The percentage of correct classifications could be printed allowing to evaluate each net and decide if re-construct it or include more examples in the training set;
  3. test set : in this case the set of 494 labelled images is used only for the evaluation of the nets obtained, while the validation set is used to controll if a net is acceptable or not, and it allows to construct and edit each net as many times as you want/need, the test set should be used to provide a final evaluation of the labelling capabilities of the net.

Each of this set of images (and hierarchy of directories) has been stored in a directory with the name of the corresponding set.

Moreover it is possibile to use all trained neural networks for an automate labelling of a set of images: given the set of directories (one for each symbol) each image is provided as input of all networks and the results are compared. The net that returns the highest output ( greater than 0.5, if any ) is considered as it recognizes the provided image and the corresponding symbol is returned.
When an image is labelled it is moved inside the directory of the associated symbol. Otherwise it stays in the main directory.
Because there are a lot of images to classify and each one has to be compared with all neural networks, it isn’t efficient to load nets from file each time it is needed. This is why they have been cached in an HashMap, associating each one the recognized symbol and retrieved when needed.

HashMap<NeuralNetwork, String> netMap = new HashMap<NeuralNetwork, String>();

During the learning phase, each neural network requires just some minutes and few iterations to converge to a low error rate ( less than 0.5% in this case ).
Just some symbols (B, E, F, H) requires hundreds of iteration and in some case there is no conversion. In these cases different net structure has been adopted increasing the number of nodes in the hidden layers.The evaluation of the validation set and the test set is very fast both for the fetching of the neural networks and also because small models could compute very fast the output value (it is just amatter of sums and multiplications).

The images contained in the test directory has also been used to control empirically what a 98% of average correct classifications means. The images has been putted out of the directory “classification” and they are moved in the directory associated to the neural network that “recognized” the symbol contained.
After that all images has been processed, some of them could stay in the main directory because there wsn’t any neurla network that recognized them and returned an high output value. Other images has been moved in the directory of the neural network that recognize them with the highest output value.

Looking inside each directory it is very simple to note which images has been labelled correctly and which one wrongly.
As you could see classification is not always correct, there are some mistakes due to the similarity of some symbols ( e.g like 2 and Z, or 1 and I ) some of them could be committed even by a human because the alteration applyed to the characters is too high to be indistinguishable.

However most of them has been recognized and it could be applied to solve the captchas which symbols belong to.

Here there is the eclipse project with the sourcecode, the dataset used and all directory hierarchies.
Here there is the set of images that I used to extract symbols and create images used during the training, validation and test phase.

Parallel DBSCAN-like algorithm

During my university career I have done some projects. One of them was for the “parallel and distributed computations” course. In the same period I chose my master thesis argument (about Data Mining) so I decided to recycle some knoledges and make a custom project: a parallelized version of the DBSCAN algorithm (it is a variant for some details).

There are a lot of density based clustering algorithms and they are used to find groups of similar items in a dataset that share some characteristics (based on a given distance function used to compute the distance between two items).

In this simple project I had to implemented a parallized (and a little bit different) version of a density based clustering similar to the DBSCAN (I already used and cite it in some previous posts).
I decided to consider only core points and noise points, and maintening the two parameters that characterize this kind of algorithms:

  • MinPts, the minimum number of instances that have to be present in the neighborhood of each core point in the dataset;
  • Eps, the neighborhood to consider when determining a core point;

Each instance that doesn’t satisfy the core point conditions is labelled as a noise point.
The distance computed between couple of instances corresponds to the euclidean distance computed on 2D points.

So the algorithm is divided in two phases, the first one determine for each instance if it is a core or a noise point and the second one associate each core point to the nearest cluster.
The construction of a cluster is done incrementally, using the density based connection notion, the same used in the DBSCAN algorithm; if the distance between two core points isn’t greater than epsilon then these core points have to belong to the same cluster.
Otherwise if the distance is greater than epsilon but exists a sequence of core points where each couple of adjacent points are enough close (dist <= epsilon) this two points are connected.
In any other case points belongs to different clusters.

All noise points will be introduced in the same cluster and will be ignored, while each other cluster will correspond to a character.

In the original project I tried to overlap the two phases exploiting the parallelism as much as possibile. However the Skandium framework that I used has a Macro Data Flow implementation and the cluster construction is done incrementally. All parallel instances has to be synchronized and the overhaed introduced when there are manyinstances could be ignored only f the first phase requires “heavy” computations.

The reported code parallelize only the first phase (very adapt to task parallelism) while the second phase is done adding one dataset instance at time to the current obtained clusters (waiting that it is finished to be processed).
In the second phase each instance is compared to the existing clusters (except the first one that is used to store noise points). If the current instance i is close (distance slower that eps) to at least one point of a cluster c, it will belong to c.
If i is close to more than one cluster it is a “conjunction” point to this clusters and they have to be merged togheter, creating a new cluster.

The number of processing elements corresponds to the number of core that there are in your architecture (corresponding to the maximum number of thread in a concurrent execution).

There are also two methods used to read and write a dataset of points in a file. They allow to see in some Data Mining tools like Weka, a graphical representation of the obtained clustering.
The code of the entire project and the adopted dataset could be found here. There is also a sample dataset of geografical GPS coordinates used to develop the project. The distance computed between two instances is the euclidean distance so obtained clusters corresponds to sets of points geograficaly close each other.
The complexity of this algorithm is O(n^2) because no index has been used to limit comparisons or to simplify clusters constructions (work in progess ;-D ).

Ping Robot Car 1

This post is an how to for the creation of a Ping Robot, a simple robot able to move in the floor and to detect obstacles using an Ultrasonic Sensor. Maybe this the most simple and cheap robot that can be developed by an electronic newbie like me!
I would always  like to realize a robot (eventually complex) with some kind of logic inside, that it is capable to model or catch information from the real world around it, and use this information to do something using actuators
(actuator – logic – sensor paradigm is the base approach in robotics).
I have seen that also others has got my same idea (even many time ago). I take some improvement from other’s experience.

Component’s price is the most limiting thing when making anything :-\ ( in particular a robot). Some robot’s platforms cost about hundreds of dollars and could be found just in online stores.

A “ping” robot realized using buyed parts is acceptable if it is useful to do something (like for hobby or for work) not when it is done just for fun!!! That is why I never bought this “robot kits”.
Because my budget is very limited I try to realize my projects using old elettronic parts or most times buying cheap components from Ebay and then assembling all together by myself (often with a messy result). In this case I decided to cannibalize a small, old RC car.
This cars could be used to obtain a platform for the developing a robot, providing, DC motors, gears, connectors and place where put all circuits for autonomous driving (even a transmitter/receiver circuit).
In this case I used a very small RC car, the kind of toy car that contains a capacitor used as a small rechargeble battery. They cost around 6$ from Ebay or online shops. Bigger ones could cost about 50$ but there are a lot of goods inside 😀


As first step I decided to remove all parts from the RC car, except for wheels. They will be used as a platform for the robot’s development in conjunction with  a wood plate and some DC motors with reduction gears like this ones (necessary to slow down the speed of the car).


It is possibile to connect geared DC motors to robot wheels and wood plate using some hot glue. Obtained result is not excellent but it is eneough for a prototype 😀 .
Four cables, two reds and two blacks are used to provide current to each motor. They will be used to turn on and off each motor and possibly to invert wheels rotation.


So, on the top of the wood plate, all needed circuits will be installed later (an IC for the logic and the ultrasonic sensor).
Now that the mechanical part is up, the next step will be to create the entire circuit containing the ultrasonic sensor and the logic necessary to drive the small car.