00001
00016 #include <defines.h>
00017
00018 #include <controllers/controllerUtil.h>
00019
00020 #include <sstream>
00021 #include <iostream>
00022
00023 #include <dtUtil/log.h>
00024
00025 using namespace std;
00026
00027
00028
00029
00045 float ControllerUtil::computeAverage(HistoryMap* histMap, float currentValue) {
00046 float sum = 0.0, result = histMap->defaultValue;
00047 int cnt = 0;
00048 std::vector<float>* hist = &(histMap->history);
00049 (*hist)[histMap->currentValueIndex] = currentValue;
00050
00051 #ifdef WITH_DEBUGGING
00052 std::ostringstream msg; msg.clear(); msg.str("");
00053 msg << "History: " ;
00054 #endif
00055 for (int x = 0; x < histMap->historySize; ++x) {
00056 #ifdef WITH_DEBUGGING
00057 msg.flags( ios::right);
00058 msg.width(3);
00059 msg << int(hist->at(x));
00060 #endif
00061 if((*hist)[x] != histMap->defaultValue) {
00062 int weight = (histMap->historySize-(histMap->currentValueIndex-x))%(histMap->historySize);
00063 if(weight == 0) weight = histMap->historySize;
00064 sum += (*hist)[x]*weight;
00065 cnt += weight;
00066 #ifdef WITH_DEBUGGING
00067 msg << "*" << weight << ",";
00068 #endif
00069 } else {
00070 #ifdef WITH_DEBUGGING
00071 msg << "*0?,";
00072 #endif
00073 }
00074 }
00075
00076 histMap->currentValueIndex = (histMap->currentValueIndex+1)%histMap->historySize;
00077 if(cnt > 0) result = sum / cnt;
00078
00079 #ifdef WITH_DEBUGGING
00080 msg << " becomes (" << sum << "/" << cnt << "): ";
00081 msg.precision(2); msg.width(6); msg.flags(ios::right);
00082 msg << fixed << result;
00083 LOG_DEBUG(msg.str());
00084 #endif
00085 return result;
00086 }
00087
00088 void ControllerUtil::print(HistoryMap *histMap) {
00089 std::vector<float>* hist = &(histMap->history);
00090 std::ostringstream msg;
00091 msg.clear(); msg.str(""); msg.flush();
00092 msg << "History [" << histMap->historySize << "]: " ;
00093
00094 for (int x = 0; x < histMap->historySize; ++x) {
00095 msg << int(hist->at(x)) << ",";
00096 }
00097 LOG_INFO(msg.str());
00098 }
00099
00106 int ControllerUtil::computeAverageClassification(HistoryMap* histMap, float currentValue) {
00107 std::vector<float>* hist = &(histMap->history);
00108 (*hist)[histMap->currentValueIndex] = currentValue;
00109
00110
00111 std::vector<int> energyCount;
00112 int histogram_size = 2;
00113 energyCount.reserve(histogram_size);
00114
00115 for (int x = 0; x < histogram_size; ++x) energyCount[x] = 0;
00116
00117 int maxCount = 0;
00118 int maxValue = 0;
00119 print(histMap);
00120
00121 std::ostringstream msg;
00122 msg.clear(); msg.str(""); msg.flush();
00123 msg << "Energy count [" << histogram_size << "]: ";
00124 for (int x = 0; x < histogram_size; ++x) {
00125 msg << energyCount[x] << ",";
00126 if(energyCount[x] > maxCount) {
00127 maxCount = energyCount[x];
00128 maxValue = x-1;
00129 }
00130 }
00131 LOG_DEBUG(msg.str());
00132 histMap->currentValueIndex = (histMap->currentValueIndex+1)%histMap->historySize;
00133 return maxValue;
00134 }
00135