KiCad PCB EDA Suite
E_SERIE Class Reference

#include <eserie.h>

Public Member Functions

 E_SERIE ()
 
void Exclude (double aValue)
 If any value of the selected E-serie not available, it can be entered as an exclude value. More...
 
void NewCalc ()
 initialize next calculation and erase results from previous calculation More...
 
void Calculate ()
 called on calculate button to execute all the 2R, 3R and 4R calculations More...
 
void SetSeries (uint32_t aSeries)
 Interface for CheckBox, RadioButton, RequriedResistor and calculated Results. More...
 
void SetRequiredValue (double aValue)
 
const std::array< R_DATA, S4R+1 > & GetResults ()
 

Private Member Functions

int buildSerieData (int aEserie, double aList[])
 Build the list of R_DATA existing for a given serie Series are E1, E6 . More...
 
uint32_t combine2 ()
 Build all 2R combinations from the selected E-serie values. More...
 
void simple_solution (uint32_t aSize)
 Search for closest two component solution. More...
 
void combine3 (uint32_t aSize)
 Check if there is a better 3 R solution than previous one using only two components. More...
 
void combine4 (uint32_t aSize)
 Check if there is a better four component solution. More...
 
void strip3 ()
 
void strip4 ()
 

Private Attributes

std::vector< std::vector< R_DATA > > m_luts
 
std::vector< R_DATAm_cmb_lut
 
std::array< R_DATA, S4R+1 > m_results
 
uint32_t m_series = E6
 
uint32_t m_enable_4R = false
 
double m_required_value = 0.0
 

Detailed Description

Definition at line 88 of file eserie.h.

Constructor & Destructor Documentation

◆ E_SERIE()

E_SERIE::E_SERIE ( )

Definition at line 86 of file eserie.cpp.

87 {
88  // Build the list of available resistor values in each En serie
89  double listValuesE1[] = { E1_VALUES };
90  double listValuesE3[] = { E3_VALUES };
91  double listValuesE6[] = { E6_VALUES };
92  double listValuesE12[] = { E12_VALUES };
93  double listValuesE24[] = { E24_VALUES };
94  // buildSerieData must be called in the order of En series, because
95  // the list of series is expected indexed by En for the serie En
96  buildSerieData( E1, listValuesE1 );
97  buildSerieData( E3, listValuesE3 );
98  buildSerieData( E6, listValuesE6 );
99  buildSerieData( E12, listValuesE12 );
100  int count = buildSerieData( E24, listValuesE24 );
101 
102  // Reserve a buffer for intermediate calculations:
103  // the buffer size is 2*count*count to store all combinaisons of 2 values
104  // there are 2*count*count = 29282 combinations for E24
105  int bufsize = 2*count*count;
106  m_cmb_lut.reserve( bufsize );
107 
108  // Store predefined R_DATA items.
109  for( int ii = 0; ii < bufsize; ii++ )
110  m_cmb_lut.emplace_back( "", 0.0 );
111 }
std::vector< R_DATA > m_cmb_lut
Definition: eserie.h:195
#define E6_VALUES
Definition: eserie.h:42
#define E24_VALUES
E-Values derived from a geometric sequence formula by Charles Renard were already accepted and widely...
Definition: eserie.h:37
Definition: eserie.h:61
Definition: eserie.h:61
Definition: eserie.h:61
#define E3_VALUES
Definition: eserie.h:44
Definition: eserie.h:61
#define E1_VALUES
Definition: eserie.h:46
Definition: eserie.h:61
int buildSerieData(int aEserie, double aList[])
Build the list of R_DATA existing for a given serie Series are E1, E6 .
Definition: eserie.cpp:114
#define E12_VALUES
Definition: eserie.h:40

References buildSerieData(), E1, E12, E12_VALUES, E1_VALUES, E24, E24_VALUES, E3, E3_VALUES, E6, E6_VALUES, and m_cmb_lut.

Member Function Documentation

◆ buildSerieData()

int E_SERIE::buildSerieData ( int  aEserie,
double  aList[] 
)
private

Build the list of R_DATA existing for a given serie Series are E1, E6 .

The values are extracted from the E96_VALUES list

Returns
the count of items added in list

Definition at line 114 of file eserie.cpp.

