Skip to content

Commit 02d1d83

Browse files
committed
First commit
0 parents  commit 02d1d83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1110996
-0
lines changed

CMakeLists.txt

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
cmake_minimum_required(VERSION 2.8.3)
2+
project(amcl_lib_pf)
3+
4+
# Build Particle Filter Library REFERENCED AS PF
5+
add_subdirectory(include/libPF)
6+
7+
## Compile as C++11, supported in ROS Kinetic and newer
8+
add_compile_options(-std=c++11)
9+
10+
## Find catkin macros and libraries
11+
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
12+
## is used, also find other catkin packages
13+
set(PACKAGE_DEPENDENCIES
14+
roscpp
15+
nav_msgs
16+
sensor_msgs
17+
std_msgs
18+
octomap_ros
19+
octomap_msgs
20+
pcl_conversions
21+
pcl_ros
22+
cmake_modules
23+
tf2_geometry_msgs
24+
tf2
25+
tf2_ros
26+
)
27+
28+
find_package(catkin REQUIRED COMPONENTS ${PACKAGE_DEPENDENCIES} )
29+
30+
find_package(OCTOMAP REQUIRED)
31+
include_directories(${OCTOMAP_INCLUDE_DIRS})
32+
33+
find_package(Eigen3 REQUIRED)
34+
35+
find_package(PCL 1.7 REQUIRED)
36+
include_directories(${PCL_INCLUDE_DIRS})
37+
link_directories(${PCL_LIBRARY_DIRS})
38+
add_definitions(${PCL_DEFINITIONS})
39+
40+
## System dependencies are found with CMake's conventions
41+
find_package(Boost REQUIRED COMPONENTS system)
42+
43+
44+
catkin_package(
45+
# INCLUDE_DIRS include
46+
# LIBRARIES amcl_lib_pf
47+
# CATKIN_DEPENDS other_catkin_pkg
48+
# DEPENDS system_lib
49+
)
50+
51+
###########
52+
## Build ##
53+
###########
54+
55+
## Specify additional locations of header files
56+
## Your package locations should be listed before other locations
57+
include_directories(
58+
include
59+
${catkin_INCLUDE_DIRS}
60+
src include/libPF/include
61+
)
62+
63+
set(amcl_lib_pf_SOURCES
64+
src/amcl_lib_pf_node.cpp
65+
src/RobotState.cpp
66+
src/AMCLDepth.cpp
67+
src/MapModel.cpp
68+
src/RobotMovementModel.cpp
69+
src/RobotObservationModel.cpp
70+
src/RobotStateDistribution.cpp
71+
)
72+
73+
74+
## Declare a C++ library
75+
# add_library(${PROJECT_NAME}
76+
# src/${PROJECT_NAME}/amcl_lib_pf.cpp
77+
# )
78+
79+
## Add cmake target dependencies of the library
80+
## as an example, code may need to be generated before libraries
81+
## either from message generation or dynamic reconfigure
82+
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
83+
84+
## Declare a C++ executable
85+
## With catkin_make all packages are built within a single CMake context
86+
## The recommended prefix ensures that target names across packages don't collide
87+
add_executable(${PROJECT_NAME}_node ${amcl_lib_pf_SOURCES})
88+
89+
## Specify libraries to link a library or executable target against
90+
target_link_libraries(${PROJECT_NAME}_node
91+
${catkin_LIBRARIES}
92+
${PCL_LIBRARIES}
93+
PF
94+
)
95+
96+

CMakeLists.txt.backup

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(libpfsample2)
3+
4+
add_subdirectory(include/libPF)
5+
6+
set(sample2_SOURCES src/main.cpp
7+
src/CarMovementModel.cpp
8+
src/CarObservationModel.cpp
9+
src/CarState.cpp
10+
src/CarStateDistribution.cpp
11+
)
12+
13+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
14+
include_directories(src include/libPF/include)
15+
16+
add_executable(sample2 ${sample2_SOURCES} )
17+
18+
target_link_libraries(sample2 PF)
19+

