-
Notifications
You must be signed in to change notification settings - Fork 2
/
py_controller.cpp
142 lines (120 loc) · 3.03 KB
/
py_controller.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
132
133
134
135
136
137
138
139
140
141
142
/*
* AUTHOR: Ken Hasselmann <arg AT kenh DOT fr>
*
* Connects ARGoS to python
*
*/
#include "py_controller.h"
using namespace argos;
using namespace boost::python;
#define INIT_MODULE PyInit_libpy_controller_interface
extern "C" PyObject* INIT_MODULE();
CPyController::CPyController(){
//init python
PyImport_AppendInittab("libpy_controller_interface", INIT_MODULE);
if (!Py_IsInitialized())
{
Py_Initialize();
}
m_interpreter = Py_NewInterpreter();
//init main module and namespace
m_main = import("__main__");
m_namesp = m_main.attr("__dict__");
}
void CPyController::Destroy() {
//launch python destroy function
try
{
object destroy = m_main.attr("destroy");
destroy();
}
catch (error_already_set)
{
PyErr_Print();
}
//Py_EndInterpreter(m_interpreter);
//Py_Finalize(); //the documentation of boost says we should NOT use this ..
}
void CPyController::Reset() {
//launch python reset function
try
{
object reset_f = m_main.attr("reset");
reset_f();
}
catch (error_already_set)
{
PyErr_Print();
}
}
// TODO: PASS TNODE
void CPyController::InitSensorsActuators(TConfigurationNode& t_node)
{
for(CCI_Actuator::TMap::iterator it = m_mapActuators.begin();
it != m_mapActuators.end();
++it)
{
m_actusensors->CreateActu(it->first, it->second, t_node); //this);
}
for(CCI_Sensor::TMap::iterator it = m_mapSensors.begin();
it != m_mapSensors.end();
++it)
{
m_actusensors->CreateSensor(it->first, it->second, t_node);
}
}
void CPyController::Init(TConfigurationNode& t_node)
{
//get instances of actuators and sensors and pass them to the wrapper
m_actusensors = boost::make_shared<ActusensorsWrapper>();
InitSensorsActuators(t_node);
//printf("%s\n", this);
/* Load script */
std::string strScriptFileName;
GetNodeAttributeOrDefault(t_node, "script", strScriptFileName, strScriptFileName);
if(strScriptFileName == "")
{
THROW_ARGOSEXCEPTION("Error loading python script \"" << strScriptFileName << "\"" << std::endl);
}
//exec user script
try
{
m_script = exec_file(strScriptFileName.c_str(), m_namesp, m_namesp);
std::cout << "strScript:" << strScriptFileName << std::endl;
std::cout << GetId().c_str() << std::endl;
}
catch(error_already_set)
{
PyErr_Print();
}
try
{
//import the wrappers's lib
PyRun_SimpleString("import libpy_controller_interface as lib");
object lib = import("libpy_controller_interface");
m_namesp["robot"] = m_actusensors;
//m_namesp["robot"] = ptr(m_actusensors);
//launch python init function
object init_f = m_main.attr("init");
init_f();
}
catch (error_already_set)
{
PyErr_Print();
}
}
void CPyController::ControlStep()
{
//here the sensors should be updated every step
//launch controlstep python function*
try
{
object controlstep = m_main.attr("controlstep");
controlstep();
}
catch (error_already_set)
{
PyErr_Print();
}
}
REGISTER_CONTROLLER(CPyController, "python_controller");