Sim: Fix potentiometers

This commit is contained in:
Mikolaj Wielgus 2022-10-24 11:04:28 +02:00
parent 9b6cc6c505
commit 0399d08aaa
3 changed files with 22 additions and 7 deletions

View File

@ -29,12 +29,19 @@
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 );
// We invert the argument passed to the model because we want pos=1 to correspond to the signal
// from + and pos=0 to the signal from -.
auto pos = static_cast<SIM_VALUE_FLOAT&>( *m_model.FindParam( "pos" )->value );
if( pos.HasValue() )
{
std::string position = ( SIM_VALUE_FLOAT( 1 ) - pos ).ToSpiceString();
return fmt::format( ".model {} potentiometer( r={} position={} )\n", aItem.modelName, r,
position );
}
else
return fmt::format( ".model {} potentiometer( r0={} )\n", aItem.modelName, r );
return fmt::format( ".model {} potentiometer( r={} )\n", aItem.modelName, r );
}
@ -43,7 +50,7 @@ std::string SPICE_GENERATOR_R_POT::TunerCommand( const SPICE_ITEM& aItem,
{
return fmt::format( "altermod @{}[position]={}",
aItem.model->SpiceGenerator().ItemName( aItem ),
aValue.ToSpiceString() );
( SIM_VALUE_FLOAT( 1 ) - aValue ).ToSpiceString() );
}

View File

@ -53,6 +53,8 @@ private:
template <typename T>
void inferredWriteDataFields( std::vector<T>& aFields ) const;
std::vector<std::string> getPinNames() const override { return { "+", "wiper", "-" }; }
static const std::vector<PARAM::INFO> makeParamInfos();
};

View File

@ -64,16 +64,22 @@ TUNER_SLIDER::TUNER_SLIDER( SIM_PLOT_FRAME* aFrame, wxWindow* aParent, SCH_SYMBO
m_item->model->GetTypeInfo().fieldValue ) );
m_value = SPICE_VALUE( m_item->model->GetTunerParam()->value->ToSpiceString() );
// Special case for potentiometers because we don't have value ranges implemented yet.
if( m_item->model->GetType() == SIM_MODEL::TYPE::R_POT )
{
std::string valueStr = m_item->model->GetTunerParam()->value->ToSpiceString();
if( valueStr != "" )
m_value = SPICE_VALUE( valueStr );
else
m_value = SPICE_VALUE( "0.5" );
m_min = SPICE_VALUE( 0 );
m_max = SPICE_VALUE( 1 );
}
else
{
m_value = SPICE_VALUE( m_item->model->GetTunerParam()->value->ToSpiceString() );
m_min = SPICE_VALUE( 0.5 ) * m_value;
m_max = SPICE_VALUE( 2.0 ) * m_value;
}