View unanswered posts | View active topics It is currently Thu Apr 17, 2014 11:20 pm






Reply to topic  [ 4 posts ] 
Help needed optimising messaging code 
Author Message
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
Post Help needed optimising messaging code
Hi, I was working on some code for sending messages between a couple of robots, but kept losing messages. I have gone back to basics and written two bits of messaging code so I can work on understanding how messaging actually works. Based on the messaging code in the RobotC API guide I have tried to add some basic handshaking to ensure all messages are received, but it is running pretty slowly.

Without handshaking (ie the code from the API guide) my For loop runs in less than 1 second. With the handshaking it takes about 9 seconds. How can I speed this code up?

I did try googling messaging protocols, but I couldn't seem to find the right keywords.

Sender code:

Code:
// Project: Messaging - sender
// Function: test messaging
// Author: Mike McFarlane 21-09-12
// Version: 0-6
// Status: working
// v0-1:
// v0-3: try different strategy of clearing messages in buffer. Result: This runs faster.
// v0-4: finish for loop and send end of task message
// v0-5: speed up optimisation. Takes about 19 s for a 1 to 25 for loop. Can't reduce delays as programs starts missing messages.
// v0-6: speed up optimisation by waiting for next message rather than clearing queue. Result: about 8s for a 1 to 25 for loop

task main()
{

   ClearMessage();

   for(int i = 1; i <= 25; i++)
   {
      nxtDisplayBigTextLine (2, "Tx = %d", i);
      sendMessageWithParm(i, 0, 0);

      while (messageParm[0] != i)
      {
         //wait
         sendMessageWithParm(i, 0, 0);
         ClearMessage();
         wait1Msec(5);
      }
      ClearMessage();
      wait1Msec(30);  // Donít send messages too frequently.
   }
   //stop
   eraseDisplay();
   /*while (bQueuedMsgAvailable())
   {
   word temp;
   ClearMessage();
   temp = message;
   }*/

   sendMessageWithParm(-101, 0, 0);
   while (messageParm[0] != -101)
   {
      sendMessageWithParm(-101, 0, 0);
      nxtDisplayBigTextLine (4, "Wait 4 End",);
      wait1Msec(5);
      ClearMessage();
   }
   eraseDisplay();
   nxtDisplayBigTextLine (2,"Finished!",);
   wait1Msec(1000);
   StopAllTasks();
}



Receiver code:

Code:
// Project: Messaging - receiver
// Function: test messaging
// Author: Mike McFarlane 21-09-12
// Version: 0-6
// Status: working
// v0-1:
// v0-3: try different strategy of clearing messages in buffer. Result: This runs faster.
// v0-4: finish for loop and send end of task message
// v0-5: speed up optimisation. Takes about 19 s for a 1 to 25 for loop. Can't reduce delays as programs starts missing messages.
// v0-6: speed up optimisation by waiting for next message rather than clearing queue. Result: about 8s for a 1 to 25 for loop


task main()
{

   ClearMessage();

   while (messageParm[0] != -101)
   {
      while (messageParm[0] == 0)
      {
         //wait
         ClearMessage();
         wait1Msec(5);
      }
      nxtDisplayBigTextLine (2, "Rx = %d", messageParm[0]);
      sendMessageWithParm (messageParm[0], 0, 0);
      ClearMessage();
      wait1Msec(30);
   }
   //stop
   eraseDisplay();
   wait1Msec(100);
   sendMessageWithParm (messageParm[0], 0, 0);
   nxtDisplayBigTextLine (2,"Finished!",);
   wait1Msec(1000);
   StopAllTasks();

}



Thanks.


Fri Sep 21, 2012 11:13 am
Profile
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
Post Re: Help needed optimising messaging code
I decided to plot the messages to the NXT display as a sort of oscilloscope and see there are plenty messages with value zero. I tweaked the timings a bit and got the run time down to about 5-6seconds, but it still seems very slow with the handshake. Any ideas, or suggestions for reading material?

