Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

SegCamBehavior.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_SegCamBehavior_h_
00003 #define INCLUDED_SegCamBehavior_h_
00004 
00005 #include "Behaviors/BehaviorBase.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, SegCamBehavior 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, SegCamBehavior 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  *  RawCamBehavior 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, SegCamBehavior will do it instead.  So, if
00040  *  SegCamBehavior 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 SegCamBehavior 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 SegCamBehavior : public BehaviorBase {
00059 public:
00060   //! constructor
00061   SegCamBehavior();
00062 
00063   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
00064   static const unsigned int UDP_WIRELESS_BUFFER_SIZE=64*1024; //!< 64KB is the max udp packet size
00065 
00066   virtual void DoStart();
00067 
00068   virtual void DoStop();
00069 
00070   virtual void processEvent(const EventBase& e);
00071 
00072   static std::string getClassDescription() {
00073     char tmp[20];
00074     snprintf(tmp,20,"%d",config->vision.rle_port); tmp[19]='\0';
00075     return std::string("Forwards segmented images from camera over port ")+tmp;
00076   }
00077   virtual std::string getDescription() const { return getClassDescription(); }
00078   
00079 protected:
00080   //! opens a new packet, writes header info; returns true if open, false if otherwise open (check cur==NULL for error)
00081   /*! see the class documentation for SegCamBehavior for the protocol documentation */
00082   bool openPacket(FilterBankGenerator& fbkgen, unsigned int time, unsigned int layer); 
00083   bool writeRLE(const FilterBankEvent& fbke); //!< writes a color image
00084   bool writeSeg(const FilterBankEvent& fbke); //!< writes a color image
00085   void closePacket(); //!< closes and sends a packet, does nothing if no packet open
00086 
00087   //! sends a packet signaling the server is closing the connection (good for UDP connections)
00088   bool sendCloseConnectionPacket();
00089     
00090   Socket * visRLE; //!< socket to send image stream over
00091   char * packet; //!< buffer being filled out to be sent
00092   char * cur; //!< current location in #packet
00093   unsigned int avail; //!< number of bytes remaining in #packet
00094   unsigned int max_buf; //!< the buffer size requested from Wireless when the socket was allocated
00095   unsigned int lastProcessedTime; //!< the time that the last event was processed
00096 
00097 private:
00098   SegCamBehavior(const SegCamBehavior&); //!< don't call
00099   SegCamBehavior& operator=(const SegCamBehavior&); //!< don't call
00100 };
00101 
00102 /*! @file
00103  * @brief Describes SegCamBehavior, which forwards segmented images from camera over wireless
00104  * @author ejt (Creator)
00105  *
00106  * $Author: ejt $
00107  * $Name: tekkotsu-2_4_1 $
00108  * $Revision: 1.9 $
00109  * $State: Exp $
00110  * $Date: 2005/07/29 18:35:05 $
00111  */
00112 
00113 #endif

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:48 2005 by Doxygen 1.4.4