#pragma once
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include "Filter.h"

float Filter::Limit(float Value, float Min, float Max)
{
	if (Value > Max) return Max;
	if (Value < Min) return Min;
	return Value;
}

float Filter::Minimum(float Value1, float Value2)
{
	if (Value1 < Value2) return Value1;
	return Value2;
}

float* Filter::Update(bool valid, float gps, float acc)
{
	const float ga = 9.80665;

	Delay.Acc[Delay.Index] = acc;
	Delay.Pos[Delay.Index] = Position.Value;
	Delay.Spd[Delay.Index++] = Speed.Value;
	if (Delay.Index >= Delay.Size) Delay.Index = 0;

	float prev = Delay.Acc[Delay.Index];
	float shift = gps - Delay.Pos[Delay.Index];

	float coef_a = 1;
	if (Position.EnAcc) coef_a = Position.Acc / fabsf(prev);
	float coef_p = 1;
	if (Position.EnGPS) coef_p = powf(fabsf(shift * Position.Length), Position.Width);

	float coef1 = Minimum(coef_a, coef_p);
	coef1 = Limit(coef1, Position.Min, Position.Max);

	

	static int count = 0;
	static float last = gps, acc_gps = 0;
	count++;
	if (valid)
	{
		Speed.ValueGPS = (gps - last) * Freq / count;
		last = gps;
		Speed.ValueAccGPS= (Speed.ValueGPS- acc_gps) * Freq / count;
		acc_gps = Speed.ValueGPS;
		count = 0;
	}

	for (int a = 0; a < Delay.Size; a++) Delay.Pos[a] += shift * coef1;
	Position.Value += shift * coef1 + (Speed.Value / Freq) * (1 - coef1);

	// SPEED

	shift = Speed.ValueGPS - Delay.Spd[Delay.Index];

	coef_a = 1;
	if (Speed.EnAcc) coef_a = Speed.Acc / fabsf(prev);
	float coef_g = 1;
	if (Speed.EnAcc) coef_g = Speed.AccGPS / fabsf(Speed.ValueAccGPS);
	float coef_s = 1;
	if (Speed.EnGPS) coef_s = powf(fabsf(shift * Speed.Length), Speed.Width);

	float coef2 = Minimum(coef_a, Minimum(coef_s, coef_g));
	coef2 = Limit(coef2, Speed.Min, Speed.Max);

	for (int a = 0; a < Delay.Size; a++) Delay.Spd[a] += shift * coef2;
	Speed.Value += shift * coef2 + (acc * ga / Freq) * (1 - coef2);

	static float coef[2];
	coef[0] = coef1;
	coef[1] = coef2;

	return coef;
}

Filter MainFilter;

HANDLE PortCOM;
HANDLE FileLOG;
HANDLE MainThread;

bool OnlineFilter = false;

bool AxeInverse = false;

float PlotsData[6];
float PlotsCoef[2] = { 0, 0 };
bool PlotsReady = false;

struct TeleLog
{
	float Valid;
	float PosX, PosY, PosZ;
	float AccX, AccY, AccZ;
};

int TeleAxe = 0;

void SaveLogFile(HANDLE file, unsigned char data[28], unsigned long size)
{
	TeleLog log;
	memcpy(&log, data, size);

	if (TeleAxe == 0) { PlotsData[0] = log.PosX; PlotsData[1] = log.AccX; }
	if (TeleAxe == 1) { PlotsData[0] = log.PosY; PlotsData[1] = log.AccY; }
	if (TeleAxe == 2) { PlotsData[0] = log.PosZ; PlotsData[1] = log.AccZ; }

	if (AxeInverse) PlotsData[1] = -PlotsData[1];

	PlotsReady = true;

	if (OnlineFilter)
	{
		bool valid = log.Valid;
		float gps = PlotsData[0], acc = PlotsData[1];

		float* coef = MainFilter.Update(valid, gps, acc);

		PlotsCoef[0] = coef[0];
		PlotsCoef[1] = coef[1];

		PlotsData[3] = MainFilter.Speed.Value;
		PlotsData[5] = MainFilter.Position.Value;
	}

	if (!file) return;

	char buf[1024];
	sprintf(buf, "%d|%f|%f|%f|%f|%f|%f\r\n", (int)log.Valid, log.PosX, log.PosY, log.PosZ, log.AccX, log.AccY, log.AccZ);

	WriteFile(file, buf, strlen(buf), 0, 0);
}

unsigned long __stdcall RecvThread(void*)
{
	long wait = 0;

	private enum class RecvModeEnum : char { Begin, Data };

	bool mode = false;

	unsigned char data[29];
	unsigned long size;

	unsigned long len;

	while (1)
	{
		if (!PortCOM)
		{
			CloseHandle(MainThread);
			CloseHandle(FileLOG);
			MainThread = 0;
			return 0;
		}

		if (wait < GetTickCount()) mode = false;

		if (!mode)
		{
			wait = GetTickCount() + 20;

			ReadFile(PortCOM, data, 1, &len, 0);
			if (!len) continue;
			if (data[0] == 'Z') mode = true;
			size = 0;
		}

		if (mode)
		{
			ReadFile(PortCOM, data + size, sizeof(data) - size, &len, 0);
			if (!len) continue;
			size += len;
			if (size < sizeof(data)) continue;

			if (data[28] == 'V') SaveLogFile(FileLOG, data, sizeof(data) - 1);

			mode = false;
		}

	}
}

namespace HexapodControl
{

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::IO;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//

		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		
		String^ RecordsFolder = "";
	private: System::Windows::Forms::OpenFileDialog^ openFileDialog_Open;
	private: System::Windows::Forms::Button^ button_File;
	private: System::Windows::Forms::GroupBox^ groupBox1;
	private: System::Windows::Forms::CheckedListBox^ checkedListBox_List;
	private: System::Windows::Forms::CheckBox^ checkBox_Auto;
	private: System::Windows::Forms::Label^ label2;
	private: System::Windows::Forms::Label^ label1;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Graph_MinY;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Track;
	private: System::Windows::Forms::Label^ label3;
	private: System::Windows::Forms::Panel^ panel_Opt;

	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Gain;

	private: System::Windows::Forms::Label^ label4;
	private: System::Windows::Forms::Button^ button_Gain_Clear;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Shift;
	private: System::Windows::Forms::Label^ label5;
	private: System::Windows::Forms::Panel^ panel2;
	private: System::Windows::Forms::RadioButton^ radioButton_Z;
	private: System::Windows::Forms::RadioButton^ radioButton_Y;
	private: System::Windows::Forms::RadioButton^ radioButton_X;
	private: System::Windows::Forms::Button^ button_Update;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Freq;
	private: System::Windows::Forms::Label^ label7;
	private: System::Windows::Forms::Label^ label6;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Move;

	private: System::Windows::Forms::Label^ label8;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Pos_Min;


	private: System::Windows::Forms::Label^ label11;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Pos_Max;


	private: System::Windows::Forms::Label^ label10;
	private: System::Windows::Forms::Label^ label9;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Delay;

	private: System::Windows::Forms::Label^ label12;
	private: System::Windows::Forms::Label^ label14;
	private: System::Windows::Forms::Label^ label13;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Pos_Acc;

	private: System::Windows::Forms::Label^ label15;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Pos_Width;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Pos_Length;


	private: System::Windows::Forms::Label^ label16;
	private: System::Windows::Forms::Label^ label17;
	private: System::Windows::Forms::CheckBox^ checkBox_Pos_EnGPS;
	private: System::Windows::Forms::CheckBox^ checkBox_Pos_EnAcc;

	private: System::Windows::Forms::DataVisualization::Charting::Chart^ chart_Coef;
	private: System::Windows::Forms::CheckBox^ checkBox_Speed_EnGPS;

	private: System::Windows::Forms::CheckBox^ checkBox_Speed_EnAcc;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Speed_Width;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Speed_Length;
	private: System::Windows::Forms::Label^ label18;
	private: System::Windows::Forms::Label^ label19;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Speed_Acc;
	private: System::Windows::Forms::Label^ label20;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Speed_Max;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Speed_Min;
	private: System::Windows::Forms::Label^ label21;
	private: System::Windows::Forms::Label^ label22;
	private: System::Windows::Forms::Label^ label23;
	private: System::Windows::Forms::Panel^ panel3;
	private: System::Windows::Forms::Panel^ panel4;
	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Speed_AccGPS;
	private: System::Windows::Forms::Label^ label24;
	private: System::Windows::Forms::Button^ button_Connect;

	private: System::Windows::Forms::ComboBox^ comboBox_Port;
	private: System::Windows::Forms::Timer^ timer_Tick;
	private: System::Windows::Forms::Button^ button_Record;
	private: System::Windows::Forms::CheckBox^ checkBox_Filter;
	private: System::Windows::Forms::CheckBox^ checkBox_Inverse;
	private: System::Windows::Forms::ComboBox^ comboBox_Baud;





