YANE-Framework Tutorial 1.1.0

examples/mpctcp/src/modelmain.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  * Copyright (C) 2011 by www.nonlinearmpc.com                              *
00004  *                                                                         *
00005  * Authors:                                                                *
00006  *  Thomas Jahn <thomas.jahn@uni-bayreuth.de>                              *
00007  *  Juergen Pannek <juergen.pannek@googlemail.com>                         *
00008  *                                                                         *
00009  ***************************************************************************/
00010 
00011 #include <yane.h>
00012 #include <examplemodels2.h>
00013 #include <iostream>
00014 #include <csignal>
00015 #include <string>
00016 #include <sstream>
00017 #include <fstream>
00018 #include <cstdlib>
00019 
00020 #include "simplesimulator.h"
00021 #include "simplehandler.h"
00022 
00023 #define ABORTONOPTIMALITY false
00024 #define SEQLENGTH 17
00025 
00026 using namespace std;
00027 
00028 bool abort_mpc = false;
00029 
00030 void abortsig ( int signalnummer )
00031 {
00032         abort_mpc = true;
00033 }
00034 
00035 int main ( int argc, char *argv [ ] )
00036 {
00037         signal ( SIGINT, abortsig );
00038 
00039         stringstream basename;
00040         basename << "protocol_model_";
00041 
00042         time_t currenttime = time ( 0 );
00043         char tbuf [ 256 ];
00044         strftime ( tbuf, 256, "%Y%m%d_%H%M%S", localtime ( & currenttime ) );
00045 
00046         basename << tbuf << "_seq" << SEQLENGTH;
00047 
00048         string debugfilename = basename.str ( ) + ".debug.txt";
00049         string statfilename = basename.str ( ) + ".stat.txt";
00050 
00051         yane::Utils::Exception::enableDebugMessage ( true );
00052 
00053         yane::NetworkedControl::Cycle * cycle = new yane::NetworkedControl::Cycle (
00054                 0.1 );
00055         yane::ExampleModels2::InvertedPendulum * model =
00056                 new yane::ExampleModels2::InvertedPendulum ( );
00057 
00058         yane::Utils::StatisticsMaster * sm = new yane::Utils::StatisticsMaster ( );
00059         yane::Utils::DebugMasterFile * dm = new yane::Utils::DebugMasterFile (
00060                 cycle, debugfilename.c_str ( ), 120 );
00061         dm->setLevel ( 6 );
00062 
00063         SimpleHandler * handler = new SimpleHandler ( model, cycle, 17, dm, sm );
00064         SimpleSimulator * simulator = new SimpleSimulator ( cycle, model, dm );
00065 
00066         simulator->setModelHandler ( handler );
00067         handler->setSimulator ( simulator );
00068 
00069         simulator->start ( );
00070         handler->start ( );
00071 
00072         while ( ! abort_mpc && ( ! simulator->isOptimal ( )
00073                 || ! ( ABORTONOPTIMALITY ) ) )
00074         {
00075                 usleep ( 100000 );
00076         }
00077 
00078         double neededtime = cycle->elapsedSeconds ( );
00079 
00080         string stat = sm->evaluateStatistics ( );
00081 
00082         handler->stop ( );
00083         simulator->stop ( );
00084 
00085         ofstream statfile ( statfilename.c_str ( ) );
00086         statfile << "elapsed seconds: " << neededtime << endl << endl;
00087         statfile << stat;
00088         statfile.close ( );
00089 
00090         cout << "elapsed seconds: " << neededtime << endl << endl;
00091         cout << sm->evaluateStatistics ( ) << endl;
00092 
00093 }