예제 #1
0
        public void HandleCSV()
        {
            if (!File.Exists(Path))
            {
                Console.WriteLine("File does not exist!");
                return;
            }

            data.ClearEntries();
            CSVTypes      Type     = GetCSVType();
            List <string> DataList = new List <string>(File.ReadAllLines(Path));

            DataList.RemoveAt(0);
            if (Type == CSVTypes.INS)
            {
                foreach (var line in DataList)
                {
                    string   LineReplaced = line.Replace("\",\"", "#");
                    int      Length       = LineReplaced.Length;
                    string[] LineSplit    = LineReplaced.Substring(1, LineReplaced.Length - 2).Split('#');
                    // INS
                    double Timer = StringToDouble(LineSplit[0]);
                    double AX    = StringToDouble(LineSplit[1]);
                    double AY    = StringToDouble(LineSplit[2]);
                    double AZ    = StringToDouble(LineSplit[3]);
                    double GX    = StringToDouble(LineSplit[4]);
                    double GY    = StringToDouble(LineSplit[5]);
                    double GZ    = StringToDouble(LineSplit[6]);
                    double Angle = 0f;
                    if (LineSplit.Length == 8)
                    {
                        Angle = Convert.ToDouble(LineSplit[7]);
                    }
                    data.AddDataEntry(new DataEntry(null, new XYZ(AX, AY, AZ, Timer), new XYZ(GX, GY, GZ, Timer), Angle));
                }
                Console.WriteLine($"Loaded {DataList.Count} lines from {Path} [{Type}]");
            }
            else if (Type == CSVTypes.POZYX)
            {
                foreach (var line in DataList)
                {
                    // Replace "," with # and skip first char " and last char "
                    string   LineReplaced = line.Replace("\",\"", "#");
                    int      Length       = LineReplaced.Length;
                    string[] LineSplit    = LineReplaced.Substring(1, LineReplaced.Length - 2).Split('#');
                    // POZYX
                    double Timer = StringToDouble(LineSplit[0]);
                    double X     = StringToDouble(LineSplit[1]);
                    double Y     = StringToDouble(LineSplit[2]);
                    double Z     = StringToDouble(LineSplit[3]);
                    data.AddDataEntry(new DataEntry(new XYZ(X, Y, Z, Timer), null, null, 0));
                }
                Console.WriteLine($"Loaded {DataList.Count} lines from {Path} [{Type}]");
            }
            else
            {
                Console.WriteLine("Type UNKNOWN!!!!");
            }
        }