I also tried reducing the task priority of the Display task with 'StartTask (Display, 5)' so it ran with a lower priority than the messaging, but this bricked the NXT every time!

Sender:
Code:
#define LongTime 30      // time in ms
#define ShortTime 10   // time in ms

//global variable
int MessageArray [100];

task Display ()
{
   int CurrentMessage = 0;

   while (true)
   {
      eraseDisplay ();
      //move the values down the queue
      for (int i = 98; i >= 0; i--)
      {
         MessageArray [i+1] = MessageArray [i];
         nxtDrawLine ((i+1), 0, (i+1), MessageArray [i+1]);
      }
      CurrentMessage = messageParm[0];

      MessageArray[0] = (CurrentMessage * 2);   // scaling line to fit display

      // set an update rate, 50ms creates a nice graph with good update speed
      wait1Msec (20);
   }
}

task main()
{
   ClearTimer(T1);
   ClearMessage();
   StartTask (Display); // start display task with slightly lower priority than main task (default 7)

   for(int i = 1; i <= 50; i++)
   {
      // nxtDisplayBigTextLine (2, "Tx = %d", i);
      sendMessageWithParm(i, 0, 0);

      while (messageParm[0] != i)
      {
         //wait
         sendMessageWithParm(i, 0, 0);
         ClearMessage();
         wait1Msec(LongTime); // needs to run slightly slower than receiver so 35ms about right, 20ms ok but variable.
      }
      ClearMessage();
      wait1Msec(ShortTime);  // Donít send messages too frequently.
   }

   //stopping
   StopTask(Display);
   eraseDisplay();
   sendMessageWithParm(-101, 0, 0);
   // make sure the stop signal -101 is definetely received
   while (messageParm[0] != -101)
   {
      sendMessageWithParm(-101, 0, 0);
      nxtDisplayBigTextLine (4, "Wait 4 End",);
      wait1Msec(ShortTime);
      ClearMessage();
   }
   eraseDisplay();
   nxtDisplayBigTextLine (2,"Finished!",);
   nxtDisplayBigTextLine (4, "T1: %d", time1[T1]);
   wait1Msec(1000);
   StopAllTasks();
}



Receiver:
Code:
#define LongTime 30      // time in ms
#define ShortTime 10   // time in ms

task main()
{

   ClearMessage();

   // keep looping while the stop signal of -101 is not received
   while (messageParm[0] != -101)
   {
      /*if (messageParm[0] != 0) // do nothing if message is 0
      {
         nxtDisplayBigTextLine (2, "Rx = %d", messageParm[0]);
         sendMessageWithParm (messageParm[0], 0, 0);
         ClearMessage();
         wait1Msec(LongTime); // 30ms about optimal, less time eg 10ms can give faster result, but result variable between 4 and 6s.
         }*/
         while (messageParm[0] == 0)
            {wait1Msec(ShortTime);}
         nxtDisplayBigTextLine (2, "Rx = %d", messageParm[0]);
         sendMessageWithParm (messageParm[0], 0, 0);
         ClearMessage();
         wait1Msec(LongTime);
   }

   //stopping
   eraseDisplay();
   wait1Msec(100);
   sendMessageWithParm (messageParm[0], 0, 0);
   nxtDisplayBigTextLine (2,"Finished!",);
   wait1Msec(1000);
   StopAllTasks();

}



Thu Sep 27, 2012 4:31 am
Profile
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
Post Re: Help needed optimising messaging code
Finally figured out a solution to this. RTFM as it appears that most of what I am trying to solve is already by RobotC, time to read the API docs:-)


Fri Oct 12, 2012 7:03 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3105
Location: Rotterdam, The Netherlands
Post Re: Help needed optimising messaging code
The Fine Materials are often a great source of information :)

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Oct 12, 2012 7:26 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.