How to control a robot from Gazebo using ROS2 Foxy? - c++

I'm utterly confused by the Gazebo/Ignition simulation programs. I'm currently using ROS2 Foxy with Ubuntu 20.04 and looking forward to controlling an UAV modeled in Gazebo via ROS2. However I'm completely lost even though there's "documentation" (which I've found it to be really mixed up between all the different versions of ROS).
What I've done so far is trying to run this script:
#include <iostream>
#include <ignition/msgs.hh>
int main()
{
ignition::msgs::Vector3d point1;
point1.set_x(1);
point1.set_y(3);
point1.set_z(5);
ignition::msgs::Vector3d point2;
point2.set_x(2);
point2.set_y(4);
point2.set_z(6);
std::cout << "Point1:\n" << point1.DebugString() << std::endl;
std::cout << "Point2:\n" << point2.DebugString() << std::endl;
return 0;
}
However I get the message that the build fails since there is no ignition directory even though I have installed it.
I'd appreciate any help in this matter since I feel I'm completely lost with installations and the CMakeLists.txt file.

Gustavo,
with this CMakeLists.txt you can compile your code:
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)
find_package(gz-cmake3 REQUIRED)
project(message-example)
gz_find_package(gz-msgs9 REQUIRED)
set(GZ_MSGS_VER ${gz-msgs9_VERSION_MAJOR})
add_executable(message-example main.cpp)
set_property(TARGET message-example PROPERTY CXX_STANDARD 17)
target_link_libraries(message-example
PRIVATE gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER})
then execute
mkdir build
cd build
cmake ..
./message-example
I don't know which Gazebo version are you using but the version for gz-msgs might be different:
Garden - msgs9
Fortress - msgs8
Citadel - msgs5
You can find here some examples related with Gazebo or if you are going to use Gazebo and ROS you should review this other repository ros_gz.

Related

Error with cmake conan and liblogicalaccess

I have installed the 2.2.1 version of the liblogicalaccess library on Kubuntu 18.04 with CMake 3.17.1 and Conan 1.25.0.
I tried to follow the C++ howto of the liblogicalaccess library and on the second line of code, when running the program it gives the following error:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
./run.sh: line 9: 6781 Aborted (core dumped) bin/./github_console
main.cpp
// my first program in C++
#include <iostream>
#include <logicalaccess/dynlibrary/librarymanager.hpp>
#include <logicalaccess/readerproviders/readerconfiguration.hpp>
#include <logicalaccess/cards/chip.hpp>
int main()
{
std::cout << "Hello World!\n";
// Reader configuration object to store reader provider and reader unit selection.
std::shared_ptr<logicalaccess::ReaderConfiguration> readerConfig(new logicalaccess::ReaderConfiguration());
// Set PCSC ReaderProvider by calling the Library Manager which will load the function from the corresponding plug-in
readerConfig->setReaderProvider(logicalaccess::LibraryManager::getInstance()->getReaderProvider("PCSC"));
}
The first line of code starting with std::shared_ptr.. just runs fine, so might this be faulty code within the library or could this be a version/compatility fault. The second line calling the getReaderProvider somehow causes the error. I have no idea where to look for the fix of this error. Came from this problem originally. That was fixed but still stuck at the same code. I did some random troubelshooting but no luck. Any advice?
EDIT:
My CmakeLists.txt is:
project(test)
cmake_minimum_required(VERSION 3.16)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
set(CMAKE_CXX_FLAGS "-I /usr/include/PCSC")
add_executable(test main.cpp)
target_link_libraries(test PUBLIC CONAN_PKG::LogicalAccess)
conanfile.txt:
[requires]
LogicalAccess/2.2.1
[generators]
cmake
[imports]
lib, * -> lib
And to build i just run cmake --build . in the project_folder/build.

How can I fix CLion problem with filesystem inlcude?