include/libPF/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
project(libPF)
2+
cmake_minimum_required(VERSION 2.8)
3+
4+
include_directories(include)
5+
6+
add_library(PF
7+
src/CRandomNumberGenerator.cpp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#ifndef RANDOMNUMBERGENERATOR_H
2+
#define RANDOMNUMBERGENERATOR_H
3+
4+
#include "libPF/RandomNumberGenerationStrategy.h"
5+
6+
namespace libPF
7+
{
8+
9+
/**
10+
* @class CRandomNumberGenerator
11+
*
12+
* @brief Class for the generation of random numbers.
13+
*
14+
* This class can generate randomly generated numbers from uniform and
15+
* gaussian distributions.
16+
* Note: this is a very simple PRNG, using the C-function rand().
17+
*
18+
* @author Stephan Wirth
19+
*/
20+
class CRandomNumberGenerator : public RandomNumberGenerationStrategy {
21+
22+
public:
23+
24+
/**
25+
* The constructor calls init().
26+
*/
27+
CRandomNumberGenerator();
28+
29+
/**
30+
* Empty destructor.
31+
*/
32+
~CRandomNumberGenerator();
33+
34+
/**
35+
* This method creates gaussian distributed random numbers (Box-Müller method).
36+
* @param standardDeviation Standard deviation d of the random number to generate.
37+
* @return N(0, d*d)-distributed random number
38+
*/
39+
double getGaussian(double standardDeviation) const;
40+
41+
/**
42+
* Generates a uniform distributed random number between min and max.
43+
* @param min the minimum value, default is 0.0
44+
* @param max the maximum value, default is 1.0
45+
* @return random number between min and max, uniform distributed.
46+
*/
47+
double getUniform(double min = 0.0, double max = 1.0) const;
48+
49+
protected:
50+
51+
/**
52+
* Initializes the seed by calling srand(time(0))
53+
*/
54+
void init();
55+
56+
private:
57+
58+
/// stores if there is a buffered gaussian variable or not
59+
mutable bool m_GaussianBufferFilled;
60+
61+
/// buffer for a gaussian distributed variable, the Box-Müller method always
62+
/// creates two variables, we return one and store the other here.
63+
mutable double m_GaussianBufferVariable;
64+
65+
};
66+
67+
} // end of namespace
68+
69+
#endif // RANDOMNUMBERGENERATOR_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef COMPAREPARTICLEWEIGHTS_H
2+
#define COMPAREPARTICLEWEIGHTS_H
3+
4+
#include "libPF/Particle.h"
5+
6+
namespace libPF
7+
{
8+
9+
/**
10+
* @class CompareParticleWeights
11+
*
12+
* @author Stephan Wirth
13+
*
14+
* @brief Class with one operator to compare two pointers of particles according
15+
* to the weight of the particles.
16+
*
17+
* With this class as compare function, std::sort() can be used on arrays of
18+
* pointers to Particle. After sorting the array with this function, the particle
19+
* with the smallest weight will be at the last position.
20+
*
21+
* @see Particle
22+
*/
23+
template <class StateType>
24+
class CompareParticleWeights {
25+
26+
public:
27+
28+
/**
29+
* @return true if the weight of the particle p1 is higher than the weight of particle p2.
30+
*/
31+
bool operator() (const libPF::Particle<StateType>* p1,
32+
const libPF::Particle<StateType>* p2) const
33+
{
34+
return p1->getWeight() > p2->getWeight();
35+
}
36+
};
37+
38+
} // end of namespace
39+
40+
#endif
41+
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#ifndef IMPORTANCERESAMPLING_H
2+
#define IMPORTANCERESAMPLING_H
3+
4+
#include "libPF/CRandomNumberGenerator.h"
5+
6+
namespace libPF
7+
{
8+
9+
/**
10+
* @class ImportanceResampling
11+
*
12+
* @brief A resampling strategy that performs importance resampling
13+
*
14+
* The resampling strategy defines how the resampling is performed in the resample step
15+
* of a particle filter.
16+
*
17+
* @author Stephan Wirth
18+
*
19+
* @see ResamplingStrategy
20+
*/
21+
22+
template <class StateType>
23+
class ImportanceResampling : public ResamplingStrategy<StateType>{
24+
25+
/**
26+
* A ParticleList is an array of pointers to Particles.
27+
*/
28+
typedef std::vector< Particle<StateType>* > ParticleList;
29+
30+
public:
31+
/**
32+
* The constructor of this base class inits some members.
33+
*/
34+
ImportanceResampling<StateType>();
35+
36+
/**
37+
* The destructor is empty.
38+
*/
39+
virtual ~ImportanceResampling();
40+
41+
/**
42+
* This is the main method of ImportanceResampling. It takes two references to
43+
* particle lists. The first reference refers to the old particle list, the
44+
* second to the new one.
45+
* @param source the source list to draw new particles from.
46+
* @param destination the destination list where to put the copies.
47+
*/
48+
void resample(const ParticleList& source, const ParticleList& destination) const;
49+
50+
/**
51+
* Sets the Random Number Generator to use in resample() to generate uniformly distributed random numbers.
52+
*/
53+
void setRNG(RandomNumberGenerationStrategy* rng);
54+
55+
private:
56+
57+
// Stores a pointer to the random number generator.
58+
const RandomNumberGenerationStrategy* m_RNG;
59+
60+
// The default random number generator
61+
CRandomNumberGenerator m_DefaultRNG;
62+
63+
};
64+
65+
66+
template <class StateType>
67+
ImportanceResampling<StateType>::ImportanceResampling() :
68+
m_RNG(&m_DefaultRNG) {
69+
}
70+
71+
template <class StateType>
72+
ImportanceResampling<StateType>::~ImportanceResampling() {
73+
}
74+
75+
76+
// resampling based on the cumulative distribution function (CDF)
77+
template <class StateType>
78+
void ImportanceResampling<StateType>::resample(const ParticleList& sourceList, const ParticleList& destinationList) const {
79+
80+
double inverseNum = 1.0f / sourceList.size();
81+
double start = m_RNG->getUniform() * inverseNum; // random start in CDF
82+
double cumulativeWeight = 0.0f;
83+
unsigned int sourceIndex = 0; // index to draw from
84+
cumulativeWeight += sourceList[sourceIndex]->getWeight();
85+
for (unsigned int destIndex = 0; destIndex < destinationList.size(); destIndex++) {
86+
double probSum = start + inverseNum * destIndex; // amount of cumulative weight to reach
87+
while (probSum > cumulativeWeight) { // sum weights until
88+
sourceIndex++;
89+
if (sourceIndex >= sourceList.size()) {
90+
sourceIndex = sourceList.size() - 1;
91+
break;
92+
}
93+
cumulativeWeight += sourceList[sourceIndex]->getWeight(); // target sum reached
94+
}
95+
*(destinationList[destIndex]) = *(sourceList[sourceIndex]); // copy particle (via assignment operator)
96+
}
97+
}
98+
99+
100+
template <class StateType>
101+
void ImportanceResampling<StateType>::setRNG(RandomNumberGenerationStrategy* rng)
102+
{
103+
m_RNG = rng;
104+
}
105+
106+
} // end of namespace
107+
#endif // IMPORTANCERESAMPLING_H
108+

0 commit comments

Comments
 (0)