Go to Post I hate to do say it with these words but...We're all in this together. - dellagd [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rating: Thread Rating: 3 votes, 4.67 average. Display Modes
  #1   Spotlight this post!  
Unread 24-01-2014, 21:06
LFRobotics's Avatar
LFRobotics LFRobotics is offline
Registered User
FRC #4623
 
Join Date: Jan 2014
Location: Little Falls, MN
Posts: 95
LFRobotics is on a distinguished road
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   Spotlight this post!  
Unread 24-01-2014, 21:52
Sparkyshires Sparkyshires is offline
Registered User
AKA: Michael Shires
FRC #0384 (Sparky)
Team Role: Programmer
 
Join Date: Jan 2012
Rookie Year: 2006
Location: Virginia
Posts: 226
Sparkyshires is an unknown quantity at this point
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);
Now, a couple things you may want to look at is decreasing the speed, so that you don't break the limit switch 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!
__________________
"Measure with a micrometer, mark with chalk, cut with an axe."
  #3   Spotlight this post!  
Unread 25-01-2014, 00:58
kylelanman's Avatar
kylelanman kylelanman is offline
Programming Mentor
AKA: Kyle
FRC #2481 (Roboteers)
Team Role: Mentor
 
Join Date: Feb 2008
Rookie Year: 2007
Location: Tremont Il
Posts: 191
kylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to all
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() {
return state == 3;
} void execute() {
if state == 0 {
motor->set(1); if limitSwitch1.get() {
state = 1;
}
} if state == 1 {
motor->set(-1) if limitSwitch2.get() {
state = 2;
}
} if state == 2 {
motor->set(0); state = 3;
}
}
Assign command to a joystick button.
__________________
"May the coms be with you"

Is this a "programming error" or a "programmer error"?

  #4   Spotlight this post!  
Unread 25-01-2014, 14:02
LFRobotics's Avatar
LFRobotics LFRobotics is offline
Registered User
FRC #4623
 
Join Date: Jan 2014
Location: Little Falls, MN
Posts: 95
LFRobotics is on a distinguished road
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:
if limit switch is active(1)
in isFinished() so that when the limit switch is pressed the command will finish

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:
addSequential(new command);
addSequential(new secondCommand);
and the motor would move until it hit the limit switch - stop - and then return to its original position.

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:
if (something) {
return true;
}
else {
return false;
}
THANKS a ton for the help!
  #5   Spotlight this post!  
Unread 25-01-2014, 19:38
brycen66 brycen66 is offline
Registered User
FRC #1678 (Citrus Circuits)
Team Role: Electrical
 
Join Date: Jan 2013
Rookie Year: 2011
Location: United States
Posts: 34
brycen66 is just really nicebrycen66 is just really nicebrycen66 is just really nicebrycen66 is just really nice
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   Spotlight this post!  
Unread 25-01-2014, 19:58
DonRotolo's Avatar
DonRotolo DonRotolo is offline
Back to humble
FRC #0832
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Atlanta GA
Posts: 7,019
DonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond repute
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.
__________________

I am N2IRZ - What's your callsign?
  #7   Spotlight this post!  
Unread 27-01-2014, 12:39
LFRobotics's Avatar
LFRobotics LFRobotics is offline
Registered User
FRC #4623
 
Join Date: Jan 2014
Location: Little Falls, MN
Posts: 95
LFRobotics is on a distinguished road
Re: How to Program Limit Switches

Ok - but how do I program it?
  #8   Spotlight this post!  
Unread 27-01-2014, 13:13
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
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   Spotlight this post!  
Unread 27-01-2014, 13:54
LFRobotics's Avatar
LFRobotics LFRobotics is offline
Registered User
FRC #4623
 
Join Date: Jan 2014
Location: Little Falls, MN
Posts: 95
LFRobotics is on a distinguished road
Re: How to Program Limit Switches

Do I add anything in RobotMap or do I put the

Quote:
DigitalInput limitSwitch1 = new DigitalInput(1);
in the subsystem?

THANKS!
  #10   Spotlight this post!  
Unread 27-01-2014, 14:44
tsaksa's Avatar
tsaksa tsaksa is offline
Registered User
FRC #0997 (CHS Robotics)
Team Role: Mentor
 
Join Date: Jul 2011
Rookie Year: 2011
Location: Corvallis Oregon
Posts: 203
tsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond repute
Re: How to Program Limit Switches

Quote:
Originally Posted by Sparkyshires View Post

Now, a couple things you may want to look at is decreasing the speed, so that you don't break the limit switch 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.
Another thing to consider is a timeout so that if the limit switch is not reached in a reasonable time, the motor still turns off. In a competition or practice it is possible (perhaps even common) for a limit switch to become damaged or disconnected, or for mechanical parts to become bent or misaligned enough that the switch never gets activated. Ask yourself what will happen if that occurs.

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.
__________________
This is the zeroth day of the rest of your life.
  #11   Spotlight this post!  
Unread 27-01-2014, 15:06
gixxy's Avatar
gixxy gixxy is offline
Programming and Arduino Mentor
AKA: Gustave Michel III
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Nov 2011
Rookie Year: 2012
Location: Ruston, LA
Posts: 207
gixxy is on a distinguished road
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);
and then assign it a command

Code:
limitSwitch.whenPressed(new ReverseMotor());
so then you just have the command reverse the motor.

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
__________________
Programmer - A creature known for converting Caffeine into Code.
Studying Computer Science @ Louisiana Tech University
Associate Consultant @ Fenway Group

2012-13: 3946 - Head of Programming, Electrical and Web
2014 - 3468 - Programming Mentor
2015 - Present - Lurker
  #12   Spotlight this post!  
Unread 27-01-2014, 18:59
LFRobotics's Avatar
LFRobotics LFRobotics is offline
Registered User
FRC #4623
 
Join Date: Jan 2014
Location: Little Falls, MN
Posts: 95
LFRobotics is on a distinguished road
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   Spotlight this post!  
Unread 29-01-2014, 12:20
tsaksa's Avatar
tsaksa tsaksa is offline
Registered User
FRC #0997 (CHS Robotics)
Team Role: Mentor
 
Join Date: Jul 2011
Rookie Year: 2011
Location: Corvallis Oregon
Posts: 203
tsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond reputetsaksa has a reputation beyond repute
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.
__________________
This is the zeroth day of the rest of your life.
  #14   Spotlight this post!  
Unread 29-01-2014, 12:32
roystur44's Avatar
roystur44 roystur44 is offline
Mentor/Sponsor
AKA: Roy Dumlao
FRC #4543 (Apollo Robotics)
Team Role: Mentor
 
Join Date: Mar 2010
Rookie Year: 2006
Location: San Jose,California
Posts: 363
roystur44 has a reputation beyond reputeroystur44 has a reputation beyond reputeroystur44 has a reputation beyond reputeroystur44 has a reputation beyond reputeroystur44 has a reputation beyond reputeroystur44 has a reputation beyond reputeroystur44 has a reputation beyond reputeroystur44 has a reputation beyond reputeroystur44 has a reputation beyond reputeroystur44 has a reputation beyond reputeroystur44 has a reputation beyond repute
Send a message via Yahoo to roystur44
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.
__________________
Roy Dumlao

Mentor/Sponsor
2006-2012 971
2013-2017 4543

Last edited by roystur44 : 29-01-2014 at 12:52.
  #15   Spotlight this post!  
Unread 30-01-2014, 15:14
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 592
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
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.
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:46.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi