arduino controlled tablesat; decreasing pwm while approaching to reference

i am new here, first post, really stuck in my project. plz help me. a video of my project is here;

basically in this video the plate is overcoming the error caused by my hand and coming to its reference. i have used;

arduino uno
mpu6050
computer fans

for coding i have used mpu6050_DMP6 library example, and pid library available on arduino website, so my system is working exactly the same way i need it to be as in above link.

but the problem starts when i remove the arduino cable (which is providing necessary damping to the system) from laptop, and run it with battery on the system(plate), so damping vanishes in this case. so when plate reaches its reference position (though pwm turns to zero) due to inertia of plate as well as of fan blades plate crosses its reference (which it do also in video i gave link above), but this overshoot keep on increasing in every cycle and system becomes unstable unlike the above case. and this problem doesnt solved by varying pid gains(i tried a lot).

so i came up with a solution: to reduce the pwm value of main fan(controlling in clockwise direction) as system approaches to its mean position, while providing a little braking with a little amount of increasing pwm of opposite fan(anticlockwise), and do it for both clockwise and anticlockwise motion to make system stable.

but though i have the logic but i am unable to code it in arduino. plz help me to develop this logic in arduino.

so basically i want to decrease fans pwm while my error is decreasing (system is approaching to set point which is initial state before disturbance). any help will be highly appreciated. and please forgive me for my language mistakes.

the code i am using is attached (as i have not written it myself thats why a lot of code’s useless portion is needed to be trimmed, so you will find many commented things );

for the time being i am only using pd, and my error never increases above 10, so values for aggk…ss and consK…s are same.

thanks for your time.

tablesat.txt (8.56 KB)