Results 1 to 19 of 19

Thread: Stopwatches not in Sync?????

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Stopwatches not in Sync?????

    I have a program that logs (supposedly) 90 data points per second. I have a stopwatch that starts when the program opens and as each data point is logged the elapsed time of the stopwatch is also logged to keep track of the timing.

    The data points are force values from a load cell with a rocket motor on top of it.

    My program is telling me that my motor produced thrust for 1.923 seconds, but the video of the firing is telling me that it only produced thrust for 1.638seconds. The program had run for 393 seconds before the motor ignited, is it possible that after 393 seconds the stopwatch could be out of sync?


    Ryan

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Stopwatches not in Sync?????

    Quote Originally Posted by rpmaurer View Post
    is it possible that after 393 seconds the stopwatch could be out of sync?
    No it isn't.
    Quote Originally Posted by MSDN
    The timer used by the Stopwatch class depends on the system hardware and operating system. IsHighResolution is true if the Stopwatch timer is based on a high-resolution performance counter. Otherwise, IsHighResolution is false, which indicates that the Stopwatch timer is based on the system timer.
    The Stopwatch class is as accurate as your computer is. You should consider the possibility of there being a delay in stopping the Stopwatch after the action being timed had stopped. Almost 0.3 of a second does seem like quite a lot though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Stopwatches not in Sync?????

    Well that's not good! Where would this delay be?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Stopwatches not in Sync?????

    Or perhaps suggestions on getting a more accurate timekeeper?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Stopwatches not in Sync?????

    Quote Originally Posted by rpmaurer View Post
    Well that's not good! Where would this delay be?
    You tell us. Unlike you, we haven't seen your code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Stopwatches not in Sync?????

    Quote Originally Posted by rpmaurer View Post
    Or perhaps suggestions on getting a more accurate timekeeper?
    Given that the Stopwatch is based on the most accurate timer available on the current system, there is no more accurate timekeeper.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Stopwatches not in Sync?????

    How are you getting timing from the video? What are the specs on the computer running the program? What OS and version of .Net are you running?

    I agree with what jmc has said.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Stopwatches not in Sync?????

    Sorry, I really do have a bad habit of not posting needed information.

    Using Corel VS X5 Pro to get the duration from ignition to burnout of the motor. Windows 8, Vb.net (2012 Express), 6GB RAM, i5 dual core.

    Here is the code that is run when the serial port recieves data:

    Code:
    Private Sub GetData()
            Try
                If InvokeRequired Then
                    Me.Invoke(New MethodInvoker(AddressOf GetData))
                Else
    
                    'get voltage data from loadcell, main batt, and ampbatt
                    inputstring = SerialPort1.ReadLine
                    loadcell = Val(inputstring.Substring(0, 4)) / 1024 * 10
                    inForce = (loadcell - zerovalue) * forcescale
    
                    'add data point
                    Datachart.Series("DataSeries").Points.AddXY(Math.Round(Timer.Elapsed.TotalSeconds, 3), inForce)
    
                    'update chart
                    If Timer.Elapsed.TotalSeconds > 10 Then
                        Datachart.ChartAreas(0).AxisX.Maximum = Timer.Elapsed.TotalSeconds
                        Datachart.ChartAreas(0).AxisX.Minimum = Datachart.ChartAreas(0).AxisX.Maximum - 10
                    End If
    
                    'respond
                    SerialPort1.Write("1")
                    Label_Time.Text = "Current Time (s): " + Timer.Elapsed.ToString
    
                    'adjust chart y bounds
                    If inForce > Datachart.ChartAreas(0).AxisY.Maximum Then
                        If firstdata = 1 Then
                            firstdata = 0
                            Exit Sub
                        End If
                        AutoAdjust = 1
                        TextBox_ChartMax.Text = inForce
                        Datachart.ChartAreas(0).AxisY.Maximum = inForce
                        AutoAdjust = 0
                    End If
    
                    'log data
                    If logging = 1 Then
                        Count = Count + 1
                        Datachart.Series("LoggedData").Points.AddXY(Math.Round(Timer.Elapsed.TotalSeconds, 3), inForce)
                        DataGrid.Rows(Count).Cells(0).Value = Count
                        DataGrid.Rows(Count).Cells(1).Value = Math.Round(Timer.Elapsed.TotalSeconds, 4)
                        DataGrid.Rows(Count).Cells(2).Value = Math.Round(loadcell, 4)
                        DataGrid.Rows(Count).Cells(3).Value = Math.Round(inForce, 4)
                        If DataGrid.RowCount - Count < 10 Then
                            DataGrid.RowCount = DataGrid.RowCount + 500
                        End If
                    End If
                End If
    
                'display batter voltage
                mainbatt = Val(inputstring.Substring(4, 4)) / 1024 * 10
                ampbatt = Val(inputstring.Substring(8, 4)) / 1024 * 10
                Label_AmpBatt.Text = Math.Round(ampbatt, 3).ToString + "V"
                Label_MainBatt.Text = Math.Round(mainbatt, 3).ToString + "V"
    
                'update data count
                datacount = datacount + 1
    
            Catch ex As Exception
    
            End Try
    
        End Sub

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Stopwatches not in Sync?????

    I guess timer is the Stopwatch??? I don't understand how you get a time stamp from Corel or see the logging of the Stopwatch. How does the SerialPort play into all of this? I see you have a blocking call to the SerialPort.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Stopwatches not in Sync?????

    Ok, the program says that the motor burned (produced thrust) for roughly 1.95s. But in the video I only see a flame (burning rocket motor) for 1.63s. The thrust data is coming from a load cell through a serial port.


    HEre is where it takes the load cell data and converts it to a force and then displays this on a chart at point (timer.elapsedtime, force)
    Code:
     'get voltage data from loadcell, main batt, and ampbatt
                    inputstring = SerialPort1.ReadLine
                    loadcell = Val(inputstring.Substring(0, 4)) / 1024 * 10
                    inForce = (loadcell - zerovalue) * forcescale
    
                    'add data point
                    Datachart.Series("DataSeries").Points.AddXY(Math.Round(Timer.Elapsed.TotalSeconds, 3), inForce)
    YEs, Timer is stopwatch, sorry for the confusion

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Stopwatches not in Sync?????

    There are only two lines there that could conceivably take any measurable time:

    1) The Datachart line (the last line). This one involves a certain amount of drawing, so it could potentially be a bit slow, but the discrepancy you are seeing is greater than 0.1 seconds, so you'd see a flicker if this was the case....and it almost certainly isn't the case. A flicker might exist even if this thing wasn't an issue. A second Stopwatch starting before, and stopping after, that line would show how long it took. I doubt it will register more than 0 ms, though, so I don't think this line is the issue.

    2) Serialport.Readline. This is roughly where I would expect the delay. ReadLine is a blocking call, which means that execution will stop at that line until something is available. If there is any delay in the operation of the serialport, this is where your delay could be coming from.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Stopwatches not in Sync?????

    Any suggestions on getting around the ReadLine?

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Stopwatches not in Sync?????

    First off, I'd use a stopwatch to confirm that there is a delay there. Start the stopwatch before, stop it after, and check the elapsed milliseconds. After all, there's no point in hunting the wrong game.
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Stopwatches not in Sync?????

    That is one thing I want to check... will let you guys know if there is still a delay.

  15. #15
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Stopwatches not in Sync?????

    Does the video have a time stamp or are you timing it some other way? How is GetData being called?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Stopwatches not in Sync?????

    Yes, video has a time stamp. GetData is called when the SerialPort recieves data.

  17. #17
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Stopwatches not in Sync?????

    Quote Originally Posted by rpmaurer View Post
    Yes, video has a time stamp. GetData is called when the SerialPort recieves data.
    If you are using the DataReceived event handler it might fire several times before you have a 'line.' Does the data coming in have a specific format?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Stopwatches not in Sync?????

    Yes, it sends as a 12 character string, where the first 4 characters are the voltage of the load cell, the second 4 are the voltage of one of the batteries, and the last 4 are the voltage of the final battery. Looks like this: 061206120612, splits into 0612 0612 0612, which translates into 2.5V, 2.5V, and 2.5V. The numbers are from 0 to 1024, based on a voltage range from 0V to 5V

  19. #19
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Stopwatches not in Sync?????

    Assuming that the handler isn't doing anything else, try it like this

    Code:
        Private Sub SerialPort1_DataReceived(sender As Object, _
                                             e As IO.Ports.SerialDataReceivedEventArgs) _
                                         Handles SerialPort1.DataReceived
            If SerialPort1.BytesToRead > 12 Then
                'call to GetData here
            End If
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width