115 {
116  double curr_coeff = FIRST_VALUE;
117  int count = 0;
118 
119  std::vector<R_DATA> curr_list;
120 
121  for( ; ; )
122  {
123  double curr_r = curr_coeff;
124 
125  for( int ii = 0; ; ii++ )
126  {
127  if( aList[ii] == 0.0 ) // End of list
128  break;
129 
130  double curr_r = curr_coeff * aList[ii];
131  curr_list.emplace_back( strValue( curr_r ), curr_r );
132  count++;
133 
134  if( curr_r >= LAST_VALUE )
135  break;
136  }
137 
138  if( curr_r >= LAST_VALUE )
139  break;
140 
141  curr_coeff *= 10;
142  }
143 
144  m_luts.push_back( std::move( curr_list ) );
145 
146  return count;
147 }
static std::string strValue(double aValue)
Definition: eserie.cpp:49
std::vector< std::vector< R_DATA > > m_luts
Definition: eserie.h:183
#define LAST_VALUE
Definition: eserie.h:53
#define FIRST_VALUE

References FIRST_VALUE, LAST_VALUE, m_luts, and strValue().

Referenced by E_SERIE().

◆ Calculate()

void E_SERIE::Calculate ( )

called on calculate button to execute all the 2R, 3R and 4R calculations

Definition at line 338 of file eserie.cpp.

339 {
340  uint32_t no_of_2Rcombi = 0;
341 
342  no_of_2Rcombi = combine2(); // combine all 2R combinations for selected E serie
343  simple_solution( no_of_2Rcombi ); // search for simple 2 component solution
344 
345  if( m_results.at( S2R ).e_value ) // if simple 2R result is not exact
346  combine3( no_of_2Rcombi ); // continiue searching for a possibly better solution
347 
348  strip3();
349  strip4();
350 }
void combine3(uint32_t aSize)
Check if there is a better 3 R solution than previous one using only two components.
Definition: eserie.cpp:284
uint32_t combine2()
Build all 2R combinations from the selected E-serie values.
Definition: eserie.cpp:251
Definition: eserie.h:66
void strip4()
Definition: eserie.cpp:372
std::array< R_DATA, S4R+1 > m_results
Definition: eserie.h:197
void simple_solution(uint32_t aSize)
Search for closest two component solution.
Definition: eserie.cpp:163
void strip3()
Definition: eserie.cpp:353

References combine2(), combine3(), m_results, S2R, simple_solution(), strip3(), and strip4().

Referenced by PANEL_E_SERIE::OnCalculateESeries().

◆ combine2()

uint32_t E_SERIE::combine2 ( )
private

Build all 2R combinations from the selected E-serie values.

Pre-calculated value combinations are saved in intermediate look up table m_cmb_lut

Returns
is the number of found combinations what also depends from exclude values

Definition at line 251 of file eserie.cpp.

252 {
253  uint32_t combi2R = 0; // target index counts calculated 2R combinations
254  std::string s;
255 
256  for( const R_DATA& i : m_luts[m_series] ) // outer loop to sweep selected source lookup table
257  {
258  if( i.e_use )
259  {
260  for( const R_DATA& j : m_luts[m_series] ) // inner loop to combine values with itself
261  {
262  if( j.e_use )
263  {
264  m_cmb_lut.at( combi2R ).e_use = true;
265  m_cmb_lut.at( combi2R ).e_value = i.e_value + j.e_value; // calculate 2R serial
266  s = i.e_name;
267  s.append( " + " );
268  m_cmb_lut.at( combi2R ).e_name = s.append( j.e_name);
269  combi2R++; // next destination
270  m_cmb_lut.at( combi2R ).e_use = true; // calculate 2R parallel
271  m_cmb_lut.at( combi2R ).e_value = i.e_value * j.e_value / ( i.e_value + j.e_value );
272  s = i.e_name;
273  s.append( " | " );
274  m_cmb_lut.at( combi2R ).e_name = s.append( j.e_name );
275  combi2R++; // next destination
276  }
277  }
278  }
279  }
280  return combi2R;
281 }
std::vector< R_DATA > m_cmb_lut
Definition: eserie.h:195
Definition: eserie.h:69
std::vector< std::vector< R_DATA > > m_luts
Definition: eserie.h:183
uint32_t m_series
Definition: eserie.h:198

References m_cmb_lut, m_luts, and m_series.

Referenced by Calculate().

◆ combine3()

void E_SERIE::combine3 ( uint32_t  aSize)
private

Check if there is a better 3 R solution than previous one using only two components.

Parameters
aSizegives the number of available combinations to be checked inside m_cmb_lut Therefore m_cmb_lut is combinated with the primary E-serie look up table The 3R result with smallest deviation will be saved in results if better than 2R

Definition at line 284 of file eserie.cpp.

