I cant for the life of me tune this PID controller, Please Help!

I’ve been trying for the past 2 days to figure out a way to make this PID controller stabilize a hovering vehicle using a vectorForce. I just simply cant figure out the correct PID gains, its either oscillating intensely or not moving at all. But no matter what gains I put, it NEVER stabilizes at the height I’m trying to get it to hover at.

–Notes:

  • Everything is done in a local script while using RenderStepped.
  • I have workspace gravity set to 0, as I am applying gravity directly in the script on the vehicle.
  • I am using @BRicey763 RoPID Module: RoPID: PID Control in Roblox!
  • The vehicle is only one part with its “Massless” property turned on

Here is the script responsible for calculating all the forces.
–Variables:

  • GlobalVehicleSettings.HoverHeight = 15
  • GlobalVehicleSettings.RayLength = 50
  • GlobalVehicleSettings.HoverForce = 300
  • GlobalVehicleSettings.HoverGravity = 200
  • GlobalVehicleSettings.FallGravity = 800
--Function to calculate the vehicles Hover
local function CalculateHover(dt)
	--Shoot a ray to check if the vehicle is on the ground
	local HoverRay = workspace:Raycast(MainVehicleInfo.CenterVectorAtt.WorldPosition,(MainVehicleInfo.CenterVectorAtt.WorldCFrame.UpVector*-1)*GlobalVehicleSettings.RayLength)
	
	--Local Vars
	local GroundNormal
	
	--If the raycast hits and the ship is on the ground
	if HoverRay then
		--Save the rayinfo in local vars for better readability
		local Height = HoverRay.Distance
		GroundNormal = HoverRay.Normal
		
		--Calculate the hover force using Briceys RoPID module
		local ForcePercent = MainVehicleInfo.PIDController:Calculate(GlobalVehicleSettings.HoverHeight,Height,dt)
Base.Position+MainVehicle.VehicleBase.CFrame.UpVector*ForcePercent)
		--print(ForcePercent)
		
		--Calulate the total hover force vector required based on the normal of the raycast
		local Force = GroundNormal * ForcePercent * GlobalVehicleSettings.HoverForce 
		
		--Calculate the gravity relative to the ships CFrame
		local Gravity = -GroundNormal * GlobalVehicleSettings.HoverGravity * Height
		
		--Apply the force to the VectorForce of the vehicle
		MainVehicleInfo.CenterVectorAtt.VectorForce.Force += Force
		MainVehicleInfo.CenterVectorAtt.VectorForce.Force += Gravity
		
	else --If the vehicle is not on the ground
		--The normal used is the global up vector
		GroundNormal = Vector3.new(0,1,0)
		
		--Calculating and applying gravity
		local Gravity = -GroundNormal*GlobalVehicleSettings.FallGravity
		MainVehicleInfo.CenterVectorAtt.VectorForce.Force += Gravity
	end
end

Here is what the current PID gains are and what it looks like
image


(“LineA” and the moving red line on the part are just representations of the ForcePercent Value from the script above)

Any help on how to stabilize the vectorForce is greatly appreciated, Thanks!

I know this is eons after your post but I wanted to ask what is the console on the bottom left that you’re using to check the PID Response, It looks like boatbombers but I’ve never been able to edit the standalone code to display PID Values like you have which is neat and would be very helpful for pid debugging

Yep it’s using boatbombers old graph module (don’t think it’s been modified in any way though.) I’m reading through my old horrendous code, looks like it just stored and displayed an array of all the final forces returned by the PID module that was used to calculate the hover force.

Graph Initialization:

local MainVehicleInfo = {
	["Graph"] = Graph.new(GraphFrame),
}

MainVehicleInfo.Graph.Resolution = 100
MainVehicleInfo.Graph.BaselineZero = false

local data = {
	LineA = {0};
}

MainVehicleInfo.Graph.Data = data

Graph Plotting:

---------- During the physics calculation step ----------

-- Calculate the hover force using @Briceys RoPID 
local hoverForce = MainVehicleInfo.PIDController:Calculate(GlobalVehicleSettings.HoverHeight, currentHeight, dt)

-- Reset the graph if it contains too many points
if #data.LineA >= 2000 then
	 data = {LineA = {0}}
end

-- Plot the new PID force on the graph
table.insert(data.LineA, hoverForce)
MainVehicleInfo.Graph.Data = data
2 Likes

Much appreciated man. Thanks ever so much!