|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools |
Rating:
|
Display Modes |
|
#1
|
||||
|
||||
|
How to Program Limit Switches
Hello - Im the head programmer/electronics guy at LF Robotics. I am the only one of my kind on the team and this is my first year so I have absolutely NO experience so PLEASE bear with me.
I have two limit switches part number V7-2B17D8-048 - Here is a picture: ![]() I have a window motor - I want the window motor to pull a cord connected to a wheel - when the window motor reaches the end of its turn It will hit a limit switch - when it hits this limit switch I want it to reverse its direction and stop at its original location when it hits a second limit switch - it will be triggered again by a push of an assigned button on the joystick. How would I program this - I have thought it over and researched it and am still royally confused on what to write code-wise to preform this task and how to wire the switches. I am using Command Based Java in Netbeans. I asked the question about wiring it in the electronics section as well. ANY help will be GREATLY appreciated - THANKS! Last edited by LFRobotics : 24-01-2014 at 21:11. |
|
#2
|
|||
|
|||
|
Re: How to Program Limit Switches
Hello, welcome to FIRST!
So just making sure, you're just asking about the logic? I program in C++ so I'm not sure about any of the specifics, but I'd imagine something like this would be a good start point, then add on more and more to it until you like it: Code:
WindowMotor.Set(1.0);
while(LimitSwitch1.Get() == false)
{
}
WindowMotor.Set(-1.0);
while(LimitSwitch2.Get() == false)
{
}
WindowMotor.Set(0.0);
or having maybe a time for it to slow down so that you don't burn out the motors by immediately flipping it in reverse.This is just the template logic, so try and mix it up and make it your own! |
|
#3
|
||||
|
||||
|
Re: How to Program Limit Switches
You are going to want an add 2 limit switches (DigitalInputs) and 1 motor (Talon, Jaguar, Victor) to a subsystem.
Solution A Create a command WaitForLimitSwitch1. The only method requiring code would be the isfinished() method. Return limitSwitch1.Get(); Create a command WaitForLimitSwitch2. The only method requiring code would be the isfinished() method. Return limitSwitch2.Get(); Create a command to turn the motor on forward. Create a command to turn the motor on backwards. Create a command to stop the motor. Create a command group and add sequentially: --TurnMotorOnFwdCommand --WaitForLimitSwitch1Command --TurnMotorOnRevCommand --WaitForLimitSwitch2Command --TurnMotorOffCommand Assign command group to a joystick button. Solution B. Create one command and implement a state machine. Use a class member called state. Perform the actions in the execute method. Always be checking if a limit switch is pressed and if it is then increment the state. ex. Code:
private int state = 0;
bool isFinished() {
|
|
#4
|
||||
|
||||
|
Re: How to Program Limit Switches
After looking around a bit I have learned that I must assign a port for it in RobotMap - I did that.
Then what I can do is create a command to move the motor in one direction and put something like this: Quote:
Then have a second command that moves the motor in the opposite direction - all the same stuff in the isFinished() except it would be assigned to the second limit switch. When the limit switch is pressed the motor would stop. I could then put these two commands in a command group like this Quote:
I have that all figured out but what I don't get is: What would I put in a subsystem for the limit switches? What would I write in the if statement to return true in isFinished when the limit switches state is 1(closed). Quote:
|
|
#5
|
|||
|
|||
|
Re: How to Program Limit Switches
Wiring the limit switch is pretty simply. Plug a 3 pin header female the DIO and crimp the ends of the ground wire and the signal wire into spade connectors. The spade connectors then go onto the two poles of the switch. If you don't have spade connectors then you can solder straight to the poles of the limit switch.
|
|
#6
|
|||||
|
|||||
|
Re: How to Program Limit Switches
What brycen66 wrote is correct. Essentially you want the limit switch to connect a DIO signal pin to ground when the switch gets 'hit'. This implies using a Normally Open (N.O.) switch, and connecting it between DIO Signal and DIO Ground.
|
|
#7
|
||||
|
||||
|
Re: How to Program Limit Switches
Ok - but how do I program it?
|
|
#8
|
|||||
|
|||||
|
Re: How to Program Limit Switches
Create a DigitalInput associated with the DIO port you have wired the switch to. Use the .Get() method to read the value of the switch input.
See Kyle's post (#3 in the thread). |
|
#9
|
||||
|
||||
|
Re: How to Program Limit Switches
Do I add anything in RobotMap or do I put the
Quote:
THANKS! |
|
#10
|
||||
|
||||
|
Re: How to Program Limit Switches
Quote:
If your motor drives a mechanism that will hit a hard stop if the motor keeps running, then bad things will most likely happen if the limit switch is not seen by your code. The mechanism might be damaged, or the motor may pull enough current to burn out. But if your code turns off the motor if it reaches a limit switch OR after running say 50% longer than it should have taken to reach the limit normally, disaster might be avoided. By the way, if you want to do this one better, remember that the timeout should never occur if the mechanism and limit switch are working properly. If your code does see this timeout, you might want to display a message on the driver station that says the limit switch should be checked. This is one way to start implementing self diagnostic code. Our team has never gotten to that level of sophistication, but has considered it and is working to get there someday. |
|
#11
|
||||
|
||||
|
Re: How to Program Limit Switches
Do you know how to assign a command to a JoystickButton in the OI?
You can do the same using a DigitalIOButton as well. Code:
Button limitSwitch = new DigitalIOButton(RobotMap.limitSwitch); Code:
limitSwitch.whenPressed(new ReverseMotor()); then do the same with the second limit switch to stop the motor. then you use a JoystickButton triggered command to move the motor back forward. If you don't really understand the Command Based Template then I suggest you watch these 6 videos by Brad Miller on the topic(they total about an hour): FRC 2012 Rapidly creating a robot programl |
|
#12
|
||||
|
||||
|
Re: How to Program Limit Switches
Good idea Tsaksa - school was out today due to cold weather so when we have it tomorrow I will test it and see how long it takes the motor to reach the limit switch - then I can put in a timeout - as for a message in the driver station - that would take an if statement and then something like a putData() - I could also rig something up in smart dashboard.
Yes I do know how to assign buttons gixxy - I have already written the code for the limit switches but good idea - I have watched all of the videos and I found them rather informative. THANKS for the help! |
|
#13
|
||||
|
||||
|
Re: How to Program Limit Switches
By the way, once you start using software techniques like this to detect or even avoid hardware problems, you will see opportunities for using them everywhere. A good example can be found in pinball machines. Most electronic pinball machines will track and report bad switches, coils, and other components for the service technician. However, they do not have enough i/o to actually test everything. Instead they often just infer what is bad. For example, if the machine runs long enough without seeing a particular drop target get hit, it flags it as potentially having bad switch so that it can get checked. It might even change the game play or rules in the mean time so that players are less aware that the game may have a problem.
Automobiles also do a great job of recognizing when sensor values do not make sense and warning you about it with a check engine light and warning code. Try not to just think about how to control mechanical and electronic parts through software, but also what might go wrong and how you might recognize that something failed. Then you can take the next step to build software that can potentially reduce the severity of problems when they occur and also warn operators of those problems. |
|
#14
|
||||
|
||||
|
Re: How to Program Limit Switches
FYI,
Andy Mark has some better limit switches that the one in your picture. Every time we have used the cheap ones the small lever breaks in the worst times. Go industrial strength limit switch in your robot feed back sensors you'll be happy in the long run. Magnets are great sensors for sensing position. Optical sensors are good for sensing if something is loaded. Last edited by roystur44 : 29-01-2014 at 12:52. |
|
#15
|
|||
|
|||
|
Re: How to Program Limit Switches
Here's some more programming thoughts about the use of limit switches:
http://wpilib.screenstepslive.com/s/...ntrol-behavior Note: the code hasn't been tested, but it compiles and has been reviewed so it's very close, if not working. It's meant to give people an idea of how to use the library classes. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|