00001 #include <signal.h>
00002 #include <unistd.h>
00003 #include <iostream>
00004 #include <vector>
00005 #include <list>
00006 #include <iterator>
00007 using namespace std;
00008
00009 #include <selforg/agent.h>
00010 #include <selforg/abstractrobot.h>
00011 #include <selforg/invertmotorspace.h>
00012 #include <selforg/one2onewiring.h>
00013
00014 #include "cmdline.h"
00015
00016 bool stop=0;
00017
00018 class MyRobot : public AbstractRobot {
00019 public:
00020 MyRobot()
00021 : AbstractRobot("MyRobot", "$Id: main.cpp,v 1.3 2006/08/04 15:16:13 martius Exp $") {
00022 myparam=0;
00023 motornumber = 3;
00024 sensornumber = 3;
00025 x = new double[sensornumber];
00026 y = new double[motornumber];
00027 }
00028
00029 MyRobot::~MyRobot(){
00030 if(x) delete[] x;
00031 if(y) delete[] y;
00032 }
00033
00034
00035
00036
00037
00038
00039
00040
00041 virtual int getSensors(sensor* sensors, int sensornumber){
00042 assert(sensornumber == this->sensornumber);
00043 memcpy(sensors, x, sizeof(sensor) * sensornumber);
00044 return sensornumber;
00045 }
00046
00047
00048
00049
00050
00051 virtual void setMotors(const motor* motors, int motornumber){
00052 assert(motornumber == this->motornumber);
00053 memcpy(y, motors, sizeof(motor) * motornumber);
00054
00055
00056
00057
00058
00059
00060 for(int i=0; i<sensornumber; i++){
00061 x[i] = sin(myparam) * y[i%motornumber];
00062 }
00063 myparam+=0.01;
00064
00065 }
00066
00067
00068 virtual int getSensorNumber(){ return sensornumber; }
00069
00070
00071 virtual int getMotorNumber() { return motornumber; }
00072
00073
00074
00075 virtual Position getPosition() const {return Position(0,0,0);}
00076
00077
00078
00079
00080 virtual Position getSpeed() const {return Position(0,0,0);}
00081
00082
00083
00084
00085 virtual matrix::Matrix getOrientation() const {
00086 matrix::Matrix m(3,3);
00087 m.toId();
00088 return m;
00089 };
00090
00091 virtual paramval getParam(const paramkey& key) const{
00092 if(key == "myparam") return myparam;
00093 else return Configurable::getParam(key);
00094 }
00095
00096 virtual bool setParam(const paramkey& key, paramval val){
00097 cerr << "huhu";
00098 if(key == "myparam") myparam = val;
00099 else return Configurable::setParam(key, val);
00100 return true;
00101 }
00102
00103 virtual paramlist getParamList() const {
00104 paramlist list;
00105 list += pair<paramkey, paramval> (string("myparam"), myparam);
00106 return list;
00107 };
00108
00109 private:
00110 int motornumber;
00111 int sensornumber;
00112
00113 double* x;
00114 double* y;
00115
00116 paramval myparam;
00117 };
00118
00119
00120 void onTermination(){
00121 stop=1;
00122 }
00123
00124
00125 int contains(char **list, int len, const char *str){
00126 for(int i=0; i<len; i++){
00127 if(strcmp(list[i],str) == 0) return i+1;
00128 }
00129 return 0;
00130 }
00131
00132 int main(int argc, char** argv){
00133 ConfigList configs;
00134 list<PlotOption> plotoptions;
00135
00136 AbstractController* controller = new InvertMotorSpace(10);
00137 controller->setParam("s4delay",2.0);
00138 controller->setParam("s4avg",2.0);
00139
00140 if(contains(argv,argc,"-g")!=0) plotoptions.push_back(PlotOption(GuiLogger));
00141 if(contains(argv,argc,"-f")!=0) plotoptions.push_back(PlotOption(File));
00142 if(contains(argv,argc,"-h")!=0) {
00143 printf("Usage: %s [-g] [-f]\n",argv[0]);
00144 printf("\t-g\tstart guilogger\n\t-f\twrite logfile\n\t-h\tdisplay this help\n");
00145 exit(0);
00146 }
00147
00148 printf("\nPress Ctrl-c to invoke parameter input shell (and again Ctrl-c to quit)\n");
00149
00150 AbstractRobot* robot = new MyRobot();
00151 Agent* agent = new Agent(plotoptions);
00152 AbstractWiring* wiring = new One2OneWiring(new ColorUniformNoise(0.1));
00153 agent->init(controller, robot, wiring);
00154
00155
00156
00157
00158 configs.push_back(robot);
00159 configs.push_back(controller);
00160 showParams(configs);
00161
00162 cmd_handler_init();
00163 while(!stop){
00164 usleep(1000);
00165 agent->step(0.1);
00166 if(control_c_pressed()){
00167 cmd_begin_input();
00168 changeParams(configs, onTermination);
00169 cmd_end_input();
00170 }
00171
00172 };
00173
00174
00175 fprintf(stderr,"terminating\n");
00176 delete agent;
00177 delete controller;
00178 delete wiring;
00179 delete robot;
00180
00181 return 0;
00182 }