From e8d6a42e1a6ff3dddcf423df23416321bfe6cbb3 Mon Sep 17 00:00:00 2001 From: Tomasz Wlostowski Date: Thu, 11 Aug 2016 14:41:49 +0200 Subject: [PATCH] simulator: working on magnitude/phase plots --- common/widgets/mathplot.cpp | 9 +++++++-- eeschema/sim/sim_plot_frame.cpp | 6 +++++- eeschema/sim/sim_plot_panel.cpp | 33 +++++++++++++++++++++++++------ eeschema/sim/sim_plot_panel.h | 32 ++++++++++++++++-------------- eeschema/widgets/tuner_slider.cpp | 10 +++++++++- eeschema/widgets/tuner_slider.h | 1 + include/widgets/mathplot.h | 12 +++++++++++ 7 files changed, 78 insertions(+), 25 deletions(-) diff --git a/common/widgets/mathplot.cpp b/common/widgets/mathplot.cpp index ef9299e96a..e9ec1c0f4a 100644 --- a/common/widgets/mathplot.cpp +++ b/common/widgets/mathplot.cpp @@ -1110,12 +1110,16 @@ void mpScaleXLog::recalculateTicks ( wxDC & dc, mpWindow & w ) double visibleDecades = log( maxVvis / minVvis ) / log(10); - //printf("visibleD %.1f %.1f %.1f\n", visibleDecades, maxVvis, minVvis); + printf("visibleD %f %f\n", minDecade, maxDecade); double d; m_tickValues.clear(); m_labeledTicks.clear(); + if ( minDecade == 0.0 ) + return; + + for ( d= minDecade; d<=maxDecade; d *= 10.0) { //printf("d %.1f\n",d ); @@ -2944,13 +2948,14 @@ double log10( double x) return log(x)/log(10.0); } +#if 0 mpFSemiLogXVector::mpFSemiLogXVector(wxString name, int flags ) : mpFXYVector ( name, flags ) {} IMPLEMENT_DYNAMIC_CLASS(mpFSemiLogXVector, mpFXYVector) - +#endif void mpFXYVector::Rewind() { diff --git a/eeschema/sim/sim_plot_frame.cpp b/eeschema/sim/sim_plot_frame.cpp index be57e88ae8..100c14b52d 100644 --- a/eeschema/sim/sim_plot_frame.cpp +++ b/eeschema/sim/sim_plot_frame.cpp @@ -305,7 +305,7 @@ bool SIM_PLOT_FRAME::updatePlot( const wxString& aSpiceName, const wxString& aNa return false; aPanel->AddTrace( aSpiceName, aName + " (mag)", size, data_x.data(), data_mag.data(), 0 ); - aPanel->AddTrace( aSpiceName, aName + " (phase)", size, data_x.data(), data_phase.data(), 0 ); + aPanel->AddTrace( aSpiceName, aName + " (phase)", size, data_x.data(), data_phase.data(), SPF_AC_PHASE ); } break; @@ -668,11 +668,15 @@ void SIM_PLOT_FRAME::onSimUpdate( wxCommandEvent& aEvent ) if( !m_simulator ) return; + aEvent.Discard(); + if( IsSimulationRunning() ) StopSimulation(); m_simConsole->Clear(); + printf("SimUpdate\n"); + // Apply tuned values if( SIM_PLOT_PANEL* plotPanel = CurrentPlot() ) { diff --git a/eeschema/sim/sim_plot_panel.cpp b/eeschema/sim/sim_plot_panel.cpp index d8d83ab684..5ae68acad0 100644 --- a/eeschema/sim/sim_plot_panel.cpp +++ b/eeschema/sim/sim_plot_panel.cpp @@ -76,7 +76,7 @@ static wxString formatSI ( double x, const wxString& unit, int decimalDigits, do if (maxValue != 0.0) rangeHit = fabs(maxValue) >= r_cur && fabs(maxValue) < r_cur * 1000.0 ; else - rangeHit = fabs(x) >= maxValue && fabs(x) < maxValue * 1000.0 ; + rangeHit = fabs(x) >= r_cur && fabs(x) < r_cur * 1000.0 ; if( (!lockSuffix && rangeHit) || (lockSuffix && suffix == powers[i].suffix ) ) { @@ -105,6 +105,7 @@ public: const wxString getLabel( int n ) { + printf("%.10f\n", m_labeledTicks[n] ); return formatSI ( m_labeledTicks[n], wxT("Hz"), 2 ); } }; @@ -270,7 +271,7 @@ SIM_PLOT_PANEL::SIM_PLOT_PANEL( SIM_TYPE aType, wxWindow* parent, wxWindowID id, m_axis_y1 = new mpScaleY( wxT( "noise [(V or A)^2/Hz]" ), mpALIGN_BORDER_LEFT ); break; -#endif + #endif case ST_TRANSIENT: m_axis_x = new TIME_SCALE( wxT( "Time" ), mpALIGN_BOTTOM ); @@ -340,6 +341,7 @@ bool SIM_PLOT_PANEL::AddTrace( const wxString& aSpiceName, const wxString& aName { TRACE* t = NULL; + // Find previous entry, if there is one auto prev = m_traces.find( aName ); bool addedNewEntry = ( prev == m_traces.end() ); @@ -353,17 +355,36 @@ bool SIM_PLOT_PANEL::AddTrace( const wxString& aSpiceName, const wxString& aName t = new TRACE_TRANSIENT( aName, aSpiceName ); break; case ST_AC: + //printf("makeFreqResp!\n"); t = new TRACE_FREQ_RESPONSE( aName, aSpiceName ); break; default: assert(false); } - assert(m_axis_x); - assert(m_axis_y1); + printf("points : %d\n", aPoints ); + + std::vector tmp(aY, aY + aPoints); + + if( m_type == ST_AC ) + { + if( aFlags & SPF_AC_PHASE) + { + for(int i = 0; i < aPoints; i++ ) + tmp[i] = tmp[i] * 180.0 / M_PI; + } else { + for(int i = 0; i < aPoints; i++ ) + tmp[i] = 20 * log( tmp[i] ) / log(10.0); + } + } + + t->SetData( std::vector( aT, aT + aPoints ), tmp ); + + if( aFlags & SPF_AC_PHASE ) + t->SetScale ( m_axis_x, m_axis_y2 ); + else + t->SetScale ( m_axis_x, m_axis_y1 ); - t->SetData( std::vector( aT, aT + aPoints ), std::vector( aY, aY + aPoints ) ); - t->SetScale ( m_axis_x, m_axis_y1 ); t->SetPen( wxPen( generateColor(), 2, wxSOLID ) ); m_traces[aName] = t; diff --git a/eeschema/sim/sim_plot_panel.h b/eeschema/sim/sim_plot_panel.h index 64921055ea..4eb44bc2fc 100644 --- a/eeschema/sim/sim_plot_panel.h +++ b/eeschema/sim/sim_plot_panel.h @@ -90,8 +90,14 @@ private: class TRACE : public mpFXYVector { public: - TRACE( const wxString& aSpiceName ) : - m_spiceName( aSpiceName ), m_cursor( nullptr ) {}; + TRACE( const wxString& aName, const wxString& aSpiceName ) : + mpFXYVector ( aName ), m_spiceName( aSpiceName ), m_cursor( nullptr ) + { + SetContinuity( true ); + SetDrawOutsideMargins( false ); + ShowName( false ); + + } const wxString& GetSpiceName() const { @@ -128,15 +134,13 @@ protected: CURSOR* m_cursor; }; -class TRACE_FREQ_RESPONSE : public TRACE, public mpFSemiLogXVector +class TRACE_FREQ_RESPONSE : public TRACE { public: - TRACE_FREQ_RESPONSE( const wxString& aName, const wxString& aSpiceName ) - : mpFSemiLogXVector( aName ), TRACE( aSpiceName ) + TRACE_FREQ_RESPONSE( const wxString& aName, const wxString& aSpiceName ) : + TRACE( aName, aSpiceName ) { - mpFSemiLogXVector::SetContinuity( true ); - mpFSemiLogXVector::SetDrawOutsideMargins( false ); - mpFSemiLogXVector::ShowName( false ); + printf("makeFreqResponse!\n"); } }; @@ -144,18 +148,16 @@ public: class TRACE_TRANSIENT : public TRACE { public: - TRACE_TRANSIENT( const wxString& aName, const wxString& aSpiceName ) : - TRACE( aSpiceName ) + TRACE_TRANSIENT( const wxString& aName, const wxString& aSpiceName ) : + TRACE( aName, aSpiceName ) { - mpFXYVector::SetName ( aName ); // hack - SetContinuity( true ); - SetDrawOutsideMargins( false ); - ShowName( false ); } }; - +enum SIM_PLOT_FLAGS { + SPF_AC_PHASE = 0x1 +}; class SIM_PLOT_PANEL : public mpWindow { diff --git a/eeschema/widgets/tuner_slider.cpp b/eeschema/widgets/tuner_slider.cpp index ee0e0cfc42..cdfdc3d357 100644 --- a/eeschema/widgets/tuner_slider.cpp +++ b/eeschema/widgets/tuner_slider.cpp @@ -36,6 +36,7 @@ TUNER_SLIDER::TUNER_SLIDER( SIM_PLOT_FRAME* aParent, SCH_COMPONENT* aComponent ) const wxString compName = aComponent->GetField( REFERENCE )->GetText(); m_name->SetLabel( compName ); m_value = SPICE_VALUE( aComponent->GetField( VALUE )->GetText() ); + m_changed = false; // Generate Spice component name char prim = NETLIST_EXPORTER_PSPICE::GetSpiceField( SPICE_PRIMITIVE, aComponent, 0 )[0]; @@ -142,6 +143,7 @@ void TUNER_SLIDER::onSliderChanged( wxScrollEvent& event ) m_value = m_min + ( m_max - m_min ) * SPICE_VALUE( m_slider->GetValue() / 100.0 ); updateValueText(); updateComponentValue(); + m_changed = true; } @@ -166,6 +168,7 @@ void TUNER_SLIDER::onValueTextEnter( wxCommandEvent& event ) { SPICE_VALUE newCur( m_valueText->GetValue() ); SetValue( newCur ); + m_changed = true; } catch( std::exception& e ) { @@ -192,5 +195,10 @@ void TUNER_SLIDER::onMinTextEnter( wxCommandEvent& event ) void TUNER_SLIDER::onSimTimer( wxTimerEvent& event ) { - wxQueueEvent( GetParent(), new wxCommandEvent( EVT_SIM_UPDATE ) ); + if(m_changed) + { + printf("Slider Ch\n"); + wxQueueEvent( GetParent(), new wxCommandEvent( EVT_SIM_UPDATE ) ); + m_changed = false; + } } diff --git a/eeschema/widgets/tuner_slider.h b/eeschema/widgets/tuner_slider.h index 05ae5e8b17..ecdc6185a3 100644 --- a/eeschema/widgets/tuner_slider.h +++ b/eeschema/widgets/tuner_slider.h @@ -94,6 +94,7 @@ private: SCH_COMPONENT* m_component; SPICE_VALUE m_min, m_max, m_value; + bool m_changed; }; #endif /* TUNER_SLIDER_H */ diff --git a/include/widgets/mathplot.h b/include/widgets/mathplot.h index dc08853270..c19fb485ba 100644 --- a/include/widgets/mathplot.h +++ b/include/widgets/mathplot.h @@ -728,7 +728,16 @@ public: m_minV = std::min(minV, m_minV); m_maxV = std::max(maxV, m_maxV); } + + printf("Extend min %.10f max %.10f\n", m_minV, m_maxV); + + if (m_minV == m_maxV) + { + m_minV = -1.0; + m_maxV = 1.0; } + } + double AbsMaxValue() const { @@ -1503,6 +1512,8 @@ protected: }; +#if 0 + class WXDLLIMPEXP_MATHPLOT mpFSemiLogXVector : public mpFXYVector { public: @@ -1520,6 +1531,7 @@ class WXDLLIMPEXP_MATHPLOT mpFSemiLogXVector : public mpFXYVector DECLARE_DYNAMIC_CLASS(mpFSemiLogXVector) }; +#endif //----------------------------------------------------------------------------- // mpText - provided by Val Greene