sc8000.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "Devices/sc8000.H"
00041
00042 #include "Component/OptionManager.H"
00043 #include "Devices/Serial.H"
00044
00045
00046 SC8000::SC8000(OptionManager& mgr, const std::string& descrName,
00047 const std::string& tagName, const char *defdev) :
00048 ModelComponent(mgr, descrName, tagName),
00049 itsPort(new Serial(mgr))
00050 {
00051
00052 itsPort->configure(defdev, 9600, "8N1", false, false, 1);
00053
00054
00055 addSubComponent(itsPort);
00056
00057
00058 zero = new rutz::shared_ptr<NModelParam<float> >[SSCNUMSERVOS];
00059 posmult = new rutz::shared_ptr<NModelParam<float> >[SSCNUMSERVOS];
00060 negmult = new rutz::shared_ptr<NModelParam<float> >[SSCNUMSERVOS];
00061 pos = new short[SSCNUMSERVOS];
00062 for (uint i = 0; i < SSCNUMSERVOS; i ++)
00063 {
00064 pos[i] = 32768; char buf[20];
00065 sprintf(buf, "Zero%d", i);
00066 zero[i] = NModelParam<float>::make(buf, this, 0.0F);
00067
00068 sprintf(buf, "PosMult%d", i);
00069 posmult[i] = NModelParam<float>::make(buf, this, 1.0F);
00070
00071 sprintf(buf, "NegMult%d", i);
00072 negmult[i] = NModelParam<float>::make(buf, this, 1.0F);
00073 }
00074 }
00075
00076
00077 SC8000::~SC8000()
00078 { }
00079
00080
00081 bool SC8000::move(const int servo, const float position)
00082 { return moveRaw(servo, calibToRaw(servo, position)); }
00083
00084
00085 float SC8000::getPosition(const int servo) const
00086 {
00087 if (servo < 0 || servo >= SSCNUMSERVOS)
00088 LFATAL("Invalid servo number %d -- ABORT", servo);
00089 return rawToCalib(servo, pos[servo]);
00090 }
00091
00092
00093 void SC8000::calibrate(const int servo, const short neutralval, const short minval,
00094 const short maxval)
00095 {
00096 if (servo < 0 || servo >= SSCNUMSERVOS)
00097 LFATAL("Invalid servo number %d -- ABORT", servo);
00098
00099 zero[servo]->setVal(float(neutralval));
00100 negmult[servo]->setVal(1.0F / (float(neutralval) - float(minval)));
00101 posmult[servo]->setVal(1.0F / (float(maxval) - float(neutralval)));
00102 }
00103
00104
00105 bool SC8000::moveRaw(const int servo, const short rawpos)
00106 {
00107
00108
00109
00110
00111
00112
00113
00114
00115 char command[6];
00116 command[0] = 126;
00117 command[1] = 126;
00118 command[2] = 1 << (servo-1);
00119 command[3] = 48;
00120
00121
00122 command[4] = (unsigned char)((rawpos >> 8) & 0xFF);
00123 command[5] = (unsigned char)(rawpos);
00124
00125
00126
00127 if (itsPort->write(command, 6) == 6)
00128 {
00129
00130 pos[servo] = rawpos;
00131 return true;
00132 }
00133 else
00134 return false;
00135 }
00136
00137
00138 short SC8000::getPositionRaw(const int servo) const
00139 {
00140 if (servo < 0 || servo >= SSCNUMSERVOS)
00141 LFATAL("Invalid servo number %d -- ABORT", servo);
00142
00143 return pos[servo];
00144 }
00145
00146
00147 float SC8000::rawToCalib(const int servo, const short rawpos) const
00148 {
00149 if (servo < 0 || servo >= SSCNUMSERVOS)
00150 LFATAL("Invalid servo number %d -- ABORT", servo);
00151
00152 float position;
00153 if (rawpos >= short(zero[servo]->getVal()))
00154 position = (float(rawpos) - zero[servo]->getVal()) *
00155 posmult[servo]->getVal();
00156 else
00157 position = (float(rawpos) - zero[servo]->getVal()) *
00158 negmult[servo]->getVal();
00159
00160 if (position < -1.0F) position = -1.0F;
00161 else if (position > 1.0F) position = 1.0F;
00162
00163 return position;
00164 }
00165
00166
00167 short SC8000::calibToRaw(const int servo, const float position) const
00168 {
00169 if (servo < 0 || servo >= SSCNUMSERVOS)
00170 LFATAL("Invalid servo number %d -- ABORT", servo);
00171
00172 if (position > 1.0F || position < -1.0F)
00173 LFATAL("Invalid position %f (range -1.0..1.0) -- ABORT", position);
00174
00175 int rawpos;
00176 if (position < 0.0F)
00177 rawpos = int(position / negmult[servo]->getVal() +
00178 zero[servo]->getVal() + 0.49999F);
00179 else
00180 rawpos = int(position / posmult[servo]->getVal() +
00181 zero[servo]->getVal() + 0.49999F);
00182
00183 if (rawpos < 0) rawpos = 0; else if (rawpos > 65535) rawpos = 65535;
00184
00185 return short(rawpos);
00186 }
00187
00188
00189
00190
00191
00192