00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 #include "agent.h"
00091 #include <signal.h>
00092 #include "printInternals.h"
00093
00094 #include "abstractrobot.h"
00095 #include "abstractcontroller.h"
00096 #include "abstractwiring.h"
00097
00098 Agent::Agent(const PlotOption& plotOption){
00099 internInit();
00100 plotOptions.push_back(plotOption);
00101 }
00102
00103
00104 Agent::Agent(const list<PlotOption>& plotOptions)
00105 : plotOptions(plotOptions){
00106 internInit();
00107 }
00108
00109 void Agent::internInit(){
00110 controller = 0;
00111 robot = 0;
00112 wiring = 0;
00113
00114 rsensors=0; rmotors=0;
00115 csensors=0; cmotors=0;
00116
00117 t=0;
00118 }
00119
00120 Agent::~Agent(){
00121 for(list<PlotOption>::iterator i=plotOptions.begin(); i != plotOptions.end(); i++){
00122 (*i).close();
00123 }
00124 trackrobot.close();
00125 if(rsensors) free(rsensors);
00126 if(rmotors) free(rmotors);
00127 if(csensors) free(csensors);
00128 if(cmotors) free(cmotors);
00129 }
00130
00131
00132
00133
00134 bool Agent::init(AbstractController* controller, AbstractRobot* robot, AbstractWiring* wiring){
00135 this->controller = controller;
00136 this->robot = robot;
00137 this->wiring = wiring;
00138 if(!controller || !robot || !wiring) return false;
00139 else{
00140 rsensornumber = robot->getSensorNumber();
00141 rmotornumber = robot->getMotorNumber();
00142 wiring->init(rsensornumber, rmotornumber);
00143 csensornumber = wiring->getControllerSensornumber();
00144 cmotornumber = wiring->getControllerMotornumber();
00145 controller->init(csensornumber, cmotornumber);
00146
00147 rsensors = (sensor*) malloc(sizeof(sensor) * rsensornumber);
00148 rmotors = (motor*) malloc(sizeof(motor) * rmotornumber);
00149 csensors = (sensor*) malloc(sizeof(sensor) * csensornumber);
00150 cmotors = (motor*) malloc(sizeof(motor) * cmotornumber);
00151
00152 for(list<PlotOption>::iterator i=plotOptions.begin(); i != plotOptions.end(); i++){
00153 if((*i).open()){
00154
00155 printNetworkDescription((*i).pipe, "Lpzrobots", controller);
00156
00157 if((*i).interval > 1) fprintf((*i).pipe, "# Recording every %dth dataset\n", (*i).interval);
00158
00159 controller->print((*i).pipe, "# ");
00160
00161 unsigned int snum = (*i).whichSensors == Robot ? rsensornumber : csensornumber;
00162 Inspectable* inspectables[2] = {controller, wiring};
00163 printInternalParameterNames((*i).pipe, snum, cmotornumber, inspectables, 2);
00164 }
00165 }
00166 return true;
00167 }
00168 }
00169
00170
00171 bool PlotOption::open(){
00172
00173
00174 signal(SIGPIPE,SIG_IGN);
00175
00176 switch(mode){
00177 case GuiLogger_File:
00178 pipe=popen("guilogger -l -m pipe -d 5","w");
00179 break;
00180 case GuiLogger:
00181 pipe=popen("guilogger -m pipe -d 5","w");
00182 break;
00183 case NeuronViz:
00184 pipe=popen("neuronviz ","w");
00185 break;
00186 default:
00187 return false;
00188 }
00189 if(pipe==0){
00190 fprintf(stderr, "%s:%i: could not open plot tool!\n", __FILE__, __LINE__);
00191 return false;
00192 }else return true;
00193 }
00194
00195 void PlotOption::close(){
00196 if (pipe) pclose(pipe);
00197 pipe=0;
00198 }
00199
00200
00201 void Agent::plot(const sensor* rx, int rsensornumber, const sensor* cx, int csensornumber,
00202 const motor* y, int motornumber){
00203 assert(controller && rx && cx && y);
00204
00205 Inspectable* inspectables[2] = {controller, wiring};
00206 for(list<PlotOption>::iterator i=plotOptions.begin(); i != plotOptions.end(); i++){
00207 if( ((*i).pipe) && (t % (*i).interval == 0) ){
00208 if((*i).whichSensors == Robot){
00209 printInternalParameters((*i).pipe, rx, rsensornumber, y, motornumber, inspectables , 2);
00210 }else{
00211 printInternalParameters((*i).pipe, cx, csensornumber, y, motornumber, inspectables , 2);
00212 }
00213 if(t% ((*i).interval * 10)) fflush((*i).pipe);
00214 }
00215 }
00216 };
00217
00218
00219
00220
00221
00222
00223
00224 void Agent::step(double noise){
00225
00226 if(!controller || !robot || !wiring || !rsensors || !rmotors || !csensors || !cmotors) {
00227 fprintf(stderr, "%s:%i: something is null: cont %i rob %i wiring %i rsens %i rmots %i csens %i cmots %i!\n",
00228 __FILE__, __LINE__, controller==0, robot==0,
00229 wiring==0, rsensors==0, rmotors==0,
00230 csensors==0, cmotors==0);
00231 }
00232
00233 int len = robot->getSensors(rsensors, rsensornumber);
00234 if(len != rsensornumber){
00235 fprintf(stderr, "%s:%i: Got not enough sensors, expected %i, got %i!\n", __FILE__, __LINE__,
00236 rsensornumber, len);
00237 }
00238
00239 wiring->wireSensors(rsensors, rsensornumber, csensors, csensornumber, noise);
00240 controller->step(csensors, csensornumber, cmotors, cmotornumber);
00241 wiring->wireMotors(rmotors, rmotornumber, cmotors, cmotornumber);
00242 robot->setMotors(rmotors, rmotornumber);
00243 plot(rsensors, rsensornumber, csensors, csensornumber, cmotors, cmotornumber);
00244
00245 trackrobot.track(robot);
00246
00247 t++;
00248 }
00249
00250
00251
00252 void Agent::setTrackOptions(const TrackRobot& trackrobot){
00253 this->trackrobot = trackrobot;
00254 if (trackrobot.trackPos || trackrobot.trackSpeed || trackrobot.trackOrientation){
00255 if(!this->trackrobot.open(robot)){
00256 fprintf(stderr, "could not open trackfile!\n");
00257 }else{
00258
00259 controller->print(this->trackrobot.file, "# ");
00260 }
00261 }
00262 }