I was looking for a date and time representation useful for registering stock quotes in a simple plain file.
I found that the standard ISO 8601 is just the answer for this, it’s called “Data elements and interchange formats — Information interchange — Representation of dates and times”. Here is a example:
This is not a requirement for my applications and it’s a huge performance overhead when works (almost 1 second for each map loaded) and when the applications is running in a environment without Internet it just waits for almost a minute and then fail with the remain decoding. A dirty workaround is open the XML file and get rid of the line containing the DTD reference.
But the correct way to programming XML decoding when we are not concerned on validate a XML schema is just the xml.parsers.expat. Instead of using a interface you just have to set some callback functions with the behaviors we want. This is a example from the documentation:
This is a very simple example of how to open two images and display them added.
I got two pictures at project Commons from Wikimediathat were highlighted on Featured Pictures. I did a crop on both to have the same size, as I’m trying to make this example as simple as possible.
In this simple OpenCV code below, we open the images, create a new one to display the result and use cvAdd to add them. We do not save the result or handle more than the ordinary case of two images with the same size.
#include
#include
#include
int main( int argc, char **argv ){
IplImage *surfer, *milkyway, *result;
int key = 0;
CvSize size;
/* load images, check, get size (both should have the same) */
surfer = cvLoadImage("surfer.jpg", CV_LOAD_IMAGE_COLOR);
milkyway = cvLoadImage("milkyway.jpg", CV_LOAD_IMAGE_COLOR);
if((!surfer)||(!milkyway)){
printf("Could not open one or more images.");
exit -1;
}
size = cvGetSize(surfer);
/* create a empty image, same size, depth and channels of others */
result = cvCreateImage(size, surfer->depth, surfer->nChannels);
cvZero(result);
/* result = surfer + milkyway (NULL mask)*/
cvAdd(surfer, milkyway, result, NULL);
/* create a window, display the result, wait for a key */
cvNamedWindow("example", CV_WINDOW_AUTOSIZE);
cvShowImage("example", result);
cvWaitKey(0);
/* free memory and get out */
cvDestroyWindow("example");
cvReleaseImage(&surfer);
cvReleaseImage(&milkyway);
cvReleaseImage(&result);
return 0;
}
/* gcc add.c -o add `pkg-config opencv --libs --cflags` */
Compile it (on a well configured OpenCV development environment) and run it:
Here’s a code developed using codes from nashruddin.com and samples from OpenCV, including the haar classifier xml. More detailed explanation on the theory about how the OpenCV face detection algorithm works can be found here.
The code:
#include
#include
#include
CvHaarClassifierCascade *cascade;
CvMemStorage *storage;
int main(int argc, char *argv[]) {
CvCapture *video = NULL;
IplImage *frame = NULL;
int delay = 0, key, i=0;
char *window_name = "Video";
char *cascadefile = "haarcascade_frontalface_alt.xml";
/* check for video file passed by command line */
if (argc>1) {
video = cvCaptureFromFile(argv[1]);
}
else {
printf("Usage: %s VIDEO_FILE\n", argv[0]);
return 1;
}
/* check file was correctly opened */
if (!video) {
printf("Unable to open \"%s\"\n", argv[1]);
return 1;
}
/* load the classifier */
cascade = ( CvHaarClassifierCascade* )cvLoad( cascadefile, 0, 0, 0 );
if(!cascade){
printf("Error loading the classifier.");
return 1;
}
/* setup the memory buffer for the face detector */
storage = cvCreateMemStorage( 0 );
if(!storage){
printf("Error creating the memory storage.");
return 1;
}
/* create a video window, auto size */
cvNamedWindow(window_name, CV_WINDOW_AUTOSIZE);
/* get a frame. Necessary for use the cvGetCaptureProperty */
frame = cvQueryFrame(video);
/* calculate the delay between each frame and display video's FPS */
printf("%2.2f FPS\n", cvGetCaptureProperty(video, CV_CAP_PROP_FPS));
delay = (int) (1000/cvGetCaptureProperty(video, CV_CAP_PROP_FPS));
while (frame) {
/* show loaded frame */
cvShowImage(window_name, frame);
/* wait delay and check for the quit key */
key = cvWaitKey(delay);
if(key=='q') break;
/* load and check next frame*/
frame = cvQueryFrame(video);
if(!frame) {
printf("error loading frame.\n");
return 1;
}
/* detect faces */
CvSeq *faces = cvHaarDetectObjects(
frame, /* image to detect objects in */
cascade, /* haar classifier cascade */
storage, /* resultant sequence of the object candidate rectangles */
1.1, /* increse window by 10% between the subsequent scans*/
3, /* 3 neighbors makes up an object */
0 /* flags CV_HAAR_DO_CANNY_PRUNNING */,
cvSize( 40, 40 )
);
/* for each face found, draw a red box */
for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {
CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
cvRectangle( frame,
cvPoint( r->x, r->y ),
cvPoint( r->x + r->width, r->y + r->height ),
CV_RGB( 255, 0, 0 ), 1, 8, 0 );
}
}
}
Yeah, I know the code needs a few adjustments. ¬¬
To compile it in a well configured OpenCV development environment:
To run it you have to put in the same directory of the binary the XML classifier (haarcascade_frontalface_alt.xml) that comes with OpenCV sources at OpenCV-2.0.0/data/haarcascades/. And so:
./faceplayer video.avi
The results I got so far is that it works well for faces but sometimes its also detects more than faces. And here a video of it working live.
A example of good result:
A example of bad result:
Maybe with some adjustments it could performs even better. But was really easy to create it using OpenCV.
A simple Java class that calculates your Jedi name based on your first, last name, mother’s maiden name and the city you grew up.
/*
Jedi first name: 1st 3 letters of last name + 1st 2 letters of first name
Jedi last name: 1st 2 letters of mother's maiden name + 1st 3 letters of city you were raised in
*/
public class JediName {
private static String capitalize(String s){
return s.substring(0,1).toUpperCase() + s.substring(1,s.length()).toLowerCase();
}
private static String sub(String s, int begin, int end){
return s.substring(begin, (s.length()>=end)?end:s.length());
}
public static String generate(String firstName, String lastName, String city, String mother ){
String jediFirstName = capitalize(sub(lastName, 0, 3) + sub(firstName, 0,2));
String jediLastName = capitalize(sub(mother,0,2)+ sub(city,0,3));
return jediFirstName + " " + jediLastName;
}
}
I used inline conditional expression ()?: to avoid raise a IndexOutOfBoundsException when the string is to small to cut the proper size we want.
And a example of how to use it:
public class JediNameTester {
public static void main(String args[]){
String myJediName = JediName.generate("Pedro", "Madeira", "Fortaleza", "Maria");
System.out.println(myJediName);
}
}
I got a simple motor from a broken domestic printer. It’s a Mitsumi m355P-9T stepping motor. Any other common stepping motor should fits. You can find one in printers, multifunction machines, copy machines, FAX, and such.
With a flexible cap of water bottle with a hole we make a connection between the motor axis and other objects.
With super glue I attached to the cap a little handcraft clay ox statue.
It’s a representation from a Brazilian folkloric character Boi Bumbá. In some traditional parties in Brazil, someone dress a structure-costume and dances in circular patterns interacting with the public.
Controlling a stepper motor is not difficult. There’s a good documentation on how to that on the Arduino Stepper Motor Tutorial. Basically it’s about sending a logical signal for each coil in a circular order (that is also called full step).
You’ll probably also use a driver chip ULN2003A or similar to give to the motor more current than your Arduino can provide and also for protecting it from a power comming back from the motor. It’s a very easy find this tiny chip on electronics or automotive stores or also from broken printers where you probably found your stepped motor.
With a simple program you can already controlling your motor.
// Simple stepped motor spin
// by Silveira Neto, 2009, under GPLv3 license
// http://silveiraneto.net/2009/03/16/bumbabot-1/
int coil1 = 8;
int coil2 = 9;
int coil3 = 10;
int coil4 = 11;
int step = 0;
int interval = 100;
void setup() {
pinMode(coil1, OUTPUT);
pinMode(coil2, OUTPUT);
pinMode(coil3, OUTPUT);
pinMode(coil4, OUTPUT);
}
void loop() {
digitalWrite(coil1, step==0?HIGH:LOW);
digitalWrite(coil2, step==1?HIGH:LOW);
digitalWrite(coil3, step==2?HIGH:LOW);
digitalWrite(coil4, step==3?HIGH:LOW);
delay(interval);
step = (step+1)%4;
}
Writing a little bit more generally code we can create function to step forward and step backward.
My motor needs 48 steps to run a complete turn. So 360º/48 steps give us 7,5º per step. Arduino has a simple Stepper Motor Library but it doesn’t worked with me and it’s also oriented to steps and I’d need something oriented to angles instead. So I wrote some routines to do that.
For this first version of BumbaBot I mapped angles with letters to easy the communication between the programs.
Notice that it’s not the final version and there’s still some bugs!
// Stepped motor control by letters
// by Silveira Neto, 2009, under GPLv3 license
// http://silveiraneto.net/2009/03/16/bumbabot-1/
int coil1 = 8;
int coil2 = 9;
int coil3 = 10;
int coil4 = 11;
int delayTime = 50;
int steps = 48;
int step_counter = 0;
void setup(){
pinMode(coil1, OUTPUT);
pinMode(coil2, OUTPUT);
pinMode(coil3, OUTPUT);
pinMode(coil4, OUTPUT);
Serial.begin(9600);
}
// tells motor to move a certain angle
void moveAngle(float angle){
int i;
int howmanysteps = angle/stepAngle();
if(howmanysteps<0){
howmanysteps = - howmanysteps;
}
if(angle>0){
for(i = 0;i
You write in your computer, sends a message thought USB and Arduino translates it into a Morse code.
Just a Arduino board with a buzzer connected at the digital output 12 (one wire in the ground and the other in the 12).
I tried to make the code as general as possible so you can easily adapt it for anthers ways of transmitting a Morse code. To do that you just need to rewrite a few functions.
Reads a character from Serial. Main function loop().
Translate a ascii char into a Morse code using a reference table. A letter ‘K’ becomes a string word “-.-“. Function say_char().
Interpret the Morse word as light and sound. Mostly at function say_morse_word(). The Interpretation needs 5 functions to say all Morse words, dot(), dash(), shortgap(), mediumgap() and intragap().
int led = 13; // LED connected to digital pin 13
int buzzer = 12; // buzzer connected to digital pin 12
int unit = 50; // duration of a pulse
char * morsecode[] = {
"-----", // 0
".----", // 1
"..---", // 2
"...--", // 3
"....-", // 4
".....", // 5
"-....", // 6
"--...", // 7
"---..", // 8
"----.", // 9
"---...", // :
"-.-.-.", // ;
"", // < (there's no morse for this simbol)
"-...-", // =
"", // > (there's no morse for this simbol)
"..--..", // ?
".--._.", // @
".-", // A
"-...", // B
"-.-.", // C
"-..", // D
".", // E
"..-.", // F
"--.", // G
"....", // H
"..", // I
".---", // J
"-.-", // K
".-..", // L
"--", // M
"-.", // N
"---", // O
".--.", // P
"--.-", // Q
".-.", // R
"...", // S
"-", // T
"..-", // U
"...-", // V
".--", // W
"-..-", // X
"-.--", // Y
"--.." // Z
};
void setup() {
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
}
void say_morse_word(char * msg){
int index = 0;
while(msg[index]!='\0'){
// say a dash
if(msg[index]=='-'){
dash();
}
// say a dot
if(msg[index]=='.'){
dot();
}
// gap beetween simbols
intragap();
index++;
}
}
// beep
void beep(int time){
int i;
int t = 100; // period of the wav. bigger means lower pitch.
int beepduration = (int)((float)time/t*1800);
digitalWrite(led, HIGH);
for(i=0;i='0')&&(letter<='Z')&&(letter!='<')&&(letter!='>')){
Serial.print(morsecode[letter-'0']);
Serial.print(' ');
say_morse_word(morsecode[letter-'0']);
shortgap();
} else {
if(letter==' '){
Serial.print(" \\ ");
mediumgap();
}else{
Serial.print("X");
}
}
}
void loop(){
if(Serial.available()){
say_char((char)Serial.read());
}
}
Additionally you can put another function to say entire strings, like say_string(“HELLO WORLD”)