Home › Forum › SOFA › Using SOFA › [SOLVED] Read data from external file
Tagged: 64_bits, Linux_ubuntu, SOFA_1806
- This topic has 11 replies, 3 voices, and was last updated 3 years, 11 months ago by nhnhan.
-
AuthorPosts
-
19 January 2019 at 11:26 #12799fatimaBlocked
Hi again,
I successfully built SOFA and added SoftRobots plugin. Now I want parallel Simulink-Matlab with Sofa. For this purpose, I should get the pressure from Matlab or a file which is created by Matlab, But I don’t know how.
In this code instead of value=”8″, I want to read the value from file.
cavity.createObject('SurfacePressureConstraint', template='Vec3d', name="pressure", triangles='@topo.triangles', valueType="1", valueFilename="8")
Thank you so much for your attention 🙂
Best,
Fatemeh21 January 2019 at 21:50 #12854Damien MarchalBlockedHi fatima,
On my side I would do something like saving the file from matlab in the “json” file format and read it from python.
Example of a ‘file.json’ containing some 3d values like that:
{ "values" : [[1.0, 1.0, 1.0], [2.0, 2.0, 2.0], [3.0, 3.0, 3.0]] }
The logic is similar for 1D values or other SurfacePressureConstraint in place of the MechanicalObject.
You can load it in python and using it in Sofa using the standard python loader:
import json def createScene(rootNode): '''Create a sofa scene showing three 'points' with position read from a json file''' data = json.load(open("file.json")) v = rootNode.createObject("MechanicalObject", position=data["values"]) v.showObject = True v.showObjectScale = 10.0
Regards,
Damien23 January 2019 at 11:07 #12868fatimaBlockedHi Damien,
Thank you so much for your response. My problem is solved 🙂
Just one more question if I use this solution, can I control an actuator which is simulated by SOFA real-time?Best regards,
Fatima29 January 2019 at 13:23 #12930Damien MarchalBlockedHello Fatima,
The solution I’m proposing is to initialize the object during the creation of the simulated scene.
If you want to interactively change the position of the MechanicalObject
you need to use a PythonScriptController. This PythonScriptController will at each frame update the position of the sofa object using data coming from an external source (a file, a ros topic, a zeromq message, whatever).To give you an idea it would look like the following:
import json import Sofa class MyController(Sofa.PythonScriptController): def __init__(self, node, dofs): '''With SofaPython it is mandatory to pass at least one argument that is the node. not doing that will crash Sofa ''' self.dofs = dofs def onBeginAnimationStep(self, dt): '''This method is called automatically by Sofa at each timestep''' data = json.load(open("file.json")) self.dofs.position = data["values"] def createScene(rootNode): '''Move an object in real-time by reading new values from a python file''' data = json.load(open("file.json")) v = rootNode.createObject("MechanicalObject", name="dofs", position=data["values"]) v.showObject = True v.showObjectScale = 10.0 MyController(rootNode, v)
Damien
NB: updating position that w3 February 2019 at 19:57 #12979fatimaBlockedDear Damien(@damien-marchaluniv-lille1-fr),
Thank you so much for your response, I understood your point and used the PythonScriptController for controlling the actuator real-time. For reading the data from Matlab real-time, I could not use a file. So I tried to send and receive data by TCP/IP connection. But I got an error which connection refused.
Please guide me how to solve this problem.Kind regards,
Fatima3 February 2019 at 20:40 #12980Damien MarchalBlockedDear Fatima.
Using TCP/IP is fine but there is more easy to use protocols. In our team when we want to exchange data that way we are using zeromq (http://zeromq.org/) or OSC (http://opensoundcontrol.org/introduction-osc) because they have good python binding and thus work very well with Sofa. But i ignore if there is way to use that in matlab, a quick search on a search engine seems to say so: (eg: https://www.qwant.com/?q=matlab%20zeromq&t=web)
Hope this helps
Regards,
Damien19 February 2019 at 17:46 #13079fatimaBlockedDear Damien(@damien-marchaluniv-lille1-fr),
Thank you so much for your help, I solved the problem with your advice.
I just have one more question, Can I export the stiffness matrix in each step in a file or print it in terminal?Please guide me again to solve this problem.
Kind regards,
Fatima19 February 2019 at 19:17 #13081Damien MarchalBlockedHi,
I don’t know which forcefield you are using but if the stiffness matrix of the force field you are using is exposed as a Data then yes it can be accessed at each time step using a PythonScriptController.
Eg taken from the previous python script controller
def onBeginAnimationStep(self, dt): '''This method is called automatically by Sofa at each timestep''' # Print the "position" Data field of the 'dofs' object. print(self.dofs.position) # Assuming there is a forcefield with a stiffness matrix exposed as Data named 'stiffnessMatrix' print(self.forcefield.stifnessMatrix)
Now if the forcefield you are using does not have such data field…then you may consider adding it by adding it in C++ and making a PR. Please tells us if you are planning to do so as we may help in digging into the c++ code.
Good lucks,
Damien20 February 2019 at 07:59 #13087fatimaBlockedDear Damien(@damien-marchaluniv-lille1-fr),
Thank you so much for your attention, I use the TetrahedronFEMForceField and I got this error when adding the line you said in PythonScriptController:
[ERROR] [PythonScript] RuntimeError: object ‘TetrahedronFEMForceField’ does no have a field ‘stifnessMatrix’;
Could you explain to me how can add this field to TetrahedronFEMForceField? or should I use other forcefields?
Kind Regards,
Fatima20 February 2019 at 20:38 #13095Damien MarchalBlockedHi Fatima,
I just looked at and TetrahedronFEMForceField does not expose the stiffnessMatrix so it is not surprising it does not work.
Adding a new Data to a component is not hard, assuming you know c++ and that the data you want to expose is available in the component. I don’t know for TetrahedronFEMFOrceField so I advice you to open an issue on that topic on github: https://github.com/sofa-framework/sofa/issues
This is where the Sofa developers are.
Damien.
23 February 2019 at 07:28 #13111fatimaBlockedHi Damien,
Thank you so much for helping me. your answers are so useful 🙂
Best wishes,
Fatima9 December 2020 at 02:38 #17978nhnhanBlocked@fsharifi135, @damien-marchaluniv-lille1-fr
Hi, sorry for the sudden contacting. I read your post in SOFA community forum which mentioned a topic that I’m interested in regarding exporting stiffness matrix. I have been trying different solutions to do such things but no significant progress occur so far. As far as I know, only HexaheronForceField exposes stiffness matrix in its data field, so that we can extract by using python scripting easily. However, for other forcefields such as tetraFEMForceField are not the case. Do you have any ideas or updates so far regarding this issue??? Thank you in advance
Best,
Nhan -
AuthorPosts
- You must be logged in to reply to this topic.