Mimic Thrust Force Model - Cuboid

Typically: "How do I... ", "How can I... " questions
DannyYeong
Posts: 44
Joined: 24 Feb 2015, 17:06

Mimic Thrust Force Model - Cuboid

Post by DannyYeong » 27 Apr 2015, 15:36

I'm trying to make a cuboid rise to a certain height and then stop. The cuboid should keep itself in air till the simulation ends. I'm using the simAddForceAndTorque command and I know that the force and torque are cumulative. How do I keep the object fixed at a certain height after rising?


Here's my model of build:
http://www.filedropper.com/thrustforcep ... uboidstest


Side question:
Do I use height comparison with a constant co-ordinate (example: {0,0,5} ) to adjust the force applied onto the cuboid?
It works like:

If cuboid reaches a a height of z= "5" (from 0,0,5), then the force applied will change. Else, keep rising.


Regards,
Danny

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

Re: Mimic Thrust Force Model - Cuboid

Post by coppelia » 28 Apr 2015, 09:21

Hello Danny,

the forces/torques you apply with simAddForceAndTorque (or simAddForce) are cumulative in a given simulation step. But the cumulated force/torque will be cleared at the end of one simulation step.

But your approach is correct. I would implement a simple P controller for your force, instead of an on/off controller.

Cheers

DannyYeong
Posts: 44
Joined: 24 Feb 2015, 17:06

Re: Mimic Thrust Force Model - Cuboid

Post by DannyYeong » 28 Apr 2015, 11:00

coppelia wrote:Hello Danny,

the forces/torques you apply with simAddForceAndTorque (or simAddForce) are cumulative in a given simulation step. But the cumulated force/torque will be cleared at the end of one simulation step.

But your approach is correct. I would implement a simple P controller for your force, instead of an on/off controller.

Cheers
I've tried using this coding:

Code: Select all

simAddForceAndTorque(propeller,force,torque)

position1={0,0,0.5}

	if simGetObjectPosition(propeller,-1)>=position1 then


		force={0,0,0}
		torque={0,0,0}
	
	else
		
		force={0,0,10}
		torque={0,0,0}

       end
Apparently I can't compare <= or >= for a vector. I want to let it "float" itself on air after it reaches a specific height. However I could only use "==" or "~=", which is not the best option since the object will always surpass the specific height due to inertia, which keeps the object flying upwards. So how do I use the Z position as a variable to be compared with a fixed constant/threshold?

DannyYeong
Posts: 44
Joined: 24 Feb 2015, 17:06

Re: Mimic Thrust Force Model - Cuboid

Post by DannyYeong » 28 Apr 2015, 15:45

DannyYeong wrote:
coppelia wrote:Hello Danny,

the forces/torques you apply with simAddForceAndTorque (or simAddForce) are cumulative in a given simulation step. But the cumulated force/torque will be cleared at the end of one simulation step.

But your approach is correct. I would implement a simple P controller for your force, instead of an on/off controller.

Cheers
I've tried using this coding:

Code: Select all

simAddForceAndTorque(propeller,force,torque)

position1={0,0,0.5}

	if simGetObjectPosition(propeller,-1)>=position1 then


		force={0,0,0}
		torque={0,0,0}
	
	else
		
		force={0,0,10}
		torque={0,0,0}

       end
Apparently I can't compare <= or >= for a vector. I want to let it "float" itself on air after it reaches a specific height. However I could only use "==" or "~=", which is not the best option since the object will always surpass the specific height due to inertia, which keeps the object flying upwards. So how do I use the Z position as a variable to be compared with a fixed constant/threshold?
Update to previous reply: I've found out how to refer the "Z" co-ordinate of the object, turns out I just have to define the object's position using

Code: Select all

pos=simGetObjectPosition(propeller,-1)
and then use the Z co-ordinate by:

Code: Select all

pos[3] -- the "3" refers to the 3rd vector, Z co-ordinate
My updated coding would be:

Code: Select all

if (sim_call_type==sim_childscriptcall_initialization) then

	propeller=simGetObjectHandle('Cuboid')
	ground={0,0,0} --ignore this for the moment
	pos1={0,0,0.5}
	

end


if (sim_call_type==sim_childscriptcall_actuation) then
	
	pos=simGetObjectPosition(propeller,-1)
	simAddForceAndTorque(propeller,force,torque)
	
	
	if pos[3]<pos1[3] then
	
		force={0,0,10}
		torque={0,0,0}
	
	else
		
		force={0,0,0}

	end

	

end
here's the file: http://www.filedropper.com/thrustforcep ... oidstest_1

I wanted the object to stay in air while "bouncing about" (like a quadcopter rising motion to achieve balance in Z direction) but it keeps hitting the ground eventhough I've programmed it to apply force if the height drops a specific level. What coding I should use if the current one is not suitable or is there any add-on branch I should include in the ifelse case?

Regards,
Danny

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

Re: Mimic Thrust Force Model - Cuboid

Post by coppelia » 29 Apr 2015, 09:00

You need to write a correct controller for your force. You also need drag (the D component of a PID controller). Maybe something like:

Code: Select all

