Robot Modeling and Control by Spong et al, chapter 7. (Equation 7.146 is the one that includes gravity.)