Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

SegCam.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_SegCam_h_
00003 #define INCLUDED_SegCam_h_
00004 
00005 #include "CameraStreamBehavior.h"
00006 #include "Shared/Config.h"
00007 
00008 class Socket;
00009 class FilterBankGenerator;
00010 class FilterBankEvent;
00011 
00012 //! Forwards segmented images from camera over wireless
00013 /*! The format used for serialization is basically defined by the
00014  *  subclass of FilterBankGenerator being used.  I suggest looking at
00015  *  that classes's documentation to determine the format used. (Generally
00016  *  either SegmentedColorGenerator or RLEGenerator)
00017  *
00018  *  However, SegCam will add a few fields at the beginning of
00019  *  each packet to assist in processing the image stream.
00020  *
00021  *  I emphasize: <i>beginning</i> of each Vision packet, <i>before</i> the FilterBankGenerator header. 
00022  *  - <@c string:"TekkotsuImage">
00023  *  - <<tt>Config::vision_config::encoding_t</tt>: Config::vision_config::ENCODE_SINGLE_CHANNEL> <i>(always just sends a single channel)</i>
00024  *  - <<tt>Config::vision_config::compression_t</tt>: Config::vision_config::COMPRESS_RLE> <i>(This is misleading - may actually be uncompressed, but this signals it's a segmented color image)</i>
00025  *  - <@c unsigned @c int: width> <i>(this is the width of the largest channel - note different channels can be sent at different resolutions!  Provides cheap "compression" of chromaticity channels)</i>
00026  *  - <@c unsigned @c int: height> <i>(similarly, height of largest channel)</i>
00027  *  - <@c unsigned @c int: timestamp> <i>(time image was taken, milliseconds since boot)</i>
00028  *  - <@c unsigned @c int: framenumber> <i>(incremented for each frame, so we can tell if/when we drop one)</i>
00029  *
00030  *  Alternatively, SegCam may send a "Close Connection" packet
00031  *  when the server is shutting down.  This is to help UDP connections, which
00032  *  otherwise wouldn't realize that they need to start trying to reconnect.
00033  *  - <@c string:"CloseConnection">
00034  *  
00035  *  This is exactly the same protocol that is followed by the
00036  *  RawCam behavior as well - the same code can parse either stream.
00037  *
00038  *  However, one odd bit - since the RLEGenerator doesn't save the color
00039  *  information itself, SegCam will do it instead.  So, if
00040  *  SegCam is using RLE compression, it will tack a footer at
00041  *  the end of the packet: (from SegmentedColorGenerator::encodeColors())
00042  *  - <@c unsigned @c int: num_cols> <i>(number of different colors available)</i>
00043  *  - for each of num_col:
00044  *    - <@c char: red> <i>red color to use for display of this index</i>
00045  *    - <@c char: green> <i>green color to use for display of this index</i>
00046  *    - <@c char: blue> <i>blue color to use for display of this index</i>
00047  *
00048  *  You can tell whether to expect the color footer by the creator string
00049  *  that follows the SegCam header.  (The compression field listed
00050  *  is considering segmented color itself a type of compression, whether
00051  *  or not it's RLE encoded, so you can't use that to tell whether the
00052  *  data is RLE encoded until you get to the data section.)
00053  *
00054  *  This is a binary protocol -- the fields listed indicate binary values
00055  *  in the AIBO's byte order (little endian).  Strings are encoded using
00056  *  the LoadSave::encode(char*,unsigned int, unsigned int) method.
00057  */ 
00058 class SegCam : public CameraStreamBehavior {
00059 public:
00060   //! constructor
00061   SegCam();
00062 
00063   //! destructor
00064   ~SegCam() { theOne=NULL; }
00065 
00066 #if defined(TGT_ERS210) || defined(TGT_ERS220) || defined(TGT_ERS2xx) || defined(TGT_ERS7)
00067   static const unsigned int TCP_WIRELESS_BUFFER_SIZE=85000; //!< 85000 bytes for use up to 416x320 pixels / 8 min expected runs * 5 bytes per run + some padding
00068 #else
00069   static const unsigned int TCP_WIRELESS_BUFFER_SIZE=301*1024; //!< 300KB for use up to 640x480 pixels / 8 min expected runs * 5 bytes per run + some padding
00070 #endif
00071   static const unsigned int UDP_WIRELESS_BUFFER_SIZE=64*1024; //!< 64KB is the max udp packet size
00072 
00073   virtual void doStart();
00074 
00075   virtual void doStop();
00076 
00077   virtual void doEvent();
00078 
00079   static std::string getClassDescription() {
00080     char tmp[20];
00081     snprintf(tmp,20,"%d",*config->vision.segcam.port); tmp[19]='\0';
00082     return std::string("Forwards segmented images from camera over port ")+tmp;
00083   }
00084   virtual std::string getDescription() const { return getClassDescription(); }
00085   
00086 protected:
00087   static SegCam* theOne; //!< global instance of SegCam acting as server
00088   //! function for network data to be sent to -- forwards to #theOne's receiveData()
00089   static int networkCallback(char* buf, int bytes) { return theOne->receiveData(buf,bytes); }
00090   
00091   void closeServer(); //!<tear down the server socket (#visRLE)
00092   void setupServer(); //!<setup the server socket (#visRLE  )
00093   
00094   //! opens a new packet, writes header info; returns true if open, false if otherwise open (check cur==NULL for error)
00095   /*! see the class documentation for SegCam for the protocol documentation */
00096   bool openPacket(FilterBankGenerator& fbkgen, unsigned int time, unsigned int layer); 
00097   bool writeRLE(const FilterBankEvent& fbke); //!< writes a color image
00098   bool writeSeg(const FilterBankEvent& fbke); //!< writes a color image
00099   void closePacket(); //!< closes and sends a packet, does nothing if no packet open
00100 
00101   //! sends a packet signaling the server is closing the connection (good for UDP connections)
00102   bool sendCloseConnectionPacket();
00103     
00104   Socket * visRLE; //!< socket to send image stream over
00105   char * packet; //!< buffer being filled out to be sent
00106   char * cur; //!< current location in #packet
00107   unsigned int avail; //!< number of bytes remaining in #packet
00108   unsigned int max_buf; //!< the buffer size requested from Wireless when the socket was allocated
00109   unsigned int lastProcessedTime; //!< the time that the last event was processed
00110 
00111 private:
00112   SegCam(const SegCam&); //!< don't call
00113   SegCam& operator=(const SegCam&); //!< don't call
00114 };
00115 
00116 /*! @file
00117  * @brief Describes SegCam, which forwards segmented images from camera over wireless
00118  * @author ejt (Creator)
00119  */
00120 
00121 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:50 2016 by Doxygen 1.6.3