core.cc

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Jakub Filak <xfilak01@stud.fit.vutbr.cz>
00003  *
00004  * This file is part of rob08
00005  *
00006  * rob08 is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * any later version.
00010  *
00011  * rob08 is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with rob08.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include "config.h"
00021 #include "core.h"
00022 #include "world.h"
00023 #include "trilobot.h"
00024 
00025 // internal includes, alphabetically sorted
00026 #include "scanner.h"
00027 // ATTENTION: do not include "term.h" if possible
00028 
00029 #ifndef BUILDING_DOX
00030 // system includes, alphabetically sorted
00031 #   include <string>
00032 #   include <iostream>
00033 #endif
00034 
00036 class CoreExporter : public IExporter
00037 {
00038   public:
00039     CoreExporter( ICanExportImport* const e, std::ostream* const output ) 
00040       : exp( e ), out( output )
00041     {}
00042 
00043     virtual void MakeExport();
00044 
00045     virtual ~CoreExporter() {}
00046 
00047   private:
00048     ICanExportImport* const exp;
00049     std::ostream* const out;
00050 };
00052 class Core: public ICore {
00053     public:
00054         Core(IWriter *);
00055         virtual ~Core();
00056 
00057         virtual bool hasError() const { return hasError_; }
00058         virtual bool dispatch(const Token &);
00059   virtual IEnviroment& GetEnviroment() { return m_world.Enviroment(); }
00060   virtual Trilobot& GetTrilobot() { return m_robot; }
00061   virtual void Export( std::ostream& output ) { this->m_robot.Serialize(output); output << std::endl; } 
00062   virtual void Import( std::istream& input ) { this->m_robot.Deserialize(input); }
00063 
00064     private:
00065         IWriter         *writer_;
00066         bool            hasError_;
00067         // TODO: implement
00068         // TODO: remove following garbage
00069         unsigned whiskMask;
00070         unsigned distance;
00071 
00072   World m_world;
00073   CoreExporter m_export;
00074   Trilobot m_robot;
00075 };
00076 
00077 // /////////////////////////////////////////////////////////////////////////////
00078 // CoreExporter 
00079 void CoreExporter::MakeExport() {
00080   if( !DEBUG_DONT_EXPORT ) {
00081     *out << "#BEGIN#" << std::endl;
00082     exp->Export( *out );
00083     *out << "#END#" << std::endl;
00084   }
00085 }
00086 
00087 // /////////////////////////////////////////////////////////////////////////////
00088 // CoreFactory implementation
00089 ICore* CoreFactory::createCore(IWriter *writer) {
00090     return new Core(writer);
00091 }
00092 
00093 // /////////////////////////////////////////////////////////////////////////////
00094 // Core implementation
00095 Core::Core(IWriter *writer):
00096     writer_(writer),
00097     hasError_(false),
00098     m_world( ),
00099     m_export( this, &std::cout ),
00100     m_robot( &m_world, &m_export )
00101 {
00102     // TODO: implement
00103     // TODO: remove following garbage
00104     whiskMask = distance = 0;
00105 }
00106 
00107 Core::~Core() {
00108     // TODO: implement
00109     // ATTENTION: do not delete IWriter object in this module
00110 }
00111 
00112 bool Core::dispatch(const Token &t) {
00113     // TODO: implement
00114     // TODO: remove following garbage
00115 
00116     TokenCommand cmd( t );
00117     m_robot.Handle( cmd );
00118 
00119     if( cmd.Handled() )
00120     {
00121   m_robot.Position();
00122   if( 0 != this->writer_ ) {
00123     cmd.Answer()->Write( *this->writer_ );
00124   }
00125   return true;
00126     }
00127 
00128     static unsigned char swVersionMajor = 7;
00129     static unsigned char swVersionMinor = 0;
00130     switch (t.type) {
00131         case ET_GET_SW_VERSION:
00132             writer_->writeByte(swVersionMajor);
00133             writer_->writeByte(swVersionMinor);
00134             break;
00135 
00136         case ET_GET_WHISKER_STATUS:
00137             writer_->writeByte(++whiskMask);
00138             break;
00139 
00140         case ET_GET_COMPASS_HEADING:
00141             writer_->writeByte(-whiskMask);
00142             break;
00143 
00144         case ET_GET_TEMP:
00145             // 67 degrees of Celsius
00146             writer_->writeByte(0x99);
00147             break;
00148 
00149         case ET_GET_WATER:
00150             // no watter now
00151             writer_->writeByte(0);
00152             break;
00153 
00154         case ET_GET_PIR:
00155             // PIR activity detected
00156             writer_->writeByte(1);
00157             break;
00158 
00159         case ET_GET_SONAR:
00160             writer_->writeByte(distance);
00161             break;
00162 
00163         case ET_GET_TILT:
00164             writer_->writeByte(0x0A);
00165             break;
00166 
00167         case ET_GET_BATT_VOLTAGE:
00168             // 11.8V (or 11.08V ???)
00169             writer_->writeByte(11);
00170             writer_->writeByte(8);
00171             break;
00172 
00173         case ET_GET_BUTTONS:
00174             writer_->writeByte(0x77);
00175             break;
00176 
00177         case ET_GET_LIGHT_LEVEL:
00178             // 4 separate light level sensors
00179             writer_->writeByte(0x11);
00180             writer_->writeByte(0x22);
00181             writer_->writeByte(0x33);
00182             writer_->writeByte(0x44);
00183             break;
00184 
00185         case ET_GET_SOUND_LEVEL:
00186             writer_->writeByte(0x88);
00187             break;
00188 
00189         case ET_GET_ENCODER_COUNTS:
00190             writer_->writeWord(distance);
00191             break;
00192 
00193         case ET_CMD_PG:
00194         case ET_CMD_PH:
00195         case ET_CMD_PL:
00196         case ET_CMD_PN:
00197         case ET_CMD_PW:
00198             // dummy implementation
00199             writer_->writeAck();
00200             break;
00201 
00202         case ET_CMD_PS:
00203             // experimental end of simulation
00204             return false;
00205 
00206         case ET_CMD_PM:
00207             distance += t.args[1];
00208             writer_->writeAck();
00209             break;
00210 
00211         default:
00212             break;
00213     }
00214     return true;
00215 }
00216 

Generated on Fri Jul 10 22:42:01 2009 for rob08 by  doxygen 1.5.4