1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | // VOID // // VOID_DataLogger.cs // // Copyright © 2014, toadicus // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation and/or other // materials provided with the distribution. // // 3. Neither the name of the copyright holder nor the names of its contributors may be used // to endorse or promote products derived from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using KSP; using System; using System.Collections.Generic; using ToadicusTools; using UnityEngine; namespace VOID { public class VOID_DataLogger : VOID_WindowModule, IVOID_BehaviorModule { /* * Fields * */ protected bool stopwatch1_running; protected bool csv_logging; protected bool first_write; protected double stopwatch1; protected string csv_log_interval_str; protected float csv_log_interval; protected double csvWriteTimer; protected double csvCollectTimer; protected List<string> csvList = new List<string>(); /* * Properties * */ /* * Methods * */ public VOID_DataLogger() { this._Name = "CSV Data Logger"; this.stopwatch1_running = false; this.csv_logging = false; this.first_write = true; this.stopwatch1 = 0; this.csv_log_interval_str = "0.5"; this.csvWriteTimer = 0; this.csvCollectTimer = 0; this.WindowPos.x = Screen.width - 520; this.WindowPos.y = 85; } public override void ModuleWindow(int _) { GUIStyle txt_white = new GUIStyle(GUI.skin.label); txt_white.normal.textColor = txt_white.focused.textColor = Color.white; txt_white.alignment = TextAnchor.UpperRight; GUIStyle txt_green = new GUIStyle(GUI.skin.label); txt_green.normal.textColor = txt_green.focused.textColor = Color.green; txt_green.alignment = TextAnchor.UpperRight; GUIStyle txt_yellow = new GUIStyle(GUI.skin.label); txt_yellow.normal.textColor = txt_yellow.focused.textColor = Color.yellow; txt_yellow.alignment = TextAnchor.UpperRight; GUILayout.BeginVertical(); GUILayout.Label("System time: " + DateTime.Now.ToString("HH:mm:ss")); GUILayout.Label(VOID_Tools.ConvertInterval(stopwatch1)); GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true)); if (GUILayout.Button("Start")) { if (stopwatch1_running == false) stopwatch1_running = true; } if (GUILayout.Button("Stop")) { if (stopwatch1_running == true) stopwatch1_running = false; } if (GUILayout.Button("Reset")) { if (stopwatch1_running == true) stopwatch1_running = false; stopwatch1 = 0; } GUILayout.EndHorizontal(); GUIStyle label_style = txt_white; string log_label = "Inactive"; if (csv_logging && vessel.situation.ToString() == "PRELAUNCH") { log_label = "Awaiting launch"; label_style = txt_yellow; } if (csv_logging && vessel.situation.ToString() != "PRELAUNCH") { log_label = "Active"; label_style = txt_green; } GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true)); csv_logging = GUILayout.Toggle(csv_logging, "Data logging: ", GUILayout.ExpandWidth(false)); GUILayout.Label(log_label, label_style, GUILayout.ExpandWidth(true)); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true)); GUILayout.Label("Interval: ", GUILayout.ExpandWidth(false)); csv_log_interval_str = GUILayout.TextField(csv_log_interval_str, GUILayout.ExpandWidth(true)); GUILayout.Label("s", GUILayout.ExpandWidth(false)); GUILayout.EndHorizontal(); float new_log_interval; if (Single.TryParse(csv_log_interval_str, out new_log_interval)) csv_log_interval = new_log_interval; GUILayout.EndVertical(); GUI.DragWindow(); } public void Update() { // CSV Logging // from ISA MapSat if (csv_logging) { //data logging is on //increment timers csvWriteTimer += Time.deltaTime; csvCollectTimer += Time.deltaTime; if (csvCollectTimer >= csv_log_interval && vessel.situation != Vessel.Situations.PRELAUNCH) { //data logging is on, vessel is not prelaunch, and interval has passed //write a line to the list line_to_csvList(); //write to the csv } if (csvList.Count != 0 && csvWriteTimer >= 15f) { // csvList is not empty and interval between writings to file has elapsed //write it string[] csvData; csvData = (string[])csvList.ToArray(); Innsewerants_writeData(csvData); csvList.Clear(); csvWriteTimer = 0f; } } else { //data logging is off //reset any timers and clear anything from csvList csvWriteTimer = 0f; csvCollectTimer = 0f; if (csvList.Count > 0) csvList.Clear(); } if (stopwatch1_running) { stopwatch1 += Time.deltaTime; } } public void FixedUpdate() {} private void Innsewerants_writeData(string[] csvArray) { var efile = KSP.IO.File.AppendText<VOID_Core>(vessel.vesselName + "_data.csv", null); foreach (string line in csvArray) { efile.Write(line); } efile.Close(); } private void line_to_csvList() { //called if logging is on and interval has passed //writes one line to the csvList string line = ""; if (first_write && !KSP.IO.File.Exists<VOID_Core>(vessel.vesselName + "_data.csv", null)) { first_write = false; line += "Mission Elapsed Time (s);Altitude ASL (m);Altitude above terrain (m);Orbital Velocity (m/s);Surface Velocity (m/s);Vertical Speed (m/s);Horizontal Speed (m/s);Gee Force (gees);Temperature (°C);Gravity (m/s²);Atmosphere Density (g/m³);\n"; } //Mission time line += vessel.missionTime.ToString("F3") + ";"; //Altitude ASL line += vessel.orbit.altitude.ToString("F3") + ";"; //Altitude (true) double alt_true = vessel.orbit.altitude - vessel.terrainAltitude; if (vessel.terrainAltitude < 0) alt_true = vessel.orbit.altitude; line += alt_true.ToString("F3") + ";"; //Orbital velocity line += vessel.orbit.vel.magnitude.ToString("F3") + ";"; //surface velocity line += vessel.srf_velocity.magnitude.ToString("F3") + ";"; //vertical speed line += vessel.verticalSpeed.ToString("F3") + ";"; //horizontal speed line += vessel.horizontalSrfSpeed.ToString("F3") + ";"; //gee force line += vessel.geeForce.ToString("F3") + ";"; //temperature line += vessel.flightIntegrator.getExternalTemperature().ToString("F2") + ";"; //gravity double r_vessel = vessel.mainBody.Radius + vessel.mainBody.GetAltitude(vessel.findWorldCenterOfMass()); double g_vessel = (VOID_Core.Constant_G * vessel.mainBody.Mass) / Math.Pow(r_vessel, 2); line += g_vessel.ToString("F3") + ";"; //atm density line += (vessel.atmDensity * 1000).ToString("F3") + ";"; line += "\n"; if (csvList.Contains(line) == false) csvList.Add(line); csvCollectTimer = 0f; } } } |