Go to Post A good thing to do when thinking about designs like this, ask yourself "what do I gain?" - lemiant [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 10-04-2016, 07:54
GoldenGollem's Avatar
GoldenGollem GoldenGollem is offline
Java Programmer
FRC #2509 (Hutchinson Tigerbots)
Team Role: Programmer
 
Join Date: Dec 2015
Rookie Year: 2015
Location: Minnesota
Posts: 24
GoldenGollem is an unknown quantity at this point
Limit Switch Programming

I have been trying to program a limit switch for java for the 2016 competitions. I am having trouble figuring out what to do and the FRC tutorial seems rather complex with my level of skill. I am wondering if there is an easy way to program a limit switch to stop a motor when it is activated. Thanks if you can help me out.
Reply With Quote
  #2   Spotlight this post!  
Unread 10-04-2016, 08:02
engunneer's Avatar
engunneer engunneer is offline
Alumni turned Mentor
AKA: Branden Gunn
FRC #4761
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1996
Location: Reading, MA
Posts: 815
engunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond repute
Re: Limit Switch Programming

Which program template are you using?

Can you post an example?
__________________
Student FRC23 (1996-1999), Mentor FRC246 (2000), Mentor FRC1318 (2007-2009), Mentor FRC93 (2011), Mentor FRC2151 (2012), Mentor FRC23 (2013), Mentor FRC4761 (2014-2017)
1998 - National Chairman's Award and Woodie Flowers Award (FRC23, Mike Bastoni ) | 2007 - PNW SF (488, 1595) | 2008 - Oregon RCA - Seattle #2 Seed, SF (488, 1696) | 2009 - Oregon #1 Seed, Winners (1983, 2635) - Seattle SF (945, 2865) - Galileo #2 Seed, SF (973, 25) | 2012 Midwest F (111, 71) | 2014 RIDE Winners (78, 125), Inspector - NEU #24, QF (3479, 3958) - NECMP #35 | 2015 Reading #11, SF (1058, 190), Inspector - RIDE #17, QF(4055, 5494), Inspector - NECMP #57 | 2016 Reading #4, SF (133, 4474), DCA, Inspector - Ride #22, SF (1735, 2067), Creativity, Inspector - NECMP #48, RCA - Archimedes
Reply With Quote
  #3   Spotlight this post!  
Unread 10-04-2016, 08:52
GoldenGollem's Avatar
GoldenGollem GoldenGollem is offline
Java Programmer
FRC #2509 (Hutchinson Tigerbots)
Team Role: Programmer
 
Join Date: Dec 2015
Rookie Year: 2015
Location: Minnesota
Posts: 24
GoldenGollem is an unknown quantity at this point
Re: Limit Switch Programming

Here is the blank template that I am using to test the limit switch.
Code:
package org.usfirst.frc.team2509.robot;

import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.DriverStation;

public class Robot extends IterativeRobot {
	Talon motor1 = new Talon(0);
	Joystick stick;
	DigitalInput limitSwitch;
	
    public void robotInit() {
        
    }
   
    public void autonomousInit() {
    	}

    public void autonomousPeriodic() {
    	}

    public void teleopPeriodic() {
        if (stick.getRawButton(1)){
        	motor1.set(0.5);
        	}else{ motor1.set(0.0); }
        if (stick.getRawButton(2)){
        	motor1.set(-0.5);
        	}else{ motor1.set(0.0); }
    }

    public void testPeriodic() {
    
    }
    
}

Last edited by GoldenGollem : 10-04-2016 at 13:01.
Reply With Quote
  #4   Spotlight this post!  
Unread 10-04-2016, 10:14
engunneer's Avatar
engunneer engunneer is offline
Alumni turned Mentor
AKA: Branden Gunn
FRC #4761
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1996
Location: Reading, MA
Posts: 815
engunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond repute
Re: Limit Switch Programming

Code:
public void teleopPeriodic() {
  if (stick.getRawButton(1) && !limitSwitch ) {
    motor1.set(0.5);
   }
  else if (stick.getRawButton(2)) {
    motor1.set(-0.5);
  }
  else { 
    motor1.set(0.0); 
  }
}
do you have two limit switches, or just one? Which way is it supposed to prevent motion?

The key changes i made here are using boolean logic to modify the if statement, and to chain both statements together using else if.

I'd also suggest formatting your code in a nested fashion. It's really useful when debugging

Lastly, i am a proponent of making sure all your logic only has one way to set the motor, so I move all my Motor Set commands to the very end, and use a variable to keep track of what i want the motor to do. This is also helpful for displaying the desired command on smartdashboard for debugging. Similarly, gather all your inputs at the begining to meaningful variable names as well

Code:
public void teleopPeriodic() {
  boolean intakeMotorForward = stick.getRawButton(1);
  boolean intakeMotorBackward = stick.getRawButton(2);

  if (intakeMotorForward && !limitSwitch ) {
    intakeMotorSpeed = 0.5;
   }
  else if (intakeMotorBackward) {
    intakeMotorSpeed = -0.5;
  }
  else { 
    intakeMotorSpeed = 0;
  }

  motor1.set(intakeMotorSpeed); 
}
__________________
Student FRC23 (1996-1999), Mentor FRC246 (2000), Mentor FRC1318 (2007-2009), Mentor FRC93 (2011), Mentor FRC2151 (2012), Mentor FRC23 (2013), Mentor FRC4761 (2014-2017)
1998 - National Chairman's Award and Woodie Flowers Award (FRC23, Mike Bastoni ) | 2007 - PNW SF (488, 1595) | 2008 - Oregon RCA - Seattle #2 Seed, SF (488, 1696) | 2009 - Oregon #1 Seed, Winners (1983, 2635) - Seattle SF (945, 2865) - Galileo #2 Seed, SF (973, 25) | 2012 Midwest F (111, 71) | 2014 RIDE Winners (78, 125), Inspector - NEU #24, QF (3479, 3958) - NECMP #35 | 2015 Reading #11, SF (1058, 190), Inspector - RIDE #17, QF(4055, 5494), Inspector - NECMP #57 | 2016 Reading #4, SF (133, 4474), DCA, Inspector - Ride #22, SF (1735, 2067), Creativity, Inspector - NECMP #48, RCA - Archimedes
Reply With Quote
  #5   Spotlight this post!  
Unread 10-04-2016, 10:23
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,038
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Limit Switch Programming


@GoldenGollem:

1) what motor controller are you using?

