From ac17165947694828ab1ba3e80d38be4a28d42ca8 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Thu, 11 Aug 2016 14:41:11 +0200 Subject: [PATCH] Initial version of simulator probe tool --- eeschema/onleftclick.cpp | 29 +++++++++++++++++++++++++++++ eeschema/schedit.cpp | 4 ++++ eeschema/schframe.cpp | 2 +- eeschema/schframe.h | 1 - eeschema/sim/ngspice.cpp | 4 +++- eeschema/sim/sim_plot_frame.cpp | 33 ++++++++++++++++++++++++++++++--- eeschema/sim/sim_plot_frame.h | 4 +--- eeschema/sim/simulate.cpp | 5 ----- 8 files changed, 68 insertions(+), 14 deletions(-) diff --git a/eeschema/onleftclick.cpp b/eeschema/onleftclick.cpp index 1f4f760ca1..114b2266e3 100644 --- a/eeschema/onleftclick.cpp +++ b/eeschema/onleftclick.cpp @@ -28,9 +28,11 @@ */ #include +#include #include #include #include +#include #include #include @@ -43,6 +45,8 @@ #include #include #include + +#include #include // fo class SCHLIB_FILTER to filter power parts @@ -321,6 +325,31 @@ void SCH_EDIT_FRAME::OnLeftClick( wxDC* aDC, const wxPoint& aPosition ) } break; + case ID_SIM_ADD_PROBE: + { + const KICAD_T wiresAndComponents[] = { SCH_LINE_T, SCH_COMPONENT_T, SCH_SHEET_PIN_T }; + item = LocateAndShowItem( aPosition, wiresAndComponents ); + + if( !item ) + break; + + NETLIST_OBJECT_LIST* netlist = BuildNetListBase(); + + for( NETLIST_OBJECT* obj : *netlist ) + { + if( obj->m_Comp == item ) + { + SIM_PLOT_FRAME* simFrame = (SIM_PLOT_FRAME*) Kiway().Player( FRAME_SIMULATOR, false ); + + if( simFrame ) + simFrame->AddVoltagePlot( obj->GetNetName() ); + + break; + } + } + } + break; + default: SetToolID( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor(), wxEmptyString ); wxFAIL_MSG( wxT( "SCH_EDIT_FRAME::OnLeftClick invalid tool ID <" ) + diff --git a/eeschema/schedit.cpp b/eeschema/schedit.cpp index 059dbf2b7a..0784edc47e 100644 --- a/eeschema/schedit.cpp +++ b/eeschema/schedit.cpp @@ -609,6 +609,10 @@ void SCH_EDIT_FRAME::OnSelectTool( wxCommandEvent& aEvent ) SetToolID( id, wxCURSOR_BULLSEYE, _( "Delete item" ) ); break; + case ID_SIM_ADD_PROBE: + SetToolID( id, wxCURSOR_BULLSEYE, _( "Add a simulator probe" ) ); + break; + default: SetRepeatItem( NULL ); } diff --git a/eeschema/schframe.cpp b/eeschema/schframe.cpp index 079d2c0654..eb46bda90b 100644 --- a/eeschema/schframe.cpp +++ b/eeschema/schframe.cpp @@ -266,7 +266,6 @@ BEGIN_EVENT_TABLE( SCH_EDIT_FRAME, EDA_DRAW_FRAME ) EVT_TOOL( ID_UPDATE_PCB_FROM_SCH, SCH_EDIT_FRAME::OnUpdatePCB ) EVT_TOOL( ID_SIM_RUN, SCH_EDIT_FRAME::OnSimulationRun ) EVT_TOOL( ID_SIM_STOP, SCH_EDIT_FRAME::OnSimulationStop ) - EVT_TOOL( ID_SIM_ADD_PROBE, SCH_EDIT_FRAME::OnSimulationAddProbe ) EVT_TOOL( ID_GET_TOOLS, SCH_EDIT_FRAME::OnCreateBillOfMaterials ) EVT_TOOL( ID_FIND_ITEMS, SCH_EDIT_FRAME::OnFindItems ) EVT_TOOL( wxID_REPLACE, SCH_EDIT_FRAME::OnFindItems ) @@ -282,6 +281,7 @@ BEGIN_EVENT_TABLE( SCH_EDIT_FRAME, EDA_DRAW_FRAME ) EVT_TOOL( ID_ZOOM_SELECTION, SCH_EDIT_FRAME::OnSelectTool ) EVT_TOOL_RANGE( ID_SCHEMATIC_VERTICAL_TOOLBAR_START, ID_SCHEMATIC_VERTICAL_TOOLBAR_END, SCH_EDIT_FRAME::OnSelectTool ) + EVT_TOOL( ID_SIM_ADD_PROBE, SCH_EDIT_FRAME::OnSelectTool ) EVT_MENU( ID_CANCEL_CURRENT_COMMAND, SCH_EDIT_FRAME::OnCancelCurrentCommand ) EVT_MENU( ID_SCH_DRAG_ITEM, SCH_EDIT_FRAME::OnDragItem ) diff --git a/eeschema/schframe.h b/eeschema/schframe.h index 0cac05e445..61a909bd7a 100644 --- a/eeschema/schframe.h +++ b/eeschema/schframe.h @@ -815,7 +815,6 @@ private: void OnUpdatePCB( wxCommandEvent& event ); void OnSimulationRun( wxCommandEvent& event ); void OnSimulationStop( wxCommandEvent& event ); - void OnSimulationAddProbe( wxCommandEvent& event ); void OnCreateBillOfMaterials( wxCommandEvent& event ); void OnFindItems( wxCommandEvent& event ); void OnFindDialogClose( wxFindDialogEvent& event ); diff --git a/eeschema/sim/ngspice.cpp b/eeschema/sim/ngspice.cpp index a927db5d93..06ddc6883b 100644 --- a/eeschema/sim/ngspice.cpp +++ b/eeschema/sim/ngspice.cpp @@ -67,8 +67,10 @@ const vector NGSPICE::GetPlot( const string& aName, int aMaxLen ) vector_info* vi = m_ngGet_Vec_Info( (char*) aName.c_str() ); - if( vi->v_realdata ) + if( vi && vi->v_realdata ) { + data.reserve( vi->v_length ); + for( int i = 0; i < vi->v_length; i++ ) data.push_back( vi->v_realdata[i] ); } diff --git a/eeschema/sim/sim_plot_frame.cpp b/eeschema/sim/sim_plot_frame.cpp index e55876e5d6..94c219b56d 100644 --- a/eeschema/sim/sim_plot_frame.cpp +++ b/eeschema/sim/sim_plot_frame.cpp @@ -98,7 +98,6 @@ SIM_PLOT_FRAME::SIM_PLOT_FRAME( KIWAY* aKiway, wxWindow* aParent ) { m_exporter = NULL; m_simulator = NULL; - m_pyConsole = NULL; m_simThread = NULL; Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( SIM_PLOT_FRAME::onClose ), NULL, this ); @@ -126,6 +125,7 @@ void SIM_PLOT_FRAME::StartSimulation() m_simConsole->Clear(); + /// @todo is it necessary to recreate simulator every time? m_simulator = SPICE_SIMULATOR::CreateInstance( "ngspice" ); m_simulator->SetConsoleReporter( new SIM_THREAD_REPORTER( this ) ); m_simulator->Init(); @@ -201,6 +201,32 @@ void SIM_PLOT_FRAME::NewPlot() } +void SIM_PLOT_FRAME::AddVoltagePlot( const wxString& aNetName ) +{ + if( !m_exporter ) + return; + + const auto& netMapping = m_exporter->GetNetIndexMap(); + + if( netMapping.count( aNetName ) == 0 ) + return; + + wxString spiceName( wxString::Format( "V(%d)", netMapping.at( aNetName ) ) ); + auto data_y = m_simulator->GetPlot( (const char*) spiceName.c_str() ); + + wxLogDebug( "probe %s", spiceName ); + + if( data_y.empty() ) + return; + + auto data_t = m_simulator->GetPlot( "time" ); + + wxLogDebug( "%lu - %lu data points", data_t.size(), data_y.size() ); + SIM_PLOT_PANEL* plotPanel = static_cast( m_plotNotebook->GetCurrentPage() ); + plotPanel->AddTrace( aNetName, data_t.size(), data_t.data(), data_y.data(), 0 ); +} + + void SIM_PLOT_FRAME::onClose( wxCloseEvent& aEvent ) { { @@ -258,8 +284,8 @@ void SIM_PLOT_FRAME::onSimFinished( wxThreadEvent& aEvent ) m_signals->Append( net.first ); } - // auto data_t = m_simulator->GetPlot( "time" ); - +// TODO remove? +#if 0 for( auto& name : m_exporter->GetProbeList() ) { char spiceName[1024]; @@ -271,4 +297,5 @@ void SIM_PLOT_FRAME::onSimFinished( wxThreadEvent& aEvent ) //wxLogDebug( "%d - %d data points\n", data_t.size(), data_y.size() ); // m_plotPanel->AddTrace(wxT("V(") + name + wxT(")"), data_t.size(), data_t.data(), data_y.data(), 0); } +#endif } diff --git a/eeschema/sim/sim_plot_frame.h b/eeschema/sim/sim_plot_frame.h index d3a12f6706..79b8c91dc6 100644 --- a/eeschema/sim/sim_plot_frame.h +++ b/eeschema/sim/sim_plot_frame.h @@ -60,8 +60,7 @@ class SIM_PLOT_FRAME : public SIM_PLOT_FRAME_BASE void StopSimulation(); void NewPlot(); - - void TogglePythonConsole(); + void AddVoltagePlot( const wxString& aNetName ); private: virtual void onClose( wxCloseEvent& aEvent ); @@ -72,7 +71,6 @@ class SIM_PLOT_FRAME : public SIM_PLOT_FRAME_BASE SCH_EDIT_FRAME* m_schematicFrame; NETLIST_EXPORTER_PSPICE* m_exporter; SPICE_SIMULATOR* m_simulator; - wxWindow* m_pyConsole; SIM_THREAD* m_simThread; wxCriticalSection m_simThreadCS; diff --git a/eeschema/sim/simulate.cpp b/eeschema/sim/simulate.cpp index c499061827..0880bce572 100644 --- a/eeschema/sim/simulate.cpp +++ b/eeschema/sim/simulate.cpp @@ -50,8 +50,3 @@ void SCH_EDIT_FRAME::OnSimulationRun( wxCommandEvent& event ) void SCH_EDIT_FRAME::OnSimulationStop( wxCommandEvent& event ) { } - - -void SCH_EDIT_FRAME::OnSimulationAddProbe( wxCommandEvent& event ) -{ -}