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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
#ifdef GUI
#include <cv.h>
#include <highgui.h>
#endif
#include <stdlib.h>
#include <iostream>
#include <rvjpeg.h>
#include <xthread.h>
#include <robotinocom.h>
#include <fstream>
#define IPFILENAME "../../robotinoip.txt"
#define OMEGAMAX 40
#define VX 100
unsigned int timestampOfLastImage = 0;
std::string getIpFromFile( std::string filename )
{
std::string ip;
std::ifstream is;
is.open( filename.c_str() );
if( is.is_open() )
{
is >> ip;
is.close();
}
else
{
ip = "127.0.0.1";
}
return ip;
}
void errorCb( void* data, int error )
{
std::cout << std::endl << "Error " << RobotinoCom::errorString( error ) << std::endl;
}
void connectedCb( void* data )
{
std::cout << std::endl << "Connected" << std::endl;
}
void connectionClosedCb( void* data )
{
std::cout << std::endl << "Connection closed." << std::endl;
}
int main( int argc, char** argv )
{
std::string hostname;
hostname = getIpFromFile( IPFILENAME );
RobotinoCom::Image rawImage;
#ifdef GUI
IplImage* src = NULL;
IplImage* gray = NULL;
#endif
RobotinoCom com;
RVJpeg rvjpeg;
RobotinoCom::CameraParameters param = com.cameraParameters();
unsigned int i = 0;
int thres = 5000;
int lineX = 0;
int omega = 0;
if( false == com.init() )
{
return 1;
}
com.setErrorCallback( &errorCb, NULL );
com.setConnectedCallback( &connectedCb, NULL );
com.setConnectionClosedCallback( &connectionClosedCb, NULL );
#ifdef GUI
cvNamedWindow("Live Image",1);
cvNamedWindow("Gray Image",1);
cvCreateTrackbar( "Threshold", "Gray Image", &thres, 20000, NULL );
#endif
std::cout << "Connecting to host " << hostname;
com.connectToHost( hostname );
while( com.state() == RobotinoCom::ConnectingState )
{
std::cout << ".";
XThread::msleep( 200 );
}
std::cout << std::endl;
if( com.error() != RobotinoCom::NoError )
{
goto CLEANUP;
}
std::cout << "Press q to quit." << std::endl;
com.setImageRequest( true );
param.compression = RobotinoCom::LowCompression;
param.resolution = RobotinoCom::QVGA;
com.setCameraParameters( param );
#ifdef GUI
while( 'q' != cvWaitKey( 1 ) )
#else
while( false == com.bumper() )
#endif
{
if( timestampOfLastImage > 0 )
{
if( com.msecsElapsed() - timestampOfLastImage < 500 )
{
if( lineX != -1 )
{
omega = -lineX + rawImage.parameters.width / 2;
if( omega < 20 && omega > -20 )
{
omega = 0;
}
if( omega > OMEGAMAX )
{
omega = OMEGAMAX;
}
if( omega < -OMEGAMAX )
{
omega = -OMEGAMAX;
}
}
else
{
if( omega > 0 )
{
omega = OMEGAMAX;
}
else
{
omega = -OMEGAMAX;
}
}
std::cout << omega << std::endl;
if( 0 == omega )
{
com.setVelocity( VX, 0, omega );
}
else
{
com.setVelocity( 0, 0, omega );
}
}
}
if( com.receiveIoStatus() == false )
{
break;
}
const RobotinoCom::Image* image = com.lockImage();
if( image != NULL )
{
timestampOfLastImage = image->timestamp;
unsigned int width = 0;
unsigned int height = 0;
if( RobotinoCom::JPG == image->parameters.type )
{
if( rvjpeg.setDecodeSource( image->data, image->dataSize ) )
{
rvjpeg.sourceImageSize( &width, &height );
}
}
else if( RobotinoCom::RAW == image->parameters.type )
{
width = image->parameters.width;
height = image->parameters.height;
}
if( 0 != width && 0 != height )
{
if( rawImage.parameters.width != width || rawImage.parameters.height != height )
{
delete [] rawImage.data;
rawImage.data = new unsigned char[width*height*3];
rawImage.dataSize = width*height*3;
rawImage.parameters.width = width;
rawImage.parameters.height = height;
}
#ifdef GUI
CvSize size;
size.width = width;
size.height = height;
if( NULL != src )
{
if( src->width != width || src->height != height )
{
cvReleaseImageHeader(&src);
cvReleaseImageHeader(&gray);
src = cvCreateImageHeader( size, IPL_DEPTH_8U, 3 );
gray = cvCreateImageHeader( size, IPL_DEPTH_8U, 1 );
}
}
else
{
src = cvCreateImageHeader( size, IPL_DEPTH_8U, 3 );
gray = cvCreateImageHeader( size, IPL_DEPTH_8U, 1 );
}
#endif
if( RobotinoCom::JPG == image->parameters.type )
{
rvjpeg.setDestination( (char*)rawImage.data, rawImage.dataSize, RVJpeg::BGR );
rvjpeg.decode();
}
else if( RobotinoCom::RAW == image->parameters.type )
{
memcpy( (void*)rawImage.data, (const void*)image->data, image->dataSize );
}
com.unlockImage();
#ifdef GUI
cvSetData( src, rawImage.data, rawImage.parameters.width * 3 );
cvSetData( gray, lineDetector.sobelImage(), lineDetector.width() );
if( lineX != -1 )
{
CvPoint p1;
p1.x = lineX - 2;
p1.y = gray->height - 5 ;
CvPoint p2;
p2.x = lineX + 2;
p2.y = gray->height - 1;
cvRectangle( gray, p1, p2, cvScalar(200) );
}
cvShowImage("Live Image",src);
cvShowImage("Gray Image",gray);
#endif
}
}
}
CLEANUP:
#ifdef GUI
cvReleaseImageHeader( &src );
cvReleaseImageHeader( &gray );
cvDestroyWindow("Live Image");
cvDestroyWindow("Gray Image");
#endif
delete [] rawImage.data;
return 0;
}
|