Home › Forum › SoftRobots › Programming with SoftRobots › [SOLVED] Modifying The Trunk Example
Tagged: 64_bits, Linux_ubuntu, SOFA_1906, SoftRobots Plugin
- This topic has 1 reply, 1 voice, and was last updated 3 years, 7 months ago by Abdelrahman.
-
AuthorPosts
-
16 February 2021 at 02:52 #18680AbdelrahmanBlocked
Hello all
I’m trying to modify the Trunk example to create data, I added a python controller script and it works fine to some point. I initialize the data I want to save in the initGraph function of the controller as pointed in the attached code and collecting the data in the onEndAnimationStep function. I tried to save data in the onKeyPressed function but it won’t save, also tried to save it from the onEndAnimationStep for example every 100 count of dt but also the same. Sorry for the long code but I would appreciate it if you could point me what is missing here. I’m using the compiled version of SOFA with the SoftRobots. Thanks.
#!/usr/bin/env python # -*- coding: utf-8 -*- import Sofa import math import csv import os class controller(Sofa.PythonScriptController): def initGraph(self, node): print '### controller called ###' self.node = node self.Simulation=self.node.getChild("Simulation") self.Trunk=self.Simulation.getChild("Trunk") # Get the cables from the parent Trunk self.cableL0=self.Trunk.getChild('cableL0') self.cableL1=self.Trunk.getChild('cableL1') self.cableL2=self.Trunk.getChild('cableL2') self.cableL3=self.Trunk.getChild('cableL3') self.cableS0=self.Trunk.getChild('cableS0') self.cableS1=self.Trunk.getChild('cableS1') self.cableS2=self.Trunk.getChild('cableS2') self.cableS3=self.Trunk.getChild('cableS3') # you can access the object through its name self.cableL0Acutator = self.cableL0.getObject('cable') self.cableL1Acutator = self.cableL1.getObject('cable') self.cableL2Acutator = self.cableL2.getObject('cable') self.cableL3Acutator = self.cableL3.getObject('cable') self.cableS0Acutator = self.cableS0.getObject('cable') self.cableS1Acutator = self.cableS1.getObject('cable') self.cableS2Acutator = self.cableS2.getObject('cable') self.cableS3Acutator = self.cableS3.getObject('cable') self.Effector = self.Trunk.getChild('Effectors') self.endEffector = self.Effector.getObject('myEndEffector') self.dataRows = [] self.dataRows.append(['cableL0Length','cableL0Disp', 'cableL1Length', 'cableL1Disp', 'cableL2Length', 'cableL2Disp', 'cableL3Length', 'cableL3Disp', 'cableS0Length', 'cableS0Disp', 'cableS1Length', 'cableS1Disp', 'cableS2Length', 'cableS2Disp', 'cableS3Length', 'cableS3Disp', 'effectorPosition']) self.incr = 0 def onEndAnimationStep(self, dt): self.dt = self.node.findData('dt').value self.incr += self.dt #cableL0LengthIO = SofaPython.Tools.ComponentDataIO(self.cableL0Acutator, ["cableLength"]) # values required [displacement] [cableLength] cableL0Length = self.cableL0Acutator.findData('cableLength').value cableL1Length = self.cableL1Acutator.findData('cableLength').value cableL2Length = self.cableL2Acutator.findData('cableLength').value cableL3Length = self.cableL3Acutator.findData('cableLength').value cableS0Length = self.cableS0Acutator.findData('cableLength').value cableS1Length = self.cableS1Acutator.findData('cableLength').value cableS2Length = self.cableS2Acutator.findData('cableLength').value cableS3Length = self.cableS3Acutator.findData('cableLength').value cableL0Disp = self.cableL0Acutator.findData('displacement').value cableL1Disp = self.cableL1Acutator.findData('displacement').value cableL2Disp = self.cableL2Acutator.findData('displacement').value cableL3Disp = self.cableL3Acutator.findData('displacement').value cableS0Disp = self.cableS0Acutator.findData('displacement').value cableS1Disp = self.cableS1Acutator.findData('displacement').value cableS2Disp = self.cableS2Acutator.findData('displacement').value cableS3Disp = self.cableS3Acutator.findData('displacement').value effectorPosition = self.endEffector.findData('position').value self.dataRows.append([cableL0Length, cableL0Disp, cableL1Length, cableL1Disp, cableL2Length, cableL2Disp, cableL3Length, cableL3Disp, cableS0Length, cableS0Disp, cableS1Length, cableS1Disp, cableS2Length, cableS2Disp, cableS3Length, cableS3Disp, effectorPosition]) #print self.dataRows #print cableL0LengthIO # next will save it to file def onKeyPressed(self,c): path = os.path.dirname(os.path.abspath(__file__))+'/data/' filename = path+'data.csv' if (c == "q"): with open(filename, 'w') as csvfile: # creating a csv writer object csvwriter = csv.writer(csvfile) # writing the fields csvwriter.writerow(self.dataRows)
17 February 2021 at 00:19 #18690AbdelrahmanBlockedI was able to solve the problem by adding this lines of codes to the function
onEndAnimationStepfilename = './data/data.csv' if len(self.dataRows)%10==0: with open(filename, 'w') as file: writer = csv.writer(file) writer.writerows(self.dataRows)
but still the sofa framework did not respond to onKeyPressed function by pressing any key.
Also I’m facing this error
[ERROR] [MechanicalObject(MousePosition)] Invalid vOp operation 1 (null(V_DERIV),0(0),null(V_DERIV),100)
[ERROR] [MechanicalObject(MappedMousePosition)] Invalid vOp operation 1 (null(V_DERIV),0(0),null(V_DERIV),100) -
AuthorPosts
- You must be logged in to reply to this topic.