Go Down

Topic: Messenger library (Read 28389 times) previous topic - next topic

tof

#30
Dec 07, 2009, 07:26 pm Last Edit: Dec 07, 2009, 07:27 pm by tof Reason: 1
Hi,

you can try the following code (I can not test it):

Code: [Select]
#include <MegaServo.h>
#include <Messenger.h>

#define NBR_SERVOS     12        // the number of servos, up to 48 for Mega, 12 for other boards
#define FIRST_SERVO_PIN 2

MegaServo myServos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boards

Messenger message = Messenger();


// Define messenger function
void messageCompleted() {
 
  int i = 0;
 while ( message.available() && i < NBR_SERVOS ) {
  myServos[i].write(message.readInt());
  i++;
 }
 
}

void setup()
{
 Serial.begin(115200);
 message.attach(messageCompleted);
 
 
 for(int i=0; i < NBR_SERVOS; i++)
   myServos[i].attach(FIRST_SERVO_PIN +i);  
}


void loop()
{
 
 while ( Serial.available( ) ) message.process(Serial.read( ) );
 
 
}  
 


From you host software, send something like the following ascii message (they are not bytes but string representations of the numbers):
"123 84 39 128 274 50 100 543 83 43 100 101"

Where each number corresponds to the position for the servo at that index (first number for first servo, etc). Do not forget to terminate the message with a carriage return!
Thomas Ouellet Fredericks

swalehm

Hi..

its me again.. i mhaving a different problem this time.. I am trying to control a couple servos through processing.. i send an ascii string as follows to the arduino "int1 int2 int3 int4 int5 int6" and then a carriage return. i receive them and store them in an array i display the array on the LCD.. the trouble is that every so often the first elements and other elements get mixed up and that cause my servos to twitch.. do you know of a fool proof method of receivin an array of integers that would guarantee that the first number received is stored in the first element in the array.??

Thanks
Swalehm

yair

Swamlehm, i suggest you take a look at this code here.
its not using any lib for communication but rather let you take care of protocol.
it might seem a bit to "cody" at first but it lets you set a value to an array index with a simple
"90a will write a value of 90 to the first servo,   180b will write 180 to the second servo"
i guess you can also adapt messanger to use this.

if you want to continue sending an array its best not to swamp the buffer with to many calls (slow the pace, simple) or devise a start/stop bit rutine to handle only valid packets (hardish).

tof

swalehm, please post your code. There can be many reasons for this behavior and there are many ways you can implement a simple integrity check. Please aslo post the software and OS you are using.
Thomas Ouellet Fredericks

teomat

Fantastic library! I'm using it to exchange data with a servocontroller class. In order to set encoder ratio, from impulses to real world coordinates, I added this simple extension method to the class.


in messenger.cpp
Code: [Select]

double Messenger::readDouble() {
     if (next()) {
           dumped = 1;
           return atof(current); // atof for double instead of atoi for int variables
     }
     return 0;
}



in messenger.h
Code: [Select]

 double readDouble();


Thanks for your great job!

jeremy a

hello,

just wondering if you could tell me the paths for the files in the zip into my Processing sketchbook library and any other?

would like to switch to this

thanks
jeremy

nduddy

Great library, thanks very much for sharing this...

I've added a few things in my own class CmdMessenger which extend from Messenger. Had to make a few small changes to the origional Messenger which I've included here along with my CmdMessenger class if its useful to anyone else.

Basicly with CmdMessenger sub class you can attach more than 1 messenger handler, which get called according to the first string (cmd) in a message. If a valid message for the command hasn't been attached then the single default message handler of Messenger is called.

http://www.arduino.cc/playground/uploads/Code/CmdMessenger.zip

The zip file contains the orgional Messenger with my small changes, the new CmdMessenger class and two sample programs (one runs on Arduino the other on a TouchShieldSlide) connected via hardware Serial1. If your using softwareSerial, which dosn't have the Serial.available() method then you will need to change the feedinSerialData() mehod.



Neil

Christopher Overstreet

