Coding Guide – CSV export

This is simple example how to export the e-skin log into CSV file.

public class CSVexport : MonoBehaviour {
    private UnityESkinSensor sensor;
    FileInfo fileInfo;
    StreamWriter streamWriter;


    void Start () {
        sensor = SensorManager.Instance.Sensor; // e-skin Sensor
        sensor.OnFrame += Sensor_OnFrame; // Register OnFrame()

        fileInfo = new FileInfo (Application.persistentDataPath +"/e-skin_log_"+System.DateTime.Now.ToString ("yyyyMMdd_HHmmss_ffff_n")+ ".csv");
        streamWriter = fileInfo.AppendText ();
        string str = "Time,Counter,ELBOW_R,ELBOW_L,SHOULDER_R,SHOULDER_L,AXILLA_R,AXILLA_L,SCAPULA_R,SCAPULA_L,WRIST_R,WRIST_L,THORAX_R,THORAX_L,BACK_R,BACK_L,ACCEL_X,ACCEL_Y,ACCEL_Z,GYRO_X,GYRO_Y,GYRO_Z";
        streamWriter.WriteLine (str);
    }

    // ESkinSensor.OnFrame event
    void Sensor_OnFrame(object sender, EventArgs e) {
        streamWriter.WriteLine(getEskinLogRecord(sensor));
    }

    public string getEskinLogRecord(UnityESkinSensor _sensor) {
        return System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff")
            +","+_sensor.Counter // Hub Counter
            +","+_sensor.GetStrainValue (StrainSensorChannel.ELBOW_R)
            +","+_sensor.GetStrainValue (StrainSensorChannel.ELBOW_L)
            +","+_sensor.GetStrainValue (StrainSensorChannel.SHOULDER_R)
            +","+_sensor.GetStrainValue (StrainSensorChannel.SHOULDER_L)
            +","+_sensor.GetStrainValue (StrainSensorChannel.AXILLA_R)
            +","+_sensor.GetStrainValue (StrainSensorChannel.AXILLA_L)
            +","+_sensor.GetStrainValue (StrainSensorChannel.SCAPULA_R)
            +","+_sensor.GetStrainValue (StrainSensorChannel.SCAPULA_L)
            +","+_sensor.GetStrainValue (StrainSensorChannel.WRIST_R)
            +","+_sensor.GetStrainValue (StrainSensorChannel.WRIST_L)
            +","+_sensor.GetStrainValue (StrainSensorChannel.THORAX_R)
            +","+_sensor.GetStrainValue (StrainSensorChannel.THORAX_L)
            +","+_sensor.GetStrainValue (StrainSensorChannel.BACK_R)
            +","+_sensor.GetStrainValue (StrainSensorChannel.BACK_L)
            +","+_sensor.GetIMUValue (IMUSensorChannel.ACCEL_X)
            +","+_sensor.GetIMUValue (IMUSensorChannel.ACCEL_Y)
            +","+_sensor.GetIMUValue (IMUSensorChannel.ACCEL_Z)
            +","+_sensor.GetIMUValue (IMUSensorChannel.GYRO_X)
            +","+_sensor.GetIMUValue (IMUSensorChannel.GYRO_Y)
            +","+_sensor.GetIMUValue (IMUSensorChannel.GYRO_Z);
    }

    void OnDestroy() {
        if(streamWriter != null) {
            streamWriter.Flush ();
            streamWriter.Close ();
        }
        sensor.OnDestroy ();
    }
}