The first application using Python
[
Front page
] [
New
|
List of pages
|
Search
|
Recent changes
|
Help
]
Start:
[[Tutorial]]
Up:[[Tutorial]] Previous: [[Embedding Python Interpre...
#contents
* Before making the first application [#h9f2fb4f]
** Header files [#n624e994]
We will use the static boost::python library (libboost_py...
Let’s get some code running. You’ll need to include the c...
#highlight(c++){{
#include <boost/python.hpp>
#include <Python.h>
#include <dlfcn.h>
}}
These header files are added in the beginning of the prog...
** Initalize Python Interpreter [#ucb6ddbe]
Add namepaces:
#highlight(c++){{
namespace py = boost::python;
using namespace std;
}}
Then move to the member function onInit()and add the foll...
#highlight(python){{
Py_Initialize();
py::object main_module = py::import("__main__");
py::object main_namespace = main_module.attr("__dict_...
}}
Note that you must initialize the Python interpreter dire...
Note: The initialization of interpreter, import of __main...
Thus, its recommended to initialize the interpreter insid...
#highlight(python){{
Py_Initialize();
}}
We need to add the following code before initializing the...
#highlight(c++){{
dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
}}
We had already included the header file for the above fun...
#highlight(python){{
#include <dlfcn.h>
}}
Thus, to begin with for all cases we need to have followi...
#highlight(python){{
dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
Py_Initialize();
}}
*The first application [#q4d1d51d]
**Hello World [#af2c97e9]
Go to onInit() member function and copy the following:
#highlight(python){{
Py_Initialize();
py::object main_module = py::import("__main__");
py::object main_namespace = main_module.attr("__dict_...
}}
The blank canvas is created upon which we can call python...
#highlight(python){{
Py_Initialize();
py::object main_module = py::import("__main__");
py::object main_namespace = main_module.attr("__dict_...
}}
#highlight(python){{
py::exec("print 'Hello, world'", main_namespace);
py::exec("print 'Hello, world'[3:5]", main_namespace);
py::exec("print '.'.join(['1','2','3'])", main_namesp...
}}
The 'exec' function runs the arbitrary code in the string...
* Creating Makefile. [#mf7be2b4]
#highlight(c){{
#sigverse header
SIG_SRC = $(SIGVERSE_PATH)/include/sigverse
#all: $(OBJS)
CC = gcc
CPP = g++
AS = as
LD = g++
AR = ar
RANLIB = ranlib
OBJCOPY = objcopy
# external libraries and headers
# change the next line to point to the location of your b...
EXTERN_DIR = $(HOME)/pool
EXTERN_LIBDIR = $(EXTERN_DIR)/lib
EXTERN_INCDIR = $(EXTERN_DIR)/include
EXTERN_BINDIR = $(EXTERN_DIR)/bin
BOOST_PYTHON_LIB = $(EXTERN_LIBDIR)/libboost_python.a
INCDIRS = .
INCDIRS += $(EXTERN_INCDIR)
# you may also need to change this directory if you wan...
# python version
INCDIRS += /usr/include/python2.7
INCDIRS += /usr/lib/python2.7/dist-packages/numpy/core/in...
INCDIR = $(foreach dir, $(INCDIRS), -I$(dir))
%.o: %.cpp
$(CPP) $(INCDIR) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
#specifying of object file
OBJS = NameOfController.so
HWOBJS= hello_world.o
all: $(OBJS) helloworld
# you need to change the name of your controller.
NameOfController.so : NameOfController.cpp
g++ -DCONTROLLER -DNDEBUG -DUSE_ODE -DdDOUBLE -I$(SIG_S...
#$^ , $+ The names of all the prerequisites, with spaces ...
# The value of $^ omits duplicate prerequisites,
# while $+ retains them and preserves their order
clean:
rm -rf *.so *.o $(HWOBJS) hello_world
}}
Then type on console to compile the code:
$make
Now, you need to set the library path using. It is better...
#highlight(c){{
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/pool/lib
}}
Then, load the xml file using sigviewer:
$sigserver.sh -w ./WorldFile.xml
After the world loads, push "Start Button" to see the res...
#highlight(end)
Up:[[Tutorial]] Previous: [[Embedding Python Interpre...
#counter
End:
[[Tutorial]]
Up:[[Tutorial]] Previous: [[Embedding Python Interpre...
#contents
* Before making the first application [#h9f2fb4f]
** Header files [#n624e994]
We will use the static boost::python library (libboost_py...
Let’s get some code running. You’ll need to include the c...
#highlight(c++){{
#include <boost/python.hpp>
#include <Python.h>
#include <dlfcn.h>
}}
These header files are added in the beginning of the prog...
** Initalize Python Interpreter [#ucb6ddbe]
Add namepaces:
#highlight(c++){{
namespace py = boost::python;
using namespace std;
}}
Then move to the member function onInit()and add the foll...
#highlight(python){{
Py_Initialize();
py::object main_module = py::import("__main__");
py::object main_namespace = main_module.attr("__dict_...
}}
Note that you must initialize the Python interpreter dire...
Note: The initialization of interpreter, import of __main...
Thus, its recommended to initialize the interpreter insid...
#highlight(python){{
Py_Initialize();
}}
We need to add the following code before initializing the...
#highlight(c++){{
dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
}}
We had already included the header file for the above fun...
#highlight(python){{
#include <dlfcn.h>
}}
Thus, to begin with for all cases we need to have followi...
#highlight(python){{
dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
Py_Initialize();
}}
*The first application [#q4d1d51d]
**Hello World [#af2c97e9]
Go to onInit() member function and copy the following:
#highlight(python){{
Py_Initialize();
py::object main_module = py::import("__main__");
py::object main_namespace = main_module.attr("__dict_...
}}
The blank canvas is created upon which we can call python...
#highlight(python){{
Py_Initialize();
py::object main_module = py::import("__main__");
py::object main_namespace = main_module.attr("__dict_...
}}
#highlight(python){{
py::exec("print 'Hello, world'", main_namespace);
py::exec("print 'Hello, world'[3:5]", main_namespace);
py::exec("print '.'.join(['1','2','3'])", main_namesp...
}}
The 'exec' function runs the arbitrary code in the string...
* Creating Makefile. [#mf7be2b4]
#highlight(c){{
#sigverse header
SIG_SRC = $(SIGVERSE_PATH)/include/sigverse
#all: $(OBJS)
CC = gcc
CPP = g++
AS = as
LD = g++
AR = ar
RANLIB = ranlib
OBJCOPY = objcopy
# external libraries and headers
# change the next line to point to the location of your b...
EXTERN_DIR = $(HOME)/pool
EXTERN_LIBDIR = $(EXTERN_DIR)/lib
EXTERN_INCDIR = $(EXTERN_DIR)/include
EXTERN_BINDIR = $(EXTERN_DIR)/bin
BOOST_PYTHON_LIB = $(EXTERN_LIBDIR)/libboost_python.a
INCDIRS = .
INCDIRS += $(EXTERN_INCDIR)
# you may also need to change this directory if you wan...
# python version
INCDIRS += /usr/include/python2.7
INCDIRS += /usr/lib/python2.7/dist-packages/numpy/core/in...
INCDIR = $(foreach dir, $(INCDIRS), -I$(dir))
%.o: %.cpp
$(CPP) $(INCDIR) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
#specifying of object file
OBJS = NameOfController.so
HWOBJS= hello_world.o
all: $(OBJS) helloworld
# you need to change the name of your controller.
NameOfController.so : NameOfController.cpp
g++ -DCONTROLLER -DNDEBUG -DUSE_ODE -DdDOUBLE -I$(SIG_S...
#$^ , $+ The names of all the prerequisites, with spaces ...
# The value of $^ omits duplicate prerequisites,
# while $+ retains them and preserves their order
clean:
rm -rf *.so *.o $(HWOBJS) hello_world
}}
Then type on console to compile the code:
$make
Now, you need to set the library path using. It is better...
#highlight(c){{
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/pool/lib
}}
Then, load the xml file using sigviewer:
$sigserver.sh -w ./WorldFile.xml
After the world loads, push "Start Button" to see the res...
#highlight(end)
Up:[[Tutorial]] Previous: [[Embedding Python Interpre...
#counter
Page: