PD of PID in a balancing robot

ironbot:
The x axis of accelerometer is set in the direction of movement of the robot and horizontally, as seen in the picture, gyroscope feels rotation around 'y' axis, i.e. the rotation of x axis between -g and g. So I combine both of their output to get the lean angle. I learned this by reading a lot online, I never seen a technique somewhere used to differentiate the signal of the motor rotation from the robot lean. May be that is the reason it is oscillating this much fast?

I can see what the code is doing, but I don't understand the logic behind it. The accelerometer doesn't differentiate between rotation about the axle and lateral acceleration, and I don't see the purpose of trying to incorporate that into your angle calculation. Did all your reading explain the theory behind this algorithm?

Going back to basics, what is the output of your gyro? Is it giving you deflection, speed or acceleration? How sensitive is it - what is the minimum value it can read (in terms of the bot's angle, angular speed or angular acceleration)?