Message Timing
Timing network communication is an important issue in computer science. If information issent too quickly, it can be lost. Imagine, for example, a brick sending information every tenth
of a second to a second brick that is processing received messages every half second. In 1.4
seconds, give or take, the message queue will over
ow and messages will be lost. The careful
programmer must take this into account and insure that received messages are processed
faster than sent messages. Listing 10.1 shows two programs, a send and a receive. The send
program transmits the sonar sensor value to the receiving program at a rate of 10 readings
per second. The receiving program processes the values at a rate of 20 readings per second.
Notice the use of the three entities summarized in Table 10.2 in the Receive program.
Because messages are being processed at a faster rate than they are transmitted, often the
message queue will be empty. The Receive program uses the bQueuedMsgAvailable()
function to see if there is a new message. If not, it does nothing. If there is a message, the
data is accessed using the messageParm[] array and then cleared using the ClearMessage()
function.
Send
# pragma config ( Sensor , S1 , Sonar , sensorSONAR )
task main () {
int x1 , x2 , x3;
x2 =0; // dummy
x3 =0; // dummy
while ( true ) {
x1 = SensorValue [ Sonar ];
sendMessageWithParms (x1 ,x2 ,x3 );
wait1Msec (100); // wait one -tenth of a second
}
}
Receive
task main () {
while ( true ) {
if ( bQueuedMsgAvailable () ) {
eraseDisplay ();
nxtDisplayCenteredTextLine (3,"%d",
messageParm [0]);
ClearMessage ();
}
wait1Msec (50); // wait one -twentieth of a second
}
}
Listing 10.1: This pair of programs demonstrates the periodic transmission of sonar data
from one brick to another. The rst program transmits data at 10 readings per second. The
second program processes data 20 times per second.
Notice also, in the Send program, the dummy variables x2 and x3. In this particular
situation, only one of the three available integers is needed. We set the other two parameters
to zero and ignore them.
No comments:
Post a Comment