Thesis Start

I planned to start working on biometrics and theory today, but lower level technical things got in the way.  I spent the day learning about the Mono distribution of .net included with unity 3.4, and it’s non-working serial reading on mac os x.

I’d prefer not to use windows all semester, parallels is great and all, but theres enough quirks already with trying to handshake a serial stream without emulating a serial port and switching it back and forth between the mac and windows environments every time I want to reup some arduino code.

SO!

I decided to try three things:

1) write some code in processing to read arduino sensor data, write it as a text file, and have unity parse the newest line in the text file to get data.

2)Write a grasshopper script to read arduino data, save it as an xml file, and have unity parse the xml file.

3)Get one of the new ethernet arduinos, and connect directly to unity over tcp, which hopefully actually works on mac.

I found some basic code on the arduino forum that would be a good proof of concept for speed and timing issues.  I just need to write some specific code in all three systems to group the data correctly with commas, and then parse it out in unity  into multiple arrays for each line.

Ex.

Heartrate,Galvanic Skin,Eye Movement  Array 0

Array 1,            Array 2,          Array 3

The code I have tweaked now, generates random numbers in Processing(quick substitute for no sensors on my microcontroller), saves this to a text file, and continues to add lines to the file.

The unity code first opens the processing application to begin streaming data, then reads the whole text file, splits it into lines, and parses the last line into a float.  Then it linearly interpolates the position of a cube in the z position of the last line of data.

This code is based on code by Brian Leister posted to the Unity Forums.

import System;
import System.IO; 

var minimumHeight : float;
var delaySeconds = 2.0;
var posvalues : Array; 

function Start (){

	var info:FileInfo = new FileInfo("/Users/me/game/Assets/randomtextfilemake/application.macosx/randomtextfilemake.app");
	System.Diagnostics.Process.Start(info.FullName);
	InvokeRepeating("mover", .1, .5);
}

function mover () {
	var poscoordinates = (Application.dataPath + "/positions.txt");
  //var poscoordinates = (Application.dataPath + "/Scripts/" + "positions.txt");
  //var poscoordinates = "/positions.txt";

 if (File.Exists(poscoordinates)){
      var sr = new StreamReader(poscoordinates);
      var txt = sr.ReadToEnd();
      posvalues = txt.ToString().Split("\n"[0]);
   } 

   zPosition = float.Parse(posvalues[(posvalues.length-2)]);

   move(zPosition);

    Debug.Log(zPosition);
}

 function move(zpositionTarget : float)
{
   for (var n: int = 0 ; n < 1 ; n++)
   { 

      // Lerp to the selected target position in 2 seconds
      var fStartTime: float = Time.time;
      var fCurrLerp: float = (Time.time - fStartTime) / delaySeconds;
      while (fCurrLerp <= 1.0)
      {
        fCurrLerp = (Time.time - fStartTime) / delaySeconds;
   		transform.localPosition.z = Mathf.Lerp(transform.localPosition.z, zpositionTarget, fCurrLerp);
    	yield;
      }
   }
}
Advertisements
This entry was posted in Thesis. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s