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
        delay(delayTime);
    }
}

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 Italiansinfuga.com, 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 😄

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

DECLARE temp LONG;
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;
EXECUTE stm;
DEALLOCATE PREPARE stm;

# 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;
EXECUTE stm;
DEALLOCATE PREPARE stm;

# 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;
EXECUTE stm;
DEALLOCATE PREPARE stm;

SET temp = temp + size;
END WHILE;

END //
DELIMITER ;

# 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 italiansinfuga.com 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.