예제 #2
0
        public static void LogData(string[] Input)
        {
            List <string> InputList = new List <string>(Input);

            if (!(Input.Length > 1))
            {
                Console.WriteLine("Invalid input, use help to get data");
                return;
            }
            switch (Input[1])
            {
            case "fixtimer":
                if (MenuController.Confirm("Are you sure you want to replace everything?", false))
                {
                    if (Input.Length == 3)
                    {
                        int Seperation = Convert.ToInt32(Input[2]);
                        FixTime(Seperation);
                    }
                    else
                    {
                        FixTime();
                    }
                }
                break;

            case "weka":
                if (MenuController.Confirm("Are you sure you want to replace everything?", false))
                {
                    string[] entries = Directory.GetFileSystemEntries(".", "*.csv", SearchOption.AllDirectories);
                    foreach (var FilePath in entries)
                    {
                        string FileContents = File.ReadAllText(FilePath);
                        string Output       = Regex.Replace(FileContents, @"\d+,\d+", delegate(Match match)
                        {
                            string v = match.ToString().Replace(",", ".");
                            return(v);
                        });
                        File.WriteAllText(FilePath, Output);
                    }
                }
                break;

            case "latex":
                if (MenuController.Confirm("Are you sure you want to replace everything?", false))
                {
                    string[] entries = Directory.GetFileSystemEntries(".", "*.csv", SearchOption.AllDirectories);
                    foreach (var FilePath in entries)
                    {
                        string FileContents = File.ReadAllText(FilePath);
                        string Output       = Regex.Replace(FileContents, @"\d+,\d+", delegate(Match match)
                        {
                            string v = match.ToString().Replace(",", ".");
                            return(v);
                        });

                        Output = Regex.Replace(Output, @"""\d+""\,", delegate(Match match)
                        {
                            string substringed = match.ToString().Substring(1, match.Length - 3);
                            double newNum      = Convert.ToDouble(substringed) / 1000f;
                            return(newNum.ToString().Replace(",", ".") + ",");
                        });

                        Output = Regex.Replace(Output, @"""(|-)\d+(|\.|\,)\d*""", delegate(Match match)
                        {
                            string v = match.ToString().Replace(@"""", "");
                            return(v);
                        });

                        Output = Regex.Replace(Output, @"""(|-)\d+(\.|\,)\d+""", delegate(Match match)
                        {
                            string v = match.ToString().Replace(@"""", "");
                            return(v);
                        });

                        File.WriteAllText(FilePath, Output);
                    }
                }
                break;

            case "combine":
                if (true)
                {
                    StringBuilder FileContents = new StringBuilder();
                    string[]      entries      = Directory.GetFileSystemEntries(".", "*INS.csv", SearchOption.AllDirectories);
                    foreach (var FilePath in entries)
                    {
                        FileContents.Append(File.ReadAllText(FilePath));
                    }
                    FileContents.Replace("Timer,AX,AY,AZ,GX,GY,GZ,A\n", "");
                    File.WriteAllText("Combined_ins.csv", "Timer,AX,AY,AZ,GX,GY,GZ,A\n" + FileContents.ToString());
                }
                break;

            case "ra":
                if (Input.Length == 3)
                {
                    try
                    {
                        dataMapper.CalculateRollingAverage(Convert.ToInt32(Input[2]));
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Invalid format!");
                    }
                }
                else
                {
                    dataMapper.CalculateRollingAverage(10);
                }
                break;

            case "load":
                if (Input.Length == 3)
                {
                    Load load = new Load(Input[2] + ".csv");
                    load.HandleCSV();
                    dataMapper = load.data;
                }
                break;

            case "calibrate":

                if (dataMapper == null)
                {
                    Console.WriteLine("No Data Mapper has been created!");
                }
                else
                {
                    if (Input.Length == 3)
                    {
                        int Timer = 0;
                        try
                        {
                            Timer = Convert.ToInt32(Input[2]) * 1000;
                            dataMapper.CalibrateINS(Timer);
                        }
                        catch (FormatException ex)
                        {
                            Console.WriteLine("Not a number givin!");
                        }
                    }
                    else
                    {
                        dataMapper.CalibrateINS();
                    }
                }
                break;

            case "start":
                if (dataMapper == null)
                {
                    Console.WriteLine("No Data Mapper has been created!");
                }
                else
                {
                    if (InputList.Count == 3)
                    {
                        if (Input[2] == "nem")
                        {
                            dataMapper.StartReading();
                            if (MenuController.Confirm("Stop?", true))
                            {
                                dataMapper.StopReading();
                            }
                        }
                        else
                        {
                            try
                            {
                                MapperTimer = Convert.ToInt32(InputList[2]);
                                Thread TimerThread = new Thread(dataMapperTimer);
                                dataMapper.StartReading();
                                TimerThread.Start();
                                Console.WriteLine($"Started Datamapper for {MapperTimer} sec");
                                TimerThread.Join();
                            }
                            catch (Exception)
                            {
                                Console.WriteLine("Invalid format!");
                            }
                        }
                    }
                    else
                    {
                        dataMapper.StartReading();
                        Console.WriteLine("Started Datamapper");
                    }
                }
                break;

            case "stop":
                if (dataMapper == null)
                {
                    Console.WriteLine("No Data Mapper has been created!");
                }
                else
                {
                    dataMapper.StopReading();
                    Console.WriteLine("Stopped data logging");
                }
                break;

            case "clear":
                if (dataMapper == null)
                {
                    Console.WriteLine("No Data Mapper has been created!");
                }
                else
                {
                    dataMapper.ClearEntries();
                    Console.WriteLine("Cleared the DataList");
                }
                break;

            case "save":
                if (dataMapper == null)
                {
                    Console.WriteLine("No Data Mapper has been created!");
                }
                else
                {
                    if (InputList.Count == 3)
                    {
                        string FileName = InputList[2];
                        if (File.Exists(FileName + "_INS.csv") || File.Exists(FileName + "_POZYX.csv"))
                        {
                            if (MenuController.Confirm("This file already exists, Overwrite?", false))
                            {
                                CSVWriterController csvWriter = new CSVWriterController(dataMapper, FileName);
                                csvWriter.Execute();
                                Console.WriteLine($"Saved to {FileName}");
                            }
                            else
                            {
                                Console.WriteLine("Cancelled!");
                            }
                        }
                        else
                        {
                            CSVWriterController csvWriter = new CSVWriterController(dataMapper, FileName);
                            csvWriter.Execute();
                            Console.WriteLine($"Saved to {FileName}");
                        }
                    }
                }
                break;

            case "new":
                if (Input.Length == 2)
                {
                    dataMapper = new DataMapper.DataMapper();
                }
                else
                {
                    switch (Input[2])
                    {
                    case "ins":
                        dataMapper = new DataMapper.DataMapper(false, true);
                        break;

                    case "pozyx":
                        dataMapper = new DataMapper.DataMapper(true, false);
                        break;

                    default:
                        break;
                    }
                }

                Console.WriteLine("Created new DataMapper!");
                break;

            case "kalman":
                dataMapper.GenerateKalman();
                Console.WriteLine("Generated data kalman filtered data of INS data.");
                break;

            case "segment":
                string SegmentFile = "Segmented.csv";
                if (File.Exists(SegmentFile))
                {
                    File.Delete(SegmentFile);
                }
                ConcurrentQueue <DataEntry> OutputSegments = dataMapper.SegmentData();
                using (var test = File.AppendText(SegmentFile))
                {
                    test.WriteLine("Timer,AX,AY,AZ,GX,GY,GZ,Angle");
                    int i = 0;
                    foreach (var item in OutputSegments)
                    {
                        Console.WriteLine($"{i++}");
                        string output = $"\"{item.INS_Accelerometer.TimeOfData}\",\"{item.INS_Accelerometer.X}\",\"{item.INS_Accelerometer.Y}\",\"{item.INS_Accelerometer.Z}\",\"{item.INS_Gyroscope.X}\",\"{item.INS_Gyroscope.Y}\",\"{item.INS_Gyroscope.Z}\",\"{item.INS_Angle}\"";
                        Console.WriteLine($"{output}");
                        test.WriteLine(output);
                    }
                }
                Console.WriteLine($"Done segmenting data! {OutputSegments.Count}");
                break;

            case "variance":
                ConcurrentQueue <Tuple <DataEntry, double, double> > varianceData = dataMapper.CalculateVariance();
                string VarianceFile = "Variance.csv";

                if (File.Exists(VarianceFile))
                {
                    File.Delete(VarianceFile);
                }

                using (var test = File.AppendText(VarianceFile))
                {
                    test.WriteLine("Timer,AX,AY,AZ,GX,GY,GZ,Angle,Variance,Slope");
                    foreach (var item in varianceData)
                    {
                        string output = $"\"{item.Item1.INS_Accelerometer.TimeOfData}\",\"{item.Item1.INS_Accelerometer.X}\",\"{item.Item1.INS_Accelerometer.Y}\",\"{item.Item1.INS_Accelerometer.Z}\",\"{item.Item1.INS_Gyroscope.X}\",\"{item.Item1.INS_Gyroscope.Y}\",\"{item.Item1.INS_Gyroscope.Z}\",\"{item.Item1.INS_Angle}\",\"{item.Item2}\",\"{item.Item3}\"";
                        test.WriteLine(output);
                    }
                }
                Console.WriteLine("Done!");
                break;

            case "acc":
                CalculateLasseStuff();
                Console.WriteLine("Done!");
                break;

            default:
                Console.WriteLine("Invalid input format, use help command!");
                break;
            }
        }