123 lines
2.9 KiB
C#
123 lines
2.9 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
|
|
namespace AccelVisualizer
|
|
{
|
|
public class StampedSensorData
|
|
{
|
|
//Acceleration in G's
|
|
float x, y, z;
|
|
|
|
//Timestamp
|
|
double seconds;
|
|
|
|
//Constructors
|
|
public StampedSensorData()
|
|
{
|
|
x = 0;
|
|
y = 0;
|
|
z = 0;
|
|
seconds = 0;
|
|
}
|
|
|
|
public StampedSensorData(float x, float y, float z, ulong seconds, ulong nanoSeconds)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
this.z = z;
|
|
this.seconds = ConvertToSeconds(seconds, nanoSeconds);
|
|
}
|
|
|
|
//Setters
|
|
public void SetX(float newX)
|
|
{
|
|
x = newX;
|
|
}
|
|
|
|
public void SetY(float newY)
|
|
{
|
|
y = newY;
|
|
}
|
|
|
|
public void SetZ(float newY)
|
|
{
|
|
y = newY;
|
|
}
|
|
|
|
public void SetSeconds(double newSeconds)
|
|
{
|
|
seconds = newSeconds;
|
|
}
|
|
|
|
//Getters
|
|
public float GetX()
|
|
{
|
|
return x;
|
|
}
|
|
|
|
public float GetY()
|
|
{
|
|
return y;
|
|
}
|
|
|
|
public float GetZ()
|
|
{
|
|
return z;
|
|
}
|
|
|
|
public double GetSeconds()
|
|
{
|
|
return seconds;
|
|
}
|
|
|
|
//Convert to seconds
|
|
public static double ConvertToSeconds(ulong seconds, ulong nanoSeconds)
|
|
{
|
|
return seconds + nanoSeconds / 1000000000.0;
|
|
}
|
|
|
|
public static StampedSensorData ReadString(string input)
|
|
{
|
|
input.Replace('.', ',');
|
|
string[] data = input.Split(';');
|
|
|
|
if(data.Length < 5)
|
|
{
|
|
//Invalid data read, return empty sensordata
|
|
return new StampedSensorData();
|
|
}
|
|
|
|
float x, y, z;
|
|
ulong seconds, nanoSeconds;
|
|
|
|
if(float.TryParse(data[0], out x)
|
|
&& float.TryParse(data[1], out y)
|
|
&& float.TryParse(data[2], out z)
|
|
&& ulong.TryParse(data[3], out seconds)
|
|
&& ulong.TryParse(data[4], out nanoSeconds))
|
|
{
|
|
//Succesfully parsed, return data
|
|
return new StampedSensorData(x, y, z, seconds, nanoSeconds);
|
|
} else
|
|
{
|
|
//Parse not succesful, return empty sensordata
|
|
return new StampedSensorData();
|
|
}
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return "X: " + x.ToString() + " Y: " + y.ToString() + " Z: " + z.ToString() + " Seconds: " + seconds.ToString();
|
|
}
|
|
|
|
public long GetTimeTicks()
|
|
{
|
|
return (long) Math.Floor(seconds * TimeSpan.TicksPerSecond);
|
|
}
|
|
}
|
|
|
|
}
|