I use a CLion as my IDE. I create project with C++17 standard.
I have a code fragment below:
#include <filesystem>
namespace fs = std::filesystem;
std::vector<std::string> getFilesArray() {
auto dir = ".";
std::vector<std::string> filesList;
for (auto item : fs::recursive_directory_iterator(dir))
{
if (!fs::is_regular_file(item.path()) || item.path().extension() != ".cpp" && item.path().extension() != ".h" && item.path().extension() != ".hpp")
continue;
filesList.push_back(item.path().string());
}
return filesList;
}
When I try to compile project I have a lot of errors like:
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/fs_path.h: In member function 'std::filesystem::__cxx11::path& std::filesystem::__cxx11::path::operator/=(const std::filesystem::__cxx11::path&)':
C:/PROGRA~2/MINGW-~1/I686-8~1.0-P/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:237:47: error: no match for 'operator!=' (operand types are 'std::filesystem::__cxx11::path' and 'std::filesystem::__cxx11::path')
|| (__p.has_root_name() && __p.root_name() != root_name()))
My Cmake file is:
cmake_minimum_required(VERSION 3.15)
project(FinalParser)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-std=c++17 -lc++fs")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(FinalParser main.cpp Parse/Parser.cpp Parse/Parser.cpp Parse/Parser.h files.h Graph/Parser.cpp Graph/Parser.h)
target_link_libraries(FinalParser stdc++fs)
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:/Users/user/Desktop/boost_1_71_0")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:/Users/user/Desktop/boost_1_71_0/libs")
FIND_PACKAGE(Boost)
IF (Boost_FOUND)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ADD_DEFINITIONS( "-DHAS_BOOST" )
ENDIF()
How can I fix it?
So, I am going to give you the full proof solution for this similar error which also comes when someone tries to use the STL header file which is #include<bits/stdc++.h>
So, this error is occurring because you are currently using the GCC version 8.2.0 which is the default on the MinGW website. To solve this problem you need to update to GCC 10.2.0
Following are the steps you need to follow:-
Download and install the Msys2 from https://www.msys2.org/
Run or launch this and type pacman -Syu and hit enter then it will ask you to continue by typing Y/N just type enter without pressing Y/n.
Close this and again launch msys2 and again type the same command until it shows nothing to do with or nothing to update.
For doing the same I am also sharing a link of the youtube video.
https://www.youtube.com/watch?v=aXF4A5UeSeM&
5. For checking that you have successfully installed GCC 10.2.0 just open cmd and type GCC --version and hit enter if it is showing 10.2.0 hurray u did it, wait this is not the end.
6. Now uninstall Clion or any IDE which you are using.
7. Before reinstalling it you need to delete some of the remaining Jetbrains dump files which are showing in your program files folder (name Jetbrains) Delete that folder and Reinstall it.
8. Now select Msys2/MingW-64, not the previous one which you have in your system already (normal MinGW) while installing it.
9. Now You can go to your add or remove program section and can delete that waste normal MinGW-64, not the msys2 (don't delete msys2) only delete ming64 the previous one.
10. Now you can enjoy running that sexy stl library without having any error.
Still, if you have any problem doing these steps just ask me in a comment.

glewInit() "Missing GL Version" in CMake Release builds only

I am building my OpenGL project using CMake with MSVC. It uses GLEW with GLFW. It runs fine when I build with --config Debug, but glewInit() fails when I run the build command with --config Release. I've tried both 64 and 32 bit builds, and it runs in debug but not release in both cases.
I've used add_definitions(-DGLEW_STATIC) in my CMakeLists.txt file to avoid needing #define GLEW_STATIC everywhere, but I've tried both and it doesn't make a difference.
I can see that the release glew.lib is getting produced (and glewd.lib gets produced for debug builds), and if I inspect the project properties in VS, the correct one is in the list of linker inputs.
I've tried with and without the window hints before creating a GL context.
There's something I don't understand going on here. Why is my GL version missing, only for Release build configuration?
EDIT: Adding minimum reproducible example (thanks squareskittles for the suggestion).
This is my CMakeLists.exe file:
cmake_minimum_required(VERSION 3.14)
project(Demo LANGUAGES CXX)
# Make sure GLEW gets built as a static lib
add_definitions(-DGLEW_STATIC)
option( glew-cmake_BUILD_SHARED "Build the shared glew library" OFF )
option( glew-cmake_BUILD_STATIC "Build the static glew library" ON )
# Build dependencies
add_subdirectory(glew)
add_subdirectory(glfw)
add_executable(demo demo/main.cpp)
target_compile_features(demo PUBLIC cxx_std_17)
target_compile_definitions(demo PRIVATE -D_SCL_SECURE_NO_WARNINGS)
target_include_directories(demo PUBLIC glew/include)
target_include_directories(demo PUBLIC glfw/include)
# Link dependencies
target_link_libraries(demo libglew_static glfw ${GLFW_LIBRARIES})
It might be important to know that glew-cmake is what is in my glew directory.
And demo/main.cpp:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cassert>
int main(int argc, char** argv)
{
// Open a window and initialize Glew
assert(glfwInit());
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(600, 400, "window", 0, 0);
glfwMakeContextCurrent(window);
std::cout << "GLFW: " << glfwGetVersionString() << "\n";
std::cout << "GLEW: " << glewGetString(GLEW_VERSION) << "\n";
GLenum glewStatus = glewInit();
if (glewStatus != GLEW_OK) {
std::cout << "GLEW ERROR: " << glewGetErrorString(glewStatus) << "\n";
exit(1);
}
std::cout << "GL: " << glGetString(GL_VERSION) << "\n";
// Shut down graphics
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
My build commands are run from a directory just below the level of my CMakeLists.txt file. My debug build command is:
cmake ..
cmake --build . --config Debug
If I build & run the debug executable, a window opens before terminating, and the output is:
GLFW: 3.3.0 Win32 WGL EGL VisualC
GLEW: 2.2.0
GL: 3.2.0 - Build 25.20.100.6617
And my release build command is:
cmake ..
cmake --build . --config Release
If I build & run the release executable, I hit the exit(1) line, and the output is:
GLFW: 3.3.0 Win32 WGL EGL VisualC
GLEW: 2.2.0
GLEW ERROR: Missing GL version
UPDATE: I've tried linking with shared libs (dlls) instead of static libs, and was confirmed that the bug still reproduces.
UPDATE: I tried building a VS project from scratch with only my minimum reproducible case, manually specifying either the static or dynamic linkages, and it actually works! So it seems like it must be something with my CMake configuration.
I found that I could get release builds to run with the exact same linkages if I manually set up a project in VS. I diffed the vcxproj file that I manually created with the one generated by CMake to figure out the differences in release configurations. It came down to the NDEBUG preprocessor definition.
I don't understand why this definition causes OpenGL not to be found (I suppose that's another question, but if you can answer it here I'll award you the answer). If I add the line
string( REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
to my CMakeLists.txt file, I'm able to build and run in release mode!

SDL_Mixer 2.0.4 "MP3 support not available" even though libmpg123 is installed

I am a beginner to programming and am trying to make a simple console MP3 player as a project. For this, I need a way to play .mp3 files. SDL Mixer provides this facility but requires libmpg123 installed on one's system.
Call to 'Mix_Init()' always fails even though I have the required dependency 'libmpg123' installed. I do not have the dependencies for other formats i.e., FLAC, OGG, MOD and MIDI if that matters.
I am running Ubuntu 18.04 on my laptop and I have SDL2 version 2.0.9, SDL_Mixer 2.0.4 both built from source (although I have tried the versions available through the apt package manager). I also have libmpg123 installed which I have also built from source (again, I have also tried the version available through the package manager).
I compiled the code in two ways:
g++ Mix_Init.cpp -lSDL2 -lSDL2_mixer
and
g++ Mix_Init.cpp -lSDL2 -lSDL2_mixer -lmpg123
I don't know which one is right but neither one fixes the problem.
#include<SDL2/SDL.h>
#include<SDL2/SDL_mixer.h>
#include<iostream>
int main(int argc, char** argv)
{
if(SDL_Init(SDL_INIT_AUDIO))
std::cerr << "SDL_Init() Error: " << SDL_GetError() << std::endl;
else {
int result {0};
if(!((result = Mix_Init(MIX_INIT_MP3)) && MIX_INIT_MP3)){
std::cerr << Mix_GetError() << std::endl;
std::cerr << "Mix_Init() returns " << result << std::endl;
}
else {
std::cout << "Success!" << std::endl;
}
}
return 0;
}
Here are the contents of my /usr/local/lib directory:
cmake libmpg123.so.0.44.8 libSDL2-2.0.so.0 libSDL2main.la libSDL2_mixer.so pkgconfig
libglfw3.a libout123.la libSDL2-2.0.so.0.9.0 libSDL2_mixer-2.0.so.0 libSDL2.so python2.7
libmpg123.la libout123.so libSDL2.a libSDL2_mixer-2.0.so.0.2.2 libSDL2_test.a python3.6
libmpg123.so libout123.so.0 libSDL2.la libSDL2_mixer.a libSDL2_test.la python3.7
libmpg123.so.0 libout123.so.0.2.2 libSDL2main.a libSDL2_mixer.la mpg123
I expect the output:
Success!
Instead I get:
MP3 support not available
Mix_Init() returns 0
UPDATE:
Apparently, I have the other dependencies installed as well...
But I have no idea how to use them.
You need to have development files for mpg123 installed when you're building SDL2_mixer. E.g. for debian/ubuntu you'll need libmpg123-dev (and likewise for other formats), and SDL_mixer's ./configure should output something like
checking mpg123.h usability... yes
checking mpg123.h presence... yes
checking for mpg123.h... yes
checking for mpg123_replace_reader_handle in -lmpg123... yes
-- dynamic libmpg123 -> libmpg123.so.0
Then make && make install. Or use distro-provided libsdl2-mixer-dev, if it fits your requirements.

How to get std::cout to show in eclipse IDE console when using boost.test?

As the question says, I am having trouble seeing std::out statements in the eclipse console when using the "c/c++ unit" run configuration with boost.test.
Here is a simple example describing the issue.
quickEclipse_stdout.cpp:
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "I see this in the eclipse console!\n";
std::cout << "I also see this in the terminal!\n";
return(0);
}
quickEclipse_stdout_test.cpp:
#include <iostream>
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(quickTest_stdout)
BOOST_AUTO_TEST_CASE(bla)
{
std::cout << "I cannot see this in the eclipse console" << std::endl;
std::cout << "but I can see it when run outside eclipse in terminal" << std::endl;
BOOST_CHECK_MESSAGE(true, "of course I see this in eclipse c/c++ test log");
}
BOOST_AUTO_TEST_SUITE_END()
I used cmake to generate project files for eclipse.
CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(quickTest CXX)
FIND_PACKAGE(Boost COMPONENTS program_options filesystem regex unit_test_framework)
IF (Boost_FOUND)
MESSAGE("Boost found!!!")
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
ADD_DEFINITIONS( "-DHAS_BOOST" )
ENDIF()
#main output to console works
ADD_EXECUTABLE(quickEclipse_stdout, quickEclipse_stdout.cpp)
#boost.test output to console doesnt work
ADD_EXECUTABLE(quickEclipse_stdout_test UTDriver.cpp quickEclipse_stdout_test.cpp)
TARGET_LINK_LIBRARIES(quickEclipse_stdout_test ${Boost_LIBRARIES})
UTDriver.cpp:
#define BOOST_TEST_MODULE myQuickEclipseUT
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
To generate the eclipse project I run the following command:
cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_ECLIPSE_VERSION=4.3 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER_ARG1=-std=c++11 ~/mySrcDir
I open the project in eclipse and enter c++ perspective
I then build the executables by double-clicking on make targets (quickEclipse_stdout, quickEclipse_stdout_test)
I goto run configurations and create "c/c++ application" for quickEclipse_stdout.
I goto run configurations and create "c/c++ unit" for quickEclipse-stdout_test and select boost as the test runner.
I run quickEclipse_stdout and see the output in eclipse console.
I run quickEclipse_stdout_test and the eclipse console is empty. I see the message in the c/c++ unit messages tab.
I can see the std::out on both if I run the executables in a terminal outside eclipse.
How can I get the std::out to show in the eclipse console when running the quickEclipse_stdout_test?
I am on Linux CentOS 6.5.
Eclipse Standard/SDK
Version: Kepler Service Release 1
Build id: 20130919-0819
Eclipse C/C++ Development Tools
Version: 8.2.1.201309180223
Im sure based on your description its redirecting stdout to its own window for testing purposes
I am exactly in your situation and I think it is a missing feature of the C/C++ unit test plugin in Eclipse.
The plugin requires the whole test output in xml format (--output_format=XML) which puts the stdout messages in the node of the xml output. The output will be directly parsed by the plugin and the information is organized in the according view. The plugin misses the option to show the stdout messages. I could simply have a further icon beside error, warning, information to show the stdout messages.
I have opened an issue for this on github: https://github.com/xgsa/cdt-tests-runner/issues/14 but the project it not actively maintained the last years...