285 {
286  uint32_t j = 0;
287  double tmp = 0; // avoid warning for being uninitialized
288  std::string s;
289 
290  m_results.at( S3R ).e_use = false; // disable 3R solution, until
291  m_results.at( S3R ).e_value = m_results.at( S2R ).e_value; // 3R becomes better than 2R solution
292 
293  for( const R_DATA& i : m_luts[m_series] ) // 3R Outer loop to selected primary E serie LUT
294  {
295  if( i.e_use ) // skip all excluded values
296  {
297  for( j = 0; j < aSize; j++ ) // inner loop combines with all 2R intermediate results
298  { // R+2R serial combi
299  tmp = m_cmb_lut.at( j ).e_value + i.e_value;
300  tmp -= m_required_value; // calculate deviation
301 
302  if( abs( tmp ) < abs( m_results.at( S3R ).e_value ) ) // compare if better
303  { // then take it
304  s = i.e_name; // mention 3rd component
305  s.append( " + ( " ); // in series
306  s.append( m_cmb_lut.at( j ).e_name ); // with 2R combination
307  s.append( " )" );
308  m_results.at( S3R ).e_name = s; // save S3R result
309  m_results.at( S3R ).e_value = tmp; // save amount of benefit
310  m_results.at( S3R ).e_use = true; // enable later use
311  }
312 
313  tmp = i.e_value * m_cmb_lut.at( j ).e_value /
314  ( i.e_value + m_cmb_lut.at( j ).e_value ); // calculate R + 2R parallel
315  tmp -= m_required_value; // calculate deviation
316 
317  if( abs( tmp ) < abs( m_results.at( S3R ).e_value ) ) // compare if better
318  { // then take it
319  s = i.e_name; // mention 3rd component
320  s.append( " | ( " ); // in parallel
321  s.append( m_cmb_lut.at( j ).e_name ); // with 2R combination
322  s.append( " )" );
323  m_results.at( S3R ).e_name = s;
324  m_results.at( S3R ).e_value = tmp; // save amount of benefit
325  m_results.at( S3R ).e_use = true; // enable later use
326  }
327  }
328  }
329  }
330 
331  // If there is a 3R result with remaining deviation consider to search a possibly better 4R solution
332  // calculate 4R for small series always
333  if(( m_results.at( S3R ).e_use == true ) && tmp )
334  combine4( aSize );
335 }
std::vector< R_DATA > m_cmb_lut
Definition: eserie.h:195
Definition: eserie.h:69
std::vector< std::vector< R_DATA > > m_luts
Definition: eserie.h:183
double m_required_value
Definition: eserie.h:200
uint32_t m_series
Definition: eserie.h:198
Definition: eserie.h:66
Definition: eserie.h:66
void combine4(uint32_t aSize)
Check if there is a better four component solution.
Definition: eserie.cpp:181
std::array< R_DATA, S4R+1 > m_results
Definition: eserie.h:197

References combine4(), m_cmb_lut, m_luts, m_required_value, m_results, m_series, S2R, and S3R.

Referenced by Calculate().

◆ combine4()

void E_SERIE::combine4 ( uint32_t  aSize)
private

Check if there is a better four component solution.

Parameters
aSsizegives the number of 2R combinations to be checked inside m_cmb_lut Occupied calculation time depends from number of available E-serie values with the power of 4 why execution for E12 is conditional with 4R check box for the case the previously found 3R solution is already exact

Definition at line 181 of file eserie.cpp.

