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


thermalvis
Author(s): Stephen Vidas
autogenerated on Sun Jan 5 2014 11:38:45