Sim: Add potentiometer model

This commit is contained in:
Mikolaj Wielgus 2022-10-22 04:33:26 +02:00
parent 2a5bc9f87e
commit 958bd1897d
8 changed files with 194 additions and 14 deletions

View File

@ -291,6 +291,7 @@ set( EESCHEMA_SRCS
sim/sim_model_kibis.cpp
sim/sim_model_ngspice.cpp
sim/sim_model_ngspice_data.cpp
sim/sim_model_r_pot.cpp
sim/sim_model_raw_spice.cpp
sim/sim_model_source.cpp
sim/sim_model_spice.cpp

View File

@ -171,11 +171,12 @@ bool DIALOG_SIM_MODEL<T>::TransferDataToWindow()
else
{
// The model is sourced from the instance.
SIM_MODEL::TYPE type = SIM_MODEL::ReadTypeFromFields( m_fields );
SIM_MODEL::TYPE type = SIM_MODEL::ReadTypeFromFields( m_fields, m_sortedSymbolPins.size() );
try
{
m_models.at( static_cast<int>( SIM_MODEL::ReadTypeFromFields( m_fields ) ) ) =
m_models.at( static_cast<int>( SIM_MODEL::ReadTypeFromFields( m_fields,
m_sortedSymbolPins.size() ) ) ) =
SIM_MODEL::Create( m_sortedSymbolPins.size(), m_fields );
}
catch( const IO_ERROR& e )

View File

@ -27,6 +27,7 @@
#include <sim/sim_model_ideal.h>
#include <sim/sim_model_mutual_inductor.h>
#include <sim/sim_model_ngspice.h>
#include <sim/sim_model_r_pot.h>
#include <sim/sim_model_kibis.h>
#include <sim/sim_model_source.h>
#include <sim/sim_model_raw_spice.h>
@ -118,6 +119,7 @@ SIM_MODEL::INFO SIM_MODEL::TypeInfo( TYPE aType )
case TYPE::NONE: return { DEVICE_TYPE_::NONE, "", "" };
case TYPE::R: return { DEVICE_TYPE_::R, "", "Ideal" };
case TYPE::R_POT: return { DEVICE_TYPE_::R, "POT", "Potentiometer" };
case TYPE::R_BEHAVIORAL: return { DEVICE_TYPE_::R, "=", "Behavioral" };
case TYPE::C: return { DEVICE_TYPE_::C, "", "Ideal" };
@ -254,6 +256,7 @@ SIM_MODEL::SPICE_INFO SIM_MODEL::SpiceInfo( TYPE aType )
switch( aType )
{
case TYPE::R: return { "R", "" };
case TYPE::R_POT: return { "A", "" };
case TYPE::R_BEHAVIORAL: return { "R", "", "", "0", false, true };
case TYPE::C: return { "C", "" };
@ -389,11 +392,13 @@ SIM_MODEL::SPICE_INFO SIM_MODEL::SpiceInfo( TYPE aType )
}
template TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<SCH_FIELD>& aFields );
template TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<LIB_FIELD>& aFields );
template TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<SCH_FIELD>& aFields,
int aSymbolPinCount );
template TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<LIB_FIELD>& aFields,
int aSymbolPinCount );
template <typename T>
TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<T>& aFields )
TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<T>& aFields, int aSymbolPinCount )
{
std::string deviceTypeFieldValue = GetFieldValue( &aFields, DEVICE_TYPE_FIELD );
std::string typeFieldValue = GetFieldValue( &aFields, TYPE_FIELD );
@ -422,7 +427,8 @@ TYPE SIM_MODEL::ReadTypeFromFields( const std::vector<T>& aFields )
// Still no type information.
// We try to infer the model from the mandatory fields in this case.
return InferTypeFromRefAndValue( GetFieldValue( &aFields, REFERENCE_FIELD ),
GetFieldValue( &aFields, VALUE_FIELD ) );
GetFieldValue( &aFields, VALUE_FIELD ),
aSymbolPinCount );
}
@ -446,7 +452,8 @@ DEVICE_TYPE SIM_MODEL::InferDeviceTypeFromRef( const std::string& aRef )
}
TYPE SIM_MODEL::InferTypeFromRefAndValue( const std::string& aRef, const std::string& aValue )
TYPE SIM_MODEL::InferTypeFromRefAndValue( const std::string& aRef, const std::string& aValue,
int aSymbolPinCount )
{
std::string typeString;
@ -470,6 +477,10 @@ TYPE SIM_MODEL::InferTypeFromRefAndValue( const std::string& aRef, const std::st
DEVICE_TYPE deviceType = InferDeviceTypeFromRef( aRef );
// Exception. Potentiometer model is determined from pin count.
if( deviceType == DEVICE_TYPE_::R && aSymbolPinCount == 3 )
return TYPE::R_POT;
for( TYPE type : TYPE_ITERATOR() )
{
if( TypeInfo( type ).deviceType == deviceType && TypeInfo( type ).fieldValue == typeString )
@ -579,7 +590,7 @@ template <typename T>
std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount,
const std::vector<T>& aFields )
{
TYPE type = ReadTypeFromFields( aFields );
TYPE type = ReadTypeFromFields( aFields, aSymbolPinCount );
// If the model has a specified type, it takes priority over the type of its base class.
if( type == TYPE::NONE )
@ -605,7 +616,7 @@ template <typename T>
std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( unsigned aSymbolPinCount,
const std::vector<T>& aFields )
{
TYPE type = ReadTypeFromFields( aFields );
TYPE type = ReadTypeFromFields( aFields, aSymbolPinCount );
if( type == TYPE::NONE )
THROW_IO_ERROR( wxString::Format( _( "Failed to read simulation model from fields" ) ) );
@ -877,6 +888,9 @@ std::unique_ptr<SIM_MODEL> SIM_MODEL::Create( TYPE aType )
case TYPE::L:
return std::make_unique<SIM_MODEL_IDEAL>( aType );
case TYPE::R_POT:
return std::make_unique<SIM_MODEL_R_POT>();
case TYPE::L_MUTUAL:
return std::make_unique<SIM_MODEL_MUTUAL_INDUCTOR>();
@ -1176,7 +1190,8 @@ void SIM_MODEL::InferredReadDataFields( unsigned aSymbolPinCount, const std::vec
// TODO: Don't call this multiple times.
if( InferTypeFromRefAndValue( GetFieldValue( aFields, REFERENCE_FIELD ),
GetFieldValue( aFields, VALUE_FIELD ) ) != GetType() )
GetFieldValue( aFields, VALUE_FIELD ),
aSymbolPinCount ) != GetType() )
{
// Not an inferred model. Nothing to do here.
return;

View File

@ -201,6 +201,7 @@ public:
NONE,
R,
R_POT,
R_BEHAVIORAL,
C,
@ -482,11 +483,12 @@ public:
template <typename T>
static TYPE ReadTypeFromFields( const std::vector<T>& aFields );
static TYPE ReadTypeFromFields( const std::vector<T>& aFields, int aSymbolPinCount );
static DEVICE_TYPE_ InferDeviceTypeFromRef( const std::string& aRef );
static TYPE InferTypeFromRefAndValue( const std::string& aRef, const std::string& aValue );
static TYPE InferTypeFromRefAndValue( const std::string& aRef, const std::string& aValue,
int aSymbolPinCount );
template <typename T>
static TYPE InferTypeFromLegacyFields( const std::vector<T>& aFields );

View File

@ -151,7 +151,8 @@ void SIM_MODEL_BEHAVIORAL::inferredReadDataFields( unsigned aSymbolPinCount,
ParsePinsField( aSymbolPinCount, GetFieldValue( aFields, PINS_FIELD ) );
if( ( InferTypeFromRefAndValue( GetFieldValue( aFields, REFERENCE_FIELD ),
GetFieldValue( aFields, VALUE_FIELD ) ) == GetType()
GetFieldValue( aFields, VALUE_FIELD ),
aSymbolPinCount ) == GetType()
&& parseValueField( GetFieldValue( aFields, VALUE_FIELD ) ) )
// If Value is device type, this is an empty model
|| GetFieldValue( aFields, VALUE_FIELD ) == DeviceTypeInfo( GetDeviceType() ).fieldValue )

View File

@ -0,0 +1,103 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2022 Mikolaj Wielgus
* Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* https://www.gnu.org/licenses/gpl-3.0.html
* or you may search the http://www.gnu.org website for the version 3 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <sim/sim_model_r_pot.h>
#include <fmt/core.h>
std::string SPICE_GENERATOR_R_POT::ModelLine( const SPICE_ITEM& aItem ) const
{
std::string r = m_model.FindParam( "r" )->value->ToSpiceString();
std::string pos = m_model.FindParam( "pos" )->value->ToSpiceString();
if( pos != "" )
return fmt::format( ".model {} potentiometer( r0={} position={} )\n", aItem.modelName, r, pos );
else
return fmt::format( ".model {} potentiometer( r0={} )\n", aItem.modelName, r );
}
SIM_MODEL_R_POT::SIM_MODEL_R_POT() :
SIM_MODEL( TYPE::R_POT, std::make_unique<SPICE_GENERATOR_R_POT>( *this ) )
{
static std::vector<PARAM::INFO> paramInfos = makeParamInfos();
for( const SIM_MODEL::PARAM::INFO& paramInfo : paramInfos )
AddParam( paramInfo );
}
void SIM_MODEL_R_POT::WriteDataSchFields( std::vector<SCH_FIELD>& aFields ) const
{
SIM_MODEL::WriteDataSchFields( aFields );
if( IsInferred() )
inferredWriteDataFields( aFields );
}
void SIM_MODEL_R_POT::WriteDataLibFields( std::vector<LIB_FIELD>& aFields ) const
{
SIM_MODEL::WriteDataLibFields( aFields );
if( IsInferred() )
inferredWriteDataFields( aFields );
}
template <typename T>
void SIM_MODEL_R_POT::inferredWriteDataFields( std::vector<T>& aFields ) const
{
std::string value = GetFieldValue( &aFields, PARAMS_FIELD );
if( value == "" )
value = GetDeviceTypeInfo().fieldValue;
WriteInferredDataFields( aFields, value );
}
const std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_R_POT::makeParamInfos()
{
std::vector<PARAM::INFO> paramInfos;
PARAM::INFO paramInfo;
paramInfo.name = "r";
paramInfo.type = SIM_VALUE::TYPE_STRING;
paramInfo.unit = "Ω";
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
paramInfo.defaultValue = "";
paramInfo.description = "Resistance";
paramInfos.push_back( paramInfo );
paramInfo.name = "pos";
paramInfo.type = SIM_VALUE::TYPE_FLOAT;
paramInfo.unit = "";
paramInfo.category = PARAM::CATEGORY::PRINCIPAL;
paramInfo.defaultValue = "0.5";
paramInfo.description = "Wiper position";
paramInfos.push_back( paramInfo );
return paramInfos;
}

View File

@ -0,0 +1,56 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2022 Mikolaj Wielgus
* Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* https://www.gnu.org/licenses/gpl-3.0.html
* or you may search the http://www.gnu.org website for the version 3 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef SIM_MODEL_R_POT_H
#define SIM_MODEL_R_POT_H
#include <sim/sim_model.h>
#include <sim/spice_generator.h>
class SPICE_GENERATOR_R_POT : public SPICE_GENERATOR
{
public:
using SPICE_GENERATOR::SPICE_GENERATOR;
std::string ModelLine( const SPICE_ITEM& aItem ) const override;
};
class SIM_MODEL_R_POT : public SIM_MODEL
{
public:
SIM_MODEL_R_POT();
void WriteDataSchFields( std::vector<SCH_FIELD>& aFields ) const override;
void WriteDataLibFields( std::vector<LIB_FIELD>& aFields ) const override;
private:
template <typename T>
void inferredWriteDataFields( std::vector<T>& aFields ) const;
static const std::vector<PARAM::INFO> makeParamInfos();
};
#endif // SIM_MODEL_POT_H

View File

@ -549,7 +549,8 @@ void SIM_PLOT_FRAME::AddTuner( SCH_SYMBOL* aSymbol )
if( !plotPanel )
return;
SIM_MODEL::TYPE type = SIM_MODEL::ReadTypeFromFields( aSymbol->GetFields() );
SIM_MODEL::TYPE type = SIM_MODEL::ReadTypeFromFields( aSymbol->GetFields(),
static_cast<int>( aSymbol->GetLibPins().size() ) );
SIM_MODEL::DEVICE_TYPE_ deviceType = SIM_MODEL::TypeInfo( type ).deviceType;
switch( deviceType )