iCub-main
IScaler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2010 RobotCub Consortium, European Commission FP6 Project IST-004370
3  * author: Arjan Gijsberts
4  * email: arjan.gijsberts@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 #include <sstream>
20 #include <cmath>
21 
22 #include <yarp/os/Bottle.h>
23 #include <yarp/os/Value.h>
24 
26 
27 namespace iCub {
28 namespace learningmachine {
29 
30 double IScaler::transform(double val) {
31  // update scaler if enabled
32  if(this->updateEnabled) {
33  this->update(val);
34  }
35  // check for division by zero
36  return (std::abs(this->scale) < 1.e-20) ? (val - this->offset) : (val - this->offset) / this->scale;
37 }
38 
39 
40 std::string IScaler::getInfo() {
41  std::ostringstream buffer;
42  buffer << "Type: " << this->getName() << ", ";
43  buffer << "Offset: " << this->offset << ", ";
44  buffer << "Scale: " << this->scale << ", ";
45  buffer << "Update: " << (this->updateEnabled ? "enabled" : "disabled");
46  return buffer.str();
47 }
48 
49 std::string IScaler::toString() {
50  yarp::os::Bottle model;
51  this->writeBottle(model);
52  return model.toString().c_str();
53 }
54 
55 bool IScaler::fromString(const std::string& str) {
56  yarp::os::Bottle model(str.c_str());
57  this->readBottle(model);
58  return true;
59 }
60 
61 void IScaler::writeBottle(yarp::os::Bottle& bot) {
62  bot.addFloat64(this->offset);
63  bot.addFloat64(this->scale);
64  bot.addInt32((this->updateEnabled ? 1 : 0));
65 }
66 
67 void IScaler::readBottle(yarp::os::Bottle& bot) {
68  this->updateEnabled = (bot.pop().asInt32() == 1);
69  this->scale = bot.pop().asFloat64();
70  this->offset = bot.pop().asFloat64();
71 }
72 
73 bool IScaler::configure(yarp::os::Searchable& config) {
74  bool success = false;
75 
76  // toggle enable/disable update
77  if(!config.findGroup("update").isNull()) {
78  this->setUpdateEnabled(!this->getUpdateEnabled());
79  success = true;
80  }
81 
82  // enable update
83  if(!config.findGroup("enable").isNull()) {
84  this->setUpdateEnabled(true);
85  success = true;
86  }
87 
88  // disable update
89  if(!config.findGroup("disable").isNull()) {
90  this->setUpdateEnabled(false);
91  success = true;
92  }
93 
94  return success;
95 }
96 
97 
98 } // learningmachine
99 } // iCub
100 
virtual std::string getInfo()
Asks the learning machine to return a string containing statistics on its operation so far.
Definition: IScaler.cpp:40
virtual std::string toString()
Asks the scaler to return a string serialization.
Definition: IScaler.cpp:49
std::string getName() const
Retrieve the name of this scaler.
Definition: IScaler.h:133
virtual void writeBottle(yarp::os::Bottle &bot)
Writes a serialization of the scaler into a bottle.
Definition: IScaler.cpp:61
double scale
The scale for the linear transformation.
Definition: IScaler.h:51
virtual void update(double val)
Feeds a single sample into the scaler, so that it can use this sample to update the offset and scale.
Definition: IScaler.h:98
bool updateEnabled
Boolean indicating whether the scaler has to update each sample.
Definition: IScaler.h:61
virtual double transform(double val)
Transforms a single sample value according to the state of the scaler.
Definition: IScaler.cpp:30
virtual void setUpdateEnabled(bool u)
Mutator for the update state.
Definition: IScaler.h:147
virtual bool fromString(const std::string &str)
Asks the scaler to initialize from a string serialization.
Definition: IScaler.cpp:55
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a scaler from a bottle.
Definition: IScaler.cpp:67
virtual bool getUpdateEnabled()
Accessor for the update state.
Definition: IScaler.h:154
double offset
The offset for the linear transformation.
Definition: IScaler.h:46
virtual bool configure(yarp::os::Searchable &config)
Definition: IScaler.cpp:73
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.