Connecting First and Second Life

January 11th, 2007  |  Published in hardware  |  2 Comments

I’ve been interested for some time in the possibilities offered by bringing external data into virtual environments like Second Life. This data might come from the web, but it could also come from the real world – from physical sensors and interfaces.

Over the last couple of weeks I’ve enjoyed playing with the Arduino hardware prototyping board. This week’s open-sourcing of the Second Life client came at exactly the right time for a new experiment.

Here’s a video demonstration (people reading the feed, start your web browsers). On the left you’ll see an Arduino reading analogue values from a potentiometer and feeding the results in via the USB-serial interface to my Mac. On the right, you’ll see a modified version of Second Life that is feeding those values in via my avatar’s chat channel. An object in the Second Life world is reacting, with perhaps a half-second lag.


How does this work? All it takes is a few small code fragments in the right places. The Arduino code is trivial:

void setup() {
Serial.begin(9600);
pinMode(0,INPUT);
}
void loop() {
delay(100);
Serial.println(analogRead(0));
}

The in-world LSL code is trivial:

vector pos;
default
{
state_entry()
{
llListen(42,"Matt Basiat",NULL_KEY,"");
pos = llGetPos();
}
listen(integer channel, string name, key id, string message) {
float val = (float)message;
llSetPos(pos + <0,0,(val/100.0)>);
}
}

The clever bit, such as it is, involves adding a bit of good old-fashioned Unix file-descriptor code to the SL client’s idle loop (located in viewer.cpp in the source) that polls the Arduino. Once I’ve got the reading, all it takes to make my avatar speak to the server is the line:

gChatBar->sendChatFromViewer(reading, CHAT_TYPE_NORMAL, FALSE);

In total, I added about 150 lines of code to a single file to achieve this. Obviously this is just a concept demo, and there are plenty of places to take it from here, but I’m encouraged by the progress.

If you’re curious about the details, here’s the patch against viewer.cpp in the newview directory of the source. It’s definitely not The Right Way To Do It – just the fastest route I could find into the heart of the code. It has hardcoded device names and other bad practice. I release it under the GPL, and must thank Tod Kurt for the use of his arduino-serial.c code and Massimo Banzi for everything he taught me about Arduino over the holiday season.

Responses

  1. Jay says:

    December 4th, 2008 at 9:48 pm (#)

    You should check out iobridge. It's basically arduino w/ all the embedded stuff worked out. You can goto their website and see your board and I/O devices and they've got embeddable widgets which would be perfect to use in SL.

  2. spinster says:

    December 10th, 2008 at 12:16 pm (#)

    This looks really interesting! Can you please clarify whether you are using SL with a hacked client, or whether you are using a standalone OpenSim?

    Also, how would I use the patch file if I wanted to experiment with this? thanks!