Sunday, November 30, 2008

Calculate activity level base on differenceImage() in JMyron

The code below use the function differenceImage in JMyron to calculate activity level.
(Skeleton based on Rijk's contriubution)



import JMyron.*;

JMyron m;//a camera object

int numPixels;


void setup(){
size(320,240);
m = new JMyron();//make a new instance of the object
m.start(width,height);//start a capture at 320x240

// m.findGlobs(0);//disable the intelligence to speed up frame rate
println("Myron " + m.version());
numPixels = width * height;
// Create an array to store the previously captured frame
previousFrame = new int[numPixels];
currentFrame=new int[numPixels];
// m.adaptivity(0.5);
}


void draw(){
//count of non-black image (base on the differenceImage array)
int nonBlackCount=0;
m.update();//update the camera view
loadPixels();
int[] diffFrame = m.differenceImage(); //get the current image of the camera
for (int i=0; i<diffFrame.length; i++){
pixels[i]=diffFrame[i];
if((red(diffFrame[i])!=0)&&(green(diffFrame[i])!=0)&&(blue(diffFrame[i]))!=0){
nonBlackCount++;
}
}
}


void mousePressed(){
}

public void stop(){
m.stop();//stop the object
super.stop();
}


Change color of movieclip (Actionscipt 2.0)

Here's the Code for changing color of a movieclip (given the movieclip is in white color)



changeMCColor=function (_mcName:String, _red:Number, _green:Number, _blue:Number, _newAlpha:Number){

// trace("_xmouse/Stage.width: "+_xmouse/Stage.width);
// var xPercent:Number = 1 - (_xmouse/Stage.width);
// var yPercent:Number = 1 - (_ymouse/Stage.height);
var matrix:Array = new Array();
if((_red>1)||(_green>1)||(_blue>1)||(_newAlpha>1)){//for 255 case
matrix = matrix.concat([_red/255, 0, 0, 0, 0]); // red
matrix = matrix.concat([0, _green/255, 0, 0, 0]); // green
matrix = matrix.concat([0, 0, _blue/255, 0, 0]); // blue
matrix = matrix.concat([0, 0, 0, _newAlpha/255, 0]); // alpha
}else{
matrix = matrix.concat([_red, 0, 0, 0, 0]); // red
matrix = matrix.concat([0, _green, 0, 0, 0]); // green
matrix = matrix.concat([0, 0, _blue, 0, 0]); // blue
matrix = matrix.concat([0, 0, 0, _newAlpha, 0]); // alpha
}

var filter:BitmapFilter = new ColorMatrixFilter(matrix);
eval("_root."+_mcName).filters = new Array(filter);
}

Saturday, November 29, 2008

Serial message from Arduino to Processing

In the previous post the example only demonstrate message being sent from Processing to Arduino, in thie example it is the other way around, sending message from Arduino to Processing. This require a little bit formatting from the Arduino side since the serial message could be very complex.
The code here use "===" as separator and ";" as message termination.

Arduino Code
/*
* Button
* by DojoDave <http://www.0j0.org>
*
* Turns on and off a light emitting diode(LED) connected to digital
* pin 13, when pressing a pushbutton attached to pin 7.
*
* http://www.arduino.cc/en/Tutorial/Button
*/

int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int inputPin2 =3;
int val = 0; // variable for reading the pin status
int val2 = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
pinMode(inputPin2, INPUT); // declare pushbutton as input
digitalWrite(inputPin, HIGH);
digitalWrite(inputPin2, HIGH);
Serial.begin(9600);
}

void loop(){
val = digitalRead(inputPin); // read input value
val2 = digitalRead(inputPin2); // read input value

if (val == LOW) { // check if the input is HIGH
Serial.print("===1");
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
Serial.print("===0");
digitalWrite(ledPin, HIGH); // turn LED ON
}
if (val2 == LOW) { // check if the input is HIGH
Serial.print("===1;");
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
Serial.print("===0;");
digitalWrite(ledPin, HIGH); // turn LED ON
}
Serial.flush();


}


Processing Code
/**
* Simple Read
*
* Read data from the serial port and change the color of a rectangle
* when a switch connected to a Wiring or Arduino board is pressed and released.
* This example works with the Wiring / Arduino program that follows below.
*/


import processing.serial.*;

Serial port; // Create object from Serial class
int val; // Data received from the serial port

void setup()
{
size(200, 200);
frameRate(10);
// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this,"COM16", 9600);
}

int buttonStatus=0;
void draw()
{
String serialMsg="";
if (0 < port.available()) { // If data is available,
serialMsg = port.readStringUntil(char(';')); // read it and store it in val
// println("serialMsg: "+serialMsg);
}
if (serialMsg.split("===").length>2){
println("serialMsg: "+serialMsg);
// println("parsedMsg: "+serialMsg.split("===")[1].charAt(0));
println("status 1: "+int(serialMsg.split("===")[1].charAt(0)-48));
// println("parsedMsg: "+serialMsg.split("===")[1].charAt(0));
println("status2: "+int(serialMsg.split("===")[2].charAt(0)-48));
buttonStatus=int(serialMsg.split("===")[1].charAt(0)-48);

}
port.clear();

background(255); // Set background to white
if (buttonStatus == 0) { // If the serial value is 0,
fill(0); // set fill to black
}
else { // If the serial value is not 0,
fill(204); // set fill to light gray
}
rect(50, 50, 100, 100);
}



/*

// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4; // Switch connected to pin 4

void setup() {
pinMode(switchPin, INPUT); // Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
if (digitalRead(switchPin) == HIGH) { // If switch is ON,
Serial.print(1, BYTE); // send 1 to Processing
} else { // If the switch is not ON,
Serial.print(0, BYTE); // send 0 to Processing
}
delay(100); // Wait 100 milliseconds
}

*/

Thursday, November 27, 2008

Using Opera for browsing IE only webpage

Opera->Quick Reference->Edit Sites Preferences -> Network -> Browser Identification -> Identify as IE

Wifi Arduino using Lantronix wiport

So, what if you want to monitor and control 100 different arduinos at the same time? Don't tell me you're going to create 100 serial ports, a Lantronix Wiport could be a good idea since it allows arduino to be reached by wifi without heavy modding.

Just connect the TX port (Digital pin 1) to the RX pin on the Wiport, then you can use UDP to recieve data sent by the arduino through the wifi network.

For the data sending part, it is a little bit complicated since the output level for the Lantronix Wiport is 3.3 V leveled. It is fine for it to recieved the 5V signal from Arduino since it has a certain tolerance, but to send serial command from Wiport to Arduino, a 74HCT541D is used for pulling the 3.3V signal to 5V.

Sharp IR sensor

About Sharp IR sensors

http://www.acroname.com/robotics/info/articles/sharp/sharp.html#e8

The output voltage for the sharp IR sensor has to be linearized for useable sensing, an article about linearization could be found here

http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html

To avoid the double sensitive zone (when moving too close to the sensor), one method COULD be combing 2 IR sensors that cover different ranges (To be implemented)

Managing quota on proftpd

To manage quotas on proftpd using a file based monitoring system, the mod_quotatab and mod_quotatab_file has to be included during compile time.

After that a very useful contributed admin tool for compiling the "limit" and "tally" table could be found here

http://www.castaglia.org/proftpd/contrib/ftpquota

All you need to do is to copy and paste the file and save it in one of the folder for generating the limit and tally table file. The instruction for using the ftpquota tool could be found here

http://www.castaglia.org/proftpd/contrib/ftpquota.html

And then just modify the /etc/proftpd/proftpd.conf following the instruction here (just change the location where you put the generated ftpquota files.

http://castaglia.org/proftpd/doc/contrib/ProFTPD-mini-HOWTO-Quotas.html

And you're done~

Cool, nice ftp server~

Wednesday, November 26, 2008

Posting program code on blog space

Convertor
http://formatmysourcecode.blogspot.com/

Java based convertor
(Article)
http://blog.js-development.com/2008/03/posting-source-code-on-blogger.html
(application)
http://juri-strumpflohner-projects.googlecode.com/files/WebCodeFormatter1.0.jar

UDP test Code


Processing code for communication between Arduino and Processing via Lantronix

/**
* (./) udp.pde - how to use UDP library as unicast connection
* (cc) 2006, Cousot stephane for The Atelier Hypermedia
* (->) http://hypermedia.loeil.org/processing/
*
* Create a communication between Processing<->Pure Data @ http://puredata.info/
* This program also requires to run a small program on Pd to exchange data
* (hum!!! for a complete experimentation), you can find the related Pd patch
* at http://hypermedia.loeil.org/processing/udp.pd
*
* -- note that all Pd input/output messages are completed with the characters
* ";\n". Don't refer to this notation for a normal use. --
*/

// import UDP library
import hypermedia.net.*;


UDP udp; // define the UDP object

/**
* init
*/
void setup() {

// create a new datagram connection on port 6000
// and wait for incomming message
udp = new UDP( this, 6002);
//udp.log( true ); // <-- printout the connection activity
udp.listen( true );
}

//process events
void draw() {;}

/**
* on key pressed event:
* send the current key value over the network
*/
void keyPressed() {

String message = str( key ); // the message to send
String ip = "localhost"; // the remote IP address
int port = 10002; // the destination port

// formats the message for Pd
//message = message+";\n";
// send the message
udp.send( message, ip, port );

}

/**
* To perform any action on datagram reception, you need to implement this
* handler in your code. This method will be automatically called by the UDP
* object each time he receive a nonnull message.
* By default, this method have just one argument (the received message as
* byte[] array), but in addition, two arguments (representing in order the
* sender IP address and his port) can be set like below.
*/
// void receive( byte[] data ) { // <-- default handler
void receive( byte[] data, String ip, int port ) { // <-- extended handler


// get the "real" message =
// forget the ";\n" at the end <-- !!! only for a communication with Pd !!!
data = subset(data, 0, data.length-2);
String message = new String( data );

// print the result
println( "receive: \""+message+"\" from "+ip+" on port "+port );
}


Arduino Processing communication

Communication between Arduino and Processing
This post is aimed at giving an example for communication between Arduino and Processing using the bundled Serial Library for both packages.

In this example, LEDs are connected to pins 2-9 of the Arduino board, and Processing control it through sending ASCII code of number 2-9 (Relative ASCII code 50 to 57).

Arduino Code

Processing Code