Arduino: error: 'abs' was not declared in this scope - arduino

I'm working on an arduino library needing the abs() function:
#include <math.h>
normTransFreq1 = abs(1.0);
Error: 'abs' was not declared in this scope
As math.h is already included in the cpp I'm entirely unsure how to fix this problem. A new install of arduino 1.5.2 didn't help.

Just found the solution:
Including math.h is not needed for the library. Instead, Arduino.h should be included by adding the following to the header file:
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

Related

Error in including boost tokenizer.hpp file when building R package on MacOS/CRAN

I'm running into issues publishing my R package to CRAN, because of a specific error when including boost libraries. The top of my one .cpp file in the package is
#include <Rcpp.h>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <omp.h>
#include <vector>
// [[Rcpp::depends(BH)]]
// [[Rcpp::plugins(openmp)]]
When running through the check on MacOS (via rhub::check(platform = "macos-highsierra-release-cran"), I get the following error:
In file included from wgt_jaccard.cpp:6:
/Users/user2suimGYX/R/BH/include/boost/tokenizer.hpp:63:9: error: field of type 'std::_1::wrap_iter<const char *>' has private constructor
: first(c.begin()), last(c.end()), f(f) { }
^
wgt_jaccard.cpp:117:19: note: in instantiation of function template specialization 'boost::tokenizer<boost::char_separator<char, std::__1::char_traits >, std::__1::__wrap_iter<const char *>, std::__1::basic_string ::tokenizer<Rcpp::internal::string_proxy<16, PreserveStorage> >' requested here
tokenizer tokens(y(i), sep);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:1420:31: note: declared private here
_LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) _NOEXCEPT_DEBUG : __i(__x) {}
^
My Makevars file's contents are
PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS) -DBOOST_NO_AUTO_PTR
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS)
CXX_STD = CXX11
I've tried searching around, but can't find much on this error. The full package is located here. Any help is much appreciated.
Relative to the repo you kindly supplied, we found a need for two changes:
First, to no (unconditionally) have an #include <omp.h> as OpenMP can be optional, esp. on macOS. A simpled #ifdef OPENMP does the job.
Second, the (arguably near-incomprehensible) compiler message had to do with the fact that the Boost type / class for tokenizer was puzzled by the Rcpp object you gave it by directly indexing from an Rcpp::CharacterVector. Been there, done that -- a more conservative approach is to first assign to std::string and to then pass that on.
With those two changes, it's all roses and it compiles on macOS under clang++ as well.
The (by now merged, thanks) PR #2 has the gory details, but is short.

PCL1.7.2 in QT5.4.1 --- error: C2589, C2059, C2181 in PCL header file

I tried to include my friend's code into my project.
The original code highly uses PCL 1.7.2 so I,
1.Installed PCL 1.7.2 package(not built with source code)
2.Edit INCLUDEPATH and LIBS in myproject.pro file, according to my friend's settings in Visual Studio 2013(yes, he wrote the code under VS2013). e.g
and more lines of path like above.
3.Include the header file in mainwindow.h
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <boost/shared_ptr.hpp>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/surface/mls.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/registration/icp.h>
#include <pcl/filters/radius_outlier_removal.h>
#include <pcl/features/normal_3d.h>
#include <pcl/surface/gp3.h>
#include <cv.h>
#include <cxcore.h>
using namespace cv;
using namespace std;
using namespace pcl;
4.Build(Compile) the project, and bunch of errors showed up
The errors are C2589, C2059 and C2181, basically lie in PCL headers like io_operators.h, pcl_io.h, correspondence.h, cloud_iterator.h. I have totally no clue of these, does this means I have to edit PCL's header files?
Those error codes are quite often related to the Windows issue warning C4003 and errors C2589 and C2059 on: x = std::numeric_limits<int>::max();
Indeed there is std::numeric_limits<>::max() in io_operators.h:66 and pcl_io.h:281.
So, if it is the only problem it can be fixed by changing order of headers by moving pcl headers on top to avoid including windows.h (that may be included from some other header) before or by defining NOMINMAX before all inclusions.

Is there a #define _KEYWORD_ in arduino.h to check against?

