Exposing Rcpp modules - c++

I am having some trouble working through the examples inside:
http://dirk.eddelbuettel.com/code/rcpp/Rcpp-modules.pdf
Specifically, section 2.2 where the author goes to expose a C++ class in RCPP and making it available to call in R.
I copied the code in to a new RCPP file and sourced it with no problem but got an error when I ran the following in R:
require(Rcpp)
require(inline)
unif_module <- Module( "unif_module", getDynLib(fx_unif ) )
The error:
Error in getDynLib(fx_unif) :
error in evaluating the argument 'x' in selecting a method for function 'getDynLib': Error: object 'fx_unif' not found
I then went though the other parts of the document where getDynLib is used and found that none of them initialized the arguments passed in to getDynLib. My question is what is fx_unif?
Thanks,
Update:
So following another example within the paper i tried:
inc = '
class Uniform {
public:
Uniform(double min_, double max_) : min(min_), max(max_) {}
NumericVector draw(int n) const {
RNGScope scope;
return runif( n, min, max );
}
double min, max;
};
double uniformRange( Uniform* w) {
return w->max - w->min;
}
RCPP_MODULE(unif_module) {
class_<Uniform>( "Uniform" )
.constructor<double,double>()
.field( "min", &Uniform::min )
.field( "max", &Uniform::max )
.method( "draw", &Uniform::draw )
.method( "range", &uniformRange )
;
}
'
fx_unif = cxxfunction(signature(), plugin="Rcpp", include=inc)
error:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! Warning message:
running command 'make -f "C:/PROGRA~1/R/R-32~1.2/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-32~1.2/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="filef4028245a6f.dll" WIN=64 TCLBIN=64 OBJECTS="filef4028245a6f.o"' had status 127
In addition: Warning message:
running command 'C:/PROGRA~1/R/R-32~1.2/bin/x64/R CMD SHLIB filef4028245a6f.cpp 2> filef4028245a6f.cpp.err.txt' had status 1

Related

Error : Warning message:In system(cmd) : 'make' not found

