Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ac simulation #4076

Draft
wants to merge 45 commits into
base: develop
Choose a base branch
from
Draft

Ac simulation #4076

wants to merge 45 commits into from

Conversation

failiz
Copy link
Contributor

@failiz failiz commented Oct 10, 2023

I have been working a few weeks on this and it is time for feedback. I do not want to keep working without knowing that I am going in the right direction and that the code will be merged in the future.

To use this branch, you also need the pull request from the fritzing-parts repository, which contains the oscilloscope, pulse generators and others.

In general, I think it is working quite well. Even long simulations (20s) are quite fast (I have also been working with simple circuits). To use it, you just need to add an oscilloscope and the simulator will simulate a transitory analysis instead a operation point analysis. The transitory analysis simulates until the maximum time that can be shown in any of the oscilloscopes (calculated based on the horizontal position of the oscilloscope and the horizontal scale: time/division). From the time that needs to be shown (in any of the oscilloscopes) to the end time, the simulator generates a fix amount of data (now it is set to 400 SimSteps). This defines the simulation step size. After the simulator is performed, the oscilloscope signals are retrieved and plotted. The status of all the components is shown as at the end of the simulation (LED lights, smokes, etc.).

TODO:

  • Generate noise when a wire is connected to the oscilloscope, but not connected to any other element.
  • Fix issues when the GND is not found. Not finding the ground could cause bad results in the simulator.
  • Create more wavegenerators. I am planning: sine, sawtooth, single pulse, generic pulse, PWM, square. Later, we can add the rest of the buttons of the wavegenerator
  • Wave generators need specific icons. The bb image is too big to be used as an icon.
  • Now the power supplies will not be sch only parts as there will be bb parts too. Decide where to place them. I propose to add them as section wavegenerators in the sim and the core.
  • Create a wavegenerator family? Now, I am using the power supply, which allows to swap with dc power supplies and brakes the wiring.
  • Add an option in edit->beta features->simulator to allow to set the max points of the simulation or the step time of the simulation. It could be that a circuit cannot be simulated without reducing the timestep.
  • Add animation of the transitory circuit (LEDs lighting and signal appearing in oscilloscope) and show sim time.
  • Probably not for now, but add switches driven by a power source. (Basically changes the resistance of the connection depending on the value of the input). This will allow to simulate m
  • Add more examples:
    • NAND gates
    • counters
    • SR flip-flops
    • 555
    • full wave rectifier
  • Add the current examples to the list of examples (so, they appear as examples)
  • Homogenize design: The oscilloscope, wavegenerator, dc power supply (made by @vanepp , not sure if he is making a new one more standard) are a bit different between them. It would be great if someone tries to make them to look nicer (same style (color line thickness, etc.), now mine are flat and not in perspective). This is much easier now that there are not published and we cna change them without obsolete them
  • Probably for later: add a control bar to set the time and show the components at that specific time (LED lights, etc.)
  • Probably for later: improve the check of the componets. If a component is damaged at anytime, the component should have a smoke symbol from that moment.
  • Fix issue when there are several oscilloscopes and one has a big time/div and the other a small one

@DanielBarie
Copy link

This is great work. Thank you!
Highly useful for teaching basic circuits/electronics. From a teaching perspective, I'd love to see that integrated in a future release.

@failiz
Copy link
Contributor Author

failiz commented Jan 30, 2024

This is great work. Thank you! Highly useful for teaching basic circuits/electronics. From a teaching perspective, I'd love to see that integrated in a future release.

Thanks @DanielBarie ! We are trying to merge it into the official code, but it could take a while. Please, feel free to give feedback on the branch. :-)
Great work documenting the building process in https://github.com/DanielBarie/FritzingBuild. I am curious, why is the battery damaged in the second picture? It seems that the voltage is set to 5V and the resistor is 220ohm. The battery should get damaged when there is a short circuit and the current is bigger than 30A (if I remember correctly).

@DanielBarie
Copy link

Thank you. That's excellent news.
As to that battery: I was suprised to see that smoke, too. 220 Ohms shouldn't lead to battery overload. Haven't had the time to figure it out, but it was a nice (if surprising) demonstration of the simulator...

@DanielBarie
Copy link

I just found your paper (Learning Hands-On Electronics from Home:
A Simulator for Fritzing). Even better. I wish I had known about it much earlier.

