I am communicating with a roomba. It sends certain int-sized values (-32768 - 32767), two bytes at a time, high byte first. I am having trouble keeping the value signed when I receive it, and put the two bytes into an int variable using bit math. At the top of my code, I declare 'distance' as an int, and later in the code, I do this:
Now I have something to help this problem; this code works:
lcd.clear();
char var1, var2;
signed short int var3, var4;
var1 = 0xab;
var2 = 0xcd;
var3 = ((signed short int) (var1 << 8) |(0xff & var2) );
if(sizeof(var3) == 2)
printf("signed short is 2 bytes\n");
if (var3 == 0xabcd) {
lcd.print("it's right!");
}
else {
lcd.print("var3=");
lcd.print(var3);
}
return;
Showing that the casting is done correctly. This must be a problem with the roomba protocol. Does anyone know anything about the ROI (Roomba Open Interface) to shed some light on this? It is really important that I get the arduino to receive negative values from the roomba, otherwise the project I am working on for my current internship will never be finished!!
Now I see in the Arduino reference page the word() constructor. For anyone who doesn't know, it takes two bytes (word(highByte, lowByte)) and puts them into a word. The problem with this is that I need this value I'm trying to create by casting two bytes to be SIGNED. Word only goes from 65,535 to 0. I need a range of -32768 to 32767. One of these two bytes (the high byte) has a leading '1', indicating that when it is combined with the low byte, you should get a NEGATIVE value. Is there not something like int(h, l)? :-?
There's another way to assemble a 16-bit integer from two 8-bit bytes:
union twobytes
{
int i;
byte b[2];
};
int assemble_bytes(byte b1,byte b2)
{
union twobytes t;
t.b[0] = b1;
t.b[1] = b2;
return(t.i);
}
I didn't try to compile this, so this may not be exactly correct, but you get the idea. Also, I'm not sure about the order of the bytes, so they may need to be switched around.
Looks like this has to do with the Roomba ROI. Unioning is not working with the roomba (but although it does work when I pass it a negative value as it should). Neither does the shift-OR approach...Something about the way the roomba sends these negative values does not work with this code...
Are you sure you're passing the right "hi-byte" and "low byte" ?
Make some test code that prints all of "high byte", "low byte", and the calculated "distance."