Optimal Path and Trajectory Planning for Serial Robots