I am working with R and C++. I am trying to include C++ in R , I have the following code:
install.packages('inline')
install.packages('Rcpp')
install.packages('rstan')
library('inline')
library('Rcpp')
library('rstan')
### # start with pure C/C++ function and assign the source code to
### # a variable which we call here includesrc
includesrc <- '
int fibonacci(const int x){
if (x == 0) return(0);
if (x == 1) return(1);
return fibonacci(x-1) + fibonacci(x-2);
}'
### # define the body of the C/C++ program
fibCppBody <- '
int x = Rcpp::as<int>(xs);
return Rcpp::wrap( fibonacci(x) );'
### # pass the above C/C++ function as an argument
### # to cxxfunction()
fibRcpp <- inline::cxxfunction(sig = signature(xs = "int"),
plugin = "Rcpp",
incl = includesrc,
body = fibCppBody)
sapply(1:10, fibRcpp, USE.NAMES = FALSE)
I ran this code which is supposed to include C++ code in R function , it is supposed to run (it is an example from here but it's not working for me.
And I got this error:
Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
Warning message:In system(cmd) : 'make' not found
Any idea? Thanks

How to print errors in lua script without running script?

I'm new using Lua in C++. I have some problem with printing error of script.
My purpose is :
Find errors of syntax, function name using luaL_dofile or luaL_dostring without running code like
build codes in Visual studio.
(When using luaL_loadfile or luaL_loadstring, it just find syntax error like 'a == 1', but can't find function name error like 'prit(a)')
Want to print all errors.(In lua, if there was 2 or more errors, it only prints error that was occurred first)
For example I made lua script like this :
test.lua
function sayHello(name)
print("Hello, " .. name .. "!")
end
for a=0, 10
print(a)
end
sayHello()
and in c++ code :
test.cpp
...
int ret;
ret = luaL_dofile(LuaState, filename);
if(ret != 0) {
cout << "error : " << lua_tostring(LuaState, -1) << endl;
lua_pop(LuaState, 1);
}
I want to print errors like :
lua: test.lua:2: attempt to concatenate local `name' (a nil value)
lua: test.lua:7: `do' expected near `print'
Could anybody teach me?

How can I fix the bug in ACADO Toolkit when using OnlineData variables?

As exposed in this topic in ACADO forum, there is a bug when you try to use OnlineData variables. In my case I am using C++ code instead of MATLAB interface and have 7 OnlineData variables. In the topic mentioned before they propose using the function SetNOD but using C++ I can't call the function. I cannot access the official forum in sourcefourge because they have some problems and I would appreciate your help.
The abbreviated code is:
include
define N 20 //Time intervals -- 51 states
int main{
USING_NAMESPACE_ACADO
//Variables
DifferentialState x, y, z, dx, dy, dz, roll, pitch, yaw, droll, dpitch, dyaw;
Control u1, u2, u3, u4;
OnlineData yaw0, obsx, obsy, obsz, obsrx, obsry, obsrz;
. . .
//Create Optimal Control Problem object
OCP ocp(t_start, t_end, N); //50 number of discretization intervals
//Fixing the bug
//Alternatives I tried
ocp.SetNOD(7);//Error A
//ocp.ModelContainer.SetNOD(7);//Error B
//Objective Function
ocp.minimizeLSQ(Q, h);
ocp.minimizeLSQEndTerm(QN, hN);
/* Constraints */
//Model constraint
ocp.setModel( f );
/* Export OCP */
OCPexport mpc( ocp );
...
if (mpc.exportCode( "path_qp_export_oases" ) != SUCCESSFUL_RETURN)
exit( EXIT_FAILURE );
return 0;
}
Error A:
/.../path_qp_generated_oases.cpp: In function ‘int main()’:
/.../path_qp_generated_oases.cpp:321:9: error: ‘class ACADO::OCP’ has no member named ‘SetNOD’
ocp.SetNOD(7);
make[2]: [.../CMakeFiles/my_examples_path_qp_generated_oases.dir/my_examples/path_qp_generated_oases.cpp.o] Error 1
make1: .../CMakeFiles/my_examples_path_qp_generated_oases.dir/all] Error 2
make: [all] Error 2
Error B:
/.../path_qp_generated_oases.cpp: In function ‘int main()’:
/.../path_qp_generated_oases.cpp:320:10: error: invalid use of ‘ACADO::ModelContainer::ModelContainer’
ocp.ModelContainer.SetNOD(7);
make2: [examples/CMakeFiles/my_examples_path_qp_generated_oases.dir/my_examples/path_qp_generated_oases.cpp.o] Error 1
make1:[examples/CMakeFiles/my_examples_path_qp_generated_oases.dir/all] Error 2
make: all Error 2
I am using the version of acado-master downloaded today:
Branch: master
Commit: 2cde3c748856ca16a4460e05149c1e5de362526f
Remote: acado/acado
Also have the same problem with acado-stable downloaded today:
~/ACADOtoolkit$ git rev-parse HEAD
e0cc4b058e1dc60c4e57f306dc7c7db41a582451
Thank you very much!!
I had the same problem.
There is currently a bug in ACADO Toolkit where it sometimes mis-counts or completely ignores the OnlineData variables that you use to define your problem.
The method is setNOD() with a lower-case "s".
Here's some example code which may help other people...
.
First make sure you install the latest ACADO source from github.
Define your non-linear optimization problem for ACADO like shown in the documentation:
DifferentialState x;
DifferentialState y;
Control u;
DifferentialEquation f;
f << dot(x) == 1.0*u;
f << dot(y) == 2.0*u;
OnlineData d0;
OnlineData d1;
OnlineData d2;
etc.
OCP ocp(0.0, steps*Ts, steps);
ocp.minimizeLSQ(W, h);
ocp.minimizeLSQEndTerm(W, h);
ocp.setModel(f);
ocp.setNOD(3);
OCPexport mpc(ocp);
You have to manually count your number of OnlineData and put that number into setNOD().
Then compile, generate the code, etc. like described in the documentation.
Now you can search through the code in the export directory to find these OnlineData variables that were previously ignored:
grep -R "od\[" *
Also note the test.c program won't set these variables.
In your own code, you will need to set their value.
If you have 3 OnlineData, you set them to be the same for each step along the control horizon like this:
for (int i = 0; i < (N + 1); ++i)
{
acadoVariables.od[i * NOD + 0] = 1.0;
acadoVariables.od[i * NOD + 1] = 9.0;
acadoVariables.od[i * NOD + 2] = 5.0;
}
Hope this helps!

ROS programming: move forward turtlebot

I want to move the turtlebot.
Firstly i created package inside my catkin_ws
$ catkin_create_pkg /..package_name../ std_msgs rospy roscpp actionlib tf geometry_msgs move_base_msgs
Then i edit CMakeList
add_executable(myProgram src/main.cpp) and target_link_libraries(<executabletargetname>, ${catkin_LIBRARIES})
Thirdly, i run this command:
catkin_make
after compile:
[100%] Building CXX object ileri/CMakeFiles/gg.dir/src/gg.cpp.o
/home/turtlebot/catkin_ws/src/ileri/src/gg.cpp:18:2: error: ‘p’ does
not name a type /home/turtlebot/catkin_ws/src/ileri/src/gg.cpp:28:2:
error: expected unqualified-id before ‘try’
/home/turtlebot/catkin_ws/src/ileri/src/gg.cpp:31:3: error: expected
unqualified-id before ‘catch’ make[2]: *
[ileri/CMakeFiles/gg.dir/src/gg.cpp.o] Error 1 make[1]: *
[ileri/CMakeFiles/gg.dir/all] Error 2
.cpp :
`geometry_msgs::PointStamped p;
geometry_msgs::PointStamped p1;
p.header.stamp = ros::Time();
std::string frame1 = "/camera_depth_optical_frame";
p.header.frame_id = frame1.c_str();
p.point.x = 0;
p.point.y = 0;
p.point.z = 1; // 1 meter
std::string frame = "map";
try
{
listener.transformPoint(frame,p,p1);
}catch(tf::TransformException& ex) { ROS_ERROR("exception while transforming..."); }
// create message for move_base_simple/goal
geometry_msgs::PoseStamped msg;
msg.header.stamp = ros::Time();
std::string frame = "/map";
msg.header.frame_id = frame.c_str();
msg.pose.position = p1.point;
msg.pose.orientation = tf::createQuaternionMsgFromYaw(0.0);
publisher.publish(msg);`
What do you think about these errors?
Are there problems about include? If you think so, which includes should i add this code?
In C++, statements go inside functions. It appears that your statement p.header.stamp = ros::Time(); appears outside a function.
Your program should also contain a int main() { } function. Try moving the statements inside the { }.

cxxfunction error message, inline R-package

I'm trying to reproduce an R example based in R help.
This is an example for cxxfunction from inline package.
require(Rcpp)
require(inline)
# Rcpp plugin
if( require( Rcpp ) ){
fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
return wrap( as<int>(x) * as<double>(y) ) ;
', plugin = "Rcpp" )
fx( 2L, 5 )
## equivalent shorter form using rcpp()
fx <- rcpp(signature(x = "integer", y = "numeric"),
' return wrap( as<int>(x) * as<double>(y) ) ; ')
}
I got this message:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! Warning message:
running command 'make -f "C:/PROGRA~1/R/R-30~1.3/etc/i386/Makeconf" -f "C:/PROGRA~1/R/R-30~1.3/share/make/winshlib.mk" ....
And in addition:
Warning message:
running command 'C:/PROGRA~1/R/R-30~1.3/bin/i386/R CMD SHLIB file3a86e316ef8.cpp ...
I'm using:
platform i386-w64-mingw32 and R-3.0.3
As Kevin said, its easier to use attributes. The attribute // [[Rcpp::export]] does all the work for you.
With the current versions on R and Rcpp installed create a file called test.cpp to put this code:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double fx(int x, double y){
return x*y;
}
/*** R
fx(2L,5)
fx(2L,5.1)
*/
Then in a R session run: Rcpp::sourceCpp('test.cpp')
That should work, if you have followed the instructions for installing R, R-tools and set PATH variables (if on windows) correctly.