petercorke / bdsim

Simulate dynamic systems expressed in block diagram form using Python
MIT License
174 stars 33 forks source link

Example of integrator of hybrid system on a Python project #2

Open traversaro opened 3 years ago

traversaro commented 3 years ago

Hi @petercorke, thanks a lot for sharing such a tool, it seems to be quite interesting.

I saw that in the README it is written that:

The biggest is that bdsim is based on a very standard variable-step integrator from the scipy library. For discontinuous inputs (step, square wave, triangle wave, piecewise constant) the transitions get missed. This also makes it inaccurate to simulate hybrid discrete-continuous time systems. We really need a better integrator, perhaps odedc from SciLab could be integrated.

Unfortunately I do not have a lot of time on working on this, but if anyone is interested in working on this, a good example of an integration of an advanced integrator that support for example hybrid system can be found in the FMPy project. In particular, the fmpy.sundials.cvode contains a Python interface to the Sundials' CVODE integrator. An example of use of such interface for integrating a hybrid system (bouncing ball) can be found in https://github.com/CATIA-Systems/FMPy/blob/e1927a128a9552f20304587c54c716750e73b05f/tests/test_cvode.py .

petercorke commented 3 years ago

Thanks for your interest, yours is the first comment!

I'm happy to work with anybody who'd like to contribute something in this area. I've got a few ideas sketched out about how I can use SciPy to do this better, but this project is on the back burner at the moment while I get the robotics and vision toolboxes up to a good level of maturity.

sawyerbfuller commented 3 years ago

Dear Peter, great piece of software. We recently introduced a similar feature in python-control called interconnect that uses a slightly different approach that entails naming signals. Your approach has the nice attribute that the code appears very simple. We will be keeping an eye on this!

I read with some interest your remark that python is still in need of an integrator for hybrid discrete-continuous systems; we are definitely in need of such a feature for python-control. Thanks @traversaro for the pointer.

petercorke commented 3 years ago

Hi @sawyerbfuller, thanks for contacting me. I think I looked at interconnect a while back, and I think there’s some interesting things we could do together. Quite complementary functionality.

Some students have been developing a GUI for me, see the attached. The model is saved in JSON and I can build a bdsim model from that, and run it. Still a work in progress quite useful I think.

I’ve added rudimentary hybrid system support which is ok for simple cases, but I’d like a stronger solution for that.

peter

On 19 Aug 2021, at 7:11 am, sawyerbfuller @.***> wrote:

Dear Peter, great piece of software. We recently introduced a similar feature in python-control called interconnect https://python-control.readthedocs.io/en/0.9.0/generated/control.interconnect.html?highlight=interconnect that uses a slightly different approach that entails naming signals. Your approach has the nice attribute that the code appears very simple. We will be keeping an eye on this!

I read with some interest your remark that python is still in need of an integrator for hybrid discrete-continuous systems; we are definitely in need of such a feature for python-control. Thanks @traversaro https://github.com/traversaro for the pointer.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/petercorke/bdsim/issues/2#issuecomment-901433037, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC2BIURTD47O2BS4R4N53HDT5QOWJANCNFSM4UKY5TBQ.

sawyerbfuller commented 3 years ago

Hi @petercorke looks like maybe github stripped the attachment? You can email it directly to me if easier at minster@uw.edu .

Very interested in your gui work, because not having a simulink-like gui -- and/or a hybrid integrator we can use with interconnect -- has been a main limitation for students using python-control vs matlab in the controls class I have been teaching.

petercorke commented 1 year ago

The Scipy solve_ivp integrator has event handling which might be usable. Would need to add a guard method to each block that throws events and pass a list of these to the solver.

sawyerbfuller commented 1 year ago

Looks like the docstrings in solve_ivp have been updated to clarify something that I had been confused by before: that setting t_eval seemed to have no effect on the points that were evaluated. Now it is clear that its purpose is only to specify which points are returned.

That said, indeed it looks like events could be used to specify time points to make sure to evaluate. But, it seems like specifiying evenly-spaced events would go through a quite roundabout process in the integrator if I understand correctly. You would need to specify a function that has repeated zero-crossings - the obvious choice being perhaps a sinusoid that crossed zero at every dt of each discrete-time system? And then the integrator would go and do a slow, interative zero-crossing search for each of those, rather than just evaluating at points you already knew ahead of time. This seems slow but better than nothing.