Go to the documentation of this file.00001 #include <vector>
00002 #include <iostream>
00003 #include <cmath>
00004 #include <stdint.h>
00005 #include <math.h>
00006
00007 #define MAX_OBSERVATION_COUNT 10 // The larger, the more memory.
00008
00009 const double p_occupied_when_laser = 0.95;
00010 const double max_log_odds = 20;
00011
00012 using std::vector;
00013
00014 typedef std::pair<int,int> pt_t;
00015
00016
00017
00018 inline int offsetFromRowCol(const int row, const int col, const int ncol)
00019 {
00020 return (row * ncol) + col;
00021 }
00022
00023
00024
00025 void updatePointOccupancy(const bool occupied, const pt_t& pt, const int ncol, vector<int8_t>& occupancy, vector<double>& log_odds)
00026 {
00027 if (occupancy.size() != log_odds.size())
00028 {
00029 std::cerr << "occupancy and count do not have the same number of elements" << std::endl;
00030 }
00031 if (ncol > occupancy.size())
00032 {
00033 std::cerr << "column count too large" << std::endl;
00034 }
00035
00036 const unsigned int nrow = occupancy.size() / ncol;
00037 if (!(0 <= pt.first && pt.first < ncol &&
00038 0 <= pt.second && pt.second < nrow))
00039 {
00040
00041 return;
00042 }
00043 int idx = offsetFromRowCol(pt.first, pt.second, ncol);
00044 double old_log_odds = log_odds[idx];
00045 double old_p_occupancy = ((double) occupancy[idx]) / 100;
00046
00047 double p;
00048 if (occupied)
00049 {
00050 p = p_occupied_when_laser;
00051 }
00052 else
00053 {
00054 p = 1 - p_occupied_when_laser;
00055 }
00056 if (old_p_occupancy < 0)
00057 {
00058 old_p_occupancy = 0.5;
00059 }
00060 if (old_p_occupancy == 1)
00061 {
00062 log_odds[idx] = -1;
00063 std::cerr << "old_p_occupancy == 1" << std::endl;
00064 }
00065 else if (old_p_occupancy == 0)
00066 {
00067 log_odds[idx] = 1;
00068 std::cerr << "old_p_occupancy == 0" << std::endl;
00069 }
00070 else
00071 {
00072
00073 log_odds[idx] = old_log_odds + std::log(p / (1 - p));
00074 }
00075 if (log_odds[idx] < -max_log_odds)
00076 {
00077 log_odds[idx] = -max_log_odds;
00078 }
00079 else if(log_odds[idx] > max_log_odds)
00080 {
00081 log_odds[idx] = max_log_odds;
00082 }
00083
00084 int8_t new_occupancy = lround((1 - 1 / (1 + std::exp(log_odds[idx]))) * 100);
00085 if (new_occupancy < 1)
00086 {
00087 new_occupancy = 1;
00088 }
00089 else if (new_occupancy > 99)
00090 {
00091 new_occupancy = 99;
00092 }
00093 occupancy[idx] = new_occupancy;
00094 }
00095
00096 pt_t pt(0, 0);
00097 const int ncol = 1;
00098 vector<int8_t> occupancy(1, -1);
00099 vector<double> log_odds(1, 0);
00100
00101 void updateOccupancyAndPrint(bool occupied)
00102 {
00103 updatePointOccupancy(occupied, pt, ncol, occupancy, log_odds);
00104 std::cout << "New log odds: " << log_odds[0] << std::endl;
00105 std::cout << "New occupancy: " << (int) occupancy[0] << std::endl;
00106 std::cout << std::endl;
00107 }
00108
00109 int main(int argc, char** argv)
00110 {
00111 std::cout << "Obstacle" << std::endl;
00112 std::cout << "-----------" << std::endl;
00113 for (unsigned int i = 0; i < (MAX_OBSERVATION_COUNT + 2); ++i)
00114 {
00115 updateOccupancyAndPrint(true);
00116 }
00117
00118 std::cout << "No obstacle" << std::endl;
00119 std::cout << "-----------" << std::endl;
00120 for (unsigned int i = 0; i < (MAX_OBSERVATION_COUNT + 2); ++i)
00121 {
00122 updateOccupancyAndPrint(false);
00123 }
00124
00125 std::cout << "Obstacle" << std::endl;
00126 std::cout << "-----------" << std::endl;
00127 for (unsigned int i = 0; i < (MAX_OBSERVATION_COUNT + 2); ++i)
00128 {
00129 updateOccupancyAndPrint(true);
00130 }
00131
00132 }
00133