$search
00001 00005 #ifndef _THERMALVIS_STREAMER_H_ 00006 #define _THERMALVIS_STREAMER_H_ 00007 00008 #include "time.h" 00009 00010 #include <signal.h> 00011 00012 #include "ros_resources.hpp" 00013 #include "tools.hpp" 00014 #include "improc.hpp" 00015 #include "video.hpp" 00016 00017 #include <dynamic_reconfigure/server.h> 00018 #include "streamerConfig.h" 00019 00020 #include "clahe.h" 00021 00022 // INTERNAL TIMESTAMP STUFF 00023 //#include "/usr/src/linux-headers-3.2.0-27/include/linux/unaligned/le_byteshift.h" 00024 /* 00025 #include <linux/kernel.h> 00026 #include <linux/list.h> 00027 #include <linux/usb.h> 00028 #include <linux/videodev2.h> 00029 #include <asm/unaligned.h> 00030 */ 00031 // SERIAL COMMS STUFF 00032 #include <termios.h> 00033 #include <stdio.h> 00034 #include <unistd.h> 00035 #include <fcntl.h> 00036 #include <sys/signal.h> 00037 #include <sys/types.h> 00038 #include <stdlib.h> 00039 #include <string.h> 00040 #include <errno.h> 00041 00042 #define BAUDRATE B38400 00043 #define MODEMDEVICE "/dev/ttyS0" 00044 #define _POSIX_SOURCE 1 //POSIX compliant source 00045 #define FALSE 0 00046 #define TRUE 1 00047 00048 volatile int STOP=FALSE; 00049 00050 void signal_handler_IO (int status); //definition of signal handler 00051 int wait_flag=TRUE; //TRUE while no signal received 00052 char devicename[80]; 00053 long Baud_Rate = 115200; // default Baud Rate (110 through 38400) 00054 long BAUD; // derived baud rate from command line 00055 long DATABITS; 00056 long STOPBITS; 00057 long PARITYON; 00058 long PARITY; 00059 int Data_Bits = 8; // Number of data bits 00060 int Stop_Bits = 1; // Number of stop bits 00061 int Parity = 0; // Parity as follows: 00062 // 00 = NONE, 01 = Odd, 02 = Even, 03 = Mark, 04 = Space 00063 int Format = 4; 00064 FILE *input; 00065 FILE *output; 00066 int status; 00067 00068 char message[90]; 00069 00070 // Other stuff from: http://ubuntuforums.org/showthread.php?t=1395180 00071 #include <stdio.h> 00072 #include <string.h> 00073 #include <unistd.h> //UNIX Standard function definitions 00074 #include <fcntl.h> //File control 00075 #include <errno.h> //Error number def 00076 #include <termios.h> //POSIX terminal control 00077 #include <termio.h> 00078 00079 #define DEFAULT_SERIAL_POLLING_RATE 0.1 00080 00081 // END SERIAL COMMS STUFF 00082 00083 #define DEFAULT_READ_RATE 25.0 00084 #define MAX_READ_RATE 100.0 00085 #define FASTEST_READ_RATE 1000.0 00086 00087 #define DEFAULT_IMAGE_WIDTH 640 00088 #define DEFAULT_IMAGE_HEIGHT 480 00089 00090 #define MAX_ROWS 1920 00091 #define MAX_COLS 2560 00092 00093 #define DATATYPE_8BIT 0 00094 #define DATATYPE_RAW 1 00095 #define DATATYPE_MM 2 00096 #define DATATYPE_DEPTH 3 00097 00098 #define UVC_STREAM_PTS (1 << 2) 00099 #define UVC_STREAM_SCR (1 << 3) 00100 00101 #define NO_REPUBLISH_CODE 0 00102 #define REPUBLISH_CODE_8BIT_MONO 1 00103 #define REPUBLISH_CODE_8BIT_COL 2 00104 #define REPUBLISH_CODE_16BIT 3 00105 00106 #define SYNCMODE_HARD 0 00107 #define SYNCMODE_SOFT 1 00108 #define SYNCMODE_IMAGEONLY 2 00109 00110 #define CALIBMODE_OFF 0 00111 #define CALIBMODE_ALT_SHUTTER 1 // Alternates shutter and sends NUCs 00112 #define CALIBMODE_ALT_OUTPUT 2 // Alternates between RAW and INS camera outputs 00113 #define CALIBMODE_LONG_SHUTTER 3 // Alternates shutter, but lets NUCs occur naturally 00114 00115 #define DETECTOR_MODE_RAW 0 00116 #define DETECTOR_MODE_LUM 1 00117 #define DETECTOR_MODE_INS 2 00118 #define DETECTOR_MODE_RAD 3 00119 #define DETECTOR_MODE_TMP 4 00120 00121 const char __PROGRAM__[] = "STREAMER"; 00122 00123 typedef dynamic_reconfigure::Server < thermalvis::streamerConfig > Server; 00124 00125 bool wantsToShutdown = false; 00126 void mySigintHandler(int sig); 00127 int getMapIndex(string mapping); 00128 void getMapping(int mapIndex, bool extremes, int& mapCode, int& mapParam); 00129 00131 struct camData_ { 00132 Mat cameraMatrix, distCoeffs, imageSize; 00133 00134 Size cameraSize; 00135 Mat newCamMat; 00136 00137 Mat R, T; 00138 }; 00139 00140 //HGH 00142 struct camExtrinsicsData_ { 00143 Mat R, T; 00144 Mat R0, R1, T0, T1, P0, P1, cameraMatrix0, cameraMatrix1, distCoeffs0, distCoeffs1; 00145 Rect roi0, roi1; 00146 }; 00147 00149 struct streamerData { 00150 00151 bool wantsToMarkDuplicates; 00152 bool wantsToOutputDuplicates; 00153 00154 int calibrationMode; // various optional modes for calibration 00155 int alternatePeriod; 00156 00157 int dummy; 00158 00159 int maxReadAttempts; 00160 00161 int inputWidth, inputHeight; 00162 bool imageDimensionsSpecified; 00163 00164 double serialPollingRate; 00165 00166 bool outputColorFlag; 00167 bool output16bitFlag; 00168 bool output8bitFlag; 00169 00170 00171 00172 bool verboseMode; 00173 00174 bool serialComms, readThermistor; 00175 string portAddress; 00176 00177 int maxNucInterval; 00178 double maxNucThreshold; 00179 00180 bool captureMode; 00181 bool readMode; 00182 bool loadMode; 00183 bool subscribeMode; 00184 bool resampleMode; 00185 bool pollMode; 00186 bool wantsToUndistort; 00187 00188 bool forceInputGray; 00189 bool fixDudPixels; 00190 00191 bool disableSkimming; 00192 00193 bool loopFlag; 00194 bool wantsToResize; 00195 bool wantsToDumpTimestamps; 00196 bool wantsToRemoveDuplicates; 00197 00198 bool temporalSmoothing; 00199 00200 bool pauseMode; 00201 int syncMode; 00202 00203 double syncDiff; 00204 00205 bool extremes; 00206 00207 double writeQuality; 00208 00209 vector<int> outputFileParams; 00210 00211 string read_addr; 00212 string source, filename, folder, capture_device, intrinsics, extrinsics, topicname; 00213 00214 int map; 00215 00216 string normalizationMode; 00217 double normFactor; 00218 int normMode; 00219 int inputDatatype; 00220 int detectorMode; 00221 int camera_number; 00222 int filterMode; 00223 double filterParam; 00224 00225 double framerate; 00226 00227 double maxThermistorDiff; 00228 00229 unsigned long soft_diff_limit; 00230 00231 bool obtainStartingData(ros::NodeHandle& nh); 00232 00233 int desiredRows; 00234 int desiredCols; 00235 00236 string timeStampsAddress; 00237 00238 int mapCode; 00239 int mapParam; 00240 00241 double maxIntensityChange; 00242 int maxPastMeans; 00243 00244 00245 bool intrinsicsProvided, wantsToRectify, wantsToWrite, wantsToKeepNames, wantsToEncode; 00246 00247 //HGH 00248 bool wantsToAddExtrinsics; 00249 string republishTopic; 00250 int republishSource; 00251 bool republishNewTimeStamp; 00252 bool drawReticle; 00253 00254 int outputFormat; 00255 string outputFolder, outputFormatString, outputType, outputTimeFile; 00256 string outputVideo, videoType; 00257 00258 double alpha; 00259 bool autoAlpha; 00260 00261 string frameID; 00262 00263 streamerData(); 00264 00265 }; 00266 00268 class streamerNode { 00269 private: 00270 00271 char nodeName[256]; 00272 00273 bool firstCall; 00274 00275 bool updateNucInterval; 00276 bool updateDetectorMode; 00277 char serialCommand[256]; 00278 00279 bool altStatus; 00280 bool performingNuc; 00281 00282 ofstream ofs; 00283 00284 int alternateCounter; 00285 00286 double pastMeans[256]; 00287 int pastMeanIndex; 00288 00289 string callLogFile, retrieveLogFile, internalLogFile, writeLogFile, duplicatesLogFile, thermistorLogFile; 00290 ofstream ofs_call_log, ofs_internal_log, ofs_retrieve_log, ofs_write_log, ofs_duplicates_log, ofs_thermistor_log; 00291 00292 Mat testMat; 00293 00294 double medianPercentile; 00295 00296 float lastThermistorReading, newThermistorReading; 00297 00298 image_transport::ImageTransport *it; 00299 streamerData configData; 00300 image_transport::Subscriber image_sub; 00301 ros::Subscriber info_sub; 00302 image_transport::CameraSubscriber camera_sub; 00303 00304 ros::Time info_time, image_time, original_time, dodgeTime; 00305 00306 double shiftDiff; 00307 00308 unsigned long original_bx, original_by; 00309 00310 00311 unsigned long internal_time; 00312 00313 int64_t firmwareTime; 00314 00315 cv_bridge::CvImagePtr cv_ptr; 00316 sensor_msgs::Image msg_color, msg_16bit, msg_8bit; 00317 00318 bool firstServerCallbackProcessed; 00319 00320 double fusionFactor; 00321 bool centerPrincipalPoint; 00322 00323 double lastMedian, newMedian, lastLowerLimit, lastUpperLimit, oldMaxDiff; 00324 00325 double minVal, maxVal; 00326 00327 Mat lastFrame; 00328 00329 bool firstFrame; 00330 00331 Mat map1, map2; 00332 00333 Mat rzMat; 00334 00335 cScheme colourMap; 00336 00337 int writeIndex; 00338 int frameCounter; 00339 int lastWritten; 00340 00341 VideoWriter vid_writer; 00342 00343 bool videoInitialized; 00344 00345 bool readyToPublish; 00346 00347 ros::Timer timer, serial_timer; 00348 00349 bool alphaChanged; 00350 00351 image_transport::Publisher pub_color_im; 00352 image_transport::Publisher pub_16bit_im; 00353 image_transport::Publisher pub_8bit_im; 00354 //HGH 00355 image_transport::Publisher pub_republish_im; 00356 00357 image_transport::CameraPublisher pub_color; 00358 image_transport::CameraPublisher pub_16bit; 00359 image_transport::CameraPublisher pub_8bit; 00360 //HGH 00361 image_transport::CameraPublisher pub_republish; 00362 00363 Mat _8bitMat, _16bitMat, colourMat; 00364 Mat preFilteredMat, smoothedMat; 00365 Mat _8bitMat_pub, _16bitMat_pub, colourMat_pub; 00366 Mat newImage, normalizedMat, frame, workingFrame, undistorted; 00367 00368 int device_num; 00369 00370 camData_ globalCameraInfo; 00371 //HGH 00372 camExtrinsicsData_ globalExtrinsicsData; 00373 00374 00375 sensor_msgs::CameraInfo camera_info; 00376 00377 ros::ServiceServer set_camera_info; 00378 00379 bool isActuallyGray; 00380 00381 00382 00383 vector<string> inputList; 00384 int fileCount; 00385 00386 00387 00388 bool videoValid; 00389 streamerSource *mainVideoSource; 00390 VideoCapture cap; 00391 00392 dynamic_reconfigure::Server<thermalvis::streamerConfig> server; 00393 dynamic_reconfigure::Server<thermalvis::streamerConfig>::CallbackType f; 00394 00395 // Serial Comms 00396 int mainfd; /* File descriptor */ 00397 00398 00399 00400 public: 00401 00402 CvCapture* capture; 00403 00404 bool isVideoValid(); 00405 void setValidity(bool val); 00406 streamerSource * getMainVideoSource(); 00407 VideoCapture * getVideoCapture(); 00408 00409 streamerNode(ros::NodeHandle& nh, streamerData startupData); 00410 00411 void handle_camera(const sensor_msgs::ImageConstPtr& msg_ptr, const sensor_msgs::CameraInfoConstPtr& info_msg); 00412 00413 void handle_image(const sensor_msgs::ImageConstPtr& msg_ptr); 00414 00415 void handle_info(const sensor_msgs::CameraInfoConstPtr& info_msg); 00416 00417 bool setCameraInfo(sensor_msgs::SetCameraInfo::Request &req, sensor_msgs::SetCameraInfo::Response &rsp); 00418 00419 void serialCallback(const ros::TimerEvent& e); 00420 void timerCallback(const ros::TimerEvent& e); 00421 bool streamCallback(bool capture = true); 00422 void acceptImage(void *ptr); 00423 bool processImage(); 00424 void publishTopics(); 00425 void writeData(); 00426 bool processFolder(); 00427 void updateCameraInfo(); 00428 void assignCameraInfo(); 00429 void overwriteCameraDims(); 00430 void assignDefaultCameraInfo(); 00431 00432 //HGH 00433 void updateCameraInfoExtrinsics(); 00434 00435 void serverCallback(thermalvis::streamerConfig &config, uint32_t level); 00436 void refreshCameraAdvertisements(); 00437 void initializeMessages(); 00438 00439 void act_on_image(); 00440 00441 // Source alternatives 00442 bool runBag(); 00443 bool runRead(); 00444 bool runLoad(); 00445 bool runDevice(); 00446 00447 void updateMap(); 00448 00449 void prepareForTermination(); 00450 00451 void markCurrentFrameAsDuplicate(); 00452 00453 bool configureSerialComms(); 00454 float getThermistorReading(); 00455 00456 bool setupDevice(); 00457 void releaseDevice(); 00458 00459 int mygetch(); 00460 int open_port(); 00461 00462 //HGH 00463 void getRectification(); 00464 00465 }; 00466 00467 boost::shared_ptr < streamerNode > *globalNodePtr; 00468 00469 #endif