if (sim_call_type==sim_childscriptcall_actuation) then
	
	local pos=simGetObjectPosition(propeller,-1)

	local accel=simGetArrayParameter(sim_arrayparam_gravity)

	-- cm is the proportional parameter of the PD controller:
	local cm=(pos1[3]-pos[3])/0.05
	if (cm>1.05) then cm=1.05 end
	if (cm<0) then cm=0 end

	-- str is the derivative paramter of the PD controller
	local str=-5 -- drag

	simAddForceAndTorque(propeller,{0,0,-accel[3]*cm})
	local linV,angV=simGetVelocity(propeller)
	local mass=simGetShapeMassAndInertia(propeller)
	local f={linV[1]*mass*str*cm,linV[2]*mass*str*cm,linV[3]*mass*str*cm}
	simAddForceAndTorque(propeller,f)
end
Cheers

DannyYeong
Posts: 44
Joined: 24 Feb 2015, 17:06

Re: Mimic Thrust Force Model - Cuboid

Post by DannyYeong » 29 Apr 2015, 10:39

coppelia wrote:You need to write a correct controller for your force. You also need drag (the D component of a PID controller). Maybe something like:

Code: Select all

if (sim_call_type==sim_childscriptcall_actuation) then
	
	local pos=simGetObjectPosition(propeller,-1)

	local accel=simGetArrayParameter(sim_arrayparam_gravity)

	-- cm is the proportional parameter of the PD controller:
	local cm=(pos1[3]-pos[3])/0.05
	if (cm>1.05) then cm=1.05 end
	if (cm<0) then cm=0 end

	-- str is the derivative paramter of the PD controller
	local str=-5 -- drag

	simAddForceAndTorque(propeller,{0,0,-accel[3]*cm})
	local linV,angV=simGetVelocity(propeller)
	local m=simGetObjectMatrix(propeller,-1)
	local mass=simGetShapeMassAndInertia(propeller)
	m[4]=0
	m[8]=0
	m[12]=0
	local mi=simGetInvertedMatrix(m)
	linV=simMultiplyVector(mi,linV)
	linV[1]=0
	linV=simMultiplyVector(m,linV)
	local f={linV[1]*mass*str*cm,linV[2]*mass*str*cm,linV[3]*mass*str*cm}
	simAddForceAndTorque(propeller,f)
end
Cheers

Hi, I don't understand the Derivative part of the coding, added my comments into it below:

Code: Select all

-- str is the derivative paramter of the PD controller
   local str=-5 -- drag

   simAddForceAndTorque(propeller,{0,0,-accel[3]*cm})
   local linV,angV=simGetVelocity(propeller)
   local m=simGetObjectMatrix(propeller,-1)
   local mass=simGetShapeMassAndInertia(propeller)
   m[4]=0
   m[8]=0
   m[12]=0
   local mi=simGetInvertedMatrix(m)
   linV=simMultiplyVector(mi,linV)
   linV[1]=0                                   -- ||||||||||||||||||why set the first value to 0?
   linV=simMultiplyVector(m,linV)    --  |||||||||||||||||Why multiplied?
   local f={linV[1]*mass*str*cm,linV[2]*mass*str*cm,linV[3]*mass*str*cm}
   simAddForceAndTorque(propeller,f)

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

Re: Mimic Thrust Force Model - Cuboid

Post by coppelia » 29 Apr 2015, 11:03

You are right, the code was copy-pasted from another example, where the drag had to be relative to the shape frame, and only radial to it. See the updated version above.

Cheers

DannyYeong
Posts: 44
Joined: 24 Feb 2015, 17:06

Re: Mimic Thrust Force Model - Cuboid

Post by DannyYeong » 06 May 2015, 14:06

coppelia wrote:You are right, the code was copy-pasted from another example, where the drag had to be relative to the shape frame, and only radial to it. See the updated version above.

Cheers
Might need some clarification on the 1.05 constant chosen, how it's that obtained?

DannyYeong
Posts: 44
Joined: 24 Feb 2015, 17:06

Re: Mimic Thrust Force Model - Cuboid

Post by DannyYeong » 06 May 2015, 17:27

DannyYeong wrote:
coppelia wrote:You are right, the code was copy-pasted from another example, where the drag had to be relative to the shape frame, and only radial to it. See the updated version above.

Cheers
Might need some clarification on the 1.05 constant chosen, how it's that obtained?

Found out for its reason, its just a self-made constant to provide a force more than the gravity force so it'll lift up. Thanks for the info provided. I tried my own, and apparently the Drag is really a problem. At this point of coding:

Code: Select all

pos=simGetObjectPosition(propeller,-1)
	grav=simGetArrayParameter(sim_arrayparam_gravity)
	mass=simGetShapeMassAndInertia(propeller)
	diff=pos1[3]/pos[3] -- <1 shows over extended, >1 shows under extended
	
	if diff<1 then -- upward force
		
		f={0,0,-grav[3]*mass*0.99}
		simAddForceAndTorque(propeller,f)

	end

	if diff>1 then --downward force and to prevent drag force
		
		f={0,0,-grav[3]*mass*1.01}
		simAddForceAndTorque(propeller,f)
	
	end
I decided to use division instead of minus and multiplying like the coding provided earlier. My file is here:

http://www.filedropper.com/thrustforcep ... uboidstest

The object is currently being dragged up but it exceeds the height and then drops. After the first process, the object goes up and down by alittle to balance itself. However, onwards process seems to go higher and more lower than the first cycle. Is this caused by the drag? If so, could I try a different approach to control the drag using another way other than velocity?

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

Re: Mimic Thrust Force Model - Cuboid

Post by coppelia » 06 May 2015, 21:19

You are using a simple 2-state controller. Either you apply a force of X, or a force of Y. It is normal that your control won't be good. You will have to use something more sofisticated, at least a P/PD controller. You will have to try.

Cheers

Post Reply