📄 software timed loops using wait (ms) and wait until next ms multiple - developer zone - national instruments.mht
字号:
<UL>
<LI><A href=3D"http://zone.ni.com/devzone/cda/tut/p/id/3166">Counter =
Control=20
<B>and</B> the <B>Wait</B> Control Code to Control Loop Rates with =
Sleep=20
Mode</A><BR>
<LI><A href=3D"http://zone.ni.com/devzone/cda/tut/p/id/7592">Tutorial: =
Timing,=20
Shift Registers, <B>and</B> Case Structures</A><BR></LI></UL></DIV>
<H2>=E7=9B=B8=E5=85=B3=E9=93=BE=E6=8E=A5 - Products and Services</H2>
<DIV class=3Dfnavbox>
<UL>
<LI><A =
href=3D"http://sine.ni.com/nips/cds/view/p/lang/zhs/nid/206683">NI=20
LabVIEW On-Demand Training Topics</A><BR>
<LI><A =
href=3D"http://sine.ni.com/nips/cds/view/p/lang/zhs/nid/13264">NI=20
<B>VI</B> logger</A><BR></LI></UL></DIV></DIV></DIV></DIV>
<DIV id=3Dprimarycontentcontainer>
<DIV class=3Dnoindex>
<H1>Software Timed Loops using Wait (ms) and Wait Until Next ms =
Multiple</H1>
<DIV class=3Ddocdetails>
<DIV class=3Dratings>14 =E8=AF=84=E7=BA=A7 | <STRONG>4.50</STRONG> out =
of 5</DIV><!-- Read in other language section --><A=20
href=3D"javascript:window.print()"><IMG class=3Dbottomalign height=3D13 =
alt=3D""=20
src=3D"http://zone.ni.com/images/global/us/icons/print.gif" width=3D18 =
border=3D0>=20
=E6=89=93=E5=8D=B0</A> </DIV></DIV>
<DIV id=3Dpagearea>
<DIV class=3Dnoindex>
<H3>=E6=A6=82=E8=A7=88</H3></DIV>
<P>This document will discuss using two VIs, <B>Wait (ms)</B> and =
<B>Wait Until=20
Next ms Multiple</B>, to give software timing to loops. This document =
assumes=20
that the LabVIEW RT Engine is the execution platform. In our discussion =
we will=20
assume that the VI is running in a time critical thread and that it is =
in the=20
only time critical loop in the system. For a complete discussion of =
priorities=20
and threads in LabVIEW RT, follow the link at the bottom of the page.=20
<BR><BR></P>
<DIV class=3Dnoindex>
<H3>=E7=9B=AE=E5=BD=95</H3>
<OL>
<LI><A href=3D"http://zone.ni.com/devzone/cda/tut/p/id/4120#toc0">Wait =
Until=20
Next ms Multiple</A>
<LI><A =
href=3D"http://zone.ni.com/devzone/cda/tut/p/id/4120#toc1">Using Wait=20
Until Next ms Multiple to Time a Loop in Time Critical Priority</A>
<LI><A href=3D"http://zone.ni.com/devzone/cda/tut/p/id/4120#toc2">Wait =
(ms)</A></LI></OL></DIV><A name=3Dtoc0></A>
<H2>Wait Until Next ms Multiple</A><BR></H2>
<DIV align=3Dcenter>
<P><IMG height=3D67 alt=3D""=20
src=3D"http://zone.ni.com/cms/images/devzone/tut/a/1b136ce2506.gif"=20
width=3D329></P></DIV><BR>When Wait Until Next ms Multiple is called, it =
will=20
return, or finish, when the millisecond timer value of the operating =
system is a=20
multiple of the <B>millisecond multiple</B> input. For instance, if the =
VI is=20
called with a <B>millisecond multiple</B> input of 10 ms, and the =
millisecond=20
timer value is 112 ms, then the VI will only wait 8 more millisecond, at =
which=20
time the millisecond timer value will be 120 ms, which is a multiple of =
10 ms.=20
Using this VI to time a loop means that the loop will be synchronized =
with the=20
operating system millisecond timer value multiples. If called in =
parallel with=20
other code in the same loop, the loop will have the period of =
<B>millisecond=20
multiple</B>. This will not be the case if the other code takes longer =
than the=20
<B>millisecond multiple</B>. <A name=3Dtoc1></A>
<H2>Using Wait Until Next ms Multiple to Time a Loop in Time Critical=20
Priority</A><BR></H2><BR>You must use this VI carefully in the time =
critical=20
priority of LabVIEW Real-Time. Remember that if any VI sleeps in a time =
critical=20
thread, then the entire thread sleeps. For this reason, you should =
control when=20
the <B>Wait Until Next ms Multiple</B> executes, rather than placing the =
VI in=20
parallel with other code. To understand why, let's look at an example =
control=20
loop. In this example, we will read an analog input and then write an =
analog=20
output. The desired timing of the system is shown in Figure 1. The =
vertical=20
arrows represent 100 millisecond multiples. <BR>
<DIV align=3Dcenter><IMG height=3D96 alt=3D""=20
src=3D"http://zone.ni.com/cms/images/devzone/tut/a/1b136ce2507.gif"=20
width=3D423><BR>Figure 1: Desired timing of control system</DIV>
<DIV align=3Dcenter>Our first attempt to solve this problem is a simple =
loop with=20
<B>Wait Until Next ms Multiple</B> (see Figure 2).<BR><IMG height=3D113 =
alt=3D""=20
src=3D"http://zone.ni.com/cms/images/devzone/tut/a/1b136ce2508.gif"=20
width=3D265><BR>Figure 2: First attempt at timed loop</DIV><BR>In this =
first=20
attempt, we do not dictate when in the sequence the <B>Wait Until Next =
ms=20
Multiple</B> executes. The behavior of this loop will differ depending =
on when=20
the <B>Wait Until Next ms Multiple</B> gets executed. If the <B>Wait =
Until Next=20
ms Multiple</B> executes first, then the analog input will be followed=20
immediately by the analog output. This behavior is usually desired, but =
is not=20
guaranteed in the example above. If the <B>Wait Until Next ms =
Multiple</B>=20
executes immediately after the analog input, then the analog output will =
not be=20
able to execute until the <B>Wait Until Next ms Multiple</B> is =
finished. Again,=20
this is because when any portion of a time critical priority VI sleeps, =
the=20
entire thread sleeps. The resulting timing would be as shown below, in =
Figure 3.=20
This behavior is usually not desired.=20
<DIV align=3Dcenter><IMG height=3D96 alt=3D""=20
src=3D"http://zone.ni.com/cms/images/devzone/tut/a/1b136ce2509.gif"=20
width=3D446><BR>Figure 3: Wait statement executes after =
AI<BR></DIV><BR>Instead of=20
placing the <B>Wait Until Next ms Multiple</B> in parallel with other =
code, we=20
can force it to execute in a specific sequence by using data flow as=20
demonstrated below. With the example below, the analog input will occur=20
immediately after the millisecond multiple, and the output will occur=20
immediately after that. <BR>
<DIV align=3Dcenter><IMG height=3D167 alt=3D""=20
src=3D"http://zone.ni.com/cms/images/devzone/tut/a/1b136ce2510.gif"=20
width=3D287><BR>Figure 4: Controlling the sequencing of events <A =
name=3Dtoc2></A>
<H2>Wait (ms)</A><BR></H2></DIV>
<DIV align=3Dcenter><IMG height=3D69 alt=3D""=20
src=3D"http://zone.ni.com/cms/images/devzone/tut/a/1b136ce2511.gif"=20
width=3D330><BR></DIV><BR>When <B>Wait (ms)</B> is called, it will =
return or=20
finish, after the time in milliseconds specified in the <B>milliseconds =
to=20
wait</B> input. For instance, if the millisecond timer value is 112 ms =
when the=20
VI is called, and <B>milliseconds to wait</B> equals 10, then the VI =
will finish=20
when the millisecond timer value equals 122 ms. If called in parallel =
with other=20
code, in priorities other than time critical, this means that the loop =
will have=20
the period of <B>milliseconds to wait</B> unless the other code takes =
longer=20
than the <B>milliseconds to wait</B>.<BR><BR>In LabVIEW Real-Time, it is =
not=20
recommended to use this VI in parallel with anything in time critical =
priority.=20
If the wait executes first, the whole thread will sleep until the =
<B>Wait=20
(ms)</B> is finished, and the code in parallel will not execute until =
the=20
<B>Wait (ms)</B> is finished. The resulting loop period will be the code =
execution time plus the <B>milliseconds to wait</B> time. <BR><BR>This =
VI does=20
not provide reliably consistent timing, even in normal priority VIs, =
since=20
jitter is accumulative. <BR><B>Related Links:</B><BR><A=20
href=3D"http://zone.ni.com/devzone/cda/tut/p/id/4324">Timing =
deterministic control=20
loops</A><BR><A=20
href=3D"http://zone.ni.com/devzone/cda/tut/p/id/4954">Understanding =
priorities in=20
LabVIEW Real-Time applications</A><BR>
<DIV class=3Dnoindex>
<DIV class=3Ddocdetails>
<DIV class=3Dratings>14 =E8=AF=84=E7=BA=A7 | <STRONG>4.50</STRONG> out =
of 5</DIV><!-- Read in other language section --><A=20
href=3D"javascript:window.print()"><IMG class=3Dbottomalign height=3D13 =
alt=3D""=20
src=3D"http://zone.ni.com/images/global/us/icons/print.gif" width=3D18 =
border=3D0>=20
=E6=89=93=E5=8D=B0</A> </DIV></DIV>
<H3>=E8=AF=BB=E8=80=85=E7=95=99=E8=A8=80 | <A=20
href=3D"http://zone.ni.com/apps/utf8/nidz_display_comments.create_comment=
?p_title=3DSoftware+Timed+Loops+using+Wait+%28ms%29+and+Wait+Until+Next+m=
s+Multiple&p_doc_id=3D1B136CE22D66D010VGNVCM100000940AA482RCRD&p_=
doc_type=3DTUT&p_url=3Dhttp://zone.ni.com/devzone/cda/tut/p/id/4120&a=
mp;p_doc_url_id=3D4120"=20
rel=3Dnofollow>=E6=8F=90=E4=BA=A4=E7=95=99=E8=A8=80</A> <SPAN =
class=3Dcta>=C2=BB</SPAN></H3>
<P></P>
<DIV class=3Ddottedline> </DIV>
<DIV class=3Dnoindex>
<DIV class=3Dlegalnotice>
<P><STRONG>=E6=B3=95=E5=BE=8B=E6=9D=A1=E6=AC=BE</STRONG><BR>=E6=9C=AC=E6=95=
=99=E7=A8=8B=E7=94=B1National =
Instruments=E5=85=AC=E5=8F=B8=EF=BC=88=E7=AE=80=E7=A7=B0=E2=80=9CNI=E2=80=
=9D=EF=BC=89=E5=BC=80=E5=8F=91=E3=80=82 =E5=B0=BD=E7=AE=A1National=20
Instruments=E5=8F=AF=E4=B8=BA=E8=AF=A5=E7=A8=8B=E5=BA=8F=E6=8F=90=E4=BE=9B=
=E6=8A=80=E6=9C=AF=E6=94=AF=E6=8C=81=EF=BC=8C=E4=BD=86=E6=98=AF=E8=AF=A5=E6=
=8C=87=E5=8D=97=E7=9A=84=E5=86=85=E5=AE=B9=E5=B9=B6=E9=9D=9E=E5=AE=8C=E5=85=
=A8=E9=80=9A=E8=BF=87=E6=B5=8B=E8=AF=95=E5=92=8C=E9=AA=8C=E8=AF=81=EF=BC=8C=
NI=E4=B8=8D=E4=BB=A5=E4=BB=BB=E4=BD=95=E6=96=B9=E5=BC=8F=E4=BF=9D=E8=AF=81=
=E5=85=B6=E8=B4=A8=E9=87=8F=EF=BC=8C=E4=B9=9F=E4=B8=8D=E4=BF=9D=E8=AF=81=E7=
=9B=B8=E5=85=B3=E4=BA=A7=E5=93=81=E6=88=96=E9=A9=B1=E5=8A=A8=E7=A8=8B=E5=BA=
=8F=E7=9A=84=E6=96=B0=E7=89=88=E6=9C=AC=E5=87=BA=E7=8E=B0=E6=97=B6=E7=BB=A7=
=E7=BB=AD=E4=B8=BA=E5=85=B6=E6=8F=90=E4=BE=9B=E6=8A=80=E6=9C=AF=E6=94=AF=E6=
=8C=81=E3=80=82=E6=9C=AC=E6=95=99=E7=A8=8B=E4=BB=85=E4=BB=A5=E5=85=B6=E2=80=
=9C=E7=8E=B0=E7=8A=B6=E2=80=9D=E5=90=91=E7=94=A8=E6=88=B7=E6=8F=90=E4=BE=9B=
=EF=BC=8C=E6=95=99=E7=A8=8B=E6=B2=A1=E6=9C=89=E4=BB=BB=E4=BD=95=E6=8B=85=E4=
=BF=9D=E3=80=82=E6=95=99=E7=A8=8B=E4=BD=BF=E7=94=A8=E5=8F=97ni.com=E7=BD=91=
=E7=AB=99=E4=B8=8A=E3=80=8A=E4=BD=BF=E7=94=A8=E6=9D=A1=E6=AC=BE=E3=80=8B=E7=
=9A=84=E7=BA=A6=E6=9D=9F=E3=80=82=20
(<A=20
href=3D"http://ni.com/legal/termsofuse/unitedstates/us/">http://ni.com/le=
gal/termsofuse/unitedstates/us/</A>)</P></DIV></DIV></DIV></DIV>
<DIV class=3Dnoindex>
<DIV class=3Dclearfloat> </DIV></DIV></DIV>
<DIV class=3Dnoindex></DIV></TD></TR></TABLE></DIV>
<DIV class=3Dclearfloat> </DIV>
<DIV></DIV>
<DIV class=3Dnoindex id=3Dfooter><A =
href=3D"http://www.ni.com/profile/zhs/"=20
rel=3Dnofollow>=E6=88=91=E7=9A=84=E4=B8=AA=E4=BA=BA=E4=BF=A1=E6=81=AF</A>=
| <A href=3D"http://www.ni.com/rss/" rel=3Dnofollow>RSS</A>=20
| <A href=3D"http://www.ni.com/legal/privacy/unitedstates/us/"=20
rel=3Dnofollow>=E9=9A=90=E7=A7=81=E6=9D=83=E8=AF=B4=E6=98=8E</A> | <A =
href=3D"http://www.ni.com/legal/"=20
rel=3Dnofollow>=E6=B3=95=E5=BE=8B=E4=BF=A1=E6=81=AF</A> | <A =
href=3D"http://sine.ni.com/apps/utf8/nicc.call_me"=20
rel=3Dnofollow>=E8=81=94=E7=B3=BBNI</A> <SPAN class=3Dcopyright>=C2=A9 =
2008 National Instruments=20
Corporation. All rights reserved.</SPAN> =
|
<SCRIPT language=3Djavascript type=3Dtext/javascript>
var subject =3D "Link to National Instruments Web Site";
var bodytext =3D window.location.href;
document.write('<a href=3D\"mailto:?subject=3D' + subject + '&body=3D' + =
bodytext + '\">');=20
document.write('<img src=3D"/images/global/us/email.gif" border=3D"0" =
alt=3D"转发该页"> =
转发该页</a>');
</SCRIPT>
</DIV>
<DIV></DIV>
<SCRIPT language=3DJavaScript=20
src=3D"http://zone.ni.com/javascript/analysis/us/tag-footer.js"=20
type=3Dtext/javascript></SCRIPT>
<DIV></DIV></BODY></HTML>
------=_NextPart_000_0000_01C952D8.5FD9E9A0
Content-Type: image/gif
Content-Transfer-Encoding: base64
Content-Location: http://zone.ni.com/images/global/us/logo.gif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -