Code for a balancing robot

Hello, I am using an MPU6050 to balance my robot, I have everything set up. I wanted to ask about how my code should look like, especially the PID part.

For the PID:
Input: Angle of interest.
Output: motors' PWM.

Kp,Ki,Kd controlled with pots to tune them.


if (the robot tilts forward) then ( the wheels should move forward with output of the PID fed into it.)
else if (the robot tilts backward) then (the wheels should move backward with the output of the PID fed into it.)

I wanted to make sure if this is the correct way of balancing a robot.

I would also like to add encoders as many balancing robot projects did so to improve balancing but I am not sure how to go about it.

Thank you for your help!