2 April 2020 at 22 h 02 min #15636stonkensParticipant
- Stanford University
I have been working with SOFA for soft robotic control for the last couple of months and have made extensive use of the PythonScriptController, expanding upon the examples in the Soft Robots plugin.
However, I recently noticed a sofapython3 distribution (https://sofapython3.readthedocs.io/en/latest/menu/install.html) and was wondering whether this could be used with the soft robots plugin? If so, would it be possible to give me a quick run-down of what the differences are and how it works?
Specifically, I interface with SOFA as follows:
– def initGraph(self, node)
– def onBeginAnimationStep(self, dt)
– def onEndAnimationStep(self, dt)
– def onKeyPressed(self, key)
To access data from the scene I use e.g. np.fromstring(str(finger.getObject(‘tetras’).findData(‘position’)), sep=’ ‘)
To change the value of actuators I use e.g.:
Thanks so much!3 April 2020 at 23 h 02 min #15642Damien MarchalParticipant
- CNRS/Defrost Team
SofaPython3 can be used without problem with SoftRobots but the examples as well as the STLIB have not been updated so they will not work anymore.
The big difference are in how to implement the ScriptController (because we have extended it a lot) and how to access the data fields. There is no initGraph anymore, in general the work done in this function is not either in the python constructor or in createScene function (or an utility function).
Here is a quick and dirty example (I d’ont have python3 right now so I didn’t test, I hope it will work, otherwise tell me so that I made fresh install to fix it ;))
import Sofa.Core class EmptyController(Sofa.Core.Controller): """ custom %EmptyController% component for SOFA """ def __init__(self, *args, **kwargs): Sofa.Core.Controller.__init__(self, *args, **kwargs) #needed self.finger=kwargs.get("target", None) def init(): pass # DEFAULT EVENTS: def onAnimateBeginEvent(self, event): """ called at the beginning of each time step """ print("AnimatedBegin", event) def onAnimateEndEvent(self, event): """ called at the end of each time step """ print("AnimatedEnd", event) def onKeypressedEvent(self, event): print("Key pressed") a = self.finger.tetra.position.value ### a is a numpy array with a read only access print(a) b = a * 2 print(b) self.finger.tetra.position.value = b ### the value is now squared print(self.finger.tetra.position.value) print(a) with self.finger.tetra.position.writeableArray() as wa: wa += a wa *= 3 print(wa) def onEvent(self, name): print("Event received with name:", name ) def createScene(root): root.addObject("MechanicalObject", name="dofs", positions=[0,0,0,1,1,1,2,2,2]) root.addObject(EmptyController(target=root.dofs))
I’m sorry the documentation does not auto-regenerate on readthedocs (for reason I don’t understand..I need to find time to investigate that).
Hope this helped.5 February 2021 at 11 h 54 min #18531HugoKeymaster
- SOFA Consortium
- You must be logged in to reply to this topic.