47 #define IMPORT_CODE_FROM_EMBOT_HW_PGA308
59 #if defined(IMPORT_CODE_FROM_EMBOT_HW_PGA308)
64 static const std::uint16_t Default = 0x8000;
78 void set(std::uint16_t zdac) { value = zdac; }
86 static const std::uint16_t Default = 0x4000;
101 void set(std::uint16_t gdac) { value = gdac; }
110 static const std::uint8_t DefaultGO = 0x06;
111 static const std::uint8_t DefaultGO_g_32_96 = 0x06;
112 static const std::uint8_t DefaultGO_g_16_80 = 0x02;
113 static const std::uint8_t DefaultGO_g_11_75 = 0x00;
114 static const std::uint8_t DefaultMUX = 0x00;
115 static const std::uint8_t DefaultGI = 0x04;
116 static const std::uint8_t DefaultOS = 0x20;
124 CFG0register(std::uint8_t GO, std::uint8_t MUX, std::uint8_t GI, std::uint8_t OS)
137 void setDefault() { value = 0; setGO(CFG0register::DefaultGO); setMUX(CFG0register::DefaultMUX); setGI(CFG0register::DefaultGI); setOS(CFG0register::DefaultOS); }
140 void setGO(std::uint8_t GO) { value |= (
static_cast<std::uint8_t
>(GO&0x07) << 13); }
141 std::uint8_t
getGO()
const {
return ((value >> 13) & 0x07); }
144 void setMUX(std::uint8_t MUX) { value |= (
static_cast<std::uint8_t
>(MUX&0x01) << 12); }
145 std::uint8_t
getMUX()
const {
return ((value >> 12) & 0x01); }
148 void setGI(std::uint8_t GI) { value |= (
static_cast<std::uint8_t
>(GI&0x0f) << 8); }
149 std::uint8_t
getGI()
const {
return ((value >> 8) & 0x0f); }
153 void setOS(std::uint8_t OS) { value |= (
static_cast<std::uint8_t
>(OS&0xff)); }
154 std::uint8_t
getOS()
const {
return ((value) & 0xff); }
162 static const std::uint16_t Default = 0x0000;
174 void set(std::uint16_t cfg1) { value = cfg1; }
182 static const std::uint16_t Default = 0x0400;
198 void set(std::uint16_t cfg2) { value = cfg2; }
207 static const std::uint16_t Default = 0x0050;
223 void set(std::uint16_t sftc) { value = sftc; }
234 static const std::uint16_t VREF = 8192;
243 enum class Parameter { GD = 0, GI = 1, muxsign = 2, GO = 3, Vcoarseoffset = 4, Vzerodac = 5 };
291 GD = GDACregister::Default;
292 GI = CFG0register::DefaultGI;
293 muxsign = CFG0register::DefaultMUX;
294 GO = CFG0register::DefaultGO;
295 Vcoarseoffset = CFG0register::DefaultOS;
296 Vzerodac = ZDACregister::Default;
313 Vcoarseoffset = cfg0.
getOS();
314 Vzerodac = zdac.
value;
323 Vcoarseoffset = cfg0.
getOS();
328 Vzerodac = zdac.
value;
338 static const std::uint16_t mapGI2val[16] = {4, 6, 8, 12, 16, 32, 64, 100, 200, 400, 480, 600, 800, 960, 1200, 1600};
339 return static_cast<float>(mapGI2val[GI]);
344 static const float mapGO2val[8] = {2.0f, 2.4f, 3.0f, 3.6f, 4.0f, 4.5f, 6.0f, 1.0f};
345 return mapGO2val[GO];
352 return valueOfGD(GD);
358 return (1.0f +
static_cast<float>(
x)/32768.0f)/3.0f;
371 std::uint8_t v = co & 0x7f;
372 if(v > 100) { v = 100; }
373 std::uint8_t negative = co >> 7;
374 return (1 == negative) ? -v : +v;
382 v = std::floor(v + 0.5f);
383 if(v > +100.0f) { v = +100.0f; }
384 r =
static_cast<std::uint8_t
>(v);
389 v = std::floor(v + 0.5f);
390 if(v > +100.0f) { v = +100.0f; }
391 r =
static_cast<std::uint8_t
>(v) | 0x80;
399 return valueOfCOR()*regvco2value(Vcoarseoffset);
404 return static_cast<float>(VREF)*(1.0f/65536.0f)*(32768.0f -
static_cast<float>(Vzerodac));
417 float v = valueOfGD(
x)*valueOfGO()*valueOfGI();
418 return (0 == muxsign) ? v : -v;
423 float gi = valueOfGI();
424 return alpha()*(valueOfCoarseOffset() + valueOfFineOffset()/gi);
432 a = alpha() * valueOfCOR();
433 b = - (alpha()*
static_cast<float>(VREF)/valueOfGI())/65536.0f;
434 c =
static_cast<float>(vout) - a*regvco2value(Vcoarseoffset) - b*
static_cast<float>(Vzerodac);
437 bool alignVOUT(
const std::uint16_t vout,
const std::uint16_t target, std::uint8_t &Y, std::uint16_t &Z)
440 computeOffsetParams(vout, a, b, c);
441 float y = (target - c - b*
static_cast<float>(Vzerodac))/a;
444 std::uint8_t
tmp = value2regvco(
y);
445 y = regvco2value(
tmp);
448 float z = (target - c - a*
y)/b;
449 if((
z > 65535.0f) || (
z < 0.0f))
454 Z =
static_cast<std::uint16_t
>(std::floor(
z + 0.5f));
463 float xgd = 98304.0f * (a/(valueOfGI()*valueOfGO()) - (1.0f/3.0f));
464 std::int32_t
x =
static_cast<std::int32_t
>(std::floor(xgd + 0.5f));
465 if((
x >= 65536) || (
x < 0))
469 GD =
static_cast<std::uint16_t
>(
x);
475 float yco = b / (alpha() * valueOfCOR());
476 std::uint8_t
y = value2regvco(yco);
478 float zco = ( regvco2value(
y) * valueOfCOR() + 0.5f*
static_cast<float>(VREF)/valueOfGI() - b/alpha() ) / ( (
static_cast<float>(VREF)/65536.0f)/valueOfGI() );
479 std::int32_t
z =
static_cast<std::int32_t
>(std::floor(zco + 0.5f));
480 if((
z >= 65536) || (
z < 0))
485 Vzerodac =
static_cast<std::uint16_t
>(
z);
512 static const uint8_t _cfgmap[
static_cast<uint8_t
>(DiscreteGain::maxnumberof)][6] =
514 {0x00, 0x80, 0x66, 0x06, 0xcb, 0x80},
515 {0x00, 0x80, 0x56, 0x0c, 0xcb, 0x80},
516 {0x00, 0x40, 0x56, 0x10, 0x0f, 0x81},
517 {0x00, 0x80, 0x46, 0x17, 0x6d, 0x7f},
518 {0x00, 0x40, 0x46, 0x1f, 0xb1, 0x7f},
519 {0x00, 0x10, 0x46, 0x2a, 0x80, 0x80},
520 {0x00, 0x40, 0x42, 0x3e, 0x62, 0x7f},
521 {0x00, 0x20, 0x42, 0x4b, 0x15, 0x80},
522 {0x00, 0x00, 0x42, 0x5e, 0x72, 0x80},
523 {0x00, 0xC0, 0x02, 0x64, 0xf6, 0x6e},
524 {0x00, 0x80, 0x02, 0x64, 0x29, 0x62},
525 {0x00, 0x40, 0x02, 0x64, 0xd4, 0x4c},
526 {0x00, 0x40, 0x00, 0x64, 0x29, 0x22}
532 case DiscreteGain::none:
533 case DiscreteGain::maxnumberof:
540 uint8_t index =
static_cast<uint8_t
>(g);
542 cfg.
load(_cfgmap[index], 6);
555 static const uint8_t _cfgmap[
static_cast<uint8_t
>(DiscreteGain::maxnumberof)][6] =
557 {0x00, 0x80, 0x66, 0x20, 0x00, 0x80},
558 {0x00, 0x80, 0x56, 0x20, 0x00, 0x80},
559 {0x00, 0x40, 0x56, 0x20, 0x00, 0x80},
560 {0x00, 0x80, 0x46, 0x20, 0x00, 0x80},
561 {0x00, 0x40, 0x46, 0x20, 0x00, 0x80},
562 {0x00, 0x10, 0x46, 0x20, 0x00, 0x80},
563 {0x00, 0x40, 0x42, 0x20, 0x00, 0x80},
564 {0x00, 0x20, 0x42, 0x20, 0x00, 0x80},
565 {0x00, 0x00, 0x42, 0x20, 0x00, 0x80},
566 {0x00, 0xC0, 0x02, 0x20, 0x00, 0x80},
567 {0x00, 0x80, 0x02, 0x20, 0x00, 0x80},
568 {0x00, 0x40, 0x02, 0x20, 0x00, 0x80},
569 {0x00, 0x40, 0x00, 0x20, 0x00, 0x80}
575 case DiscreteGain::none:
576 case DiscreteGain::maxnumberof:
583 uint8_t index =
static_cast<uint8_t
>(g);
585 cfg.
load(_cfgmap[index], 6);
599 if(((g == DiscreteGain::val06) && (
offset > (62240))) || ((g == DiscreteGain::val04) && (
offset > (40800))))
606 if(
false == load_step1(g))
630 if((
z > 65535.0f) || (
z < 0.0f))
635 uint16_t Z =
static_cast<std::uint16_t
>(std::floor(
z + 0.5f));
645 int32_t
tmp =
static_cast<int32_t
>(round(8.0f*tsf.
beta()));
646 if((
tmp >= 0) && (
tmp < 65536))
668 static const float mapofgains[
static_cast<uint8_t
>(DiscreteGain::maxnumberof)] =
670 256,128,96,64,48, 36, 24, 20, 16, 10, 8, 6, 4
675 uint8_t index =
static_cast<uint8_t
>(dg);
676 if(index >=
static_cast<uint8_t
>(DiscreteGain::maxnumberof))
686 dg = DiscreteGain::none;
689 for(
int i=0; i<static_cast<uint8_t>(DiscreteGain::maxnumberof); i++)
716 const std::uint8_t PGA308::Registers::defval[PGA308::Registers::sizeofregisters] = {0x00, 0x40, 0x46, 0x1f, 0xb1, 0x7f};
718 bool PGA308::Registers::load(
const void *
data,
const size_t size)
720 if((
nullptr ==
data) || (size != sizeofregisters))
722 GD = GI = S = GO = Voffsetcoarse = Vzerodac = 0;
725 const uint16_t *u16 =
reinterpret_cast<const uint16_t*
>(
data);
726 const uint8_t *u08 =
reinterpret_cast<const uint8_t*
>(
data);
727 GD = u16[0]; Vzerodac = u16[2];
728 GI = (u08[2] >> 4) & 0x0f;
729 S = (u08[2] >> 3) & 0x01;
730 GO = (u08[2]) & 0x07;
731 Voffsetcoarse = u08[3];
735 bool PGA308::Registers::fill(
void *
data,
size_t &size)
741 uint16_t *u16 =
reinterpret_cast<uint16_t*
>(
data);
742 uint8_t *u08 =
reinterpret_cast<uint8_t*
>(
data);
743 u16[0] = GD; u16[2] = Vzerodac;
745 u08[2] = ((
static_cast<uint8_t
>(GI) & 0x0f) << 4) | ((
static_cast<uint8_t
>(S) & 0x01) << 3) | ((
static_cast<uint8_t
>(GO) & 0x07));
746 u08[3] = Voffsetcoarse;
747 size = sizeofregisters;
812 if(
nullptr != wideparams)
814 get(wideparams->
g, wideparams->
o);
845 return pImpl->
load(g);
853 return load(discreteparams.
dg, discreteparams.
o);
864 return get(wideparams.
g, wideparams.
o);
889 namespace strain {
namespace amplifier {
909 const bool step1 =
true;
911 const bool step2 =
true;
926 wp.
load(4.0, 32*1024);
941 wp.
load(48.0, 32531);
974 pga.
import(regs, &wparams);
977 if(
true ==
convert(wparams, dparams))
997 wp.
load(48.0, 32*1024);
1023 pga.
import(regs, &wparams);
1026 if(
true ==
convert(wparams, dparams))
1044 thePGAs[0].
load(discreteparams);
1047 thePGAs[0].
get(regs);
1049 uint8_t payload[8] = {0};
1054 uint8_t rxpayload[8] = {0};
1055 regs.
load(rxpayload);
1058 rxregs.
load(rxpayload);
1061 thePGAs[0].
load(rxregs);
1062 thePGAs[0].
get(gg, oo);
1073 namespace strain {
namespace dsp {
namespace fsc {
1078 FSC ret =
static_cast<FSC>(v);
1097 return static_cast<double>(v);
1101 bool convert(
const std::vector<double> &in, std::vector<FSC> &
out)
1106 for(
int i=0; i<in.size(); i++)
1117 bool convert(
const std::vector<FSC> &in, std::vector<double> &
out)
1121 for(
int i=0; i<in.size(); i++)
1133 namespace strain {
namespace dsp {
namespace q15 {
1143 return valU16-0x8000;
1146 bool convert(
const std::vector<double> &in, std::vector<Q15> &
out)
1151 for(
int i=0; i<in.size(); i++)
1162 bool convert(
const std::vector<Q15> &in, std::vector<double> &
out)
1166 for(
int i=0; i<in.size(); i++)
1175 return valQ15+0x8000;
1180 return static_cast<double>(v) / 32768.0;
1199 else if (
x < -0x8000)
1206 return static_cast<Q15>(
x);
1211 bool saturated =
false;
1212 return add(a, b, saturated);
1232 tmp += (1 << (15 - 1));
1241 bool saturated =
false;
1242 return mul(a, b, saturated);
1250 std::uint8_t neg = 0;
1270 rr = (n1 << 15) / n2;
1294 for(
int r=0; r<res.
nrows; r++)
1296 for(
int c=0; c<res.
ncols; c++)
1329 std::uint32_t nsat = 0;
1332 for(
int r=0; r<res.
nrows; r++)
1334 for(
int c=0; c<res.
ncols; c++)
1339 const Q15 *col_wise = &m2.
data[c];
1340 for(
int i=0; i<m1.
ncols; i++)
1344 Q15 x =
mul(*row_wise, *col_wise, sat1);
1345 v =
add(v,
x, sat2);
1352 col_wise += m2.
ncols;
1387 const char * filename = fname.c_str();
1389 std::fstream filestr;
1390 filestr.open (filename, fstream::in);
1391 if (!filestr.is_open()){
1401 filestr.getline (buffer,256);
1402 filestr.getline (buffer,256);
1403 sscanf (buffer,
"%d",&file_version);
1406 if((4 != file_version) && (3 != file_version))
1418 filestr.getline (buffer,256);
1419 filestr.getline (buffer,256);
1420 if(0 != strcmp(buffer,
"strain2"))
1430 char serial_no[256] = {0};
1431 filestr.getline (buffer,256);
1432 filestr.getline (buffer,256);
1433 snprintf(serial_no,
sizeof(serial_no),
"%s", buffer);
1436 reg.
serial = std::string(serial_no);
1444 filestr.getline (buffer,256);
1447 filestr.getline (buffer,256);
1449 unsigned int t08[6] = {0};
1450 sscanf (buffer,
"0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x", &t08[0], &t08[1], &t08[2], &t08[3], &t08[4], &t08[5]);
1451 for(
int j=0; j<6; j++)
1468 filestr.getline (buffer,256);
1469 for (i=0;i<36; i++){
1470 unsigned int tmp = 0;
1471 filestr.getline (buffer,256);
1472 sscanf (buffer,
"%x", &
tmp);
1480 filestr.getline (buffer,256);
1481 filestr.getline (buffer,256);
1483 sscanf (buffer,
"%d",&cc);
1487 filestr.getline (buffer,256);
1489 filestr.getline (buffer,256);
1491 sscanf (buffer,
"%d", &tt);
1492 set.
digital.
tare[i] =
static_cast<std::uint16_t
>(tt);
1497 filestr.getline (buffer,256);
1499 filestr.getline (buffer,256);
1501 sscanf (buffer,
"%d", &fs);
1506 reg.
sets.push_back(set);
virtual bool load(const void *data, const size_t size)
std::uint8_t Voffsetcoarse
virtual bool fill(void *data, size_t &size)
bool import(const Registers ®s, WideParams *wideparams=nullptr)
bool load(const Registers ®s)
bool get(WideParams &wideparams)
double sat(const double val, const double min, const double max)
Gain convert(const DiscreteGain dg)
static const float mapofgains[static_cast< uint8_t >(DiscreteGain::maxnumberof)]
void testIT(std::string &output)
const Offset midrangeOffset
FSC convert(const double v, bool &saturated)
std::uint16_t Q15toU16(const Q15 valQ15)
Q15 saturate(const Q15result x, bool &saturated)
Q15 convert(const double v, bool &saturated)
bool multiply(const matrix &m1, const matrix &m2, matrix &res, bool &saturated)
Q15 div(const Q15 a, const Q15 b, bool &saturated)
Q15 mul(const Q15 a, const Q15 b, bool &saturated)
Q15 U16toQ15(const std::uint16_t valU16)
Q15 opposite(const Q15 v)
double convert(const Q15 v)
Q15 add(const Q15 a, const Q15 b)
bool read(const std::string fname, FullRegulation ®)
void load(DiscreteGain _dg, Offset _o)
Example_strain2_ampl_regs_t()
void setMUX(std::uint8_t MUX)
std::uint8_t getOS() const
std::uint8_t getGO() const
std::uint8_t getGI() const
std::uint8_t getMUX() const
CFG0register(std::uint16_t v)
void setGI(std::uint8_t GI)
void setOS(std::uint8_t OS)
CFG0register(std::uint8_t GO, std::uint8_t MUX, std::uint8_t GI, std::uint8_t OS)
void setGO(std::uint8_t GO)
void set(std::uint16_t cfg1)
CFG1register(std::uint16_t v)
CFG2register(std::uint16_t v)
void set(std::uint16_t cfg2)
GDACregister(std::uint16_t v)
void set(std::uint16_t gdac)
void set(std::uint16_t sftc)
SFTCregister(std::uint16_t v)
void load(const Registers ®s)
bool alignVOUT(const std::uint16_t vout, const std::uint16_t target, std::uint8_t &Y, std::uint16_t &Z)
std::uint8_t Vcoarseoffset
float valueOfGD(std::uint16_t x)
void assign(const CFG0register &cfg0, const ZDACregister &zdac, const GDACregister &gdac)
float regvco2value(std::uint8_t co)
void load(const ZDACregister &zdac)
void obtain(CFG0register &cfg0, ZDACregister &zdac, GDACregister &gdac) const
float valueOfFineOffset()
void load(const CFG0register &cfg0)
static const std::uint16_t VREF
void load(const GDACregister &gdac)
std::uint8_t value2regvco(float v)
float valueOfCoarseOffset()
float alpha(std::uint16_t x)
void computeOffsetParams(const std::uint16_t vout, float &a, float &b, float &c)
void get(Registers ®s)
ZDACregister(std::uint16_t v)
void set(std::uint16_t zdac)
TransferFunctionConfig tsf
bool load(const DiscreteGain g, const Offset offset)
bool load_step1(const DiscreteGain g)
bool get(Gain &gain, Offset &offset)
bool load(const DiscreteGain g)
void load(Gain _g, Offset _o)
std::uint8_t amplregs[strain::numberofchannels][strain::amplifier::PGA308::Registers::sizeofregisters]
strain::dsp::Q15 matrix[strain::numberofchannels *strain::numberofchannels]
strain::dsp::FSC fullscale[strain::numberofchannels]
std::uint16_t tare[strain::numberofchannels]
std::uint8_t set2useatbootstrap