Skip to content

Commit d2e2a62

Browse files
committed
Merged battery roy's branch into revolve battery branched from development.
2 parents 6d98821 + 04614a2 commit d2e2a62

35 files changed

Lines changed: 1622 additions & 158 deletions

cpprevolve/revolve/gazebo/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ set_source_files_properties(
169169
# Plugin C++ files
170170
file(GLOB_RECURSE
171171
REVOLVE_GZ_SRC
172+
battery/*.cpp
172173
brains/*.cpp
173174
motors/*.cpp
174175
sensors/*.cpp
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Created by Roy Basmacier on 2019-07-09.
3+
//
4+
5+
#include "Battery.h"
6+
7+
using namespace revolve::gazebo;
8+
9+
Battery::Battery(double initial_charge)
10+
: initial_charge(initial_charge), current_charge(initial_charge), time_init(std::to_string(time(0))), robot_name("")
11+
{}
12+
13+
void Battery::Update(double global_time, double delta_time)
14+
{
15+
double sum = 0.0;
16+
// std::cout << "battery: " << this->Voltage() << "V" << std::endl;
17+
for (const auto &consumer: this->PowerLoads()) {
18+
// std::cout << "comsumer: " << consumer.first << " -> " << consumer.second << std::endl;
19+
sum += consumer.second; // TODO add constant so its linear
20+
}
21+
this->current_charge += sum * delta_time; // charge is measured in joules
22+
23+
//TODO properly save battery data somewhere
24+
std::ofstream b_info_file;
25+
b_info_file.open("output/cpg_bo/" + this->robot_name + "/" + this->time_init + "/battery.txt", std::ios_base::app);
26+
if (b_info_file.fail())
27+
std::cout << "Failed to open: " << b_info_file.fail() << " " << "output/cpg_bo/" + this->robot_name + "/" + this->time_init + "/battery.txt" << std::endl;
28+
b_info_file << global_time << " " << sum << " " << current_charge << std::endl;
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Created by Roy Basmacier on 2019-07-09.
3+
//
4+
5+
#ifndef REVOLVE_BATTERY_H
6+
#define REVOLVE_BATTERY_H
7+
8+
9+
#include <gazebo/gazebo.hh>
10+
#include <gazebo/physics/physics.hh>
11+
#include <gazebo/common/common.hh>
12+
#include <gazebo/msgs/msgs.hh>
13+
14+
#include <revolve/gazebo/Types.h>
15+
16+
namespace revolve{
17+
namespace gazebo{
18+
19+
class Battery : public ::gazebo::common::Battery
20+
{
21+
public:
22+
explicit Battery(double initial_charge);
23+
24+
void Update(double global_time, double delta_time);
25+
26+
protected:
27+
/// \brief initial charge of the battery in joules
28+
double initial_charge; // it is set in RobotController.cpp
29+
30+
/// \brief current charge of the battery in joules
31+
double current_charge;
32+
33+
/// \brief the time of initiation (for creating data files of battery delete later)
34+
std::string time_init;
35+
36+
std::string robot_name;
37+
38+
friend class RobotController;
39+
friend class Evaluator;
40+
friend class DifferentialCPG;
41+
};
42+
43+
}
44+
}
45+
46+
#endif //REVOLVE_BATTERY_H

cpprevolve/revolve/gazebo/brains/DifferentialCPG.cpp

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ using Init_t = limbo::init::LHS<DifferentialCPG::Params>;
6464
using Kernel_t = limbo::kernel::MaternFiveHalves<DifferentialCPG::Params>;
6565
using GP_t = limbo::model::GP<DifferentialCPG::Params, Kernel_t, Mean_t>;
6666

67+
68+
69+
6770
/**
6871
* Constructor for DifferentialCPG class.
6972
*
@@ -74,10 +77,13 @@ DifferentialCPG::DifferentialCPG(
7477
const ::gazebo::physics::ModelPtr &_model,
7578
const sdf::ElementPtr robot_config,
7679
const std::vector< revolve::gazebo::MotorPtr > &_motors,
77-
const std::vector< revolve::gazebo::SensorPtr > &_sensors)
80+
const std::vector< revolve::gazebo::SensorPtr > &_sensors,
81+
std::shared_ptr<::revolve::gazebo::Battery> battery
82+
)
7883
: next_state(nullptr)
7984
, input(new double[_sensors.size()])
8085
, output(new double[_motors.size()])
86+
, battery_(battery)
8187
{
8288

8389
this->learner = robot_config->GetElement("rv:brain")->GetElement("rv:learner");
@@ -279,17 +285,24 @@ DifferentialCPG::DifferentialCPG(
279285
}
280286
}
281287

288+
289+
282290
// Create directory for output.
283-
this->directory_name = controller->GetAttribute("output_directory")->GetAsString();
291+
// this->directory_name = controller->GetAttribute("output_directory")->GetAsString();
284292
if(this->directory_name.empty())
285293
{
286-
this->directory_name = "output/cpg_bo/";
287-
this->directory_name += std::to_string(time(0)) + "/";
294+
std::cout << "§yes§";
295+
this->directory_name = "output/cpg_bo/" + this->robot->GetName() + "/"; // CHANGETHIS
296+
this->directory_name += this->battery_->time_init + "/";
288297
}
298+
else
299+
std::cout << "§no§\n";
300+
301+
289302

290-
std::system(("mkdir -p " + this->directory_name).c_str());
303+
std::system(("mkdir -p " + this->directory_name).c_str());
291304

292-
// Initialise array of neuron states for Update() method
305+
// Initialise array of neuron states for Update() methodc
293306
this->next_state = new double[this->neurons.size()];
294307
this->n_weights = (int)(this->connections.size()/2) + this->n_motors;
295308

@@ -353,7 +366,7 @@ DifferentialCPG::DifferentialCPG(
353366
}
354367

355368
// Initiate the cpp Evaluator
356-
this->evaluator.reset(new Evaluator(this->evaluation_rate));
369+
this->evaluator.reset(new Evaluator(battery, this->evaluation_rate));
357370
this->evaluator->directory_name = this->directory_name;
358371
}
359372

@@ -370,9 +383,21 @@ DifferentialCPG::~DifferentialCPG()
370383
/**
371384
* Dummy function for limbo
372385
*/
386+
387+
373388
struct DifferentialCPG::evaluation_function{
374389
// Number of input dimension (samples.size())
375-
BO_PARAM(size_t, dim_in, 18);
390+
// spider9:18,
391+
// spider13:26,
392+
// spider17:34,
393+
// gecko7:13,
394+
// gecko12:23,
395+
// gecko17:33,
396+
// babyA:16,
397+
// babyB:22,
398+
// babyC:32,
399+
// one+:12
400+
BO_PARAM(size_t, dim_in, 13); // CHANGETHIS
376401

377402
// number of dimensions of the fitness
378403
BO_PARAM(size_t, dim_out, 1);
@@ -409,6 +434,9 @@ void DifferentialCPG::bo_init_sampling(){
409434
Eigen::VectorXd init_sample(this->n_weights);
410435

411436
// For all weights
437+
srand((unsigned)time(NULL));
438+
// trash first one, because it could be the same and I do not know why
439+
auto trash_first = rand();
412440
for (size_t j = 0; j < this->n_weights; j++)
413441
{
414442
// Generate a random number in [0, 1]. Transform later
@@ -456,6 +484,9 @@ void DifferentialCPG::bo_init_sampling(){
456484
Eigen::VectorXd init_sample(this->n_weights);
457485

458486
// For all dimensions
487+
srand((unsigned)time(NULL));
488+
// trash first one, because it could be the same and I do not know why
489+
auto trash_first = rand();
459490
for (size_t j = 0; j < this->n_weights; j++)
460491
{
461492
// Take a LHS
@@ -637,6 +668,7 @@ void DifferentialCPG::bo_step(){
637668
limbo::acquifun<limbo::acqui::UCB<DifferentialCPG::Params, GP_t>>> boptimizer;
638669

639670
// Optimize. Pass dummy evaluation function and observations .
671+
640672
boptimizer.optimize(DifferentialCPG::evaluation_function(),
641673
this->samples,
642674
this->observations);
@@ -814,7 +846,7 @@ void DifferentialCPG::Update(
814846
{
815847
std::cout << std::endl << "I am finished " << std::endl;
816848
}
817-
std::exit(0);
849+
// std::exit(0);
818850
}
819851

820852
// Evaluation policy here

cpprevolve/revolve/gazebo/brains/DifferentialCPG.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ namespace revolve
6868
const ::gazebo::physics::ModelPtr &_model,
6969
const sdf::ElementPtr robot_config,
7070
const std::vector< MotorPtr > &_motors,
71-
const std::vector< SensorPtr > &_sensors);
71+
const std::vector< SensorPtr > &_sensors,
72+
std::shared_ptr<::revolve::gazebo::Battery> battery);
7273

7374
public: void set_ode_matrix();
7475

@@ -259,6 +260,9 @@ namespace revolve
259260
/// \brief Use frame of reference {-1,0,1} version or not
260261
private: bool use_frame_of_reference;
261262

263+
/// \brief shared pointer to battery
264+
protected: std::shared_ptr<::revolve::gazebo::Battery> battery_;
265+
262266
// BO Learner parameters
263267
private: double kernel_noise_;
264268
private: bool kernel_optimize_noise_;

0 commit comments

Comments
 (0)