Not very experienced with Java.  I am a maxmsp whiz.  This library seems like what I want (firmata is too bloated), but maybe it can't do this?

I just want to route messages, like in the example but parse numbers that follow the message name.  ie  send a value over max-messanger like this:

dimmer1 $1
dimmer2 $1

Sorry if this is obvious.

I started using the library with the message callback function similar to the one from the examples (CheckString):

Code: [Select]
void messageCompleted() {
 // This loop will echo each element of the message separately
 while ( message.available() ) {
   if ( message.checkString("on") ) {
     digitalWrite(13,HIGH);
   } else if ( message.checkString("off") ) {
     digitalWrite(13,LOW);
   }
 }
}


I found out that there is one danger with this. It you sent any message that doesn't match the strings which you are checking against, you get stuck in an endless while loop, because the message is never removed from the stack.

This problem became apparent when i was using 2 modules communicating with XBee on the same network. They both listen to different messages and so every message would get either one of hem to hang.

I solved this by simply adding an
Code: [Select]

else{
message.readInt();
}

at the end of the function. This way if the message doesn't match with any of the strings, it is removed and the loop continues with the next.

It would be helpful if this caveat is mentioned in the examples. Maybe  it would be elegant to have an explicit method for skipping an element in the message stack?






I'm trying to use the Messenger library in Processing.

First of all I'm missing the string functions, I guess they're just not implemented yet?

So I want to match strings, or at least combinations of ascii characters manually. It seems so simple but I cannot figure it out.

I want to match something like "BFOO". The only way to access these characters seems to be readChar(). The first call returns 'B', but then the rest seems to be thrown away, and I don't know how to get to the remaining characters.

The reference says:
Quote

char readChar()

Returns the element as a character. If the character is part of a word, the whole word is removed from the completed message.


So this seems to be correct, but I don't understand the idea behind it. How is that useful, and how can I access those other characters?


steven-b

I'm having trouble sending out a array of values from Processing to Arduino. Can anyone guide on how to do this?

Can this library even do this?

Thanks!

tof

As usual, could you be more specific and provide an example?
What are the specifics or the array sent? Size, update speed, data size, etc...
Thomas Ouellet Fredericks

basuraman

hello!

i am trying to use Messenger to receive a 2d array from my PC. the message looks like this:

2,3,4 32,0,0, 3,16,16....<<60 more sets like this>>...32,32,32 [crg rtrn]

Messenger reads this in copyString seems to work, but when i try to access the stored Message as an array, i can only seem to access it as one long character string.

i thought the [space] character broke the incoming string into Elements (of an array) - is this not the case?

if it IS the case...how do i go about finding the individual space-separated groups once i copy the incoming message?

or am i missing something? maybe i have to build my own array of character arrays?

any thoughts? i can post an example if it would help..

thanks!
-josh

tof

Quote
i thought the [space] character broke the incoming string into Elements (of an array) - is this not the case?


The space character does break the string into elements... but your example "2,3,4 32,0,0, 3,16,16.." shows commas and not strings as separators. You can configure Messenger to work with commas instead if you wish.

Even if you sent  "2 3 4 32 0 0 3 16 16...", you could not use copyString to retrieve the individual elements (because copyString copies everything as a string as its name implies). You would  use readInt instead to read each integer separately.
Thomas Ouellet Fredericks

basuraman

ah! yes - sorry for the confusion ...

i suppose some background of my larger project would be helpful...

i've built an RGB LED Matrix and found some code to write colors to each of 64 LEDs. it works great, but now i would like to send constantly updated color values from my PC to display on the matrix LEDs. i though it would be best to send them as a group of 64 at a time - a whole '64 pixel frame' if you will..

SO, my message actually contains spaces AND commas:
2,3,4  -space-  32,0,0 -space-  etc.  

so each element is supposed to be a 3 value set of integers. once i have these sets in an array, i want to parse them using the commas and probably strtok_r.  so, basically i am hoping to store a 2 dimensional array from Messenger. still possible?

thanks!

Go Up