I am using the Irobot create's OI to send and receive serial bytes for sensor information.
From the create oi manual:
The distance that Create has traveled in millimeters since the
distance it was last requested is sent as a signed 16-bit value,
high byte first. This is the same as the sum of the distance
traveled by both wheels divided by two. Positive values indicate
travel in the forward direction; negative values indicate travel
in the reverse direction. If the value is not polled frequently
enough, it is capped at its minimum or maximum.
The angle in degrees that iRobot Create has turned since the
angle was last requested is sent as a signed 16-bit value, high
byte first. Counter-clockwise angles are positive and clockwise
angles are negative. If the value is not polled frequently
enough, it is capped at its minimum or maximum.
I have this code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
int Biscuit::biscReceiveByte(signed char &byte) {
usleep(10000);
return (read(deviceDescriptor, &byte, 1) == 1 ? BISC_SUCCESS : BISC_ERR);
}
int Biscuit::biscReceiveByte(unsigned char &byte) {
usleep(10000);
return (read(deviceDescriptor, &byte, 1) == 1 ? BISC_SUCCESS : BISC_ERR);
}
int Biscuit::biscReceiveInt(signed int &num) {
signed char highbyte, lowbyte;
if(biscReceiveByte(highbyte) == BISC_ERR) return BISC_ERR;
if(biscReceiveByte(lowbyte) == BISC_ERR) return BISC_ERR;
num = highbyte; //send x_high to rightmost 8 bits
num = num<<8; //shift x_high over to leftmost 8 bits
num |= lowbyte; //logical OR keeps x_high intact in combined and fills in //rightmost 8 bits
return BISC_SUCCESS;
}
int Biscuit::biscReceiveInt(unsigned int &num) {
unsigned char highbyte, lowbyte;
if(biscReceiveByte(highbyte) == BISC_ERR) return BISC_ERR;
if(biscReceiveByte(lowbyte) == BISC_ERR) return BISC_ERR;
num = highbyte; //send x_high to rightmost 8 bits
num = num<<8; //shift x_high over to leftmost 8 bits
num |= lowbyte; //logical OR keeps x_high intact in combined and fills in //rightmost 8 bits
return BISC_SUCCESS;
}
int Biscuit::biscDistance() {
if(biscSendByte(BISC_SENSOR) == BISC_ERR) return BISC_ERR;
if(biscSendByte(BISC_SENSOR_DISTANCE) == BISC_ERR) return BISC_ERR;
//The wait is mandatory
usleep(2000000);
unsigned int distance=0;
//signed int distance=0; //NOT WORKING SOMETIMES
if(biscReceiveInt(distance) == BISC_ERR) return BISC_ERR;
return distance;
}
int Biscuit::biscAngle() {
if(biscSendByte(BISC_SENSOR) == BISC_ERR) return BISC_ERR;
if(biscSendByte(BISC_SENSOR_ANGLE) == BISC_ERR) return BISC_ERR;
//The wait is mandatory
usleep(2000000);
signed int angle=0;
if(biscReceiveInt(angle) == BISC_ERR) return BISC_ERR;
return angle;
|
Declaration in robot.cpp
1 2 3
|
int odometry_distance=0; //distance direct from odometry in mm
//signed int odometry_distance=0;
signed int angle=0; //odometry angle reading
|
The create OI sends a signed int for both angle and distance measurement request ( the sign indicating direction). I created a signed int variable in Robot.cpp. However, I am not getting consistent results with the distance measurement if I use an signed int variable. I seem to be getting consistent results with the angle measurement if I use a signed int variable for angle. For the distance measurement, I have to use an unsigned int and handle the value wrap to highest int ( negative value wraps to 65535 and then downward) and subtract from that number 65535 to get the negative distance when backing up. What is wrong with the code?
Thanks
Chris