FIRST Tech Challenge Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I add a limit switch for Teleop mode

A team asked how to add a limit switch for DC motors so I thought I would post the solution here for everyone.

The generated teleop code uses events to run different cases IE "Button 1" but you may want some code to always be running so it won;t have to wait for an event. We can paste this code under the for loop so it will run on each itteration.

I this example I have a DC motor for the elbow of my robot arm and a limit switch on port 4 that will get pressed when the elbow is all the way extended. Once the button is pressed I want to stop the motor and prevent you from trying to extend it more.

A) This code in the event case will prevent the motor from starting if the button is pressed.

B) This code will look for a change in the state of the button. If the button was released last time AND pressed this time the function will return a true. That way this will only trigger once on the "rising edge" of the button. Once the limit is reached we still want to be able to move it back away from the limit switch. This function uses a Functional Global Variable to keep track of the button's last state. I created the NXT Button Rising Edge sub VI (linked below) if you want to use this code.

C) I take the output from the B code and if we detect a rising edge then stop the elbow motor.

D) by default the watch dog built into the motor controllers will stop the motors if no commands are recvied. This "Feed Watchdog" function is not required for limit switch applications but will be critical if you want to push and hold a button so a motor will move longer than 2 seconds.

Limit.png

I attached my teleop VI as an exmaple as well. Just paste the A-D blocks into your generated teleop VI.

Download All
Message 1 of 1
(4,627 Views)