How to stop an object from jittering in Python with Motion Actuator?

I have an object that I am trying to get to avoid walls via two ray sensors that detect the wall objects in Python. All goes well with the motion until it turns, and then the problem starts - it jitters.

This is very unnatural, so how can I make it smooth in Python rather than having it all jittery with dRot?

When you say “jitter”… you mean it repeatively turns in the direction you want and instantly into the other ? Then maybe your “control” of direction changes needs a bit of hysteresis ??
You also mention two… sensors… so maybe some back coupling…

The “jittering” is really when the object turns, but suddenly turns the opposite way very rapidly. This creates an effect where the object will twitch or “spasm” rapidly, which is undesirable in animals such as snakes because they will never jitter when trying to turn from a wall.

I’m trying to code the motion and the ray sensors so when the rays detect the wall object (as determined by the ‘wall’ property), the object will turn around smoothly and not jitter like this. I’ve tried Character, Servo, etc and even damping, and adjusting physics, but this does not work.

Can you please show a sample code of what would work for this?

I suggested some thoughts in a more general way because you was vague about your implementation… any code or node setup could be unsuited for your use case… so:

You may have to elaborate this to get a more suited answer (from anyone).