private void SubjectPositionWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            SubjectPositionThread thread_call = (SubjectPositionThread)e.Argument;

            e.Result = thread_call.GetArmPosition();
        }
        public ElbowMotionTrackingController(int _window_size, int _biceps_emg_channel, int _triceps_emg_channel, ExperimentInterface _experiment_interface) //add other arguments from startup
        {
            experiment_interface = _experiment_interface;

            subject = new Body();
            emg_sampling_frequency = 2000;
            time_step                     = 1 / emg_sampling_frequency;
            window_size                   = _window_size;
            brace_position                = 0;
            subject_position              = 0;
            biceps_emg_channel            = _biceps_emg_channel;
            triceps_emg_channel           = _triceps_emg_channel;
            brace_encoder_offset          = 0;
            gear_ratio                    = 713; //Taylor's brace gear ratio
            save_motion_data              = false;
            save_system_data              = false;
            track_motion_data             = false;
            current_estimation_controller = 0;
            upper_position_limit          = 130;
            lower_position_limit          = -20;
            command_position              = 0;
            command_frequency             = Convert.ToDouble(experiment_interface.textBox8.Text);
            counter = 0;

            //initialize complex data types
            estimation_controllers = new List <EstimationController>();
            kfmm = new KalmanFilterEstimationController();
            pmm  = new ProportionalEstimationController(time_step);
            npmm = new PolynomialEstimationController(time_step, 3);
            estimation_controllers.Add(kfmm);
            estimation_controllers.Add(pmm);
            estimation_controllers.Add(npmm);
            actuation_controller  = new WearMEBraceController(epos, this);
            epos_manager          = new DeviceManager("EPOS", "MAXON_RS232", "RS232", experiment_interface.textBox9.Text);
            epos_manager.Baudrate = 115200;
            epos_manager.Timeout  = 500;
            trigno                       = new DelsysTrignoCSharpLibrary.Driver();
            collection_arm               = new CollectionArmLibrary.CollectionArm(experiment_interface.textBox13.Text, 38400);
            subject_positions            = new List <double>(window_size);
            brace_positions              = new List <double>(window_size);
            triceps_emg                  = new List <double>(window_size);
            biceps_emg                   = new List <double>(window_size);
            position_estimates           = new List <double>(window_size);
            previous_biceps_emg          = new List <double>(window_size);
            previous_triceps_emg         = new List <double>(window_size);
            previous_subject_positions   = new List <double>(window_size);
            previous_brace_positions     = new List <double>(window_size);
            command_positions            = new List <double>(window_size);
            previous_biceps_emg_filter1  = new List <double>(window_size);
            previous_triceps_emg_filter1 = new List <double>(window_size);
            for (int i = 0; i < window_size; i++)
            {
                previous_biceps_emg.Add(0);
                previous_triceps_emg.Add(0);
                previous_subject_positions.Add(0);
                previous_brace_positions.Add(0);
                previous_biceps_emg_filter1.Add(0);
                previous_triceps_emg_filter1.Add(0);
                position_estimates.Add(0);
            }
            muscle_activation_parameters = new List <List <double> >();
            kalman_filter_parameters     = new List <double>();
            proportional_parameters      = new List <double>();
            subject_motion_data          = new List <List <double> >();
            stop_watch        = new Stopwatch();
            stop_watch2       = new Stopwatch();
            stop_watch3       = new Stopwatch();
            finished_logging  = true;
            signal_processing = new SignalProcessing();

            //Thread initialization
            emg_data_thread                             = new EMGDataThread();
            emg_data_thread.window_size                 = window_size;
            emg_data_thread.trigno                      = trigno;
            emg_data_thread.biceps_emg_channel          = biceps_emg_channel;
            emg_data_thread.triceps_emg_channel         = triceps_emg_channel;
            emg_data_worker                             = new BackgroundWorker();
            emg_data_worker.DoWork                     += new DoWorkEventHandler(EMGDataWorker_DoWork);
            emg_data_worker.RunWorkerCompleted         += new RunWorkerCompletedEventHandler(EMGDataWorker_RunWorkerCompleted);
            subject_position_thread                     = new SubjectPositionThread();
            subject_position_thread.collection_arm      = collection_arm;
            subject_position_worker                     = new BackgroundWorker();
            subject_position_worker.DoWork             += new DoWorkEventHandler(SubjectPositionWorker_DoWork);
            subject_position_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(SubjectPositionWorker_RunWorkerCompleted);
            logger_thread                     = new LoggerThread();
            logger_worker                     = new BackgroundWorker();
            logger_worker.DoWork             += new DoWorkEventHandler(LoggerWorker_DoWork);
            logger_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(LoggerWorker_RunWorkerCompleted);

            tracking_loop_timer    = 0;
            data_log_timer         = 0;
            subject_position_timer = 0;
            brace_position_timer   = 0;
            brace_command_timer    = 0;
        }