Hey David,
I happened to be working on the same thing as you with regards to adding cost functions as plugins to the DWAPlanner and came across this exact same problem.
I believe the issue is that you cannot link against the same library that contains the plugin that you are trying to load.
I can get your example to run with the following changes to your CMakeLists.txt:
add_library(cost_functions
src/map_grid_cost_function.cpp
src/goal_dist_cost_function.cpp
src/path_dist_cost_function.cpp
src/offset_grid_cost_function.cpp
src/goal_align_cost_function.cpp
src/path_align_cost_function.cpp
src/obstacle_cost_function.cpp
src/oscillation_cost_function.cpp
src/prefer_forward_cost_function.cpp
)
add_library(dwa_local_planner src/dwa_planner.cpp src/dwa_planner_ros.cpp src/simple_scored_sampling_planner.cpp)
target_link_libraries(dwa_local_planner base_local_planner)
add_dependencies(dwa_local_planner dwa_local_planner_gencfg)
add_dependencies(dwa_local_planner nav_msgs_gencpp)
add_executable(plugin_quiz src/plugin_quiz.cpp)
target_link_libraries(plugin_quiz ${catkin_LIBRARIES} dwa_local_planner)
add_executable(plugin_quiz2 src/plugin_quiz2.cpp)
target_link_libraries(plugin_quiz2 ${catkin_LIBRARIES} dwa_local_planner)
However, I now get the following error after running:
Warning: class_loader::ClassLoader: SEVERE WARNING!!! Attempting to unload library while objects created by this loader exist in the heap! You should delete your objects before attempting to unload the library or destroying the ClassLoader. The library will NOT be unloaded.
at line 94 in /tmp/buildd/ros-hydro-class-loader-0.2.3-0precise-20131010-0141/src/class_loader.cpp
Segmentation fault (core dumped)
The segfault appears to be occurring with the deletion of the WorldModel in the destructor of ObstacleCostFunction - changing world_model_ to a boost::shared_ptr appears to fix this, though I'm not sure what the root cause is. I'm not sure about the cause of the class_loader warning either, but calling tc_.reset() at the end of your plugin_quiz2.cpp appears to fix it.
With these changes, I intermittently get the following error when the program finishes:
plugin_quiz2: pthread_mutex_lock.c:62: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed.
Aborted (core dumped)
Let me know if that helps. Looking forward to seeing this addition to the planner!
Hey David, I can try to help you debug this. What do you mean when you say "in isolation"? First thing to check is after sourcing your environment, check LD_LIBRARY_PATH to see if it's pointing to the right place and has the right ordering. Then make sure the library with the respective class (dwa_local_planner::OscillationCostFunction) actually exists.
Other things to check -- make sure the plugin name is correct in your plugin xml file and corresponds with what you're attempting to load.
I have clarified what I meant by in isolation. The library is built, contains the respective class and is on the LD_LIBRARY_PATH.
I understand now what you mean. Everything looks good it terms of your package.xml, CMakeLists.txt and plugin XML. However, your error message is interesting nwo that I read it a second time -- you try to create a DWAPlannerROS but its trying to lookup the factory for OscillationCostFunction. Does that mean the DWAPlannerROS plugin has a classloader inside of it that tries to load that cost function boject?
Ah ok, so DWAPlannerROS is creating a DWAPlanner inside of it which itself has a plugin classloader for creating the OscillationCostFunction which is a TrajectoryCostFunciion...
Wait where's the plugin XML file for OscillationCostFunction...that's probably what's wrong
Its default_critics.xml, as listed in the package.xml