Center of Mass Visualizer data read

Typically: "How do I... ", "How can I... " questions
Post Reply
Jay
Posts: 11
Joined: 14 Oct 2015, 16:34

Center of Mass Visualizer data read

Post by Jay »

Hi all,

I am trying to read the position of the center of mass of my vehicle to MATLAB. I realized that in 'centerOfMassVisulizer' customization script

Code: Select all

      if (totalMass~=0) then
		local centerOfMassOfModel={miri[1]/totalMass,miri[2]/totalMass,miri[3]/totalMass}
		local c={centerOfMassOfModel[1],centerOfMassOfModel[2],centerOfMassOfModel[3]}
		local xx=size
		simAddDrawingObjectItem(drawingObject,{c[1]-xx,c[2],c[3],c[1]+xx,c[2],c[3]})
		simAddDrawingObjectItem(drawingObject,{c[1],c[2]-xx,c[3],c[1],c[2]+xx,c[3]})
		simAddDrawingObjectItem(drawingObject,{c[1],c[2],c[3]-xx,c[1],c[2],c[3]+xx}) 
c[1], c[2], and c[3] are actually the center of mass position. So I was wondering what kind of command I can use in MATLAB side to read these information. Any kind of help will be greatly apprecaited.

Regards,
Jay

coppelia
Site Admin
Posts: 7928
Joined: 14 Dec 2012, 00:25

Re: Center of Mass Visualizer data read

Post by coppelia »

Hi Jay,

the simplest would be to pack the information needed in a string signal, like for instance:

Code: Select all

local data={1.23,3.45,5.67}
local packed=simPackFloats(data)
simSetStringSignal("myData",packed)
On Matlab side, you would then use simxGetStringSignal and simxUnpackFloats.

Cheers

Jay
Posts: 11
Joined: 14 Oct 2015, 16:34

Re: Center of Mass Visualizer data read

Post by Jay »

coppelia wrote:Hi Jay,

the simplest would be to pack the information needed in a string signal, like for instance:

Code: Select all

local data={1.23,3.45,5.67}
local packed=simPackFloats(data)
simSetStringSignal("myData",packed)
On Matlab side, you would then use simxGetStringSignal and simxUnpackFloats.

Cheers
Hi Admin,

Thank you for your help, I appreciate it. However, when I followed your tutor to put the code at the bottom of the ''centerOfMassVisualizer" code as follows

Code: Select all

visualizeCenterOfMass=function(baseHandle,size)
	local allNonStaticShapes={}
	
	local miri={0,0,0}
	local totalMass=0
	for i=1,#allNonStaticShapes,1 do
		local mass,inertia,com=simGetShapeMassAndInertia(allNonStaticShapes[i],nil)
		miri[1]=miri[1]+mass*com[1]
		miri[2]=miri[2]+mass*com[2]
		miri[3]=miri[3]+mass*com[3]
		totalMass=totalMass+mass
	end

	if (totalMass~=0) then
		local centerOfMassOfModel={miri[1]/totalMass,miri[2]/totalMass,miri[3]/totalMass}
		local c={centerOfMassOfModel[1],centerOfMassOfModel[2],centerOfMassOfModel[3]}
		local xx=size
		simAddDrawingObjectItem(drawingObject,{c[1]-xx,c[2],c[3],c[1]+xx,c[2],c[3]})
		simAddDrawingObjectItem(drawingObject,{c[1],c[2]-xx,c[3],c[1],c[2]+xx,c[3]})
		simAddDrawingObjectItem(drawingObject,{c[1],c[2],c[3]-xx,c[1],c[2],c[3]+xx})
		-- Add a banner:
		local bn={centerOfMassOfModel[1],centerOfMassOfModel[2],centerOfMassOfModel[3],0,0,0}
		simRemoveBanner(bannerHandle)
		bannerHandle=simAddBanner(string.format("Total mass=%.3f Kg",totalMass),0,sim_banner_bitmapfont+sim_banner_overlay,bn,-1,black,purple)
	else
		centerOfMassOfModel={0,0,0}
		simAddDrawingObjectItem(drawingObject,nil)
		simRemoveBanner(bannerHandle)
		bannerHandle=simAddBanner("Could not find dynamic objects..",0,sim_banner_bitmapfont+sim_banner_overlay,{0,0,0,0,0,0},-1,black,white)
	end


[b]local centerOfMassData={c[1],c[2],c[3]}
local stringData=simPackFloats(centerOfMassData)
simSetStringSignal("centerOfMass", stringData)[/b]

end
I cannot read the data to MATLAB. But when I do test by putting the following code in the following way,

Code: Select all

if (totalMass~=0) then
		local centerOfMassOfModel={miri[1]/totalMass,miri[2]/totalMass,miri[3]/totalMass}
		local c={centerOfMassOfModel[1],centerOfMassOfModel[2],centerOfMassOfModel[3]}
		local xx=size
		simAddDrawingObjectItem(drawingObject,{c[1]-xx,c[2],c[3],c[1]+xx,c[2],c[3]})
		simAddDrawingObjectItem(drawingObject,{c[1],c[2]-xx,c[3],c[1],c[2]+xx,c[3]})
		simAddDrawingObjectItem(drawingObject,{c[1],c[2],c[3]-xx,c[1],c[2],c[3]+xx})
		-- Add a banner:
		local bn={centerOfMassOfModel[1],centerOfMassOfModel[2],centerOfMassOfModel[3],0,0,0}
		simRemoveBanner(bannerHandle)
		bannerHandle=simAddBanner(string.format("Total mass=%.3f Kg",totalMass),0,sim_banner_bitmapfont+sim_banner_overlay,bn,-1,black,purple)
	else
		centerOfMassOfModel={0,0,0}
		simAddDrawingObjectItem(drawingObject,nil)
		simRemoveBanner(bannerHandle)
		bannerHandle=simAddBanner("Could not find dynamic objects..",0,sim_banner_bitmapfont+sim_banner_overlay,{0,0,0,0,0,0},-1,black,white)
	end


local centerOfMassData={c[1],c[2],c[3]}
local stringData=simPackFloats(centerOfMassData)
simSetStringSignal("centerOfMass", stringData)

end


[b]centerOfMassData={1,2,3}
stringData=simPackFloats(centerOfMassData)
simSetStringSignal("centerOfMass", stringData)[/b]
 
I can read [1 2 3] in MATLAB. I was wondering how can I fix this problem.

Regards,
Jay

coppelia
Site Admin
Posts: 7928
Joined: 14 Dec 2012, 00:25

Re: Center of Mass Visualizer data read

Post by coppelia »

Is there any error message in the status bar when you run the simulation? There should be an error saying that the variable c is nil or something similar. It is important not to ignore error messages. Otherwise you will never get your code right.
You basically need to put the code in the section where c is still in scope, otherwise c is undefined (i.e. nil)

Cheers

Post Reply