00001 /* 00002 * test-AerialRetina.C 00003 */ 00004 00005 #include <iostream> 00006 #include <IceE/IceE.h> 00007 #include <dc1394/dc1394.h> 00008 #include "miniIce/ImageIce.h" 00009 #include "miniIce/SimpleRobotSimEvents.h" 00010 #include "SimpleRobotBrainComponent.H" 00011 00012 //NOTE IS CURRENTLY FOR NON-COMPRESSED 8-bit IMAGES 00013 #define ICE_IMAGE_DEPTH 1 00014 #define ICE_FRAME_SKIP_RATE 4 00015 #define EXPECTED_SIZE 640*240 00016 //the above is: in that number of frames, we should send 1, 00017 // so basically it means that framerate across ice = 60/ICE_FRAME_SKIP_RATE 00018 00019 00020 class AerialRetina : public SimpleRobotBrainComponent { 00021 private: 00022 dc1394camera_t * camera; 00023 std::string cameraName; 00024 uint32_t cameraMode; 00025 int curSkip; 00026 00027 public: 00028 AerialRetina (dc1394camera_t * camera, std::string cameraName, 00029 uint32_t cameraMode, char * iceStormIP, char * myName, 00030 Ice::CommunicatorPtr ic) : 00031 SimpleRobotBrainComponent(iceStormIP, myName, ic) { 00032 this->camera = camera; 00033 this->cameraName = cameraName; 00034 this->cameraMode = cameraMode; 00035 curSkip = 0; 00036 } 00037 virtual ~AerialRetina () {}; 00038 00039 void registerTopics() { 00040 this->registerPublisher("RetinaMessageTopic"); 00041 } 00042 00043 ImageIceMod::ImageIce dc2Ice(dc1394video_frame_t * input) { 00044 printf("here"); 00045 ImageIceMod::ImageIce output; 00046 printf("here2"); 00047 unsigned int height, width; 00048 dc1394_get_image_size_from_video_mode(camera, cameraMode, &width, &height); 00049 output.height = (int) height; 00050 output.width = (int) width; 00051 output.pixSize = ICE_IMAGE_DEPTH; 00052 00053 int size = height*width*ICE_IMAGE_DEPTH; 00054 if (size == EXPECTED_SIZE*ICE_IMAGE_DEPTH) { 00055 output.data.resize(size); 00056 std::copy(input->image, input->image + size, output.data.begin()); 00057 } 00058 00059 return output; 00060 } 00061 00062 void run () { 00063 registerTopics(); 00064 this->start(); 00065 00066 if (dc1394_video_set_transmission(camera, DC1394_ON) != DC1394_SUCCESS) { 00067 printf ("FATAL: Could not start camera stream.\n"); 00068 return; 00069 } 00070 dc1394video_frame_t * frame = NULL; 00071 00072 while (true) { 00073 RobotSimEvents::RetinaMessagePtr msg = new RobotSimEvents::RetinaMessage; 00074 msg->cameraID = cameraName; 00075 00076 dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame); 00077 printf ("gotframe...\n"); 00078 curSkip++; 00079 if (curSkip < ICE_FRAME_SKIP_RATE) { 00080 printf("about to send..\n"); 00081 msg->img = dc2Ice(frame); 00082 this->publish("RetinaMessageTopic", msg); 00083 printf("Sending image...\n"); 00084 curSkip = 0; 00085 } 00086 00087 //for some reason the ENQUEUE?!?! function FREES a buffer 00088 dc1394_capture_enqueue(camera, frame); 00089 } 00090 00091 dc1394_video_set_transmission(camera, DC1394_OFF); 00092 } 00093 00094 }; 00095 00096 00097 int main (int argc, char* argv[]) { 00098 00099 char * stormAddress = NULL; 00100 int cameraNum = -1; 00101 //STEP 1: PARSE CMDLINE ARGS 00102 // we don't have that nice model* stuffs, so we have to do this the very hard, 00103 // very old fashion way, sorry :( 00104 for (argc--, argv++; argc > 0; argc--, argv++) { 00105 if (strstr(*argv, "--icestorm-ip") != NULL) 00106 stormAddress = *argv + strlen("--icestorm-ip="); 00107 else if (strstr(*argv, "--camera-num") != NULL) 00108 cameraNum = atoi(*argv + strlen("--camera-num=")); 00109 } 00110 00111 if (stormAddress == NULL || cameraNum == -1) { 00112 printf("usage : test-AerialRetina --icestorm-ip=ICESTORM_IP_ADDRESS --camera-num=#\n"); 00113 return 1; 00114 } 00115 00116 //================================================= 00117 //STEP 2: initialize dc1394 stuffs: 00118 //================================================= 00119 dc1394camera_t * camera = NULL; 00120 dc1394_t * d; 00121 dc1394camera_list_t * list; 00122 uint32_t cameraMode; 00123 00124 printf ("Initializing cameras..."); 00125 d = dc1394_new (); 00126 00127 if (d && dc1394_camera_enumerate (d, &list) == DC1394_SUCCESS && 00128 list->num > 0 && cameraNum > -1 && cameraNum < list->num) { 00129 00130 camera = dc1394_camera_new (d, list->ids[cameraNum].guid); 00131 if (!camera) { 00132 printf ("FAILURE - getting first camera\n"); 00133 return 1; 00134 } 00135 00136 //clean camera: 00137 dc1394_reset_bus(camera); 00138 00139 //setup the camera: 00140 dc1394video_modes_t modes; 00141 dc1394error_t err; 00142 dc1394_video_get_supported_modes(camera, &modes); 00143 cameraMode = modes.modes[modes.num-1]; 00144 dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400); 00145 dc1394_video_set_mode(camera, modes.modes[modes.num-1]); 00146 err=dc1394_video_set_framerate(camera, DC1394_FRAMERATE_60); 00147 DC1394_ERR_RTN(err, "Could not set framerate\n"); 00148 err=dc1394_capture_setup(camera, 4, DC1394_CAPTURE_FLAGS_DEFAULT); 00149 DC1394_ERR_RTN(err, "Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera\n"); 00150 00151 } else { 00152 printf ("FAILURE - initialization & enumeration\n"); 00153 return 1; 00154 } 00155 00156 dc1394_camera_free_list (list); 00157 printf ("SUCCESS\n"); 00158 00159 00160 //========================================== 00161 //STEP 3: Initialize ICE stuffs: 00162 //========================================== 00163 00164 printf ("Initializing ICE Components..."); 00165 char nameBuf[64]; 00166 sprintf(nameBuf, "camera%i", cameraNum); 00167 std::string cameraName(nameBuf); 00168 AerialRetina ar(camera, cameraName, cameraMode, stormAddress, 00169 "AerialRetina", Ice::initialize(argc, argv)); 00170 ar.registerTopics(); 00171 printf ("SUCCESS\n"); 00172 00173 ar.run(); 00174 //Yay, running: 00175 // d^.^b 00176 00177 //Clean up dc1394 stuffs: 00178 dc1394_camera_free(camera); 00179 dc1394_free(d); 00180 00181 return 0; 00182 }