	private: System::Windows::Forms::NumericUpDown^ numericUpDown_Graph_MaxY;

	protected:

	protected:
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::DataVisualization::Charting::Chart^ chart_Graph;
	protected:


	private: System::Windows::Forms::Panel^ panel1;



	private: System::Windows::Forms::GroupBox^ groupBox_P1;







	private: System::ComponentModel::IContainer^ components;
	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			System::Windows::Forms::DataVisualization::Charting::ChartArea^ chartArea1 = (gcnew System::Windows::Forms::DataVisualization::Charting::ChartArea());
			System::Windows::Forms::DataVisualization::Charting::Legend^ legend1 = (gcnew System::Windows::Forms::DataVisualization::Charting::Legend());
			System::Windows::Forms::DataVisualization::Charting::Series^ series1 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::Series^ series2 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::Series^ series3 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::Series^ series4 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::Series^ series5 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::Series^ series6 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::ChartArea^ chartArea2 = (gcnew System::Windows::Forms::DataVisualization::Charting::ChartArea());
			System::Windows::Forms::DataVisualization::Charting::Legend^ legend2 = (gcnew System::Windows::Forms::DataVisualization::Charting::Legend());
			System::Windows::Forms::DataVisualization::Charting::Series^ series7 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			System::Windows::Forms::DataVisualization::Charting::Series^ series8 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
			this->chart_Graph = (gcnew System::Windows::Forms::DataVisualization::Charting::Chart());
			this->panel1 = (gcnew System::Windows::Forms::Panel());
			this->panel2 = (gcnew System::Windows::Forms::Panel());
			this->checkBox_Inverse = (gcnew System::Windows::Forms::CheckBox());
			this->numericUpDown_Speed_AccGPS = (gcnew System::Windows::Forms::NumericUpDown());
			this->label24 = (gcnew System::Windows::Forms::Label());
			this->checkBox_Speed_EnGPS = (gcnew System::Windows::Forms::CheckBox());
			this->checkBox_Speed_EnAcc = (gcnew System::Windows::Forms::CheckBox());
			this->numericUpDown_Speed_Width = (gcnew System::Windows::Forms::NumericUpDown());
			this->numericUpDown_Speed_Length = (gcnew System::Windows::Forms::NumericUpDown());
			this->label18 = (gcnew System::Windows::Forms::Label());
			this->label19 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown_Speed_Acc = (gcnew System::Windows::Forms::NumericUpDown());
			this->label20 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown_Speed_Max = (gcnew System::Windows::Forms::NumericUpDown());
			this->numericUpDown_Speed_Min = (gcnew System::Windows::Forms::NumericUpDown());
			this->label21 = (gcnew System::Windows::Forms::Label());
			this->label22 = (gcnew System::Windows::Forms::Label());
			this->label23 = (gcnew System::Windows::Forms::Label());
			this->checkBox_Pos_EnGPS = (gcnew System::Windows::Forms::CheckBox());
			this->checkBox_Pos_EnAcc = (gcnew System::Windows::Forms::CheckBox());
			this->numericUpDown_Pos_Width = (gcnew System::Windows::Forms::NumericUpDown());
			this->numericUpDown_Pos_Length = (gcnew System::Windows::Forms::NumericUpDown());
			this->label16 = (gcnew System::Windows::Forms::Label());
			this->label17 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown_Pos_Acc = (gcnew System::Windows::Forms::NumericUpDown());
			this->label15 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown_Pos_Max = (gcnew System::Windows::Forms::NumericUpDown());
			this->label14 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown_Delay = (gcnew System::Windows::Forms::NumericUpDown());
			this->label13 = (gcnew System::Windows::Forms::Label());
			this->label12 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown_Pos_Min = (gcnew System::Windows::Forms::NumericUpDown());
			this->label11 = (gcnew System::Windows::Forms::Label());
			this->label10 = (gcnew System::Windows::Forms::Label());
			this->label9 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown_Freq = (gcnew System::Windows::Forms::NumericUpDown());
			this->label7 = (gcnew System::Windows::Forms::Label());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->button_Update = (gcnew System::Windows::Forms::Button());
			this->panel3 = (gcnew System::Windows::Forms::Panel());
			this->comboBox_Baud = (gcnew System::Windows::Forms::ComboBox());
			this->button_Record = (gcnew System::Windows::Forms::Button());
			this->checkBox_Filter = (gcnew System::Windows::Forms::CheckBox());
			this->button_Connect = (gcnew System::Windows::Forms::Button());
			this->comboBox_Port = (gcnew System::Windows::Forms::ComboBox());
			this->button_File = (gcnew System::Windows::Forms::Button());
			this->radioButton_Z = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton_X = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton_Y = (gcnew System::Windows::Forms::RadioButton());
			this->groupBox_P1 = (gcnew System::Windows::Forms::GroupBox());
			this->chart_Coef = (gcnew System::Windows::Forms::DataVisualization::Charting::Chart());
			this->openFileDialog_Open = (gcnew System::Windows::Forms::OpenFileDialog());
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->panel_Opt = (gcnew System::Windows::Forms::Panel());
			this->numericUpDown_Move = (gcnew System::Windows::Forms::NumericUpDown());
			this->label8 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown_Shift = (gcnew System::Windows::Forms::NumericUpDown());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->button_Gain_Clear = (gcnew System::Windows::Forms::Button());
			this->numericUpDown_Gain = (gcnew System::Windows::Forms::NumericUpDown());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->panel4 = (gcnew System::Windows::Forms::Panel());
			this->checkedListBox_List = (gcnew System::Windows::Forms::CheckedListBox());
			this->checkBox_Auto = (gcnew System::Windows::Forms::CheckBox());
			this->numericUpDown_Track = (gcnew System::Windows::Forms::NumericUpDown());
			this->numericUpDown_Graph_MaxY = (gcnew System::Windows::Forms::NumericUpDown());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->numericUpDown_Graph_MinY = (gcnew System::Windows::Forms::NumericUpDown());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->timer_Tick = (gcnew System::Windows::Forms::Timer(this->components));
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->chart_Graph))->BeginInit();
			this->panel1->SuspendLayout();
			this->panel2->SuspendLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_AccGPS))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_Width))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_Length))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_Acc))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_Max))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_Min))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Pos_Width))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Pos_Length))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Pos_Acc))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Pos_Max))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Delay))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Pos_Min))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Freq))->BeginInit();
			this->panel3->SuspendLayout();
			this->groupBox_P1->SuspendLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->chart_Coef))->BeginInit();
			this->groupBox1->SuspendLayout();
			this->panel_Opt->SuspendLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Move))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Shift))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Gain))->BeginInit();
			this->panel4->SuspendLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Track))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Graph_MaxY))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Graph_MinY))->BeginInit();
			this->SuspendLayout();
			// 
			// chart_Graph
			// 
			chartArea1->AxisY->Interval = 1;
			chartArea1->Name = L"ChartArea1";
			this->chart_Graph->ChartAreas->Add(chartArea1);
			this->chart_Graph->Dock = System::Windows::Forms::DockStyle::Fill;
			legend1->Name = L"Legend1";
			this->chart_Graph->Legends->Add(legend1);
			this->chart_Graph->Location = System::Drawing::Point(3, 16);
			this->chart_Graph->Name = L"chart_Graph";
			series1->ChartArea = L"ChartArea1";
			series1->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series1->Color = System::Drawing::Color::Blue;
			series1->Legend = L"Legend1";
			series1->Name = L"GPS";
			series1->XValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series1->YValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series2->ChartArea = L"ChartArea1";
			series2->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series2->Color = System::Drawing::Color::Red;
			series2->Legend = L"Legend1";
			series2->Name = L"ACC";
			series2->XValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series2->YValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series3->ChartArea = L"ChartArea1";
			series3->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series3->Color = System::Drawing::Color::Fuchsia;
			series3->Legend = L"Legend1";
			series3->Name = L"a_spd";
			series3->XValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series3->YValueType = System::Windows::Forms::DataVisualization::Charting::ChartValueType::Double;
			series4->BorderWidth = 2;
			series4->ChartArea = L"ChartArea1";
			series4->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series4->Color = System::Drawing::Color::Black;
			series4->Legend = L"Legend1";
			series4->Name = L"f_spd";
			series5->ChartArea = L"ChartArea1";
			series5->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series5->Color = System::Drawing::Color::Green;
			series5->Legend = L"Legend1";
			series5->Name = L"s_pos";
			series6->BorderWidth = 2;
			series6->ChartArea = L"ChartArea1";
			series6->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series6->Color = System::Drawing::Color::Tomato;
			series6->Legend = L"Legend1";
			series6->Name = L"f_pos";
			this->chart_Graph->Series->Add(series1);
			this->chart_Graph->Series->Add(series2);
			this->chart_Graph->Series->Add(series3);
			this->chart_Graph->Series->Add(series4);
			this->chart_Graph->Series->Add(series5);
			this->chart_Graph->Series->Add(series6);
			this->chart_Graph->Size = System::Drawing::Size(689, 544);
			this->chart_Graph->TabIndex = 1;
			this->chart_Graph->Text = L"chart1";
			// 
			// panel1
			// 
			this->panel1->Controls->Add(this->panel2);
			this->panel1->Controls->Add(this->panel3);
			this->panel1->Dock = System::Windows::Forms::DockStyle::Left;
			this->panel1->Location = System::Drawing::Point(0, 0);
			this->panel1->Name = L"panel1";
			this->panel1->Size = System::Drawing::Size(135, 688);
			this->panel1->TabIndex = 2;
			// 
			// panel2
			// 
			this->panel2->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
			this->panel2->Controls->Add(this->checkBox_Inverse);
			this->panel2->Controls->Add(this->numericUpDown_Speed_AccGPS);
			this->panel2->Controls->Add(this->label24);
			this->panel2->Controls->Add(this->checkBox_Speed_EnGPS);
			this->panel2->Controls->Add(this->checkBox_Speed_EnAcc);
			this->panel2->Controls->Add(this->numericUpDown_Speed_Width);
			this->panel2->Controls->Add(this->numericUpDown_Speed_Length);
			this->panel2->Controls->Add(this->label18);
			this->panel2->Controls->Add(this->label19);
			this->panel2->Controls->Add(this->numericUpDown_Speed_Acc);
			this->panel2->Controls->Add(this->label20);
			this->panel2->Controls->Add(this->numericUpDown_Speed_Max);
			this->panel2->Controls->Add(this->numericUpDown_Speed_Min);
			this->panel2->Controls->Add(this->label21);
			this->panel2->Controls->Add(this->label22);
			this->panel2->Controls->Add(this->label23);
			this->panel2->Controls->Add(this->checkBox_Pos_EnGPS);
			this->panel2->Controls->Add(this->checkBox_Pos_EnAcc);
			this->panel2->Controls->Add(this->numericUpDown_Pos_Width);
			this->panel2->Controls->Add(this->numericUpDown_Pos_Length);
			this->panel2->Controls->Add(this->label16);
			this->panel2->Controls->Add(this->label17);
			this->panel2->Controls->Add(this->numericUpDown_Pos_Acc);
			this->panel2->Controls->Add(this->label15);
			this->panel2->Controls->Add(this->numericUpDown_Pos_Max);
			this->panel2->Controls->Add(this->label14);
			this->panel2->Controls->Add(this->numericUpDown_Delay);
			this->panel2->Controls->Add(this->label13);
			this->panel2->Controls->Add(this->label12);
			this->panel2->Controls->Add(this->numericUpDown_Pos_Min);
			this->panel2->Controls->Add(this->label11);
			this->panel2->Controls->Add(this->label10);
			this->panel2->Controls->Add(this->label9);
			this->panel2->Controls->Add(this->numericUpDown_Freq);
			this->panel2->Controls->Add(this->label7);
			this->panel2->Controls->Add(this->label6);
			this->panel2->Controls->Add(this->button_Update);
			this->panel2->Dock = System::Windows::Forms::DockStyle::Top;
			this->panel2->Location = System::Drawing::Point(0, 157);
			this->panel2->Name = L"panel2";
			this->panel2->Size = System::Drawing::Size(135, 532);
			this->panel2->TabIndex = 5;
			// 
			// checkBox_Inverse
			// 
			this->checkBox_Inverse->AutoSize = true;
			this->checkBox_Inverse->Location = System::Drawing::Point(10, 36);
			this->checkBox_Inverse->Name = L"checkBox_Inverse";
			this->checkBox_Inverse->Size = System::Drawing::Size(102, 17);
			this->checkBox_Inverse->TabIndex = 42;
			this->checkBox_Inverse->Text = L"Axe acc inverse";
			this->checkBox_Inverse->UseVisualStyleBackColor = true;
			this->checkBox_Inverse->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox_Inverse_CheckedChanged);
			// 
			// numericUpDown_Speed_AccGPS
			// 
			this->numericUpDown_Speed_AccGPS->DecimalPlaces = 4;
			this->numericUpDown_Speed_AccGPS->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 196608 });
			this->numericUpDown_Speed_AccGPS->Location = System::Drawing::Point(57, 432);
			this->numericUpDown_Speed_AccGPS->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 262144 });
			this->numericUpDown_Speed_AccGPS->Name = L"numericUpDown_Speed_AccGPS";
			this->numericUpDown_Speed_AccGPS->Size = System::Drawing::Size(65, 20);
			this->numericUpDown_Speed_AccGPS->TabIndex = 41;
			this->numericUpDown_Speed_AccGPS->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 2, 0, 0, 131072 });
			this->numericUpDown_Speed_AccGPS->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// label24
			// 
			this->label24->AutoSize = true;
			this->label24->Location = System::Drawing::Point(6, 434);
			this->label24->Name = L"label24";
			this->label24->Size = System::Drawing::Size(51, 13);
			this->label24->TabIndex = 40;
			this->label24->Text = L"AccGPS:";
			// 
			// checkBox_Speed_EnGPS
			// 
			this->checkBox_Speed_EnGPS->AutoSize = true;
			this->checkBox_Speed_EnGPS->Location = System::Drawing::Point(107, 471);
			this->checkBox_Speed_EnGPS->Name = L"checkBox_Speed_EnGPS";
			this->checkBox_Speed_EnGPS->Size = System::Drawing::Size(15, 14);
			this->checkBox_Speed_EnGPS->TabIndex = 39;
			this->checkBox_Speed_EnGPS->UseVisualStyleBackColor = true;
			this->checkBox_Speed_EnGPS->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox_Pos_EnAcc_CheckedChanged);
			// 
			// checkBox_Speed_EnAcc
			// 
			this->checkBox_Speed_EnAcc->AutoSize = true;
			this->checkBox_Speed_EnAcc->Location = System::Drawing::Point(107, 407);
			this->checkBox_Speed_EnAcc->Name = L"checkBox_Speed_EnAcc";
			this->checkBox_Speed_EnAcc->Size = System::Drawing::Size(15, 14);
			this->checkBox_Speed_EnAcc->TabIndex = 38;
			this->checkBox_Speed_EnAcc->UseVisualStyleBackColor = true;
			this->checkBox_Speed_EnAcc->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox_Pos_EnAcc_CheckedChanged);
			// 
			// numericUpDown_Speed_Width
			// 
			this->numericUpDown_Speed_Width->DecimalPlaces = 1;
			this->numericUpDown_Speed_Width->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 65536 });
			this->numericUpDown_Speed_Width->Location = System::Drawing::Point(36, 469);
			this->numericUpDown_Speed_Width->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 10, 0, 0, 0 });
			this->numericUpDown_Speed_Width->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Speed_Width->Name = L"numericUpDown_Speed_Width";
			this->numericUpDown_Speed_Width->Size = System::Drawing::Size(65, 20);
			this->numericUpDown_Speed_Width->TabIndex = 35;
			this->numericUpDown_Speed_Width->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 2, 0, 0, 0 });
			this->numericUpDown_Speed_Width->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// numericUpDown_Speed_Length
			// 
			this->numericUpDown_Speed_Length->DecimalPlaces = 1;
			this->numericUpDown_Speed_Length->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 65536 });
			this->numericUpDown_Speed_Length->Location = System::Drawing::Point(36, 495);
			this->numericUpDown_Speed_Length->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 65536 });
			this->numericUpDown_Speed_Length->Name = L"numericUpDown_Speed_Length";
			this->numericUpDown_Speed_Length->Size = System::Drawing::Size(65, 20);
			this->numericUpDown_Speed_Length->TabIndex = 37;
			this->numericUpDown_Speed_Length->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Speed_Length->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// label18
			// 
			this->label18->AutoSize = true;
			this->label18->Location = System::Drawing::Point(6, 497);
			this->label18->Name = L"label18";
			this->label18->Size = System::Drawing::Size(28, 13);
			this->label18->TabIndex = 36;
			this->label18->Text = L"Len:";
			// 
			// label19
			// 
			this->label19->AutoSize = true;
			this->label19->Location = System::Drawing::Point(6, 471);
			this->label19->Name = L"label19";
			this->label19->Size = System::Drawing::Size(29, 13);
			this->label19->TabIndex = 34;
			this->label19->Text = L"Wid:";
			// 
			// numericUpDown_Speed_Acc
			// 
			this->numericUpDown_Speed_Acc->DecimalPlaces = 4;
			this->numericUpDown_Speed_Acc->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 196608 });
			this->numericUpDown_Speed_Acc->Location = System::Drawing::Point(36, 406);
			this->numericUpDown_Speed_Acc->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 262144 });
			this->numericUpDown_Speed_Acc->Name = L"numericUpDown_Speed_Acc";
			this->numericUpDown_Speed_Acc->Size = System::Drawing::Size(65, 20);
			this->numericUpDown_Speed_Acc->TabIndex = 33;
			this->numericUpDown_Speed_Acc->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 2, 0, 0, 131072 });
			this->numericUpDown_Speed_Acc->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// label20
			// 
			this->label20->AutoSize = true;
			this->label20->Location = System::Drawing::Point(6, 408);
			this->label20->Name = L"label20";
			this->label20->Size = System::Drawing::Size(29, 13);
			this->label20->TabIndex = 32;
			this->label20->Text = L"Acc:";
			// 
			// numericUpDown_Speed_Max
			// 
			this->numericUpDown_Speed_Max->DecimalPlaces = 4;
			this->numericUpDown_Speed_Max->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 131072 });
			this->numericUpDown_Speed_Max->Location = System::Drawing::Point(36, 349);
			this->numericUpDown_Speed_Max->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Speed_Max->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 262144 });
			this->numericUpDown_Speed_Max->Name = L"numericUpDown_Speed_Max";
			this->numericUpDown_Speed_Max->Size = System::Drawing::Size(83, 20);
			this->numericUpDown_Speed_Max->TabIndex = 29;
			this->numericUpDown_Speed_Max->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Speed_Max->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// numericUpDown_Speed_Min
			// 
			this->numericUpDown_Speed_Min->DecimalPlaces = 4;
			this->numericUpDown_Speed_Min->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 131072 });
			this->numericUpDown_Speed_Min->Location = System::Drawing::Point(36, 375);
			this->numericUpDown_Speed_Min->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Speed_Min->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 262144 });
			this->numericUpDown_Speed_Min->Name = L"numericUpDown_Speed_Min";
			this->numericUpDown_Speed_Min->Size = System::Drawing::Size(83, 20);
			this->numericUpDown_Speed_Min->TabIndex = 31;
			this->numericUpDown_Speed_Min->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 262144 });
			this->numericUpDown_Speed_Min->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// label21
			// 
			this->label21->AutoSize = true;
			this->label21->Location = System::Drawing::Point(6, 377);
			this->label21->Name = L"label21";
			this->label21->Size = System::Drawing::Size(27, 13);
			this->label21->TabIndex = 30;
			this->label21->Text = L"Min:";
			// 
			// label22
			// 
			this->label22->AutoSize = true;
			this->label22->Location = System::Drawing::Point(6, 351);
			this->label22->Name = L"label22";
			this->label22->Size = System::Drawing::Size(30, 13);
			this->label22->TabIndex = 28;
			this->label22->Text = L"Max:";
			// 
			// label23
			// 
			this->label23->AutoSize = true;
			this->label23->Location = System::Drawing::Point(40, 333);
			this->label23->Name = L"label23";
			this->label23->Size = System::Drawing::Size(38, 13);
			this->label23->TabIndex = 27;
			this->label23->Text = L"Speed";
			// 
			// checkBox_Pos_EnGPS
			// 
			this->checkBox_Pos_EnGPS->AutoSize = true;
			this->checkBox_Pos_EnGPS->Location = System::Drawing::Point(107, 270);
			this->checkBox_Pos_EnGPS->Name = L"checkBox_Pos_EnGPS";
			this->checkBox_Pos_EnGPS->Size = System::Drawing::Size(15, 14);
			this->checkBox_Pos_EnGPS->TabIndex = 25;
			this->checkBox_Pos_EnGPS->UseVisualStyleBackColor = true;
			this->checkBox_Pos_EnGPS->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox_Pos_EnAcc_CheckedChanged);
			// 
			// checkBox_Pos_EnAcc
			// 
			this->checkBox_Pos_EnAcc->AutoSize = true;
			this->checkBox_Pos_EnAcc->Location = System::Drawing::Point(107, 234);
			this->checkBox_Pos_EnAcc->Name = L"checkBox_Pos_EnAcc";
			this->checkBox_Pos_EnAcc->Size = System::Drawing::Size(15, 14);
			this->checkBox_Pos_EnAcc->TabIndex = 24;
			this->checkBox_Pos_EnAcc->UseVisualStyleBackColor = true;
			this->checkBox_Pos_EnAcc->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox_Pos_EnAcc_CheckedChanged);
			// 
			// numericUpDown_Pos_Width
			// 
			this->numericUpDown_Pos_Width->DecimalPlaces = 1;
			this->numericUpDown_Pos_Width->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 65536 });
			this->numericUpDown_Pos_Width->Location = System::Drawing::Point(36, 268);
			this->numericUpDown_Pos_Width->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 10, 0, 0, 0 });
			this->numericUpDown_Pos_Width->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Pos_Width->Name = L"numericUpDown_Pos_Width";
			this->numericUpDown_Pos_Width->Size = System::Drawing::Size(65, 20);
			this->numericUpDown_Pos_Width->TabIndex = 21;
			this->numericUpDown_Pos_Width->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 2, 0, 0, 0 });
			this->numericUpDown_Pos_Width->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// numericUpDown_Pos_Length
			// 
			this->numericUpDown_Pos_Length->DecimalPlaces = 1;
			this->numericUpDown_Pos_Length->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 65536 });
			this->numericUpDown_Pos_Length->Location = System::Drawing::Point(36, 294);
			this->numericUpDown_Pos_Length->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 65536 });
			this->numericUpDown_Pos_Length->Name = L"numericUpDown_Pos_Length";
			this->numericUpDown_Pos_Length->Size = System::Drawing::Size(65, 20);
			this->numericUpDown_Pos_Length->TabIndex = 23;
			this->numericUpDown_Pos_Length->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Pos_Length->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// label16
			// 
			this->label16->AutoSize = true;
			this->label16->Location = System::Drawing::Point(6, 296);
			this->label16->Name = L"label16";
			this->label16->Size = System::Drawing::Size(28, 13);
			this->label16->TabIndex = 22;
			this->label16->Text = L"Len:";
			// 
			// label17
			// 
			this->label17->AutoSize = true;
			this->label17->Location = System::Drawing::Point(6, 270);
			this->label17->Name = L"label17";
			this->label17->Size = System::Drawing::Size(29, 13);
			this->label17->TabIndex = 20;
			this->label17->Text = L"Wid:";
			// 
			// numericUpDown_Pos_Acc
			// 
			this->numericUpDown_Pos_Acc->DecimalPlaces = 4;
			this->numericUpDown_Pos_Acc->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 196608 });
			this->numericUpDown_Pos_Acc->Location = System::Drawing::Point(36, 233);
			this->numericUpDown_Pos_Acc->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 262144 });
			this->numericUpDown_Pos_Acc->Name = L"numericUpDown_Pos_Acc";
			this->numericUpDown_Pos_Acc->Size = System::Drawing::Size(65, 20);
			this->numericUpDown_Pos_Acc->TabIndex = 19;
			this->numericUpDown_Pos_Acc->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 2, 0, 0, 131072 });
			this->numericUpDown_Pos_Acc->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// label15
			// 
			this->label15->AutoSize = true;
			this->label15->Location = System::Drawing::Point(6, 235);
			this->label15->Name = L"label15";
			this->label15->Size = System::Drawing::Size(29, 13);
			this->label15->TabIndex = 18;
			this->label15->Text = L"Acc:";
			// 
			// numericUpDown_Pos_Max
			// 
			this->numericUpDown_Pos_Max->DecimalPlaces = 4;
			this->numericUpDown_Pos_Max->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 131072 });
			this->numericUpDown_Pos_Max->Location = System::Drawing::Point(36, 176);
			this->numericUpDown_Pos_Max->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Pos_Max->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 262144 });
			this->numericUpDown_Pos_Max->Name = L"numericUpDown_Pos_Max";
			this->numericUpDown_Pos_Max->Size = System::Drawing::Size(83, 20);
			this->numericUpDown_Pos_Max->TabIndex = 11;
			this->numericUpDown_Pos_Max->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Pos_Max->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// label14
			// 
			this->label14->AutoSize = true;
			this->label14->Location = System::Drawing::Point(46, 96);
			this->label14->Name = L"label14";
			this->label14->Size = System::Drawing::Size(29, 13);
			this->label14->TabIndex = 17;
			this->label14->Text = L"GPS";
			// 
			// numericUpDown_Delay
			// 
			this->numericUpDown_Delay->Location = System::Drawing::Point(43, 112);
			this->numericUpDown_Delay->Name = L"numericUpDown_Delay";
			this->numericUpDown_Delay->Size = System::Drawing::Size(45, 20);
			this->numericUpDown_Delay->TabIndex = 15;
			this->numericUpDown_Delay->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 30, 0, 0, 0 });
			this->numericUpDown_Delay->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// label13
			// 
			this->label13->AutoSize = true;
			this->label13->Location = System::Drawing::Point(89, 114);
			this->label13->Name = L"label13";
			this->label13->Size = System::Drawing::Size(38, 13);
			this->label13->TabIndex = 16;
			this->label13->Text = L"frames";
			// 
			// label12
			// 
			this->label12->AutoSize = true;
			this->label12->Location = System::Drawing::Point(6, 114);
			this->label12->Name = L"label12";
			this->label12->Size = System::Drawing::Size(37, 13);
			this->label12->TabIndex = 14;
			this->label12->Text = L"Delay:";
			// 
			// numericUpDown_Pos_Min
			// 
			this->numericUpDown_Pos_Min->DecimalPlaces = 4;
			this->numericUpDown_Pos_Min->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 131072 });
			this->numericUpDown_Pos_Min->Location = System::Drawing::Point(36, 202);
			this->numericUpDown_Pos_Min->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Pos_Min->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 262144 });
			this->numericUpDown_Pos_Min->Name = L"numericUpDown_Pos_Min";
			this->numericUpDown_Pos_Min->Size = System::Drawing::Size(83, 20);
			this->numericUpDown_Pos_Min->TabIndex = 13;
			this->numericUpDown_Pos_Min->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 262144 });
			this->numericUpDown_Pos_Min->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// label11
			// 
			this->label11->AutoSize = true;
			this->label11->Location = System::Drawing::Point(6, 204);
			this->label11->Name = L"label11";
			this->label11->Size = System::Drawing::Size(27, 13);
			this->label11->TabIndex = 12;
			this->label11->Text = L"Min:";
			// 
			// label10
			// 
			this->label10->AutoSize = true;
			this->label10->Location = System::Drawing::Point(6, 178);
			this->label10->Name = L"label10";
			this->label10->Size = System::Drawing::Size(30, 13);
			this->label10->TabIndex = 10;
			this->label10->Text = L"Max:";
			// 
			// label9
			// 
			this->label9->AutoSize = true;
			this->label9->Location = System::Drawing::Point(40, 160);
			this->label9->Name = L"label9";
			this->label9->Size = System::Drawing::Size(44, 13);
			this->label9->TabIndex = 9;
			this->label9->Text = L"Position";
			// 
			// numericUpDown_Freq
			// 
			this->numericUpDown_Freq->Location = System::Drawing::Point(43, 62);
			this->numericUpDown_Freq->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 200, 0, 0, 0 });
			this->numericUpDown_Freq->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 10, 0, 0, 0 });
			this->numericUpDown_Freq->Name = L"numericUpDown_Freq";
			this->numericUpDown_Freq->Size = System::Drawing::Size(58, 20);
			this->numericUpDown_Freq->TabIndex = 7;
			this->numericUpDown_Freq->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 25, 0, 0, 0 });
			this->numericUpDown_Freq->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Base_Max_ValueChanged);
			// 
			// label7
			// 
			this->label7->AutoSize = true;
			this->label7->Location = System::Drawing::Point(105, 64);
			this->label7->Name = L"label7";
			this->label7->Size = System::Drawing::Size(20, 13);
			this->label7->TabIndex = 8;
			this->label7->Text = L"Hz";
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Location = System::Drawing::Point(6, 64);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(31, 13);
			this->label6->TabIndex = 1;
			this->label6->Text = L"Freq:";
			// 
			// button_Update
			// 
			this->button_Update->Location = System::Drawing::Point(26, 7);
			this->button_Update->Name = L"button_Update";
			this->button_Update->Size = System::Drawing::Size(75, 23);
			this->button_Update->TabIndex = 0;
			this->button_Update->Text = L"UPDATE";
			this->button_Update->UseVisualStyleBackColor = true;
			this->button_Update->Click += gcnew System::EventHandler(this, &Form1::button_Update_Click);
			// 
			// panel3
			// 
			this->panel3->Controls->Add(this->comboBox_Baud);
			this->panel3->Controls->Add(this->button_Record);
			this->panel3->Controls->Add(this->checkBox_Filter);
			this->panel3->Controls->Add(this->button_Connect);
			this->panel3->Controls->Add(this->comboBox_Port);
			this->panel3->Controls->Add(this->button_File);
			this->panel3->Controls->Add(this->radioButton_Z);
			this->panel3->Controls->Add(this->radioButton_X);
			this->panel3->Controls->Add(this->radioButton_Y);
			this->panel3->Dock = System::Windows::Forms::DockStyle::Top;
			this->panel3->Location = System::Drawing::Point(0, 0);
			this->panel3->Name = L"panel3";
			this->panel3->Size = System::Drawing::Size(135, 157);
			this->panel3->TabIndex = 9;
			// 
			// comboBox_Baud
			// 
			this->comboBox_Baud->FormattingEnabled = true;
			this->comboBox_Baud->Items->AddRange(gcnew cli::array< System::Object^  >(3) { L"57600", L"115200", L"230400" });
			this->comboBox_Baud->Location = System::Drawing::Point(5, 43);
			this->comboBox_Baud->Name = L"comboBox_Baud";
			this->comboBox_Baud->Size = System::Drawing::Size(124, 21);
			this->comboBox_Baud->TabIndex = 26;
			this->comboBox_Baud->Text = L"57600";
			// 
			// button_Record
			// 
			this->button_Record->BackColor = System::Drawing::Color::Gainsboro;
			this->button_Record->Location = System::Drawing::Point(75, 72);
			this->button_Record->Name = L"button_Record";
			this->button_Record->Size = System::Drawing::Size(54, 23);
			this->button_Record->TabIndex = 11;
			this->button_Record->Text = L"Record";
			this->button_Record->UseVisualStyleBackColor = false;
			this->button_Record->Click += gcnew System::EventHandler(this, &Form1::button_Record_Click);
			// 
			// checkBox_Filter
			// 
			this->checkBox_Filter->AutoSize = true;
			this->checkBox_Filter->Location = System::Drawing::Point(5, 77);
			this->checkBox_Filter->Name = L"checkBox_Filter";
			this->checkBox_Filter->Size = System::Drawing::Size(72, 17);
			this->checkBox_Filter->TabIndex = 25;
			this->checkBox_Filter->Text = L"Online Filt";
			this->checkBox_Filter->UseVisualStyleBackColor = true;
			this->checkBox_Filter->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox_Filter_CheckedChanged);
			// 
			// button_Connect
			// 
			this->button_Connect->Location = System::Drawing::Point(5, 14);
			this->button_Connect->Name = L"button_Connect";
			this->button_Connect->Size = System::Drawing::Size(64, 23);
			this->button_Connect->TabIndex = 10;
			this->button_Connect->Text = L"Connect";
			this->button_Connect->UseVisualStyleBackColor = true;
			this->button_Connect->Click += gcnew System::EventHandler(this, &Form1::button_Connect_Click);
			// 
			// comboBox_Port
			// 
			this->comboBox_Port->FormattingEnabled = true;
			this->comboBox_Port->Location = System::Drawing::Point(75, 16);
			this->comboBox_Port->Name = L"comboBox_Port";
			this->comboBox_Port->Size = System::Drawing::Size(54, 21);
			this->comboBox_Port->TabIndex = 9;
			this->comboBox_Port->DropDown += gcnew System::EventHandler(this, &Form1::comboBox_Port_DropDown);
			// 
			// button_File
			// 
			this->button_File->Location = System::Drawing::Point(5, 101);
			this->button_File->Name = L"button_File";
			this->button_File->Size = System::Drawing::Size(124, 23);
			this->button_File->TabIndex = 4;
			this->button_File->Text = L"Open from File";
			this->button_File->UseVisualStyleBackColor = true;
			this->button_File->Click += gcnew System::EventHandler(this, &Form1::button_File_Click);
			// 
			// radioButton_Z
			// 
			this->radioButton_Z->AutoSize = true;
			this->radioButton_Z->Location = System::Drawing::Point(97, 130);
			this->radioButton_Z->Name = L"radioButton_Z";
			this->radioButton_Z->Size = System::Drawing::Size(32, 17);
			this->radioButton_Z->TabIndex = 8;
			this->radioButton_Z->Text = L"Z";
			this->radioButton_Z->UseVisualStyleBackColor = true;
			this->radioButton_Z->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton_X_CheckedChanged);
			// 
			// radioButton_X
			// 
			this->radioButton_X->AutoSize = true;
			this->radioButton_X->Checked = true;
			this->radioButton_X->Location = System::Drawing::Point(8, 130);
			this->radioButton_X->Name = L"radioButton_X";
			this->radioButton_X->Size = System::Drawing::Size(32, 17);
			this->radioButton_X->TabIndex = 6;
			this->radioButton_X->TabStop = true;
			this->radioButton_X->Text = L"X";
			this->radioButton_X->UseVisualStyleBackColor = true;
			this->radioButton_X->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton_X_CheckedChanged);
			// 
			// radioButton_Y
			// 
			this->radioButton_Y->AutoSize = true;
			this->radioButton_Y->Location = System::Drawing::Point(51, 130);
			this->radioButton_Y->Name = L"radioButton_Y";
			this->radioButton_Y->Size = System::Drawing::Size(32, 17);
			this->radioButton_Y->TabIndex = 7;
			this->radioButton_Y->Text = L"Y";
			this->radioButton_Y->UseVisualStyleBackColor = true;
			this->radioButton_Y->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton_X_CheckedChanged);
			// 
			// groupBox_P1
			// 
			this->groupBox_P1->Controls->Add(this->chart_Graph);
			this->groupBox_P1->Controls->Add(this->chart_Coef);
			this->groupBox_P1->Dock = System::Windows::Forms::DockStyle::Fill;
			this->groupBox_P1->Location = System::Drawing::Point(135, 0);
			this->groupBox_P1->MinimumSize = System::Drawing::Size(676, 688);
			this->groupBox_P1->Name = L"groupBox_P1";
			this->groupBox_P1->Size = System::Drawing::Size(695, 688);
			this->groupBox_P1->TabIndex = 3;
			this->groupBox_P1->TabStop = false;
			// 
			// chart_Coef
			// 
			chartArea2->AxisY->Maximum = 1;
			chartArea2->AxisY->Minimum = 0;
			chartArea2->Name = L"ChartArea1";
			this->chart_Coef->ChartAreas->Add(chartArea2);
			this->chart_Coef->Dock = System::Windows::Forms::DockStyle::Bottom;
			legend2->Name = L"Legend1";
			this->chart_Coef->Legends->Add(legend2);
			this->chart_Coef->Location = System::Drawing::Point(3, 560);
			this->chart_Coef->Name = L"chart_Coef";
			series7->BorderWidth = 2;
			series7->ChartArea = L"ChartArea1";
			series7->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series7->Color = System::Drawing::Color::Red;
			series7->Legend = L"Legend1";
			series7->Name = L"cof_p";
			series8->ChartArea = L"ChartArea1";
			series8->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::FastLine;
			series8->Color = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(0)), static_cast<System::Int32>(static_cast<System::Byte>(192)),
				static_cast<System::Int32>(static_cast<System::Byte>(0)));
			series8->Legend = L"Legend1";
			series8->Name = L"cof_s";
			this->chart_Coef->Series->Add(series7);
			this->chart_Coef->Series->Add(series8);
			this->chart_Coef->Size = System::Drawing::Size(689, 125);
			this->chart_Coef->TabIndex = 2;
			this->chart_Coef->Text = L"chart1";
			// 
			// openFileDialog_Open
			// 
			this->openFileDialog_Open->FileName = L"openFileDialog1";
			this->openFileDialog_Open->Multiselect = true;
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->panel_Opt);
			this->groupBox1->Controls->Add(this->panel4);
			this->groupBox1->Dock = System::Windows::Forms::DockStyle::Right;
			this->groupBox1->Location = System::Drawing::Point(830, 0);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(128, 688);
			this->groupBox1->TabIndex = 4;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L" options ";
			// 
			// panel_Opt
			// 
			this->panel_Opt->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
			this->panel_Opt->Controls->Add(this->numericUpDown_Move);
			this->panel_Opt->Controls->Add(this->label8);
			this->panel_Opt->Controls->Add(this->numericUpDown_Shift);
			this->panel_Opt->Controls->Add(this->label5);
			this->panel_Opt->Controls->Add(this->button_Gain_Clear);
			this->panel_Opt->Controls->Add(this->numericUpDown_Gain);
			this->panel_Opt->Controls->Add(this->label4);
			this->panel_Opt->Dock = System::Windows::Forms::DockStyle::Top;
			this->panel_Opt->Enabled = false;
			this->panel_Opt->Location = System::Drawing::Point(3, 261);
			this->panel_Opt->Name = L"panel_Opt";
			this->panel_Opt->Size = System::Drawing::Size(122, 154);
			this->panel_Opt->TabIndex = 8;
			// 
			// numericUpDown_Move
			// 
			this->numericUpDown_Move->Location = System::Drawing::Point(36, 60);
			this->numericUpDown_Move->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 10000, 0, 0, 0 });
			this->numericUpDown_Move->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 10000, 0, 0, System::Int32::MinValue });
			this->numericUpDown_Move->Name = L"numericUpDown_Move";
			this->numericUpDown_Move->Size = System::Drawing::Size(75, 20);
			this->numericUpDown_Move->TabIndex = 8;
			this->numericUpDown_Move->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Shift_ValueChanged);
			// 
			// label8
			// 
			this->label8->AutoSize = true;
			this->label8->Location = System::Drawing::Point(2, 62);
			this->label8->Name = L"label8";
			this->label8->Size = System::Drawing::Size(37, 13);
			this->label8->TabIndex = 7;
			this->label8->Text = L"Move:";
			// 
			// numericUpDown_Shift
			// 
			this->numericUpDown_Shift->DecimalPlaces = 3;
			this->numericUpDown_Shift->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 65536 });
			this->numericUpDown_Shift->Location = System::Drawing::Point(36, 8);
			this->numericUpDown_Shift->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 32000, 0, 0, 0 });
			this->numericUpDown_Shift->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 32000, 0, 0, System::Int32::MinValue });
			this->numericUpDown_Shift->Name = L"numericUpDown_Shift";
			this->numericUpDown_Shift->Size = System::Drawing::Size(75, 20);
			this->numericUpDown_Shift->TabIndex = 6;
			this->numericUpDown_Shift->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Shift_ValueChanged);
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(6, 10);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(31, 13);
			this->label5->TabIndex = 5;
			this->label5->Text = L"Shift:";
			// 
			// button_Gain_Clear
			// 
			this->button_Gain_Clear->Location = System::Drawing::Point(9, 86);
			this->button_Gain_Clear->Name = L"button_Gain_Clear";
			this->button_Gain_Clear->Size = System::Drawing::Size(102, 23);
			this->button_Gain_Clear->TabIndex = 4;
			this->button_Gain_Clear->Text = L"clean";
			this->button_Gain_Clear->UseVisualStyleBackColor = true;
			this->button_Gain_Clear->Click += gcnew System::EventHandler(this, &Form1::button_Gain_Clear_Click);
			// 
			// numericUpDown_Gain
			// 
			this->numericUpDown_Gain->DecimalPlaces = 3;
			this->numericUpDown_Gain->Increment = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 131072 });
			this->numericUpDown_Gain->Location = System::Drawing::Point(36, 34);
			this->numericUpDown_Gain->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1000, 0, 0, 0 });
			this->numericUpDown_Gain->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 196608 });
			this->numericUpDown_Gain->Name = L"numericUpDown_Gain";
			this->numericUpDown_Gain->Size = System::Drawing::Size(75, 20);
			this->numericUpDown_Gain->TabIndex = 3;
			this->numericUpDown_Gain->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Gain->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Shift_ValueChanged);
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(6, 36);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(32, 13);
			this->label4->TabIndex = 0;
			this->label4->Text = L"Gain:";
			// 
			// panel4
			// 
			this->panel4->Controls->Add(this->checkedListBox_List);
			this->panel4->Controls->Add(this->checkBox_Auto);
			this->panel4->Controls->Add(this->numericUpDown_Track);
			this->panel4->Controls->Add(this->numericUpDown_Graph_MaxY);
			this->panel4->Controls->Add(this->label3);
			this->panel4->Controls->Add(this->numericUpDown_Graph_MinY);
			this->panel4->Controls->Add(this->label2);
			this->panel4->Controls->Add(this->label1);
			this->panel4->Dock = System::Windows::Forms::DockStyle::Top;
			this->panel4->Location = System::Drawing::Point(3, 16);
			this->panel4->Name = L"panel4";
			this->panel4->Size = System::Drawing::Size(122, 245);
			this->panel4->TabIndex = 9;
			// 
			// checkedListBox_List
			// 
			this->checkedListBox_List->Dock = System::Windows::Forms::DockStyle::Top;
			this->checkedListBox_List->FormattingEnabled = true;
			this->checkedListBox_List->Location = System::Drawing::Point(0, 0);
			this->checkedListBox_List->Name = L"checkedListBox_List";
			this->checkedListBox_List->Size = System::Drawing::Size(122, 109);
			this->checkedListBox_List->TabIndex = 0;
			this->checkedListBox_List->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::checkedListBox_List_SelectedIndexChanged);
			this->checkedListBox_List->DoubleClick += gcnew System::EventHandler(this, &Form1::checkedListBox_List_SelectedIndexChanged);
			// 
			// checkBox_Auto
			// 
			this->checkBox_Auto->AutoSize = true;
			this->checkBox_Auto->Location = System::Drawing::Point(20, 118);
			this->checkBox_Auto->Name = L"checkBox_Auto";
			this->checkBox_Auto->Size = System::Drawing::Size(47, 17);
			this->checkBox_Auto->TabIndex = 1;
			this->checkBox_Auto->Text = L"auto";
			this->checkBox_Auto->UseVisualStyleBackColor = true;
			this->checkBox_Auto->CheckedChanged += gcnew System::EventHandler(this, &Form1::checkBox_Auto_CheckedChanged);
			// 
			// numericUpDown_Track
			// 
			this->numericUpDown_Track->Location = System::Drawing::Point(44, 203);
			this->numericUpDown_Track->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Track->Name = L"numericUpDown_Track";
			this->numericUpDown_Track->Size = System::Drawing::Size(57, 20);
			this->numericUpDown_Track->TabIndex = 7;
			this->numericUpDown_Track->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 10, 0, 0, 0 });
			// 
			// numericUpDown_Graph_MaxY
			// 
			this->numericUpDown_Graph_MaxY->Location = System::Drawing::Point(53, 143);
			this->numericUpDown_Graph_MaxY->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 10, 0, 0, 0 });
			this->numericUpDown_Graph_MaxY->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
			this->numericUpDown_Graph_MaxY->Name = L"numericUpDown_Graph_MaxY";
			this->numericUpDown_Graph_MaxY->Size = System::Drawing::Size(60, 20);
			this->numericUpDown_Graph_MaxY->TabIndex = 2;
			this->numericUpDown_Graph_MaxY->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 3, 0, 0, 0 });
			this->numericUpDown_Graph_MaxY->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Graph_MaxY_ValueChanged);
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(6, 205);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(38, 13);
			this->label3->TabIndex = 6;
			this->label3->Text = L"Track:";
			// 
			// numericUpDown_Graph_MinY
			// 
			this->numericUpDown_Graph_MinY->Location = System::Drawing::Point(53, 169);
			this->numericUpDown_Graph_MinY->Maximum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, System::Int32::MinValue });
			this->numericUpDown_Graph_MinY->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 10, 0, 0, System::Int32::MinValue });
			this->numericUpDown_Graph_MinY->Name = L"numericUpDown_Graph_MinY";
			this->numericUpDown_Graph_MinY->Size = System::Drawing::Size(60, 20);
			this->numericUpDown_Graph_MinY->TabIndex = 3;
			this->numericUpDown_Graph_MinY->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 3, 0, 0, System::Int32::MinValue });
			this->numericUpDown_Graph_MinY->ValueChanged += gcnew System::EventHandler(this, &Form1::numericUpDown_Graph_MaxY_ValueChanged);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(9, 171);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(37, 13);
			this->label2->TabIndex = 5;
			this->label2->Text = L"Min Y:";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(6, 145);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(40, 13);
			this->label1->TabIndex = 4;
			this->label1->Text = L"Max Y:";
			// 
			// timer_Tick
			// 
			this->timer_Tick->Interval = 1;
			this->timer_Tick->Tick += gcnew System::EventHandler(this, &Form1::timer_Tick_Tick);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(958, 688);
			this->Controls->Add(this->groupBox_P1);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->panel1);
			this->MinimumSize = System::Drawing::Size(974, 727);
			this->Name = L"Form1";
			this->Text = L"Filter Tuning GPS";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->chart_Graph))->EndInit();
			this->panel1->ResumeLayout(false);
			this->panel2->ResumeLayout(false);
			this->panel2->PerformLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_AccGPS))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_Width))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_Length))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_Acc))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_Max))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Speed_Min))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Pos_Width))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Pos_Length))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Pos_Acc))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Pos_Max))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Delay))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Pos_Min))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Freq))->EndInit();
			this->panel3->ResumeLayout(false);
			this->panel3->PerformLayout();
			this->groupBox_P1->ResumeLayout(false);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->chart_Coef))->EndInit();
			this->groupBox1->ResumeLayout(false);
			this->panel_Opt->ResumeLayout(false);
			this->panel_Opt->PerformLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Move))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Shift))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Gain))->EndInit();
			this->panel4->ResumeLayout(false);
			this->panel4->PerformLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Track))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Graph_MaxY))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown_Graph_MinY))->EndInit();
			this->ResumeLayout(false);

		}
