coding for limit switch

I’m attempting to write some code for turret aiming and I am using two limit switches to limit the swing of the turret. Currently the turret uses the camera to aim at the goal using a PID controller and there are no limit switches.

I don’t know how to write the code that will cause the motor to move in the opposite direction and not confuse the PID controller. The code is written such that if the target moves out of the field of view of the camera, the turrnet will rotate in the direction the target was last seen.

Will the PID controller get confused if I force the motor to spin in the opposite direction after hitting the limit switch?

I also don’t know how write the code to allow the limit switch to be let go before evaluating the switches again.

We are using labview.

I attached what I already have written for that section of code. I just put in a timer to make the motor move in the opposite direction for a certain amount of time assuming that the switch will be let go by then. I would like to do this better than I already have done. Any suggestions? Thanks!





Instead of forcing the motor to run for “a while” when a limit switch is tripped, you can just use the switch to keep the motor from going any further in the wrong direction. Don’t worry about moving it away, just worry about keeping it from going past the limit.

The typical way to do this is to splice into the speed control value. Set it to zero if one limit switch is active and the incoming value is positive, or if the other switch is active and the value is negative. This will stop the motor as it drives into the limit, and you can rely on other signals to drive the motor back.

If you do just stop when you hit the limit switch,your PID will probably generate a significant amount of error for a relativley long period of time. If you’re using the error amount to perform hardware diagnostics(like we did last year), this could cause false positives