variable size array of uint8 as msg field has strange values
Hi everybody, i'm using MKR1300 arduino board and rosserial. I have this msg type:
uint32 id
uint8 lenght
uint8[] value
uint64 timestamp
These fields should contain information on CAN type packages. In the field value I am inserting the sequence of bytes in this way (arduino side):
void AddOneToROStail(int packetSize){
int i = 0;
if(countR == BUFFER_LENGHT){
Serial.print("Overrun from CAN..losing packets");
return;
}
buff_toROS[headR].id = CAN.packetId();
buff_toROS[headR].len = packetSize;
buff_toROS[headR].timestamp = now();
//get the raw bytes
buff_toROS[headR].value = new unsigned char[packetSize];
for(i=0; i<buff_toROS[headR].len; i++){
buff_toROS[headR].value[i] = CAN.read();
}
headR = headR + 1;
headR = headR%BUFFER_LENGHT; //make sure head [0 % BUFFER_LENGHT-1]
countR = countR + 1; //new alement in the queue
}
So when a package arrives I fill a struct and immediately after that I publish it on the ros topic. The problem is that despite being bytes if I try to do echo of the topic the values become negative and this is problematic when converting from byte array to actual value reported by the sensor. I tried making (on the serial monitor of arduino) control prints but everything seems to be correct:
Packet ID:21 Packet length:2 Byte sequence: 134|1|
Packet ID:20 Packet length:2 Byte sequence: 134|3|
Packet ID:18 Packet length:2 Byte sequence: 240|0|
Packet ID:19 Packet length:2 Byte sequence: 134|1|
Packet ID:21 Packet length:2 Byte sequence: 134|1|
Packet ID:20 Packet length:2 Byte sequence: 137|3|
Packet ID:18 Packet length:2 Byte sequence: 240|0|
Than that when I run "rostopic echo /fromarduino":
id: 18
lenght: 2
value: [-16, 0]
timestamp: 62
---
id: 19
lenght: 2
value: [124, 1]
timestamp: 62
---
id: 21
lenght: 2
value: [124, 1]
timestamp: 62
---
id: 20
lenght: 2
value: [-112, 3]
timestamp: 62
Don't mind the timestamp that works correctly. I really hope some of you can help me. Thanks in advance.
As you have an cortex m0 you may use the rosbridge
Afaik, arrays require special handling in
rosserial
. See rosserial/Overview/Limitations: Arrays.I don't completely follow the code snippet you posted, so you may already be setting the length, but it's something to check.
Note: I'm not referring to the
length
field of your custom message.rosserial
adds an additional<fieldname>_length
field for every array that doesn't have a fixed size.yes I've already done it. As I said it is strange that, after setting the array length and after filling the array (via pointer), just before publishing, if I try to print the contents of the array I get all unsigned int (so 0-255 which is what i want) while when echo using "rostopic echo /fromarduino" they become signed int (-128.127) .. This is the code piece for publish one to ros (part of the arduino sketch):