iRobot Corporation

Projects using iRobot Create
Register  ·  Sign In  ·  Help
Jump to Page:   1
  Reply   Reply  

C++ programming iRobot Create
Options    Options  
mircea
Visitor
Posts: 5
Registered: 12-05-2007


mircea

Message 1 of 4

Viewed 4,973 times


hi!

 

i tried to drive my irobot sending a script from c++. the robot behaves strange.... meaning, it starts only after i call several times the method for sending the script, and then it doesn't  stop :robotsad: . is there any rule regarding sending bytes to serial port? does the order differ?

 

i wrote the code in c++ for windows.

 

void CCreateRobot::robotvery-happy:rive(int distance, int velocity, int radius, bool backwards)

{

unsigned char *buffer = new unsigned char [15];

 

if (backwards)

{

distance = -distance;

velocity = -velocity;

}

buffer[0] = 152; // script

buffer[1] = 13; // script length

buffer[2] = 137; // drive

buffer[3] = (unsigned char) ((velocity & 0xFF00) >> 8); // velocity high

buffer[4] = (unsigned char) (velocity & 0xFF); // velocity low

buffer[5] = (unsigned char)((radius & 0xFF00) >> 8); // radius high

buffer[6] = (unsigned char) (radius & 0xFF); // radius low

buffer[7] = 156;

buffer[8] = (unsigned char)((distance & 0xFF00) >> 8); // distance high

buffer[9] = (unsigned char)(distance & 0xFF); // distance low

buffer[10] = 137;

buffer[11] = 0;

buffer[12] = 0;

buffer[13] = 0;

buffer[14] = 0;

if (SendBytes(buffer, sizeof(buffer)))

{

Sleep(200);

buffer[0] = 153;

SendBytes(buffer, 1);

}

delete [] buffer;

}

 

 

and the method for sending bytes:

 

BOOL CCreateRobot::SendBytes(unsigned char * buffer, DWORD dwBytesToWrite)

{

if ((m_hPort == NULL) || (m_hPort == INVALID_HANDLE_VALUE))

{

return false;

}

//HERE

OVERLAPPED osWrite = {0};

DWORD dwWritten;

DWORD dwRes;

BOOL fRes;

// Create this write operation's OVERLAPPED structure's hEvent.

osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if (osWrite.hEvent == NULL)

// error creating overlapped event handle

 return FALSE;

// Issue write.

 if (!WriteFile(m_hPort, buffer, dwBytesToWrite, &dwWritten, &osWrite))

{

if (GetLastError() != ERROR_IO_PENDING)

{

// WriteFile failed, but isn't delayed. Report error and abort.

fRes = FALSE;

}

else

// Write is pending.

dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);

switch(dwRes)

{

// OVERLAPPED structure's event has been signaled.

 case WAIT_OBJECT_0: if (!GetOverlappedResult(m_hPort, &osWrite, &dwWritten, FALSE))

fRes = FALSE;

else

// Write operation completed successfully.

fRes = TRUE;

break;

 

default:

// An error has occurred in WaitForSingleObject.

// This usually indicates a problem with the

// OVERLAPPED structure's event handle.

fRes = FALSE;

break;

}

}

 

else

// WriteFile completed immediately.

fRes = TRUE;

CloseHandle(osWrite.hEvent);

return fRes;

}

 

any help would be appreciated.


 

http://mirceacimpoi.spaces.live.com
04-13-2008 02:47 PM
  Reply   Reply  

Re: C++ programming iRobot Create
Options    Options  
JohnQ
Super Contributor
Posts: 556
Registered: 01-24-2007


JohnQ

Message 2 of 4

Viewed 4,952 times


It's possible that your serial write is buffered so that it does not transmit any characters until you have sent a buffer full (like 256 bytes).  You can try flushing the write stream after each write to see if that solves the problem.
 

John Q. - Sunny Tucson Arizona USA
04-14-2008 07:59 AM
  Reply   Reply  

Re: C++ programming iRobot Create
Options    Options  
kipkills
Visitor
Posts: 5
Registered: 03-27-2008


kipkills

Message 3 of 4

Viewed 4,946 times


I think I am trying to do a similar thing on linux. I want to mimic the "Send Numbers" function that Realterm has, using my own c++ function. However even though the LED on the irobot's serial cable indicates that i am sending the commands, nothing happens.

 

    portName=open("/dev/ttyS0",O_RDWR | O_EXCL); 

    char output[6];
    output[0]=128; output[1]=132; output[2]=139;
    output[3]=2; output[4]=0; output[5]=0;
    write(portName, output, sizeof(output));

04-14-2008 10:35 AM
  Reply   Reply  

Re: C++ programming iRobot Create
Options    Options  
dankrusi
Contributor
Posts: 14
Registered: 03-16-2009


dankrusi

Message 4 of 4

Viewed 3,040 times


This most definately sounds like your serial port is not being flushed quickly enough (the buffer is set too large). You can change this by calling the appropriate Windows API code (looks like you are using Windows).

 

-dan

03-16-2009 03:37 AM
Jump to Page:   1