LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to save data every 5 ms

Hi guys, I am reading these two sensors from an integrated cable with its own communication protocol. I am using a stream_send option where i simply give just once at the beginning the initial command to the cable, 0x01 0x1E 0x00 0x45. The sensors start giving me their data through a constant stream of 8 bytes, without me having to make the request each time i want a data. The last two bytes of these 8 bytes are always the same and are a sort of tail. Since i don't know the specific frequency of this stream of data, and i would like to sample my data every 5 ms (200 Hz) and i cannot control the stream frequency, i thought that i could receive the data through this stream, but enable the saving just every 5 ms. For some reason my labview does not work. If i save everything that i get from the serial communication it works, if i try to count and save every 5 ms it doesn't work. Can someone help me understand what i am doing wrong? Thank you every one

0 Kudos
Message 1 of 12
(1,829 Views)

There's quite a few things wrong with your code, mostly style issues. Wire bends, right to left wiring, etc.

 

But the simples part might be what's causing you trouble:

wiebeCARYA_0-1640703732490.png

Are you sure that times is called every ms? If it returns 6, 11, 14, 18, etc, the output might never become 0. You need to keep the previous save time, and compare the current time with the last time + interval time.

wiebeCARYA_1-1640704274345.png

 

Are you sure that output ever gets 0? The integer is converted to a double, and that causes rounding. The output might be 0.00000000001. That 5 needs to be an integer. You probably be OK in this case, but you're walking on thin ice. And without good reason.

0 Kudos
Message 2 of 12
(1,817 Views)

@dave97 wrote:

The last two bytes of these 8 bytes are always the same and are a sort of tail.


If the last two bytes are always the same then wouldn't it be simpler to just save when you receive those bytes?

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 3 of 12
(1,803 Views)

wiebe@CARYA wrote:

There's quite a few things wrong with your code, mostly style issues. Wire bends, right to left wiring, etc..


I could not agree more!

 

  • Blatant overuse of local variables causing potential race conditions (example: you read "ON" from a local variable while the control terminal sits right there next to it disconnected. Just wire from the terminal and delete the local)
  • No, determination of "first" does not require an indicator and three(!!) local variables.
  • Avoid overlapping objects on the front panel, they can cause extra redraw penalties (e.g. your plot legends)
  • No, converting an 8 byte string into two DBL numerics does not require a postcard full of code! Correctly done, that will fit on a postage stamp!
  • "Index array" is resizable. You don't need to wire indices if you want the elements in order.
  • "Insert into array" is not the correct function to append a few arrays. "built array (concatenate mode)" would be correct.
  • Creating/destroying file refs is NOT the correct way to save/not save and a 20ms wait won't protect you from anything.
  • Byte order is meaningless if you are writing strings. Why all these long enum diagram constants cluttering the code?
  • Where do you ensure that "stop" is false next time the program starts? (Why not use latch action with the terminal wired as is and a stop event in the lower loop?)
  • Why do you enable/disable visa events if you never wait on one?
  • Your U8 array diagram constants should either show all elements or a scrollbar. It is confusing if some elements are not visible.
  • Two sequential sequence structures before the toplevel loop is one two many. You can put everything inside one.
  • etc. etc. etc!
  • This is just the tip of the iceberg!
0 Kudos
Message 4 of 12
(1,792 Views)

Thank you for your reply. I see your point, it could be, i thought that tick counter returned the ms value of the timer. How can I store the old value of my timer until the new value is 5 ms higher than the old value? I can't figure out a method.

0 Kudos
Message 5 of 12
(1,780 Views)

Hi Dave,

 


@dave97 wrote:
How can I store the old value of my timer until the new value is 5 ms higher than the old value? I can't figure out a method.

You should carefully read message#2 again and look at the images in that message!

Ever heard of shift registers?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 12
(1,775 Views)

@GerdW wrote:

Hi Dave,

 


@dave97 wrote:
How can I store the old value of my timer until the new value is 5 ms higher than the old value? I can't figure out a method.

You should carefully read message#2 again and look at the images in that message!

Ever heard of shift registers?


In retrospect, that code wouldn't work.

 

The ms count is a U32 so 0 + 5 could take a long time to get the first trigger. The compare won't work well... Once the count wraps to 0, it will again take a long time before prev + 5 > current...

 

But the shift register is the key.

 

Something like this:

wiebeCARYA_1-1640770413748.png

 

The subtract results in a valid dT, despite (or because) both values are unsigned.

 

If the U32 wrapped, you'd get e.g. 5 - (max U32-5) = 10

 

Of course you can save the U32 drama and use Get Data/Time In Seconds or High Resolution Relative Seconds.vi

0 Kudos
Message 7 of 12
(1,744 Views)

I tried the same configuration, but I think it is possible that the difference never get exactly equal to 5 as you said in your first answer. When i tried what you showed me my file ends up to be empty. So after the difference I had to put a greater compared with 5. Now the file doesn't end up empty, but the frequency is not 200 Hz, it is like 10 Hz. In 10 seconds i get 100 samples. I don't get what is wrong..

0 Kudos
Message 8 of 12
(1,730 Views)

As far as speeding up your code, there are a few things to look at.

 

1. Your data processing is horribly inefficient.  There is no reason at all to convert into a Boolean Array, combine, and convert back to numbers.  Just use Unflatten From String on your original string data and you can go straight to your integer values before doing the math on those values.

 

2. You may need to go with a Producer/Consumer setup in order to separate the acquisition, processing, and logging into 2 loops.  File IO tends to be slow, so this might be a way to gain up the processing rate.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 9 of 12
(1,720 Views)

Thank you for your suggestions, i changed the convertion into Boolean Array with your method that for sure is more efficient. I am looking at the producer/consumer setup now. 
However my problem remains the same, i don't understand how to save my data every 5 ms. I am sure that it is not a problem of speeding up my code, because i succeeded in saving my stream of data with the original frequency that is much higher than 200 Hz. 

0 Kudos
Message 10 of 12
(1,701 Views)