HoloLens 2 Gait Training
The purpose of this project is to create gait training application.
Loading...
Searching...
No Matches
DataExporter.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4using System.Threading.Tasks;
5using System.IO;
6using System;
7using System.Linq;
8using TMPro;
9
13public class DataExporter : MonoBehaviour
14{
15
16 // Show the last file generated
17 [SerializeField] private TextMeshPro m_LastFileLabel;
18 [SerializeField] private TextMeshPro m_SignalFileLabel;
19 [SerializeField] private TextMeshPro m_AnimLenLabel;
20
23
26 private bool m_CoroutineIsRunning = false;
27
30 private string m_DirectoryLocation = null;
31
35 private void Start()
36 {
37 m_DirectoryLocation = Application.persistentDataPath + "/";
38 }
39
44 public void ExportDataFiles()
45 {
46 if( m_CoroutineIsRunning == false )
47 {
48 StartCoroutine( ExportFiles() );
49 }
50 else
51 {
52 StopCoroutine( ExportFiles() );
53 }
54 }
55
59 IEnumerator ExportFiles()
60 {
61 m_AvatarAnimationState = GameObject.FindGameObjectWithTag("Avatar").GetComponent<AvatarAnimationState>();
63
64 if( m_AvatarAnimationState == null )
65 {
66 m_LastFileLabel.text = "ERROR: Avatar needs to walk to get timestamps";
68 yield return null;
69 }
70
71 ExportGenericFiles( "Noise", m_SignalFileLabel, NoiseController.Instance.BaseNoise.NoiseValueList );
72
74
75
77 yield return null;
78 }
79
83 private async void ExportGenericFiles( string name, TextMeshPro label, List<float> valList )
84 {
85 DateTime dob = DateTime.Now;
86 string timeStamp = dob.ToString( "MM_dd_yyyyTHH_mm_ss" );
87 string type = NoiseController.Instance.BaseNoise.CurrentPattern.text.Split().Last();
88 string postfix = type + name + "_" + timeStamp + ".txt";
89 string fullPath = Path.Combine( m_DirectoryLocation, postfix );
90
91
92 if( type.Equals("Pink") || type.Equals("Random") )
93 {
94 if( valList.Any() )
95 {
96 foreach( float value in valList )
97 {
98 await WriteToFile( fullPath, value );
99 }
100 }
101
102 }
103 else if( type.Equals("ISO") )
104 {
105 if( name.Equals("Noise") )
106 {
107 await WriteToFile( fullPath, NoiseController.Instance.BaseNoise.PreferredWalkingSpeed );
108 }
109 else
110 {
111 if( valList.Any() )
112 {
113 foreach( float value in valList )
114 {
115 await WriteToFile( fullPath, value );
116 }
117 }
118 }
119
120 }
121 else
122 {
123 await WriteToFile( fullPath, -9999 );
124 }
125
126 CheckFileExists( fullPath, label );
127 }
128
135 private async Task WriteToFile(string path, float line)
136 {
137 using( StreamWriter myStreamWriter = new StreamWriter( path, append: true ) )
138 {
139 if( line != -9999 )
140 {
141 await myStreamWriter.WriteLineAsync( "" + Mathf.Abs(line) );
142 }
143 else
144 {
145 await myStreamWriter.WriteLineAsync( "Error Code: " + line );
146 }
147 }
148 }
149
153 private void CheckFileExists( string path, TextMeshPro label )
154 {
155 int idx = path.LastIndexOf('/');
156 string fileName = "";
157
158 if( idx != -1 )
159 {
160 fileName = path.Substring( idx + 1 );
161 }
162
163 if ( System.IO.File.Exists( path ) )
164 {
165 label.text = fileName;
166 }
167 else
168 {
169 label.text = "None";
170 }
171 }
172}
This class is used to control the avartar animation state.
List< float > AnimationLength
Property to get the Animation length (Read-Only)
This class is a helper class used to export data to files.
Definition: DataExporter.cs:14
void CheckFileExists(string path, TextMeshPro label)
Check the file exists and set the name of last file generated by the user.
string m_DirectoryLocation
Directory location where files stored. C:\Users\yourname\AppData\LocalLow\DefaultCompany_BarMetronome...
Definition: DataExporter.cs:30
AvatarAnimationState m_AvatarAnimationState
Need this to access heel strike time stamps.
Definition: DataExporter.cs:22
TextMeshPro m_AnimLenLabel
Definition: DataExporter.cs:19
TextMeshPro m_SignalFileLabel
Definition: DataExporter.cs:18
async void ExportGenericFiles(string name, TextMeshPro label, List< float > valList)
Export the data currently being used in the walking trials.
Definition: DataExporter.cs:83
bool m_CoroutineIsRunning
Check Coroutine is running or not. This is to prevent calling for multiple coroutines.
Definition: DataExporter.cs:26
void Start()
Initialize persistentDataPath
Definition: DataExporter.cs:35
void ExportDataFiles()
This function start a Coroutine to export data Mapped to export data button.
Definition: DataExporter.cs:44
async Task WriteToFile(string path, float line)
Asynchronously write and append line by line to a file. The using statement before the StreamWrite al...
TextMeshPro m_LastFileLabel
Definition: DataExporter.cs:17
IEnumerator ExportFiles()
This function exports signal data and left/right heel strike time stamps.
Definition: DataExporter.cs:59
This class initiate appopriate noise objects.