how do you calculate the PID values and stabilise the quadcopter using the on board sensors The gyro accelerometer and magnetometer
welcome to the Robotics stack exchange! your question is very broad, so I will try to give you an overview of what you need to do:
First determine orientation based on information from both the gyroscope and accelerometer. The accelerometer is accurate in the long term while the gyroscope is accurate in the short term, so you will need some kind of sensor fusion algorithm to determine the"true" orientation of the quad. The most popular one is the Kalman Filter but many others are available to choose from.
The next thing you will want to do is build a simple rate controller where you try to match the rate of rotation on a certain axis to the rate commanded by the controller
after you have a rate controller you can build a simple stabilized controller, where instead of commanding the rate you command a certain angle. You take the desired angle and the current angle and based on those values calculate a desired rate to close that gap and then feed that into the rate controller.
You will probably need a RC controller and receiver set, you will need to read the PWM values from the receiver in order to take command input
Here is my favorite resource on the matter it helped me when I was making my first flight controller from scratch.