Go to the documentation of this file.00001
00006 #ifndef ROBONETDATAELEMENT_H_
00007 #define ROBONETDATAELEMENT_H_
00008
00009 #include <cstdlib>
00010 #include "robot_instance/BitWizard.h"
00011 #include "robot_instance/DataElement.h"
00012 #include <boost/interprocess/smart_ptr/shared_ptr.hpp>
00013
00016 class PushToWriteElementException: public std::exception
00017 {
00018 virtual const char* what() const throw()
00019 {
00020 return "You cannot push() raw values into 'Writable' RobonetDataElements.";
00021 }
00022 };
00023
00026 class DataMangler
00027 {
00028 public:
00029
00030 static uint32_t combineRawValues(std::pair<uint16_t, uint16_t> rawPair);
00031 static std::pair<uint16_t, uint16_t> separateRawValues(uint32_t value);
00032
00033
00034
00041 template <typename T>
00042 static T fromUnsignedIntTo(uint32_t data)
00043 {
00044 void* voidPtr;
00045 T output;
00046
00047 voidPtr = (void*)&data;
00048 output = *((T*)voidPtr);
00049 return output;
00050 }
00051
00058 template <typename T>
00059 static uint32_t toUnsignedIntFrom(T value)
00060 {
00061 void* voidPtr;
00062 uint32_t output;
00063
00064 voidPtr = (void*)&value;
00065 output = *((uint32_t*)voidPtr);
00066 return output;
00067 }
00068 };
00069
00070 template <>
00071 bool DataMangler::fromUnsignedIntTo<bool>(uint32_t data);
00072
00076 class RobonetDataElement : public DataElement
00077 {
00078 public:
00079
00080
00084 typedef std::pair<std::string, std::string> OffsetPair;
00085
00088 typedef std::pair<uint16_t, uint16_t> RawPair;
00089
00090 enum ElementStyle
00091 {
00092 Normal = 0,
00093
00094
00095 MotCom = 1,
00096 Temperature = 2,
00097 BrakePwm = 3,
00098
00099
00100 FixedPointMilli = 4,
00101 UnsignedFixedPointMilli = 5,
00102 FixedPointCenti = 6,
00103 UnsignedFixedPointCenti = 7,
00104
00105
00106 PowConTemperature = 8,
00107 PowConCurrentMany = 9,
00108 PowConVoltageMany = 10,
00109 };
00110
00111 enum TrimValues
00112 {
00113 MotComMax = 255,
00114 MotComMin = -255,
00115 TemperatureMin = 0,
00116 BrakePwmMax = 4096,
00117 };
00118
00119 template<typename T>
00120 int16_t TrimMotCom(const T t)
00121 {
00122 int16_t retval = t;
00123
00124 if (retval > MotComMax)
00125 {
00126 retval = MotComMax;
00127 }
00128 else if (retval < MotComMin)
00129 {
00130 retval = MotComMin;
00131 }
00132
00133 return retval;
00134 }
00135
00136 template<typename T>
00137 float TrimTemperature(const T t)
00138 {
00139 float retval = t;
00140
00141 if (retval < TemperatureMin)
00142 {
00143 retval = TemperatureMin;
00144 }
00145
00146 return retval;
00147 }
00148
00149 template<typename T>
00150 uint16_t TrimBrakePwm(const T t)
00151 {
00152 float retval = t;
00153
00154 if (retval > BrakePwmMax)
00155 {
00156 retval = BrakePwmMax;
00157 }
00158
00159 return retval;
00160 }
00161
00162
00163 RobonetDataElement(const std::string& type, DataElement::ReadWrite readWrite, const std::string& channel, const std::string& node, OffsetPair offsetPair, const std::string& group, ElementStyle mangleOption = Normal, std::shared_ptr<SMT::Resource> resource = NULL);
00164 virtual ~RobonetDataElement();
00165 template <typename T> void setValue(const T v);
00166 ElementSize getRawSize();
00167 void setChannel(const std::string& channel);
00168 void setNode(const std::string& node);
00169 void setOffsetPair(OffsetPair offsetPair);
00170 void setGroup(const std::string& group);
00171 std::string getChannel();
00172 std::string getNode();
00173 OffsetPair getOffsetPair();
00174 std::string getGroup();
00175 RawPair getRawPair();
00176 void push(RawPair rawPair);
00177 const RawPair pull();
00178 std::string toString();
00179
00180 private:
00181
00182 std::string channel;
00183 std::string node;
00184 std::string group;
00185 OffsetPair offsetPair;
00186 RawPair rawPair;
00187 ElementStyle style;
00188 BitWizard<uint16_t> bitWizard16;
00189
00190 static const BitMinion::BitIndexEnumType MOTCOM_SIGN_BIT_INDEX;
00191 static const BitMinion::BitIndexEnumType MOTCOM_CERTIFICATE_INDEX;
00192 static const BitMinion::BitCountMaskEnumType MOTCOM_CERTIFICATE_SIZE;
00193 static const uint16_t MOTCOM_CERTIFICATE_VALUE;
00194 static const BitMinion::BitIndexEnumType BRAKEPWM_CERTIFICATE_INDEX;
00195 static const BitMinion::BitCountMaskEnumType BRAKEPWM_CERTIFICATE_SIZE;
00196 static const uint16_t BRAKEPWM_CERTIFICATE_VALUE;
00197 };
00198
00199
00202 typedef boost::shared_ptr<RobonetDataElement> RobonetDataElementPtr;
00203
00204 template <typename T>
00205 void RobonetDataElement::setValue(const T v)
00206 {
00207 T newValue = v;
00208
00209 switch (style)
00210 {
00211 case MotCom:
00212 newValue = TrimMotCom(newValue);
00213 break;
00214
00215 case Temperature:
00216 newValue = TrimTemperature(newValue);
00217 break;
00218
00219 case BrakePwm:
00220 newValue = TrimBrakePwm(newValue);
00221 break;
00222
00223
00224
00225 default:
00226 break;
00227 }
00228
00229
00230 DataElement::setValue<T>( newValue );
00231 }
00232
00233 #endif