I would prefer to use standard Arduino API's if they are available. I need a way to check if arduino.h is present. If it is not available (i.e. executing code on BeagleBone Black), then I can use my own functions.
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
Taken from: http://www.arduino.cc/en/Main/ReleaseNotes [internals]

windows version of R package much slower than linux version

I'm running ubuntu, 64bit. I have this minimal test package that i made to learn how to do these things (I'm following this tutorial, except i also have some c code in the package).
The package build/runs in linux so i set about making it run in windows too.
I followed this answer and used the online windows package builder maintained by Uwe Ligges to get a (working) zip version of my package.
Now, when i install that .zip package on windows (7-64) the small demo code runs slower than the linux version. As in 30 times slower. I doubt the difference is always so large.
I'm wondering what i'm doing wrong and how i can fix this gap.
EDIT:
this is the source code (it's a minimal working example):
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <fstream>
#include <iostream>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/LU>
#include <Eigen/SVD>
using namespace std;
using namespace Eigen;
using Eigen::MatrixXf;
using Eigen::VectorXf;
float median(VectorXf& x) {
int n=x.rows();
int half=(n+1)/2;
half--;
float med;
nth_element(x.data(),x.data()+half,x.data()+x.size());
if((n%2)==1){
med=x(half);
} else {
float tmp0=x(half);
float tmp1=x.segment(half+1,half-1).minCoeff();
med=0.5*(tmp0+tmp1);
}
return med;
}
VectorXf fx01(MatrixXf& x){
int p=x.cols();
int n=x.rows();
VectorXf Recept(n);
VectorXf Result(p);
for(int i=0;i<p;i++){
Recept=x.col(i);
Result(i)=median(Recept);
}
return Result;
}
extern "C"{
void mse(int* n,int* p,float* x,float* medsout){
MatrixXf x_cen=Map<MatrixXf>(x,*n,*p);
VectorXf MedsOut=fx01(x_cen);
Map<VectorXf>(medsout,*p)=MedsOut.array();
}
}
EDIT2:
Following cbeleites suggestion i ran the code multiple times. Doing this I found
a strange thing: the function's timing are actually the same as linux except when
i call apply() before calling my function --I was always comparing the timing
of the colwise median my pack computes to the timing of doing apply(X,2,median)--
Ok, problem solved. For now. Still i'm curious now: why would a good old fashioned
call to apply() (on a huge matrix X) wreck things so badly (system.time went from
90sec to 3sec)?
One possibility that comes to my mind where calculations on different machines can differ about is the BLAS (if there are linear algebra calculations in the example).
Do you have an optimized BLAS installed on Ubuntu (e.g. libopenblas) but not on Windows?

How to include an already written library to a custom library in Arduino

I'm creating a new library to control the keypad and LCD together. Most of the code seems to compile, but when it reaches the line where I define the LiquidCristal variable, it says:
'LiquidCrystal' does not name a type when creating a custom library
This is the extract of content of my LCDKeypad.h
// Include types & constants of Wiring core API
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "WConstants.h"
#endif
// Include LCD library
#include <LiquidCrystal.h>
The error is in this line:
private:
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 ); // <<-- Error
Alright, I was reading a lot and I found a very interesting article about this subject:
Including multiple libraries
It says that the compiler does not search for libraries that are not included in the sketch file. The way to hack this is to force the compiler to link them before loading your libraries, including, in my case LiquidCrystal.h in the sketch.
Let's say my library "LCDkeypad" requires LiquidCrystal.
My main program (sketch) needs to include LiquidCrystal in order to load it for my library "LCDKeypad".
Now, one interesting thing is using forward declaration, so in my LCDKeypad.h I will declare
"Class LiquidCrystal" but not include the library. I will do it in LiquidCrystal.cpp and in the sketch.
I hope this is clear.
There are two ways of doing it
If you are writing your own code Just create header file .h extension and relevant c code as name_c.While adding into main program, you need to specify header file name in double quotes.
code:
#ifndef LCD_H
#define LCD_H
//Declaration of variable /functions
#endif
If you are trying to download from link. Then you need paste the code into D:\arduino\arduino\libraries
Error problem:
overlapping of multiple declaration of variable.
overlapping of library functions

Resources