Skip to main content

RDC 2013

Submitted by admin on

Programming iRobot Create

[code lang="c++"]

#include <stdio.h> #include

#include "RobotConnector.h"

#include "cv.h" #include "highgui.h"

using namespace std;

#define Create_Comport "COM4"

bool isRecord = false;

int main() { CreateData robotData; RobotConnector robot;

ofstream record; record.open(\"../data/robot.txt\"); if( !robot.Connect(Create_Comport) ) { cout << \"Error : Can't connect to robot @\" << Create_Comport << endl; return -1; } robot.DriveDirect(0, 0); cvNamedWindow(\"Robot\"); while(true) { cvWaitKey(30); double vx, vz; vx = vz = 0; if (robotData.bumper[0]) { cout << \"bump left\" << endl; } if (robotData.bumper[1]) { cout << \"bump right\" << endl; } double vl = vx - vz; double vr = vx + vz; int velL = (int)(vl*Create_MaxVel); int velR = (int)(vr*Create_MaxVel); int color = (abs(velL)+abs(velR))/4; color = (color < 0) ? 0 : (color > 255) ? 255 : color; int inten = (robotData.cliffSignal[1] + robotData.cliffSignal[2])/8 - 63; inten = (inten < 0) ? 0 : (inten > 255) ? 255 : inten; //cout << color << \" \" << inten << \" \" << robotData.cliffSignal[1] << \" \" << robotData.cliffSignal[2] << endl; robot.LEDs(velL > 0, velR > 0, color, inten); if( !robot.DriveDirect(velL, velR) ) cout << \"SetControl Fail\" << endl; if( !robot.ReadData(robotData) ) { //cout << \"ReadData Fail\" << endl; } if( isRecord ) record << robotData.cliffSignal[0] << \"\\t\" << robotData.cliffSignal[1] << \"\\t\" << robotData.cliffSignal[2] << \"\\t\" << robotData.cliffSignal[3] << endl; cout << \"Robot \" << robotData.cliffSignal[0] << endl; } robot.Disconnect(); return 0;

} [/code]