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, const std::string& aName ) : value( aValue ), name( aName ) {}
51};
52
53
65{
66public:
68
69 enum
70 {
75 };
76
83 void SetSeries( uint32_t aSeries );
84
91 void NewCalc( double aTargetValue );
92
100 void Exclude( double aValue );
101
106 void Calculate();
107
112 const std::array<std::optional<RESISTANCE>, NUMBER_OF_LEVELS>& GetResults()
113 {
114 return m_results;
115 }
116
117private:
122 std::vector<RESISTANCE> buildSeriesData( const ESERIES::ESERIES_VALUES& aList );
123
127 void prepare1RBuffer();
128
133 void prepare2RBuffer();
134
139 std::pair<RESISTANCE&, RESISTANCE&> findIn2RBuffer( double aTargetValue );
140
147
148private:
149 std::vector<std::vector<RESISTANCE>> m_e_series;
150 std::vector<bool> m_exclude_mask;
151 std::vector<RESISTANCE> m_buffer_1R;
152 std::vector<RESISTANCE> m_buffer_2R;
153
155 double m_target = 0;
156
157 std::array<std::optional<RESISTANCE>, NUMBER_OF_LEVELS> m_results;
158};
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
const double epsilon
RESISTANCE(double aValue, const std::string &aName)