182 {
183  uint32_t i,j;
184  double tmp;
185  std::string s;
186 
187  m_results.at( S4R ).e_use = false; // disable 4R solution, until
188  m_results.at( S4R ).e_value = m_results.at( S3R ).e_value; // 4R becomes better than 3R solution
189 
190  #ifdef BENCHMARK
191  PROF_COUNTER timer; // start timer to count execution time
192  #endif
193 
194  for( i = 0; i < aSize; i++ ) // 4R search outer loop
195  { // scan valid intermediate 2R solutions
196  for( j = 0; j < aSize; j++ ) // inner loop combines all with itself
197  {
198  tmp = m_cmb_lut.at( i ).e_value + m_cmb_lut.at( j ).e_value; // calculate 2R+2R serial
199  tmp -= m_required_value; // calculate 4R deviation
200 
201  if( abs( tmp ) < abs( m_results.at(S4R).e_value ) ) // if new 4R is better
202  {
203  m_results.at( S4R ).e_value = tmp; // save amount of benefit
204  std::string s = "( ";
205  s.append( m_cmb_lut.at( i ).e_name ); // mention 1st 2 component
206  s.append( " ) + ( " ); // in series
207  s.append( m_cmb_lut.at( j ).e_name ); // with 2nd 2 components
208  s.append( " )" );
209  m_results.at( S4R ).e_name = s; // save the result and
210  m_results.at( S4R ).e_use = true; // enable for later use
211  }
212 
213  tmp = ( m_cmb_lut[i].e_value * m_cmb_lut.at( j ).e_value ) /
214  ( m_cmb_lut[i].e_value + m_cmb_lut.at( j ).e_value ); // calculate 2R|2R parallel
215  tmp -= m_required_value; // calculate 4R deviation
216 
217  if( abs( tmp ) < abs( m_results.at( S4R ).e_value ) ) // if new 4R is better
218  {
219  m_results.at( S4R ).e_value = tmp; // save amount of benefit
220  std::string s = "( ";
221  s.append( m_cmb_lut.at( i ).e_name ); // mention 1st 2 component
222  s.append( " ) | ( " ); // in parallel
223  s.append( m_cmb_lut.at( j ).e_name ); // with 2nd 2 components
224  s.append( " )" );
225  m_results.at( S4R ).e_name = s; // save the result
226  m_results.at( S4R ).e_use = true; // enable later use
227  }
228  }
229  }
230 
231  #ifdef BENCHMARK
232  printf( "Calculation time = %d mS", timer.msecs() );
233  fflush( 0 );
234  #endif
235 }
std::vector< R_DATA > m_cmb_lut
Definition: eserie.h:195
Definition: eserie.h:66
double m_required_value
Definition: eserie.h:200
A thread-safe event counter.
Definition: profile.h:225
Definition: eserie.h:66
std::array< R_DATA, S4R+1 > m_results
Definition: eserie.h:197

References m_cmb_lut, m_required_value, m_results, S3R, and S4R.

Referenced by combine3().

◆ Exclude()

void E_SERIE::Exclude ( double  aValue)

If any value of the selected E-serie not available, it can be entered as an exclude value.

Parameters
aValueis the value to exclude from calculation Values to exclude are set to false in the selected E-serie source lookup table

Definition at line 150 of file eserie.cpp.

151 {
152  if( aValue ) // if there is a value to exclude other than a wire jumper
153  {
154  for( R_DATA& i : m_luts[m_series] ) // then search it in the selected E-Serie lookup table
155  {
156  if( i.e_value == aValue ) // if the value to exclude is found
157  i.e_use = false; // disable its use
158  }
159  }
160 }
Definition: eserie.h:69
std::vector< std::vector< R_DATA > > m_luts
Definition: eserie.h:183
uint32_t m_series
Definition: eserie.h:198

References m_luts, and m_series.

Referenced by PANEL_E_SERIE::OnCalculateESeries().

◆ GetResults()

const std::array<R_DATA,S4R+1>& E_SERIE::GetResults ( )
inline

Definition at line 118 of file eserie.h.

118 { return m_results; }
std::array< R_DATA, S4R+1 > m_results
Definition: eserie.h:197

References m_results.

Referenced by PANEL_E_SERIE::OnCalculateESeries().

◆ NewCalc()

void E_SERIE::NewCalc ( )

initialize next calculation and erase results from previous calculation

Definition at line 238 of file eserie.cpp.

239 {
240  for( R_DATA& i : m_cmb_lut )
241  i.e_use = false; // before any calculation is done, assume that
242 
243  for( R_DATA& i : m_results )
244  i.e_use = false; // no combinations and no results are available
245 
246  for( R_DATA& i : m_luts[m_series])
247  i.e_use = true; // all selected E-values available
248 }
std::vector< R_DATA > m_cmb_lut
Definition: eserie.h:195
Definition: eserie.h:69
std::vector< std::vector< R_DATA > > m_luts
Definition: eserie.h:183
uint32_t m_series
Definition: eserie.h:198
std::array< R_DATA, S4R+1 > m_results
Definition: eserie.h:197

References m_cmb_lut, m_luts, m_results, and m_series.

Referenced by PANEL_E_SERIE::OnCalculateESeries().

◆ SetRequiredValue()

void E_SERIE::SetRequiredValue ( double  aValue)
inline

Definition at line 115 of file eserie.h.

115 { m_required_value = aValue; }
double m_required_value
Definition: eserie.h:200

References m_required_value.

Referenced by PANEL_E_SERIE::OnCalculateESeries().

◆ SetSeries()

void E_SERIE::SetSeries ( uint32_t  aSeries)
inline

Interface for CheckBox, RadioButton, RequriedResistor and calculated Results.

Definition at line 114 of file eserie.h.

114 { m_series = aSeries; }
uint32_t m_series
Definition: eserie.h:198

References m_series.

Referenced by PANEL_E_SERIE::OnESeriesSelection().

◆ simple_solution()

void E_SERIE::simple_solution ( uint32_t  aSize)
private

Search for closest two component solution.