failiz added 25 commits February 6, 2024 12:20
Add instructions to be able to submit sketches
…spice (if not the prefix is ignored). There are two u symbols in unicode and only one of them was being replaced.
…nterp option to interpolate results in tran analysis.
…o signals and dispay volt/s and time/s in oscilloscope in BB view
… is no ground symbol, the simulator cannot find the ground. This creates problems when requesting voltages at node 0, as ngspice cannot return the voltage at node 0. This commit fixes this by trying to find an isolated negative power supply if it has not found one before.
…is necessary to allow setting negative offsets in the oscilloscope part
…g time. This shows the LED, multimeters and others components at the max simulated time
…lating as the oscilloscope part was modified (screen is now balck and the screen is a bit smaller to allow us to place labels and axes)
…y is not found when generating the spice netlist.
… details of the error and the details can be copied to the clipboard. Before the dialog height was bigger than the screen height for long netlists. The Dialog is based on QBoxMessage instead of FBoxMessage.
…perties and added first example with transformer
…ol, the pattern of the validator becomes invalid
…nnected to it. Return a vector with 0s instead of an empty vector (in case that we ask for the voltage of the ground)
@KjellMorgenstern
Copy link
Member

I tried this today. The capacitors in the in the FlipFlop example get a negative voltage and blow up. A real world elco might survive the small voltage.
image

I flipped them without further thoughts (not 100% sure if that is the intended circuit):
image

Capacitors don't blow up, and the DC analysis ends up (randomly) in one state of the flipflop.

It looks like the transient analysis it not affected by this change, which is a bit surprising. Does it calculate the capacitor charge regardless of polarity ?

@@ -69,7 +69,7 @@ Simulator::Simulator(MainWindow *mainWindow) : QObject(mainWindow) {

QSettings settings;
int enabled = settings.value("simulatorEnabled", 0).toInt();
enable(enabled);
enable(true);
Copy link
Member

@KjellMorgenstern KjellMorgenstern Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is now save to remove that setting entirely. If there are no objections, I'll to that for 1.0.3

While on it, will it make sense to have a switch between DC and transient analysis?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am OK with removing the enable simulator setting.

}
}


Copy link
Member

@KjellMorgenstern KjellMorgenstern Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably answers my above question.
However there might be many use cases for transient analysis without an oscilloscope.
We could add a little arrow on the play button, like many other buttons in the menu bar have:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as soon as there is an oscilloscope, the transitory analysis is used. Before the trans analysis only made sense with the oscilloscope as there was no animation. Since I added the animation, users could use the LEDs in the breadboard to see the effects on their circuits.
So, yes, maybe it is a good idea to add the trans analysis as an option in the simulate button. However, in that case, how do we specify the simulation time?
Could you implement the button? The last time I played with adding buttons took me ages to make it correctly.


std::cout << std::endl;
return svg;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this and other methods might be better placed in items/oscilloscope.cpp or similar.
So, we'd create an Oscilloscope class, that can display probe and comVectors ... in the future it could also hold code to act on buttons and knobs.

Is there a specific reason it has to be here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I also thought about that several times. I can create an oscilloscope item and move that code there.

@failiz
Copy link
Contributor Author

failiz commented Feb 16, 2024

@KjellMorgenstern , regarding the electrolytic capacitors in the example (flipflop_led_flashing_circuit), I took this circuit from internet. Simulation says that they get -0.76V and that is why they show smoke. If I remember correctly I set a threshold of 0.5V to consider it a failure. I have to try this in reality. Probably, we could just increase the threshold to avoid the smoke. I think that less than 1V is OK.
The spice simulator only has ideal capacitors and they do not have polarity. We just make them electrolytic by showing smoke if the voltage is in reverse and above the threshold.
In any case, I can see that you wired the BB view in the modification (thanks!) but I do not see that you turned them around. And I do not think they should be turned around as the voltage reaches around 6V and that definitely will destroy the capacitor.

…d now internal resistance can be changed in power supplies
…ile sim analysis is still on going. Adapt the the speed of the animation to the user´s value if the animation takes too long.
… lab power supplies. Add max current as property to change in lab power supplies. Added behavioral sources as valid spice type.

WARNING: lab power supplies need lo load the analog code mode.. Right now, the code mode should be in the CWD as the executable. TODO: Check for several paths to find the code models.
void updatePotentiometer(unsigned long, ItemBase *);
void updateDcMotor(unsigned long, ItemBase *);
void updateIRSensor(unsigned long, ItemBase *);
void updateBattery(unsigned long, ItemBase *);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please set your editor to use single tabs, not fours spaces, for indentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done:
image

@failiz
Copy link
Contributor Author

failiz commented Sep 8, 2024

Will you replace all the spaces to tabs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants