directconnect/directconnect.cpp

A simple example for the usage of the controller without the framework. This version means that you have the direct control of the data flow.

00001 #include<assert.h>
00002 #include<iostream>
00003 #include<vector>
00004 using namespace std;
00005 
00006 #include <selforg/sinecontroller.h>
00007 #include <selforg/invertmotorspace.h>
00008 
00009 const int MNumber = 2;
00010 const int SNumber = 2;
00011 
00012 /** The robot control should go here
00013     @param sensors list of sensor values (to be written) (doubles)
00014     @param sensornumber length of the sensors vector
00015     @param motors list of motor values (doubles) (to send to the robot)
00016     @param motornumber length of the motors vector
00017 
00018  */
00019 void myrobot(double* sensors, int sensornumber, const double* motors, int motornumber){  
00020   assert(sensornumber >= 2 && motornumber >= 2); // check dimensions
00021   //the robot consits here just of a bit noise
00022   sensors[0]=motors[0]+(double(rand())/RAND_MAX-0.5)*0.3;
00023   sensors[1]=motors[1]+(double(rand())/RAND_MAX-0.5)*0.3;
00024 
00025 }
00026 
00027 
00028 int main(){
00029 
00030   AbstractController* controller = new InvertMotorSpace(10);
00031   controller->init(2,2); // initialise with 2 motors and 2 sensors
00032   controller->setParam("epsA",0.01); // set parameter epsA (learning rate for Model A)
00033   controller->print(stderr,0); // print parameters (see Configurable) to stderr
00034 
00035   
00036   // sensor and motor arrays (doubles*)
00037   double sensors[SNumber];
00038   double motors[MNumber];
00039   
00040   memset(motors,0,sizeof(double)*MNumber);  // clear motors
00041 
00042   // the robot is here respresented by the function myrobot
00043   for(int i=0; i < 1000; i++){
00044     // call robot with motors and receive sensors 
00045     myrobot(sensors, SNumber, motors, MNumber);
00046     cout << i << " S0: " << sensors[0] << ", " <<" S1: " << sensors[1];
00047 
00048     // print some internal parameters of the controller
00049     list<Inspectable::iparamval> list_ = controller->getInternalParams();  
00050     vector<Inspectable::iparamval> v(list_.begin(), list_.end());  
00051     cout << i << " C00: " << v[4] << ", " <<" C01: " << v[5] << ", " <<
00052       " C10: " << v[6] << ", " <<" C11: " << v[7]  << endl;
00053     
00054     // call controller with sensors and receive motors (both dimension 2)    
00055     controller->step(sensors, SNumber, motors, MNumber); 
00056     cout << i << " Motor values: " << motors[0] << ", " << motors[1] << endl;
00057   }
00058   delete controller;
00059   return 0;
00060 }
00061 
00062 

Generated on Fri Oct 30 16:29:00 2009 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.4.7