Ask Your Question
0

variable size array of uint8 as msg field has strange values

asked 2019-09-03 05:59:58 -0500

dimaluigi gravatar image

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.

edit retag flag offensive close merge delete

Comments

As you have an cortex m0 you may use the rosbridge

duck-development gravatar imageduck-development ( 2019-09-03 14:42:21 -0500 )edit

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.

gvdhoorn gravatar imagegvdhoorn ( 2019-09-04 02:39:45 -0500 )edit

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):

void PublishOneToROS(){

    ros_pkt.id = buff_toROS[headR].id;
    ros_pkt.timestamp = buff_toROS[headR].timestamp;
    l = buff_toROS[headR].len;
    ros_pkt.lenght = l;

    //define lenght and assign pointer
    ros_pkt.value_length = l;
    ros_pkt.value = buff_toROS[headR].value;

    tailR = tailR + 1;
    tailR = tailR%BUFFER_LENGHT;
    countR = countR - 1;

    fromarduino.publish(&ros_pkt);

    free(buff_toROS[headR].value);
    free(ros_pkt.value);
}
dimaluigi gravatar imagedimaluigi ( 2019-09-04 05:48:05 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2019-09-03 14:29:05 -0500

duck-development gravatar image

updated 2019-09-03 14:31:34 -0500

If you programm in c++ then there will bei no signum Error. I Think it is an ros topic echo isue. It will be negativ in echo at it get bigger then 128

edit flag offensive delete link more

Comments

Yes I've just checked that: it goes fine until it reach 127, then it start to go backwards from -127 to 0 and it starts again. I can find a way to cast a signed byte tuple to an integer in python (to rapresent the actual value of the sensor) but still don't understand why this is appening..

dimaluigi gravatar imagedimaluigi ( 2019-09-03 14:48:23 -0500 )edit

If look here http://wiki.ros.org/msg then you see it. It is an uin8[] is an str in python

duck-development gravatar imageduck-development ( 2019-09-03 14:56:33 -0500 )edit

This thing dosen't compute for me..when i try to print the data type of the uint8[] array in the python script it returns:

<type 'tuple'>

But assuming that everything can remain this way, can anyone suggest me a way to proper convert a signed int tuple (-128, 127) in just one single integer?

dimaluigi gravatar imagedimaluigi ( 2019-09-03 17:26:17 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-09-03 05:59:58 -0500

Seen: 26 times

Last updated: Sep 03