KiCad PCB EDA Suite
Loading...
Searching...
No Matches
resistor_substitution_utils.h
Go to the documentation of this file.
1/*
2 * This program source code file
3 * is part of KiCad, a free EDA CAD application.
4 *
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include "eseries.h"
24#include <array>
25#include <optional>
26#include <string>
27#include <utility>
28#include <vector>
29
30const double epsilon = 1e-12; // machine epsilon for floating-point equality testing
31
32// First value of resistor in Ohm
33// It should be first value of the decade, i.e. power of 10
34// This value is only pertinent to the resistor calculator.
35// It is used to reduce the computational complexity of its calculations.
36// There are valid resistor values using E-series numbers below this
37// value and above the below LAST_VALUE.
38#define RES_EQUIV_CALC_FIRST_VALUE 10
39
40// Last value of resistor in Ohm
41// This value is only pertinent to the resistor calculator. See above.
42#define RES_EQUIV_CALC_LAST_VALUE 1e6
43
44// Struct representing resistance value together with its composition, e.g. {20.0, "10R + 10R"}
46{
47 double value;
48 std::string name;
49
50 RESISTANCE( double aValue = 0.0, std::string aName = "" ) :
51 value( aValue ), name( std::move( aName ) )
52 {
53 }
54};
55
56
68{
69public:
71
72 enum
73 {
78 };
79
86 void SetSeries( uint32_t aSeries );
87
94 void NewCalc( double aTargetValue );
95
103 void Exclude( double aValue );
104
109 void Calculate();
110
115 const std::array<std::optional<RESISTANCE>, NUMBER_OF_LEVELS>& GetResults()
116 {
117 return m_results;
118 }
119
120private:
125 std::vector<RESISTANCE> buildSeriesData( const ESERIES::ESERIES_VALUES& aList );
126
130 void prepare1RBuffer();
131
136 void prepare2RBuffer();
137
142 std::pair<RESISTANCE&, RESISTANCE&> findIn2RBuffer( double aTargetValue );
143
150
151private:
152 std::vector<std::vector<RESISTANCE>> m_e_series;
153 std::vector<bool> m_exclude_mask;
154 std::vector<RESISTANCE> m_buffer_1R;
155 std::vector<RESISTANCE> m_buffer_2R;
156
158 double m_target = 0;
159
160 std::array<std::optional<RESISTANCE>, NUMBER_OF_LEVELS> m_results;
161};
Performs calculations on E-series values primarily to find target values as combinations (serial,...
std::array< std::optional< RESISTANCE >, NUMBER_OF_LEVELS > m_results
RESISTANCE calculate2RSolution()
Calculate the best combination consisting of exactly 2, 3 or 4 resistors.
std::vector< RESISTANCE > m_buffer_2R
std::pair< RESISTANCE &, RESISTANCE & > findIn2RBuffer(double aTargetValue)
Find in 2R buffer two values nearest to the given value (one smaller and one larger).
std::vector< RESISTANCE > m_buffer_1R
std::vector< RESISTANCE > buildSeriesData(const ESERIES::ESERIES_VALUES &aList)
Add values from aList to m_e_series tables.
void Exclude(double aValue)
If any value of the selected E-series not available, it can be entered as an exclude value.
const std::array< std::optional< RESISTANCE >, NUMBER_OF_LEVELS > & GetResults()
Accessor to calculation results.
void SetSeries(uint32_t aSeries)
Set E-series to be used in calculations.
std::vector< std::vector< RESISTANCE > > m_e_series
void prepare1RBuffer()
Build 1R buffer, which is selected E-series table with excluded values removed.
void Calculate()
Executes all the calculations.
void NewCalc(double aTargetValue)
Initialize next calculation, clear exclusion mask and erase results from previous calculation.
void prepare2RBuffer()
Build 2R buffer, which consists of all possible combinations of two resistors from 1R buffer (serial ...
std::vector< bool > m_exclude_mask
@ E6
Definition: eseries.h:72
STL namespace.
const double epsilon
RESISTANCE(double aValue=0.0, std::string aName="")