/******************************************************************************* * SimulatedRobotControl.c * * ----------------------- * * copyright : (C) 2013 by Richard R. Carrillo and Niceto R. Luque * * email : rcarrillo, nluque at ugr.es * *******************************************************************************/ /*************************************************************************** * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * ***************************************************************************/ /*! * \file SimulatedRobotControl.c * * \author Niceto R. Luque * \author Richard R. Carrillo * \date 7 of November 2013 * In this file the main robot-control loop is implemented. */ #include using namespace std; #if defined(_DEBUG) && (defined(_WIN32) || defined(_WIN64)) # define _CRTDBG_MAP_ALLOC # include // To check for memory leaks #endif #if defined(__APPLE_CC__) // MAC OS X includes # define REAL_TIME_OSX #elif defined (__linux__) // Linux includes # define REAL_TIME_LINUX #elif (defined(_WIN32) || defined(_WIN64)) # define REAL_TIME_WINNT #else # error "Unsupported platform" #endif #if defined(REAL_TIME_OSX) # include # include # include # include #elif defined(REAL_TIME_LINUX) # include #elif defined(REAL_TIME_WINNT) # include #endif #include #include "../include/interface/C_interface_for_robot_control.h" //ROBOT// #include "../include/arm_robot_simulator/ArmRobotSimulation.h" #include "../include/openmp/openmp.h" // Neural-network simulation files #define NET_FILE "NetDistributedPlasticity6dcnALLLearnings.net"//"NetDistributedPlasticity2dcnCos.net"// Neural-network definition file used by EDLUT #define INPUT_WEIGHT_FILE "WeightNetDistributedPlasticity6dcnALLLearnings.net"//"WeightNetDistributedPlasticity2dcnFinal.net"// Neural-network input weight file used by EDLUT #define OUTPUT_WEIGHT_FILE "OutputWeight_3_.dat" // Neural-network output weight file used by EDLUT #define WEIGHT_SAVE_PERIOD 0 // The weights will be saved each period (in seconds) (0=weights are not saved periodically) //#define INPUT_ACTIVITY_FILE "ActivityClosingLoop6DCNGR25trajectory1seg_during5000seg.dat"//"ActivityClosingLoop2DCNGR25trajectory1seg_during5000seg.dat" //Optional input activity file //#define INPUT_ACTIVITY_FILE "Activity.dat"//"ActivityClosingLoop2DCNGR25trajectory1seg_during5000seg.dat" //Optional input activity file #define INPUT_ACTIVITY_FILE 0 #define OUTPUT_ACTIVITY_FILE "OutputActivity3.dat" // Output activity file used to register the neural network activity #define LOG_FILE "vars3.dat" // Log file used to register the simulation variables #define REAL_TIME_NEURAL_SIM 0 // EDLUT's simulation mode (0=No real-time neural network simulation 1=For real robot control) #define FIRST_REAL_TIME_RESTRICTION 0.7 #define SECOND_REAL_TIME_RESTRICTION 0.8 #define THIRD_REAL_TIME_RESTRICTION 0.9 #define NUMBER_OF_OPENMP_QUEUES 3 #define NUMBER_OF_OPENMP_THREADS 3 #define ERROR_AMP 1 // Amplitude of the injected error #define NUM_REP 1 // Number of repetition of the exponential shape along the Trajectory Time #define TRAJECTORY_TIME 1 // Simulation time in seconds required to execute the desired trajectory once #define MAX_TRAJ_EXECUTIONS 1000 // Maximum number of trajectories repetitions that will be executed by the robot #define ERROR_DELAY_TIME 0.1 // Delay after calculating the error vars #define ROBOT_JOINT_ERROR_NORM 160 // proportional error {160} ///////////////////////////// MAIN LOOP ////////////////////////// int main(int ac, char *av[]) { int errorn; long total_output_spks; double cerebellar_output_vars[NUM_OUTPUT_VARS]={0.0,0.0,0.0,0.0,0.0,0.0}; // Corrective cerebellar output torque double cerebellar_error_vars[NUM_JOINTS*3]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}; // error corrective cerebellar output torque double cerebellar_learning_vars[NUM_OUTPUT_VARS]={0.0, 0.0,0.0, 0.0,0.0, 0.0}; // Error-related learning signals // Error-related vars(contruction of the error-base reference) double cerebellar_gaussian_poscenters[NUM_REP*NUM_JOINTS*3]; double cerebellar_gaussian_negcenters[NUM_REP*NUM_JOINTS*3]; double cerebellar_gaussian_sigmas[NUM_REP*NUM_JOINTS*3]; double input_error_vars[NUM_JOINTS*3]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}; double error_vars[NUM_JOINTS]={0.0,0.0,0.0}; // delayed Error-related learning signals double *delayed_cerebellar_vars; double *delayed_cerebellar_vars_normalized; double *delayed_cerebellar_learning_vars; double *delayed_error_vars; // Simul variables Simulation *neural_sim; // Robot variables int n_robot_joints; // Time variables double sim_time,cur_traject_time; double time; float slot_elapsed_time,sim_elapsed_time; int n_traj_exec; // Delays struct delay cerebellar_delay; struct delay cerebellar_learning_delay; struct delay cerebellar_delay_normalized; // Variable for logging the simulation state variables struct log var_log; #if defined(REAL_TIME_WINNT) // Variables for consumed-CPU-time measurement LARGE_INTEGER startt,endt,freq; #elif defined(REAL_TIME_OSX) uint64_t startt, endt, elapsed; static mach_timebase_info_data_t freq; #elif defined(REAL_TIME_LINUX) // Calculate time taken by a request - Link with real-time library -lrt struct timespec startt, endt, freq; #endif #if defined(_DEBUG) && (defined(_WIN32) || defined(_WIN64)) // _CrtMemState state0; _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); #endif #if defined(REAL_TIME_WINNT) if(!QueryPerformanceFrequency(&freq)) puts("QueryPerformanceFrequency failed"); #elif defined (REAL_TIME_LINUX) if(clock_getres(CLOCK_REALTIME, &freq)) puts("clock_getres failed"); #elif defined (REAL_TIME_OSX) // If this is the first time we've run, get the timebase. // We can use denom == 0 to indicate that sTimebaseInfo is // uninitialised because it makes no sense to have a zero // denominator is a fraction. if (freq.denom == 0 ) { (void) mach_timebase_info(&freq); } #endif // Initialize variable log if(!(errorn=create_log(&var_log, MAX_TRAJ_EXECUTIONS, TRAJECTORY_TIME))){ // Initialize EDLUT and load neural network files neural_sim=create_neural_simulation(NET_FILE, INPUT_WEIGHT_FILE, INPUT_ACTIVITY_FILE, OUTPUT_WEIGHT_FILE, OUTPUT_ACTIVITY_FILE, WEIGHT_SAVE_PERIOD, NUMBER_OF_OPENMP_QUEUES, NUMBER_OF_OPENMP_THREADS); if(neural_sim){ total_output_spks=0L; puts("Simulating..."); sim_elapsed_time=0.0; errorn=0; // _CrtMemCheckpoint(&state0); bool real_time_neural_simulation=false; if(REAL_TIME_NEURAL_SIM==1){ #ifdef _OPENMP omp_set_nested(true); real_time_neural_simulation=true; cout<<"\nFixed REAL TIME SIMULATION option\n"<1) default(shared) private( n_traj_exec, sim_time, cur_traject_time) { //Select the correspondent device. if(NumberOfGPUs>0){ HANDLE_ERROR(cudaSetDevice(GPUsIndex[openMP_index % NumberOfGPUs])); } if(omp_get_thread_num()>0){ for(n_traj_exec=0;n_traj_exec Number of updates: " << neural_sim.GetSimulationUpdates(i) << endl; /*asdfgf*/ cout << "Thread "< Number of InternalSpike: " << neural_sim.GetTotalSpikeCounter(i) << endl; /*asdfgf*/ cout << "Thread "< Number of Propagated Spikes and Events: " << neural_sim.GetTotalPropagateCounter(i)<<", "<< neural_sim.GetTotalPropagateEventCounter(i)<< endl; /*asdfgf*/ cout << "Thread "< Mean number of spikes in heap: " << neural_sim.GetHeapAcumSize(i)/(float)neural_sim.GetSimulationUpdates(i) << endl; /*asdfgf*/ } cout << "Total InternalSpike: " << neural_sim.GetTotalSpikeCounter()<