#pragma endregion

	  private: void ToCharString(String^ str1, char* str2)
	  {
	  	for (int a = 0; a < str1->Length; a++) *str2++ = str1[a];
	  	*str2 = '\0';
	  }

    private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) 
		{
			try
			{
				String^ folder = Environment::GetFolderPath(Environment::SpecialFolder::ApplicationData);
				String^ specificFolder = Path::Combine(folder, "Telem");
				//---
				RecordsFolder = Path::Combine(specificFolder, "Records");
				Directory::CreateDirectory(RecordsFolder);

				openFileDialog_Open->InitialDirectory = RecordsFolder;
			}
			catch(...) { }

			for each (auto s in chart_Graph->Series)
			{
				checkedListBox_List->Items->Add(s->Name, true);
			}

			MainDataRecord->Clear();
			for (int a = 0; a < MainDataGain->Length; a++) { MainDataGain[a] = 1; MainDataShift[a] = 0; MainDataMove[a] = 0; }

			numericUpDown_Graph_MaxY_ValueChanged(sender, e);
    }

		private: System::Collections::Generic::List<float>^ MainDataVaildGPS = gcnew System::Collections::Generic::List<float>();
		private: System::Collections::Generic::List<array<float>^>^ MainDataRecord = gcnew System::Collections::Generic::List<array<float>^>();
		private: array<float>^ MainDataGain = gcnew array<float>(6);
	  private: array<float>^ MainDataShift = gcnew array<float>(6);
		private: array<float>^ MainDataMove = gcnew array<float>(6);

	  private: System::Void button_File_Click(System::Object^ sender, System::EventArgs^ e)
		{
			if (button_Connect->Text != "Connect")
			{
				CloseHandle(PortCOM);
				PortCOM = 0;
				timer_Tick->Enabled = false;
				button_Connect->Text = "Connect";
			}

			if (openFileDialog_Open->ShowDialog() != System::Windows::Forms::DialogResult::OK) return;

			MainDataVaildGPS->Clear();
			MainDataRecord->Clear();
			panel_Opt->Enabled = true;

			System::Globalization::CultureInfo^ nonInvariantCulture = gcnew System::Globalization::CultureInfo("en-US");
			nonInvariantCulture->NumberFormat->NumberDecimalSeparator = ".";
			System::Threading::Thread::CurrentThread->CurrentCulture = nonInvariantCulture;

			array<String^>^ text = File::ReadAllLines(openFileDialog_Open->FileName);

			for each (String ^ line in text)
			{
				array<String^>^ elems =line->Split('|');
				if (elems->Length != 7) break;
				array<float>^ values = gcnew array<float>(MainDataGain->Length);

				int shift = 0;
				if (radioButton_Y->Checked) shift = 1;
				if (radioButton_Z->Checked) shift = 2;

				MainDataVaildGPS->Add(Convert::ToSingle(elems[0]));

				values[0] = Convert::ToSingle(elems[1 + shift]);
				values[1] = Convert::ToSingle(elems[4 + shift]);
				
				MainDataRecord->Add(values);
			}
			//---
			memset(&MainFilter, 0, sizeof(MainFilter));
			//---
			button_Gain_Clear_Click(sender, e);
	  }

		private: bool Update_GainShift = true;

	  private: System::Void checkedListBox_List_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) 
		{
			for (int a = 0; a < checkedListBox_List->Items->Count; a++)
			{
				chart_Graph->Series[a]->Enabled = checkedListBox_List->GetItemChecked(a);
			}
			int index = checkedListBox_List->SelectedIndex;
			if (index == -1) return;
			Update_GainShift = false;
			numericUpDown_Gain->Value = (Decimal)MainDataGain[index];
			numericUpDown_Shift->Value = (Decimal)MainDataShift[index];
			numericUpDown_Move->Value = (Decimal)MainDataMove[index];
			Update_GainShift = true;
	  }

    private: System::Void checkBox_Auto_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
		{
			if (checkBox_Auto->Checked)
			{
				chart_Graph->ChartAreas[0]->AxisY->Interval = double::NaN;
				chart_Graph->ChartAreas[0]->AxisY->Minimum = double::NaN;
				chart_Graph->ChartAreas[0]->AxisY->Maximum = double::NaN;
				numericUpDown_Graph_MinY->Enabled = false;
				numericUpDown_Graph_MaxY->Enabled = false;
			}
			else
			{
				chart_Graph->ChartAreas[0]->AxisY->Interval = 1;
				numericUpDown_Graph_MaxY_ValueChanged(sender, e);
				numericUpDown_Graph_MinY->Enabled = true;
				numericUpDown_Graph_MaxY->Enabled = true;
			}
    }
    private: System::Void numericUpDown_Graph_MaxY_ValueChanged(System::Object^ sender, System::EventArgs^ e)
		{
			chart_Graph->ChartAreas[0]->AxisY->Minimum = (double)numericUpDown_Graph_MinY->Value;
			chart_Graph->ChartAreas[0]->AxisY->Maximum = (double)numericUpDown_Graph_MaxY->Value;
    }

	  private: System::Void button_Gain_Clear_Click(System::Object^ sender, System::EventArgs^ e)
		{
			for (int a = 0; a < MainDataGain->Length; a++)
			{
				MainDataGain[a] = 1;
				MainDataShift[a] = 0;
				MainDataMove[a] = 0;
				chart_Graph->Series[a]->Points->Clear();
			}
			Update_GainShift = false;
			numericUpDown_Gain->Value = 1;
			numericUpDown_Shift->Value = 0;
			numericUpDown_Move->Value = 0;
			Update_GainShift = true;
			//---
			RedrawChart();
	  }

		private: System::Void numericUpDown_Shift_ValueChanged(System::Object^ sender, System::EventArgs^ e)
		{
			if (!Update_GainShift) return;
			int index = checkedListBox_List->SelectedIndex;
			if (index == -1) return;
			MainDataGain[index] = Convert::ToSingle(numericUpDown_Gain->Value);
			MainDataShift[index] = Convert::ToSingle(numericUpDown_Shift->Value);
			MainDataMove[index] = Convert::ToSingle(numericUpDown_Move->Value);
			//---
			RedrawChart();
		}

		private: void RedrawChart()
		{
			for each (auto s in chart_Graph->Series) s->Points->Clear();

			for (int a = 0; a < MainDataRecord->Count; a++)
			{
				array<float>^ values = MainDataRecord[a];
				for (int b = 0; b < chart_Graph->Series->Count; b++) chart_Graph->Series[b]->Points->AddXY(a + 1 + MainDataMove[b], (MainDataShift[b] + values[b]) * MainDataGain[b]);
			}
		}

    private: System::Void button_Update_Click(System::Object^ sender, System::EventArgs^ e)
		{
			const float ga = 9.80665;
			const float freq = (float)numericUpDown_Freq->Value;

			memset(&MainFilter, 0, sizeof(MainFilter));

			MainFilter.Freq = freq;
			MainFilter.Position.Max = (float)numericUpDown_Pos_Max->Value;
			MainFilter.Position.Min = (float)numericUpDown_Pos_Min->Value;

			MainFilter.Position.EnAcc = checkBox_Pos_EnAcc->Checked;
			MainFilter.Position.Acc = (float)numericUpDown_Pos_Acc->Value;

			MainFilter.Position.EnGPS = checkBox_Pos_EnGPS->Checked;
			MainFilter.Position.Width = (float)numericUpDown_Pos_Width->Value;
			MainFilter.Position.Length = (float)numericUpDown_Pos_Length->Value;

			MainFilter.Delay.Size = (int)numericUpDown_Delay->Value;

			MainFilter.Speed.Max = (float)numericUpDown_Speed_Max->Value;
			MainFilter.Speed.Min = (float)numericUpDown_Speed_Min->Value;

			chart_Coef->ChartAreas[0]->AxisY->Maximum = max((double)numericUpDown_Speed_Max->Value, (double)numericUpDown_Pos_Max->Value);
								 
			MainFilter.Speed.EnAcc = checkBox_Speed_EnAcc->Checked;
			MainFilter.Speed.Acc = (float)numericUpDown_Speed_Acc->Value;
			MainFilter.Speed.AccGPS = (float)numericUpDown_Speed_AccGPS->Value;
								 
			MainFilter.Speed.EnGPS = checkBox_Speed_EnGPS->Checked;
			MainFilter.Speed.Width = (float)numericUpDown_Speed_Width->Value;
			MainFilter.Speed.Length = (float)numericUpDown_Speed_Length->Value;
			
			if (button_Connect->Text != "Connect") return;

			float speed = 0, position = 0;

			chart_Coef->Series[0]->Points->Clear();
			chart_Coef->Series[1]->Points->Clear();

			for (int a=0; a< MainDataRecord->Count; a++)
			{
				bool valid = MainDataVaildGPS[a];
				auto l = MainDataRecord[a];
				float gps = l[0], acc = l[1];

				speed += acc * ga / freq;
				position += speed / freq;
				l[2] = speed;
				l[4] = position;

				float* coef = MainFilter.Update(valid, gps, acc);

				chart_Coef->Series[0]->Points->AddY((double)coef[0]);
				chart_Coef->Series[1]->Points->AddY((double)coef[1]);

				l[3] = MainFilter.Speed.Value;
				l[5] = MainFilter.Position.Value;
			}

			RedrawChart();
    }
    
    private: System::Void numericUpDown_Base_Max_ValueChanged(System::Object^ sender, System::EventArgs^ e)
		{
			button_Update_Click(sender, e);
    }

    private: System::Void checkBox_Pos_EnAcc_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
		{
			button_Update_Click(sender, e);
    }

    private: System::Void comboBox_Port_DropDown(System::Object^ sender, System::EventArgs^ e)
		{
			comboBox_Port->Items->Clear();
			for each (String ^ p in System::IO::Ports::SerialPort::GetPortNames())
			{
				comboBox_Port->Items->Add(p);
			}
    }

    private: System::Void button_Connect_Click(System::Object^ sender, System::EventArgs^ e)
		{
			if (button_Connect->Text == "Connect")
			{
				long baud;
				try
				{
					baud = Convert::ToInt32(comboBox_Baud->Text);
					comboBox_Baud->Text = baud.ToString();
				}
				catch (...)
				{
					MessageBox::Show("Error to set baud", "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
					return;
				}

				char text[128] = "\\\\.\\";

				ToCharString(comboBox_Port->Text, text + 4);

				PortCOM = CreateFileA(text, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
				if (PortCOM == INVALID_HANDLE_VALUE)
				{
					PortCOM = 0;
					MessageBox::Show("Error to connect", "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
					return;
				}
				//---
				DCB serial = { sizeof(serial) };
				GetCommState(PortCOM, &serial);
				serial.BaudRate = baud;
				serial.ByteSize = 8;
				serial.StopBits = ONESTOPBIT;
				serial.Parity = NOPARITY;
				SetCommState(PortCOM, &serial);
				//---
				COMMTIMEOUTS tim;
				GetCommTimeouts(PortCOM, &tim);
				tim.ReadIntervalTimeout = 1;
				tim.ReadTotalTimeoutConstant = 1;
				tim.ReadTotalTimeoutMultiplier = 1;
				SetCommTimeouts(PortCOM, &tim);
				//---
				timer_Tick->Enabled = true;
				button_Connect->Text = "Disonnect";
				//---
				for (int a = 0; a < chart_Graph->Series->Count; a++) chart_Graph->Series[a]->Points->Clear();
				//---
				MainThread = CreateThread(0, 0, RecvThread, 0, 0, 0);
			}
			else
			{
				CloseHandle(PortCOM);
				PortCOM = 0;
				timer_Tick->Enabled = false;
				button_Connect->Text = "Connect";
			}
    }

    private: System::Void timer_Tick_Tick(System::Object^ sender, System::EventArgs^ e)
		{
			if (!PlotsReady) return;
			PlotsReady = false;

			for (int a = 0; a < chart_Graph->Series->Count; a++)
			{
				chart_Graph->Series[a]->Points->AddY((double)PlotsData[a]);
				while (chart_Graph->Series[a]->Points->Count > numericUpDown_Track->Value * numericUpDown_Freq->Value) chart_Graph->Series[a]->Points->RemoveAt(0);
			}

			for (int a = 0; a < chart_Coef->Series->Count; a++)
			{
				chart_Coef->Series[a]->Points->AddY((double)PlotsCoef[a]);
				while (chart_Coef->Series[a]->Points->Count > numericUpDown_Track->Value * numericUpDown_Freq->Value) chart_Coef->Series[a]->Points->RemoveAt(0);
			}
    }

    private: System::Void button_Record_Click(System::Object^ sender, System::EventArgs^ e)
		{
			if (button_Record->Text == "Stop")
			{
				if (FileLOG) CloseHandle(FileLOG);
				FileLOG = 0;
				button_Record->Text = "Record";
				button_Record->BackColor = Color::Gainsboro;
				return;
			}

			DateTime dt = DateTime::Now;
			String^ file = Path::Combine(RecordsFolder, "record=" + dt.ToString("yyyy-MM-dd+HH_mm_ss") + ".tel");
			try
			{
				char text[256];

				ToCharString(file, text);

				FileLOG = CreateFileA(text, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0);
				if (FileLOG == INVALID_HANDLE_VALUE)
				{
					FileLOG = 0;
					MessageBox::Show("Error to create file", "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
					return;
				}
				button_Record->Text = "Stop";
				button_Record->BackColor = Color::Tomato;
			}
			catch (...)
			{
				MessageBox::Show("Incorrect save file!", "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
			}
    }

    private: System::Void checkBox_Filter_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
		{
			OnlineFilter = checkBox_Filter->Checked;
			button_Update_Click(sender, e);
    }

    private: System::Void radioButton_X_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
		{
			if (sender == radioButton_X && radioButton_X->Checked) TeleAxe = 0;
			if (sender == radioButton_Y && radioButton_Y->Checked) TeleAxe = 1;
			if (sender == radioButton_Z && radioButton_Z->Checked) TeleAxe = 2;
    }

    private: System::Void checkBox_Inverse_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
		{
			AxeInverse = checkBox_Inverse->Checked;
    }
};
}
