View Single Post
  #3   Spotlight this post!  
Unread 04-02-2015, 21:27
curtis0gj curtis0gj is offline
Registered User
FRC #5033 (Beavertronics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Canada
Posts: 121
curtis0gj will become famous soon enough
Re: Limit Swtich Help

Quote:
Originally Posted by Jon Stratis View Post
Ok, some analysis of the logic.

First, the only place you set limitPressed to false is at the very beginning. by having
Code:
            if(limit.get()) {
            	
            	limitPressed = true;
            	
            }
in operatorControl, this means that once the limit switch is pressed, limitPressed will ALWAYS be true - there is nothing to set it to false later.

Also, there is an interesting case where you hit the limit switch, so you set them to 0, but if you're still holding button 4 you then immediately set them to 1. What happens is you start to quickly oscillate between setting the motors to 0 and 1 as long as both button 4 and the limit switch is pressed.

Now, a note about how limit switches work (I'm afraid I can't view the picture from where I am... it's blocked by the school's connection). There are two ways to wire them, and they act exactly opposite in code. If you wire signal and ground to the common/normally open connectors, the switch will be TRUE when not touched, and FALSE when pressed. Connect between the common/normally closed connectors, and it operates the other way.

So, here's some code that should help:

Code:
//ensure that limitPressed is true only when the limit switch is pressed
limitPressed = limit.get();

//output the value of limitPressed so we can verify proper functionality of the limit switch
System.out.println(“limitPressed=“ + limitPressed);


//if we hit the limit switch, stop all motion
if (limitPressed)
{
	victor1.set(0);
	victor2.set(0);
}
//otherwise, if we hit button 4 move forward
else if (xbox.getRawButton(4))
{
	victor1.set(1);
	victor2.set(1);
}
//otherwise, if we aren’t pressing anything don’t move.
else 
{
	victor1.set(0);
	victor2.set(0);
}
This code only works to power the motors in a single direction, with the assumption that moving in that direction will trigger the limit switch. Without knowing the exact setup, I can't be sure about powering in both directions and stopping at both limits... I'll leave the other direction up to you.

Note a couple of things in the code:

First, I'm writing the value of the limitPressed variable to the console. This will let you look in the RIOLog to see what it is at all times - before moving anything, try manually pressing it and see how the value changes. You want to make sure this works before doing anything else!

Next, ALL of the controls for the motors are contained in a single if/else-if block. This means that only one of them will be active at a time. The default state (the final else) is to stop the motors - if you aren't touching anything, they'll stop. The limit switch control comes first to ensure that, if the limit switch is pressed, it absolutely doesn't move.

Give that a try, see if it helps, and see if you can figure out the other direction from there!
Thank you so much, our team has been struggling along in the programming department this year mostly because I am the only programmer and it is my first year programming in java. But this is a great help and I think I will be far more confident when using booleans in the future. I will post an update tomorrow afternoon regarding the limit switch status, thanks!
Reply With Quote