Esempio n. 1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            rbTime.Checked = true;
            X = "time";
            rbYA.Checked = true;
            Y = "A";

            sampleParser = new ArduinoSampleParser();
            comManager = new ComManager("COM5",  sampleParser, UpdateChart);

            readsAccumulated = chart.Series.Add("readsXZ");
            readsAccumulated.ChartType = SeriesChartType.FastLine;
            readsAccumulated.Color = Color.Red;
            readsAccumulated.BorderWidth = 2;

            readsRaw = chart.Series.Add("readsRaw");
            readsRaw.ChartType = SeriesChartType.FastLine;
            readsRaw.Color = Color.Blue;
            readsRaw.BorderWidth = 1;

            rowingSignals = chart.Series.Add("rowingSignals");
            rowingSignals.ChartType = SeriesChartType.Point;
            rowingSignals.Color = Color.Blue;
            rowingSignals.BorderWidth = 3;

            AddChartLine(0);
            AddChartLine(3000);
            AddChartLine(-3000);

            chart.ChartAreas[0].AxisX.Minimum = MINX;
            chart.ChartAreas[0].AxisX.Maximum = MAXX;

            chart.ChartAreas[0].AxisY.Minimum = MINY;
            chart.ChartAreas[0].AxisY.Maximum = MAXY;

            //chart.ChartAreas[0].AxisY.IsLogarithmic = true;
        }
Esempio n. 2
0
        public ComManager(string portName, ArduinoSampleParser sampleParser, Action<Sample, Sample, RowingSignal> updateChart)
        {
            accumulated = new Sample();
            accumulatedTwo = new Sample();
            horizontalSignals = new List<RowingSignal>();

            mySerialPort = new SerialPort("COM5");

            mySerialPort.BaudRate = 38400;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;

            mySerialPort.DataReceived += new SerialDataReceivedEventHandler((sender, e) => {
                try
                {
                    SerialPort sp = (SerialPort)sender;
                    string lastRead = sp.ReadLine();
                    Sample lastSample = sampleParser.Parse(lastRead);

                    RowingSignal signal = null;
                    const int STROKE_RATE_SMOOTHING = 5;
                    double strokeTime = -1;
                    DateTime signalTime = DateTime.Now;
                    if (lastSample.a >= HORIZONTAL_SIGNAL_THRESHOLD &&
                        ((horizontalSignals.LastOrDefault() != null && horizontalSignals.Last().Phase == RowingPhases.Catch)
                            || horizontalSignals.LastOrDefault() == null)
                    )
                    {

                        var lastRelease = horizontalSignals.LastOrDefault(s => s.Phase == RowingPhases.Release);
                        if(lastRelease != null)
                        {
                            strokeTime = (signalTime - lastRelease.TimeStamp).TotalMilliseconds;
                        }

                        signal = new RowingSignal { TimeStamp = signalTime, Phase = RowingPhases.Release, StrokeTime = strokeTime };
                        horizontalSignals.Add(signal);
                    }
                    else if (lastSample.a <= -HORIZONTAL_SIGNAL_THRESHOLD &&
                                ((horizontalSignals.LastOrDefault() != null &&  horizontalSignals.Last().Phase == RowingPhases.Release)
                                    || horizontalSignals.LastOrDefault() == null))
                    {
                        var lastCatch = horizontalSignals.LastOrDefault(s => s.Phase == RowingPhases.Catch);
                        if(lastCatch != null)
                        {
                            strokeTime = (signalTime - lastCatch.TimeStamp).TotalMilliseconds;
                        }

                        signal = new RowingSignal { TimeStamp = signalTime, Phase = RowingPhases.Catch, StrokeTime = strokeTime };
                        horizontalSignals.Add(signal);
                    }

                    if (horizontalSignals.Count >= STROKE_RATE_SMOOTHING)
                    {
                        var smoothedStrokeRate = horizontalSignals.OrderByDescending(s => s.TimeStamp).Take(STROKE_RATE_SMOOTHING).Average(s => 60000 / s.StrokeTime);
                        horizontalSignals.Last().SmoothedStrokeRate = smoothedStrokeRate;
                    }

                    SmoothingFunction(lastSample, ref smoothedValue, 0.97);

                    accumulated = accumulated + (lastSample - smoothedValue);
                    SmoothingFunction(accumulated, ref smoothAccumulated, 0.97);
                    accumulatedTwo = accumulatedTwo + ( accumulated - smoothAccumulated);

                    updateChart(accumulated, lastSample, signal);

                }
                catch (InvalidFormatException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            });

            mySerialPort.Open();
        }