Skip to content

Commit

Permalink
compile more than one robot simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
GiorgioSimonini committed Oct 18, 2024
1 parent 158b806 commit 8180f7a
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 72 deletions.
18 changes: 12 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ bin/
**generatedFiles/
*_gen.h
*_gen.cpp
src/thunder_robot/robots/
src/thunder_robot/library/
src/thunder_robot/src/
src/thunder_robot_chrono/robots/
src/thunder_robot_chrono/library/
src/thunder_robot_chrono/src/
src/thunder_robot/robots/**
src/thunder_robot/library/**
src/thunder_robot/src/**
src/thunder_robot_chrono/robots/**
src/thunder_robot_chrono/library/**
src/thunder_robot_chrono/src/**
!src/thunder_robot/robots/.gitkeep
!src/thunder_robot/library/.gitkeep
!src/thunder_robot/src/.gitkeep
!src/thunder_robot_chrono/robots/.gitkeep
!src/thunder_robot_chrono/library/.gitkeep
!src/thunder_robot_chrono/src/.gitkeep
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Thunder - [thunder_dynamics](https://github.com/CentroEPiaggio/thunder_dynamics) v0.2.1
# Thunder - [thunder_dynamics](https://github.com/CentroEPiaggio/thunder_dynamics) - v0.2.1 + 0.0.1

The aim of `thunder_dynamics` is to generate code useful for robot's control.

Expand Down
1 change: 1 addition & 0 deletions src/thunder/library/robot.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ namespace thunder_ns{
// double _mu_;

public:
std::string robotName = "robot";
const int STD_PAR_LINK = 10;
/* Init variables
numJoints: number of joints
Expand Down
16 changes: 8 additions & 8 deletions src/thunder/src/robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,15 +689,15 @@ namespace thunder_ns{
return 1;
}

int Robot::add_function(std::string name, casadi::SX expr, std::vector<std::string> f_args, std::string descr){
// maybe directly model[name] = ...?
if (model.count(name)){
int Robot::add_function(std::string f_name, casadi::SX expr, std::vector<std::string> f_args, std::string descr){
// maybe directly model[f_name] = ...?
if (model.count(f_name)){
// key already exists
return 0;
} else {
model[name] = expr;
fun_args[name] = f_args;
fun_descr[name] = descr;
model[f_name] = expr;
fun_args[f_name] = f_args;
fun_descr[f_name] = descr;

casadi::SXVector inputs(f_args.size());
// for (const auto& arg : f_args) {
Expand All @@ -709,9 +709,9 @@ namespace thunder_ns{
i++;
}

casadi::Function fun(name+"_fun", inputs, {densify(expr)});
casadi::Function fun(robotName+"_"+f_name+"_fun", inputs, {densify(expr)});
// cout<<"fun: "<<fun<<endl;
casadi_fun[name] = fun;
casadi_fun[f_name] = fun;
}

return 1;
Expand Down
17 changes: 10 additions & 7 deletions src/thunder/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace thunder_ns{
int change_to_robot(const string from_robot, const string to_robot, Robot& robot, const string file_path_h, const string file_path_cpp){

// - get parameters from robot - //
std::string robotName = robot.robotName;
int n_joints = robot.get_numJoints();
int numElasticJoints = robot.get_numElasticJoints();
bool ELASTIC = robot.get_ELASTIC();
Expand Down Expand Up @@ -144,6 +145,7 @@ namespace thunder_ns{
replace_all(file_content_h, from_robot, to_robot);

// - add variables - //
replace_all(file_content_h, "/*#-ROBOT_NAME-#*/", robotName);
replace_all(file_content_h, "/*#-n_joints-#*/", to_string(n_joints));
replace_all(file_content_h, "/*#-ELASTIC-#*/", to_string(ELASTIC));
replace_all(file_content_h, "/*#-numElasticJoints-#*/", to_string(numElasticJoints));
Expand All @@ -163,7 +165,7 @@ namespace thunder_ns{
replace_all(file_content_h, "/*#-isElasticJoint-#*/", eJ_str);

// - insert functions - //
string functions_string = "";
string functions_string = "\n";
for (int i=0; i<functions.size(); i++){
functions_string.append("\t\t// - " + functions[i].description + " - //\n");
functions_string.append("\t\tEigen::MatrixXd get_" + functions[i].name + "();\n\n");
Expand Down Expand Up @@ -199,9 +201,10 @@ namespace thunder_ns{
replace_all(file_content_cpp, from_robot, to_robot);

// - insert functions - //
string functions_string = "";
string functions_string = "\n";
for (int i=0; i<functions.size(); i++){
std::string fun_name = functions[i].name;
std::string fun_name = "get_" + functions[i].name;
std::string fun_name_gen = robotName + "_" + functions[i].name;
std::vector<std::string> fun_args = functions[i].args;
std::vector<int> out_size = functions[i].out_size;
// // function arguments
Expand All @@ -211,11 +214,11 @@ namespace thunder_ns{
// }
// other parts
functions_string.append("// - " + functions[i].description + " - //\n");
functions_string.append("Eigen::MatrixXd thunder_" + to_robot + "::get_" + fun_name + "(){\n");
functions_string.append("Eigen::MatrixXd thunder_" + to_robot + "::" + fun_name + "(){\n");
functions_string.append("\tEigen::MatrixXd out;\n");
functions_string.append("\tout.resize("+to_string(out_size[0])+","+to_string(out_size[1])+");\n");
functions_string.append("\tlong long p3[" + fun_name + "_fun_SZ_IW];\n");
functions_string.append("\tdouble p4[" + fun_name + "_fun_SZ_W];\n");
functions_string.append("\tlong long p3[" + fun_name_gen + "_fun_SZ_IW];\n");
functions_string.append("\tdouble p4[" + fun_name_gen + "_fun_SZ_W];\n");
// inputs
functions_string.append("\tconst double* input_[] = {" + fun_args[0]+".data()");
for (int j=1; j<fun_args.size(); j++){
Expand All @@ -224,7 +227,7 @@ namespace thunder_ns{
functions_string.append("};\n");
// output
functions_string.append("\tdouble* output_[] = {out.data()};\n");
functions_string.append("\tint check = " + fun_name + "_fun(input_, output_, p3, p4, 0);\n");
functions_string.append("\tint check = " + fun_name_gen + "_fun(input_, output_, p3, p4, 0);\n");
functions_string.append("\treturn out;\n");
functions_string.append("}\n\n");
}
Expand Down
1 change: 1 addition & 0 deletions src/thunder/thunder_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ int main(int argc, char* argv[]){

// --- Robot creation --- //
Robot robot = robot_from_file(config_file, 0);
robot.robotName = robot_name;
compute_kinematics(robot);
compute_dynamics(robot);
compute_regressors(robot);
Expand Down
1 change: 1 addition & 0 deletions src/thunder/thunder_robot_template/thunder_robot.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class thunder_robot{
Eigen::Matrix3d hat(const Eigen::Vector3d v);

public:
const std::string robotName = "/*#-ROBOT_NAME-#*/";
const int n_joints = /*#-n_joints-#*/;
const bool ELASTIC = /*#-ELASTIC-#*/;
const int numElasticJoints = /*#-numElasticJoints-#*/;
Expand Down
Empty file.
Empty file.
Empty file added src/thunder_robot/src/.gitkeep
Empty file.
95 changes: 48 additions & 47 deletions src/thunder_robot/thunder_robot_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
// #include <yaml-cpp/yaml.h>

// #include "thunder_robot.h"
#include "thunder_RRR.h"
#include "thunder_seaRRR.h"

// #define NJ 3
// #define N_PARAM_DYN 30
// const std::string inertial_file = "../robots/seaRRR_inertial_DYN.yaml";
// const std::string elastic_file = "../../thunder/robots/RRR_sea/seaRRR.yaml";
const std::string inertial_file = "../robots/seaRRR_inertial_DYN.yaml";
const std::string elastic_file = "../../thunder/robots/RRR_sea/seaRRR.yaml";
const std::string inertial_file = "../robots/RRR_inertial_DYN.yaml";
// const std::string elastic_file = "../../thunder/robots/RRR_sea/seaRRR.yaml";
const std::string saved_inertial_file = "../robots/robot/saved_robot_inertial_DYN.yaml";

using namespace std::chrono;
Expand All @@ -36,7 +37,7 @@ int main(){
auto time_stop = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(time_stop - time_start).count();

thunder_seaRRR robot;
thunder_RRR robot;

robot.load_par_DYN(inertial_file);
const int NJ = robot.get_numJoints();
Expand Down Expand Up @@ -93,10 +94,10 @@ int main(){
myG = robot.get_G();
cout<<"\n\nG\n"<<myG;
// --- Should be commented if Dl does not exists, Uncomment for link friction --- //
if (robot.Dl_order){
Dl = robot.get_Dl();
cout<<"\n\nD_link\n"<<Dl;
}
// if (robot.Dl_order){
// Dl = robot.get_Dl();
// cout<<"\n\nD_link\n"<<Dl;
// }
// --- end --- //
Yr = robot.get_Yr();
cout<<"\n\nYr\n"<<Yr;
Expand All @@ -115,46 +116,46 @@ int main(){
// robot.save_par_DYN(saved_inertial_file);

// --- Should be commented if ELASTIC = 0, Uncomment for elastic behavior --- //
if (robot.ELASTIC){
int NEJ = robot.numElasticJoints;
cout<<endl<<"num elastic joints: "<< NEJ<<endl;
robot.load_par_elastic(elastic_file);

Eigen::VectorXd x(NEJ), dx(NEJ), ddxr(NEJ);
x = 2*x.setOnes();
dx = 2*dx.setOnes();
ddxr = 2*ddxr.setOnes();
robot.set_x(x);
robot.set_dx(dx);
robot.set_ddxr(ddxr);

int N_PARAM_K = NEJ*robot.K_order;
int N_PARAM_D = NEJ*robot.D_order;
int N_PARAM_DM = NEJ*robot.Dm_order;
Eigen::VectorXd par_K(N_PARAM_K);
Eigen::VectorXd par_D(N_PARAM_D);
Eigen::VectorXd par_Dm(N_PARAM_DM);
Eigen::MatrixXd K(NEJ, 1);
Eigen::MatrixXd D(NEJ, 1);
Eigen::MatrixXd Dm(NEJ, 1);
Eigen::MatrixXd reg_K(NJ, N_PARAM_K);
Eigen::MatrixXd reg_D(NJ, N_PARAM_D);
Eigen::MatrixXd reg_Dm(NJ, N_PARAM_DM);

par_K = robot.get_par_K();
par_D = robot.get_par_D();
par_Dm = robot.get_par_Dm();
cout<<endl<<"par_K:"<<endl<<par_K.transpose()<<endl;
cout<<endl<<"par_D:"<<endl<<par_D.transpose()<<endl;
cout<<endl<<"par_Dm:"<<endl<<par_Dm.transpose()<<endl;

K = robot.get_K();
cout<<endl<<"K\n"<<K<<endl;
// D = robot.get_D();
// cout<<endl<<"D_coupling\n"<<D<<endl;
Dm = robot.get_Dm();
cout<<endl<<"D_motor\n"<<Dm<<endl;
}
// if (robot.ELASTIC){
// int NEJ = robot.numElasticJoints;
// cout<<endl<<"num elastic joints: "<< NEJ<<endl;
// robot.load_par_elastic(elastic_file);

// Eigen::VectorXd x(NEJ), dx(NEJ), ddxr(NEJ);
// x = 2*x.setOnes();
// dx = 2*dx.setOnes();
// ddxr = 2*ddxr.setOnes();
// robot.set_x(x);
// robot.set_dx(dx);
// robot.set_ddxr(ddxr);

// int N_PARAM_K = NEJ*robot.K_order;
// int N_PARAM_D = NEJ*robot.D_order;
// int N_PARAM_DM = NEJ*robot.Dm_order;
// Eigen::VectorXd par_K(N_PARAM_K);
// Eigen::VectorXd par_D(N_PARAM_D);
// Eigen::VectorXd par_Dm(N_PARAM_DM);
// Eigen::MatrixXd K(NEJ, 1);
// Eigen::MatrixXd D(NEJ, 1);
// Eigen::MatrixXd Dm(NEJ, 1);
// Eigen::MatrixXd reg_K(NJ, N_PARAM_K);
// Eigen::MatrixXd reg_D(NJ, N_PARAM_D);
// Eigen::MatrixXd reg_Dm(NJ, N_PARAM_DM);

// par_K = robot.get_par_K();
// par_D = robot.get_par_D();
// par_Dm = robot.get_par_Dm();
// cout<<endl<<"par_K:"<<endl<<par_K.transpose()<<endl;
// cout<<endl<<"par_D:"<<endl<<par_D.transpose()<<endl;
// cout<<endl<<"par_Dm:"<<endl<<par_Dm.transpose()<<endl;

// K = robot.get_K();
// cout<<endl<<"K\n"<<K<<endl;
// // D = robot.get_D();
// // cout<<endl<<"D_coupling\n"<<D<<endl;
// Dm = robot.get_Dm();
// cout<<endl<<"D_motor\n"<<Dm<<endl;
// }
// --- end --- //

return 0;
Expand Down
Empty file.
Empty file.
Empty file.
7 changes: 4 additions & 3 deletions src/todo.todo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
improvings:
☐ multiple robot library in the same folder, problem function names
✔ multiple robot library in the same folder @done (10/18/2024, 1:30:21 PM)
☐ robotName in config file. Needed for creation of library without the command './thunder gen', problems in name of generated functions
☐ idea for parameters: thunder created a configuration files with everything, maybe this file can be used to create robot and by thunder_robot both.
☐ thunder_robot can only load par_DYN and par_REG, general method needed for all the parameters
☐ thunder_utils with LinkProp and other classes needed both by thunder and thunder_robot
Expand All @@ -11,11 +12,11 @@ improvings:
☐ generalize path with absolute_path
☐ clean everything
☐ FrameOffset not so general, use T as internal transformation and provide conversions
✔ create library thunder_utils that contain all that cannot be generated, best if it is useful both on thunder and thunder_robot @done (8/27/2024, 11:13:54 AM)
☐ is possible to have two different robots? due to casadi definitions on generated file?
☐ create library thunder_utils that contain all that cannot be generated, best if it is useful both on thunder and thunder_robot
☐ elastic matrices and quantities are related on motor side equation, have to be modified to use in link side equation by selection matrix
☐ genYaml have to be into Robot, now is used to create parameter file from config_file
☐ improve the linkProp structure, must be general to all parameters in the map args
☐ copy robot from thunder to the main folder instead of having a fixed copy

will be in Thunder:
✔ elasticity @done (10/17/2024, 3:05:53 PM)
Expand Down

0 comments on commit 8180f7a

Please sign in to comment.