Parameters
aSizeis the number of valid 2R combinations in m_cmb_lut on where to search The 2R result with smallest deviation will be saved in results

Definition at line 163 of file eserie.cpp.

164 {
165  uint32_t i;
166 
167  m_results.at( S2R ).e_value = std::numeric_limits<double>::max(); // assume no 2R solution or max deviation
168 
169  for( i = 0; i < aSize; i++ )
170  {
171  if( abs( m_cmb_lut.at( i ).e_value - m_required_value ) < abs( m_results.at( S2R ).e_value ) )
172  {
173  m_results.at( S2R ).e_value = m_cmb_lut.at( i ).e_value - m_required_value; // save signed deviation in Ohms
174  m_results.at( S2R ).e_name = m_cmb_lut.at( i ).e_name; // save combination text
175  m_results.at( S2R ).e_use = true; // this is a possible solution
176  }
177  }
178 }
std::vector< R_DATA > m_cmb_lut
Definition: eserie.h:195
double m_required_value
Definition: eserie.h:200
Definition: eserie.h:66
std::array< R_DATA, S4R+1 > m_results
Definition: eserie.h:197

References m_cmb_lut, m_required_value, m_results, and S2R.

Referenced by Calculate().

◆ strip3()

void E_SERIE::strip3 ( )
private

Definition at line 353 of file eserie.cpp.

354 {
355  std::string s;
356 
357  if( m_results.at( S3R ).e_use ) // if there is a 3 term result available
358  { // what is connected either by two "|" or by 3 plus
359  s = m_results.at( S3R ).e_name;
360 
361  if( ( std::count( s.begin(), s.end(), '+' ) == 2 )
362  || ( std::count( s.begin(), s.end(), '|' ) == 2 ) )
363  { // then strip one pair of braces
364  s.erase( s.find( "(" ), 1 ); // it is known sure, this is available
365  s.erase( s.find( ")" ), 1 ); // in any unstripped 3R result term
366  m_results.at( S3R ).e_name = s; // use stripped result
367  }
368  }
369 }
Definition: eserie.h:66
std::array< R_DATA, S4R+1 > m_results
Definition: eserie.h:197

References m_results, and S3R.

Referenced by Calculate().

◆ strip4()

void E_SERIE::strip4 ( )
private

Definition at line 372 of file eserie.cpp.

373 {
374  std::string s;
375 
376  if( m_results.at( S4R ).e_use ) // if there is a 4 term result available
377  { // what are connected either by 3 "+" or by 3 "|"
378  s = m_results.at( S4R ).e_name;
379 
380  if( ( std::count( s.begin(), s.end(), '+' ) == 3 )
381  || ( std::count( s.begin(), s.end(), '|' ) == 3 ) )
382  { // then strip two pair of braces
383  s.erase( s.find( "(" ), 1 ); // it is known sure, they are available
384  s.erase( s.find( ")" ), 1 ); // in any unstripped 4R result term
385  s.erase( s.find( "(" ), 1 );
386  s.erase( s.find( ")" ), 1 );
387  m_results.at( S4R ).e_name = s; // use stripped result
388  }
389  }
390 }
Definition: eserie.h:66
std::array< R_DATA, S4R+1 > m_results
Definition: eserie.h:197

References m_results, and S4R.

Referenced by Calculate().

Member Data Documentation

◆ m_cmb_lut

std::vector<R_DATA> E_SERIE::m_cmb_lut
private

Definition at line 195 of file eserie.h.

Referenced by combine2(), combine3(), combine4(), E_SERIE(), NewCalc(), and simple_solution().

◆ m_enable_4R

uint32_t E_SERIE::m_enable_4R = false
private

Definition at line 199 of file eserie.h.

◆ m_luts

std::vector<std::vector<R_DATA> > E_SERIE::m_luts
private

Definition at line 183 of file eserie.h.

Referenced by buildSerieData(), combine2(), combine3(), Exclude(), and NewCalc().

◆ m_required_value

double E_SERIE::m_required_value = 0.0
private

Definition at line 200 of file eserie.h.

Referenced by combine3(), combine4(), SetRequiredValue(), and simple_solution().

◆ m_results

std::array<R_DATA, S4R+1> E_SERIE::m_results
private

Definition at line 197 of file eserie.h.

Referenced by Calculate(), combine3(), combine4(), GetResults(), NewCalc(), simple_solution(), strip3(), and strip4().

◆ m_series

uint32_t E_SERIE::m_series = E6
private

Definition at line 198 of file eserie.h.

Referenced by combine2(), combine3(), Exclude(), NewCalc(), and SetSeries().


The documentation for this class was generated from the following files: