I am trying to use C++ to talk to the Create 2 robot. Does anyone have basic code to write/read from the Create 2 using C++ or C?
I am having trouble with converting Create 2 commands (like 145
) into one char
.
As ott mentioned, you can simply make the type an unsigned char
which is an 8 bit data type. Here is some code snipits that should work, but are untested. This uses the "Drive" example from the OI Spec.
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
unsigned char buff[5] = {137, 255, 56, 1, 244};
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
struct termios tio;
bzero(&tio, sizeof(tio));
tio.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
tio.c_iflag = IGNPAR;
tio.c_cc[VTIME] = 1;
tio.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &tio);
int res = write(fd, buff, 5);
if (res < 0)
{
int err = errno;
perror("write error: ");
printf("\nWARNING: write returned %d, errno: %d\n", res, err);
}
bzero
is deprecated. Per its manpage, “This function is deprecated (marked as LEGACY in POSIX.1-2001): use memset(3) in new programs. POSIX.1-2008 removes the specification of bzero().” So instead of bzero(&tio, sizeof(tio));
you could say memset(&tio, 0, sizeof tio);
. [For the #includes, manpages show #include <string.h>
for memset, and #include <strings.h>
for bzero.]
$\endgroup$
Commented
May 6, 2015 at 15:57
unsigned char opcode;
and then assignopcode = 145;
. $\endgroup$