-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopMotion.cpp
131 lines (104 loc) · 4.94 KB
/
StopMotion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "StopMotion.h"
template <typename T> string tostr(const T& t)
{
ostringstream os;
os<<t;
return os.str();
} // template to convert double variables to string
double* stopMotion(SimObj *target, Vector3d desiredTargetVelocity, double K_p, double K_i, double K_d )
{
Vector3d currentTargetVelocity;
target->getLinearVelocity(currentTargetVelocity);
cout << "The currentTargetVelocity = " << currentTargetVelocity.x() << " , " << currentTargetVelocity.z() << endl;
double *pointer;
double P_value[1];
pointer=P_value;
double P_Value_X;
double P_Value_Z;
dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
Py_Initialize(); //initialization of the python interpreter
try
{
// load the main module
py::object main_module = py::import("__main__");
// // load the dictionary object out of the main module to create a blank canvas on which python variables and functions can be executed.
py::object main_namespace = main_module.attr("__dict__");
main_module.attr("K_p") = K_p;
main_module.attr("K_i") = K_i;
main_module.attr("K_d") = K_d;
main_module.attr("desiredTargetVelocity") = "[" + tostr(desiredTargetVelocity.x())+" , "+ tostr(desiredTargetVelocity.y())+ " , " + tostr(desiredTargetVelocity.z()) + "]";
main_module.attr("currentTargetVelocity") = "[" + tostr(currentTargetVelocity.x())+" , "+ tostr(currentTargetVelocity.y())+ " , " + tostr(currentTargetVelocity.z()) + "]";
py::exec("import ast", main_namespace);
py::exec("desiredTargetVelocity = ast.literal_eval(desiredTargetVelocity)", main_namespace);
// py::exec("print goalPos", main_namespace);
py::exec("currentTargetVelocity = ast.literal_eval(currentTargetVelocity)", main_namespace);
// py::exec("print currentPos", main_namespace);
py::exec("import PID as pid", main_namespace);
py::exec("p_x=pid.PID(K_p, K_i, K_d)",main_namespace);
py::exec("p_z=pid.PID(K_p, K_i, K_d)",main_namespace);
py::exec("p_x.setPoint(desiredTargetVelocity[0])", main_namespace);
py::exec("p_z.setPoint(desiredTargetVelocity[2])", main_namespace);
// py::exec("print p_z.getPoint()", main_namespace);
py::exec("pid_x=p_x.update(currentPos[0])", main_namespace);
py::exec("pid_z=p_z.update(currentPos[2])", main_namespace);
// cout << "Update in x is " << py::extract<double>(main_module.attr("pid_x")) << endl;
// cout << "Update in z is " << py::extract<double>(main_module.attr("pid_z")) << endl;
P_Value_X = py::extract<double>(main_module.attr("pid_x"));
P_Value_Z = py::extract<double>(main_module.attr("pid_z"));
P_value[0] = P_Value_X;
P_value[1] = P_Value_Z;
return pointer;
}
catch(boost::python::error_already_set const &){
// Parse and output the exception
std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str << std::endl;
}
}
std::string parse_python_exception(){
PyObject *type_ptr = NULL, *value_ptr = NULL, *traceback_ptr = NULL;
// Fetch the exception info from the Python C API
PyErr_Fetch(&type_ptr, &value_ptr, &traceback_ptr);
// Fallback error
std::string ret("Unfetchable Python error");
// If the fetch got a type pointer, parse the type into the exception string
if(type_ptr != NULL){
py::handle<> h_type(type_ptr);
py::str type_pstr(h_type);
// Extract the string from the boost::python object
py::extract<std::string> e_type_pstr(type_pstr);
// If a valid string extraction is available, use it
// otherwise use fallback
if(e_type_pstr.check())
ret = e_type_pstr();
else
ret = "Unknown exception type";
}
// Do the same for the exception value (the stringification of the exception)
if(value_ptr != NULL){
py::handle<> h_val(value_ptr);
py::str a(h_val);
py::extract<std::string> returned(a);
if(returned.check())
ret += ": " + returned();
else
ret += std::string(": Unparseable Python error: ");
}
// Parse lines from the traceback using the Python traceback module
if(traceback_ptr != NULL){
py::handle<> h_tb(traceback_ptr);
// Load the traceback module and the format_tb function
py::object tb(py::import("traceback"));
py::object fmt_tb(tb.attr("format_tb"));
// Call format_tb to get a list of traceback strings
py::object tb_list(fmt_tb(h_tb));
// Join the traceback strings into a single string
py::object tb_str(py::str("\n").join(tb_list));
// Extract the string, check the extraction, and fallback in necessary
py::extract<std::string> returned(tb_str);
if(returned.check())
ret += ": " + returned();
else
ret += std::string(": Unparseable Python traceback");
}
}