You're correct- I wasn't thinking enough when I wrote that. the D term should be kd*the difference between 2 successive errors over the sampling period.
as for
(ki*(integral +distance error*dt))
If you mean integral as in (sum of previous errors * sampling time) rather than the whole integral 'I' term, then you're right.
so, output = kp*(distance error) + ki*(distance error + (sum previous distance errors))*sampling time + kd*((distance error - previous error)/sampling time)
>when deriving the transfer function constant should i add the controller before or after the derivation?
The transfer function for the motor will be a 2nd order function. It will "transfer" the voltage input to a velocity output. You don't need to change the PID function to simulate the motor velocity, but you do need to add a function which takes the PID sum as an input, and produces a voltage output (this is based on your hardware)- this is where you need to define how a number calculated on the MCU ends up driving the motor. If I remember correctly I think the MATLAB website actually has good examples for motor transfer functions.