2) would you please post a picture showing how you have the limit switch mounted.



Reply With Quote
  #6   Spotlight this post!  
Unread 10-04-2016, 12:56
GoldenGollem's Avatar
GoldenGollem GoldenGollem is offline
Java Programmer
FRC #2509 (Hutchinson Tigerbots)
Team Role: Programmer
 
Join Date: Dec 2015
Rookie Year: 2015
Location: Minnesota
Posts: 24
GoldenGollem is an unknown quantity at this point
Re: Limit Switch Programming

First off, thanks Engunner for the idea of using booleans to organize the code. I am using one limit switch to stop the motor from moving too far in the reverse. I am, however having issues with
Code:
if (intakeMotorForward && !limitSwitch ) {
It is showing that there is and error there.
Reply With Quote
  #7   Spotlight this post!  
Unread 10-04-2016, 15:41
engunneer's Avatar
engunneer engunneer is offline
Alumni turned Mentor
AKA: Branden Gunn
FRC #4761
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1996
Location: Reading, MA
Posts: 815
engunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond repute
Re: Limit Switch Programming

Quote:
Originally Posted by GoldenGollem View Post
First off, thanks Engunner for the idea of using booleans to organize the code. I am using one limit switch to stop the motor from moving too far in the reverse. I am, however having issues with
Code:
if (intakeMotorForward && !limitSwitch ) {
It is showing that there is and error there.
grain of salt needed - i'm a C# programmer, not java, so you may need to tweak syntax. I teach my students concepts and pseudo-code, and they have to translate it to reality.

Code:
if (intakeMotorForward && (limitSwitch == false)) {
perhaps?
__________________
Student FRC23 (1996-1999), Mentor FRC246 (2000), Mentor FRC1318 (2007-2009), Mentor FRC93 (2011), Mentor FRC2151 (2012), Mentor FRC23 (2013), Mentor FRC4761 (2014-2017)
1998 - National Chairman's Award and Woodie Flowers Award (FRC23, Mike Bastoni ) | 2007 - PNW SF (488, 1595) | 2008 - Oregon RCA - Seattle #2 Seed, SF (488, 1696) | 2009 - Oregon #1 Seed, Winners (1983, 2635) - Seattle SF (945, 2865) - Galileo #2 Seed, SF (973, 25) | 2012 Midwest F (111, 71) | 2014 RIDE Winners (78, 125), Inspector - NEU #24, QF (3479, 3958) - NECMP #35 | 2015 Reading #11, SF (1058, 190), Inspector - RIDE #17, QF(4055, 5494), Inspector - NECMP #57 | 2016 Reading #4, SF (133, 4474), DCA, Inspector - Ride #22, SF (1735, 2067), Creativity, Inspector - NECMP #48, RCA - Archimedes
Reply With Quote
  #8   Spotlight this post!  
Unread 10-04-2016, 16:02
rich2202 rich2202 is offline
Registered User
FRC #2202 (BEAST Robotics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Wisconsin
Posts: 1,168
rich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond repute
Re: Limit Switch Programming

Quote:
Originally Posted by GoldenGollem View Post
First off, thanks Engunner for the idea of using booleans to organize the code. I am using one limit switch to stop the motor from moving too far in the reverse. I am, however having issues with
Code:
if (intakeMotorForward && !limitSwitch ) {
It is showing that there is and error there.

Did you define the variable and assign a value to it (get the limit switch status)?
Reply With Quote
  #9   Spotlight this post!  
Unread 10-04-2016, 20:42
Brian Ho Brian Ho is offline
Registered User
FRC #4343 (MaxTech)
Team Role: Mentor
 
Join Date: Nov 2012
Rookie Year: 2012
Location: Canada
Posts: 4
Brian Ho is an unknown quantity at this point
Re: Limit Switch Programming

Quote:
Originally Posted by GoldenGollem View Post
First off, thanks Engunner for the idea of using booleans to organize the code. I am using one limit switch to stop the motor from moving too far in the reverse. I am, however having issues with
Code:
if (intakeMotorForward && !limitSwitch ) {
It is showing that there is and error there.
There are 2 simple problems with your code.

First,
On top, where you declared
Code:
DigitalInput limitSwitch;
change to
Code:
DigitalInput limitSwitch = new DigitalInput(x);
where x is the port number where your limit switch is plugged in at the DIO part of the roborio

Second,
It's not just
Code:
!limitSwitch
You need to use
Code:
!limitSwitch.get()
Which gets you the boolean value of the limit switch which may be true or false depending if you wired it as normally opened or normally closed.
Reply With Quote
  #10   Spotlight this post!  
Unread 10-04-2016, 21:32
engunneer's Avatar
engunneer engunneer is offline
Alumni turned Mentor
AKA: Branden Gunn
FRC #4761
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1996
Location: Reading, MA
Posts: 815
engunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond repute
Re: Limit Switch Programming

Quote:
Originally Posted by Brian Ho View Post
You need to use
Code:
!limitSwitch.get()
Which gets you the boolean value of the limit switch which may be true or false depending if you wired it as normally opened or normally closed.
To fit this into my earlier recommendation about collecting the inputs and outputs at the edges of the code, be sure your limit switch has a meaningful name and that "true" and "false" make sense. then you only have to do inversion once when you read the input, and not have to think about it when you use it later.

Code:
boolean intakeHitLimitInNegativeDirection = !limitswitch.get();

...rest of code...
and be sure your electrical team keeps consistent wiring on motor connections, such that sending a positive command always moves your motor in the same direction from system to system. Even better, label the mechanism with arrows specifying the plus direction until you are fully confident in all your code.

For future code, consider looking at the command template. It feels more complicated than iterative, but certain concepts in command are far more powerful than iterative, and Robot Builder is a big benefit as well.
__________________
Student FRC23 (1996-1999), Mentor FRC246 (2000), Mentor FRC1318 (2007-2009), Mentor FRC93 (2011), Mentor FRC2151 (2012), Mentor FRC23 (2013), Mentor FRC4761 (2014-2017)
1998 - National Chairman's Award and Woodie Flowers Award (FRC23, Mike Bastoni ) | 2007 - PNW SF (488, 1595) | 2008 - Oregon RCA - Seattle #2 Seed, SF (488, 1696) | 2009 - Oregon #1 Seed, Winners (1983, 2635) - Seattle SF (945, 2865) - Galileo #2 Seed, SF (973, 25) | 2012 Midwest F (111, 71) | 2014 RIDE Winners (78, 125), Inspector - NEU #24, QF (3479, 3958) - NECMP #35 | 2015 Reading #11, SF (1058, 190), Inspector - RIDE #17, QF(4055, 5494), Inspector - NECMP #57 | 2016 Reading #4, SF (133, 4474), DCA, Inspector - Ride #22, SF (1735, 2067), Creativity, Inspector - NECMP #48, RCA - Archimedes
Reply With Quote
  #11   Spotlight this post!  
Unread 10-04-2016, 13:00
GoldenGollem's Avatar
GoldenGollem GoldenGollem is offline
Java Programmer
FRC #2509 (Hutchinson Tigerbots)
Team Role: Programmer
 
Join Date: Dec 2015
Rookie Year: 2015
Location: Minnesota
Posts: 24
GoldenGollem is an unknown quantity at this point
Re: Limit Switch Programming

@Ether
We are using a Talon SR motor control with the motor. I do not have a picture of the actual limit switch mount (we have just added it and we are not in the shop today). We have tested the mechanics behind it and that half of it should all work so we just need the coding aspect of it done. Thanks -GG
Reply With Quote
  #12   Spotlight this post!  
Unread 10-04-2016, 14:41
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,038
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Limit Switch Programming

Quote:
Originally Posted by GoldenGollem View Post
I do not have a picture of the actual limit switch mount (we have just added it and we are not in the shop today). We have tested the mechanics behind it and that half of it should all work
Just a heads-up: Here's why I asked.


Reply With Quote
Reply


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 04:55.

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