As for your actual error, it would appear it can be reduced to this MWE:
using Symbolics
@variables v
Differential(v)^0
It looks like the 0th differential isn’t supported by Symbolics? If it isn’t but it should be, consider checking whether there is an open issue on its github and opening one if not.
Could you replace this with D_cst_m = iszero(m) ? Returns(cosθ) : Differential(cosθ)^m (and similar for the l version in another function)? Or is there another way you can special case the Differential()^0 case to work?
Anyway, that appears to address the error at hand.