UltraScan III
us_hardware.cpp
Go to the documentation of this file.
1 #include "us_hardware.h"
3 #include "us_settings.h"
4 
5 // Read rotor information and generate a rotorCalID,coeffs map from Local
6 bool US_Hardware::readRotorMap( QMap< QString, QString >& rotor_map )
7 {
8  bool ok = false;
9  QString path = US_Settings::appBaseDir() + "/etc/rotors";
10 
11  rotor_map.clear();
12  rotor_map[ "0" ] = "0 0"; // Create a simulation entry (no stretch)
13 
14  QDir dir( path );
15 
16  if ( ! dir.exists() )
17  return ok;
18 
19  QStringList filter( "C*.xml" );
20  QStringList fnames = dir.entryList( filter, QDir::Files, QDir::Name );
21 
22  for ( int ii = 0; ii < fnames.size(); ii++ )
23  { // Browse the calibration files
24  QFile file( path + "/" + fnames[ ii ] );
25 
26  if ( ! file.open( QIODevice::ReadOnly | QIODevice::Text ) )
27  {
28  qDebug() << "*ERROR: Could not read rotor file\n" << fnames[ ii ];
29  continue;
30  }
31 
32  QXmlStreamReader xml( &file );
33 
34  while ( ! xml.atEnd() )
35  { // Get ID and coefficients from a file
36  xml.readNext();
37 
38  if ( xml.isStartElement() && xml.name() == "Calibration" )
39  {
40  QXmlStreamAttributes att = xml.attributes();
41 
42  QString calID = att.value( "id" ).toString();
43  QString coeffs = att.value( "coeff1" ).toString() + " "
44  + att.value( "coeff2" ).toString();
45 
46  rotor_map[ calID ] = coeffs;
47  ok = true;
48  }
49  }
50 
51  }
52 
53  return ok;
54 }
55 
56 // Read rotor information and generate a rotorCalID,coeffs map from DB
58  QMap< QString, QString >& rotor_map )
59 {
60  bool ok = false;
61 
62  if ( db == 0 || ! db->isConnected() )
63  return readRotorMap( rotor_map );
64 
65  rotor_map.clear();
66  rotor_map[ "0" ] = "0 0"; // Create a simulation entry (no stretch)
67 
68  QStringList query;
69  QStringList labIDs;
70  QStringList rotorIDs;
71  QStringList calibIDs;
72 
73  query << "get_lab_names";
74  db->query( query );
75 
76  if ( db->lastErrno() != US_DB2::OK )
77  {
78  qDebug() << "*ERROR* Unable to get lab IDs";
79  return ok;
80  }
81 
82  while ( db->next() ) // Get a list of lab IDs
83  {
84  labIDs << db->value( 0 ).toString(); // Add a lab ID
85  }
86 
87  for ( int ii = 0; ii < labIDs.size(); ii++ )
88  { // Add to the list of rotor IDs, from each lab
89  QString labID = labIDs[ ii ];
90 
91  query.clear();
92  query << "get_rotor_names" << labID;
93  db->query( query );
94 
95  if ( db->lastErrno() != US_DB2::OK )
96  {
97  qDebug() << "*WARNING* Unable to get rotors for lab ID" << labID;
98  continue;
99  }
100 
101  while ( db->next() )
102  {
103  rotorIDs << db->value( 0 ).toString(); // Add rotor ID
104  }
105  }
106 
107  for ( int ii = 0; ii < rotorIDs.size(); ii++ )
108  { // Add to the list of calibration IDs, from each rotor
109  QString rotorID = rotorIDs[ ii ];
110 
111  query.clear();
112  query << "get_rotor_calibration_profiles" << rotorID;
113  db->query( query );
114 
115  if ( db->lastErrno() != US_DB2::OK )
116  {
117  qDebug() << "*WARNING* Unable to get calIDs for rotor ID" << rotorID;
118  continue;
119  }
120 
121  while ( db->next() )
122  {
123  calibIDs << db->value( 0 ).toString(); // Add a calibration ID
124  }
125  }
126 
127  for ( int ii = 0; ii < calibIDs.size(); ii++ )
128  { // Build coefficient pairs mapped to each calibration ID
129  QString calibID = calibIDs[ ii ];
130 
131  query.clear();
132  query << "get_rotor_calibration_info" << calibID;
133  db->query( query );
134 
135  if ( db->lastErrno() != US_DB2::OK )
136  {
137  qDebug() << "*WARNING* Unable to get info for calib ID" << calibID;
138  continue;
139  }
140 
141  while ( db->next() )
142  { // Map this coefficient pair to a calibration ID
143  QString coeffs = db->value( 4 ).toString() + " " +
144  db->value( 5 ).toString();
145 
146  rotor_map[ calibID ] = coeffs;
147  ok = true; // Mark that at least one pair found
148  }
149  }
150 
151  return ok;
152 }
153 
154 // Get rotor coefficients for a given rotor calibration identifier
155 bool US_Hardware::rotorValues( QString rCalID,
156  QMap< QString, QString >rotor_map, double* rotcoeffs )
157 {
158  bool ok = rotor_map.contains( rCalID ); // Flag: calibration ID found?
159 
160  if ( ok )
161  { // Return the coefficient pair for the given calibration ID
162  QString coeffs = rotor_map[ rCalID ];
163  rotcoeffs[ 0 ] = coeffs.section( " ", 0, 0 ).toDouble();
164  rotcoeffs[ 1 ] = coeffs.section( " ", 1, 1 ).toDouble();
165  }
166 
167  return ok;
168 }
169 
171 {
172  serial_number = 0;
173  guid = "";
174  name = "";
175  material = "";
176  channels = 1;
177  shape = "standard";
178  angle = 2.5;
179  width = 0.0;
180  path_length.clear();
181  bottom_position.clear();
182 }
183 
184 // Read centerpiece information from database or local
186  QList< US_AbstractCenterpiece >& centerpieces )
187 {
188  centerpieces.clear();
190  bool got_cp = false;
191 
192  while ( db != NULL && db->isConnected() )
193  { // Get centerpiece information from the database
194  QList< int > cpids;
195  QStringList query;
196 
197  query << "get_abstractCenterpiece_names";
198  db->query( query );
199 
200  if ( db->lastErrno() != US_DB2::OK )
201  {
202  qDebug() << "*ERROR* Unable to get centerpiece information from DB";
203  break;
204  }
205 
206  while ( db->next() )
207  {
208  cpids << db->value( 0 ).toString().toInt();
209  }
210 
211  qSort( cpids );
212 
213  for ( int ii = 0; ii < cpids.size(); ii++ )
214  {
215  cp.serial_number = cpids[ii ];
216  QString cpid = QString::number( cp.serial_number );
217  query.clear();
218  query << "get_abstractCenterpiece_info" << cpid;
219  db->query( query );
220  db->next();
221 
222  cp.guid = db->value( 0 ).toString();
223  cp.name = db->value( 1 ).toString();
224  cp.channels = db->value( 2 ).toString().toInt();
225  cp.channels = qMax( cp.channels, 1 );
226  QString bottoms = db->value( 3 ).toString();
227  cp.shape = db->value( 4 ).toString();
228  cp.shape = cp.shape.isEmpty() ? "sector" : cp.shape;
229  cp.maxRPM = db->value( 5 ).toString().toDouble();
230  double pathlen = db->value( 6 ).toString().toDouble();
231  cp.angle = db->value( 7 ).toString().toDouble();
232  cp.width = db->value( 8 ).toString().toDouble();
233  cp.material = cp.name.section( " ", 0, 0 );
234 
235  cp.path_length .clear();
236  cp.bottom_position.clear();
237  for ( int jj = 0; jj < cp.channels; jj++ )
238  {
239  cp.bottom_position << bottoms.section( ":", jj, jj ).toDouble();
240  cp.path_length << pathlen;
241  }
242 
243  centerpieces << cp; // Add centerpiece entry
244  }
245 
246  got_cp = true;
247  break;
248  }
249 
250  if ( ! got_cp )
251  { // Get centerpiece information from local (if db==NULL or db failed)
252  QFile cp_file( US_Settings::appBaseDir() + "/etc/abstractCenterpieces.xml" );
253 
254  if ( ! cp_file.open( QIODevice::ReadOnly | QIODevice::Text ) )
255  return false;
256 
257  QXmlStreamReader xml( &cp_file );
258  double xversion = 1.0;
259 
260  while ( ! xml.atEnd() )
261  {
262  xml.readNext();
263 
264  if ( xml.isStartElement() )
265  {
266  if ( xml.name() == "abstractCenterpieces" )
267  {
268  QXmlStreamAttributes a = xml.attributes();
269  xversion = a.value( "version" ).toString().toDouble();
270  }
271 
272  if ( xml.name() == "abstractCenterpiece" )
273  {
274  if ( cp.serial_number > 0 ) centerpieces << cp;
275  cp = US_AbstractCenterpiece();
276 
277  QXmlStreamAttributes a = xml.attributes();
278  cp.serial_number = a.value( "id" ).toString().toInt();
279  cp.guid = a.value( "guid" ).toString();
280  cp.name = a.value( "name" ).toString();
281  cp.material = a.value( "materialName" ).toString();
282  cp.channels = a.value( "channels" ).toString().toInt();
283  cp.shape = a.value( "shape" ).toString();
284  cp.angle = a.value( "angle" ).toString().toDouble();
285  cp.width = a.value( "width" ).toString().toDouble();
286 
287  if ( xversion == 1.0 )
288  {
289  if ( ( cp.channels % 2 ) == 0 )
290  cp.channels--;
291  if ( cp.serial_number == 2 || cp.serial_number == 7 )
292  cp.maxRPM = 42000;
293  else if ( cp.serial_number == 4 )
294  cp.maxRPM = 48000;
295  else
296  cp.maxRPM = 60000;
297  }
298  else
299  {
300  cp.maxRPM = a.value( "maxRPM" ).toString().toDouble();
301  }
302  }
303 
304  if ( xml.name() == "row" )
305  {
306  QXmlStreamAttributes a = xml.attributes();
307  cp.path_length << a.value( "pathlen" ).toString().toDouble();
308  cp.bottom_position << a.value( "bottom" ).toString().toDouble();
309  }
310  }
311  }
312 
313  centerpieces << cp; // Add last centerpiece
314 
315  cp_file.close();
316  got_cp = true;
317  }
318 
319  return got_cp;
320 }
321 
322 // Read centerpiece information from local
324  QList< US_AbstractCenterpiece >& centerpieces )
325 {
326  return read_centerpieces( NULL, centerpieces );
327 }
328 
329