Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Limit Swtich Help (http://www.chiefdelphi.com/forums/showthread.php?t=133983)

curtis0gj 02-04-2015 05:16 PM

Limit Swtich Help
 
Hi guys, our team is trying to use a limit switch to stop the lifting mech powered by 2 victor motor controllers from bottoming out or flying off the top. But I can't get the switch to work properly.

Code:

public class Robot extends SampleRobot {
       
    RobotDrive robot;
    Joystick stick;
    Joystick xbox;

    Victor victor1;
    Victor victor2;
    DigitalInput limit;
   
   
    boolean limitPressed = false;

    boolean buttonPressedForwardVictor = false;

public Robot() {
           
            robot = new RobotDrive(0, 1);
        stick = new Joystick(1);
        xbox = new Joystick(0);

        limit = new DigitalInput(4);       
       
        victor1 = new Victor(4);
        victor2 = new Victor(5);
 
    }

public void operatorControl() {
           
            while (isOperatorControl() && isEnabled()) {
               
            stick.getThrottle();
            robot.arcadeDrive(stick.getY(), stick.getX());
            Timer.delay(0.1);
   
            if(limit.get()) {
                   
                    limitPressed = true;
                   
            }
           
            if(limitPressed = true) {
                   
                    victor1.set(0);
                    victor2.set(0);
                   
            }
           
            if (xbox.getRawButton(4)) {
                   
                    victor1.set(1);
                    victor2.set(1);
                    buttonPressedForwardVictor = true;
                   
            } else if (buttonPressedForwardVictor = true) {
                   
                    victor1.set(0);
                    victor2.set(0);
                    buttonPressedForwardVictor = false;
            }
           
            if (xbox.getRawButton(1)) {
                   
                    victor1.set(-1);
                    victor2.set(-1);
            }
           
                   
            }
           
            }


Jon Stratis 02-04-2015 05:21 PM

Re: Limit Swtich Help
 
You need to use "==" inside of an if statement to get a Boolean. A single "=" is an assignment operation and won't do what you want it to do.

curtis0gj 02-04-2015 05:38 PM

Re: Limit Swtich Help
 
Quote:

Originally Posted by Jon Stratis (Post 1438369)
You need to use "==" inside of an if statement to get a Boolean. A single "=" is an assignment operation and won't do what you want it to do.

No luck with that but I did make those changes.

Ether 02-04-2015 05:39 PM

Re: Limit Swtich Help
 
Quote:

A single "=" is an assignment operation and won't do what you want it to do.
True. (Hey, a little Boolean humor there).

Quote:

Originally Posted by Jon Stratis (Post 1438369)
You need to use "==" inside of an if statement to get a Boolean.

You don't need "==" to return a Boolean: a single "=" inside an if() statement will indeed return a Boolean. But as you said above, not what the OP was expecting.

If you already have an appropriately-named Boolean variable, you can cleanly code it by simply testing the variable:

if (limitPressed) {...}



curtis0gj 02-04-2015 06:12 PM

Re: Limit Swtich Help
 
Quote:

Originally Posted by Ether (Post 1438376)
True. (Hey, a little Boolean humor there).



You don't need "==" to return a Boolean: a single "=" inside an if() statement will indeed return a Boolean. But as you said above, not what the OP was expecting.

If you already have an appropriately-named Boolean variable, you can cleanly code it by simply testing the variable:

if (limitPressed) {...}



So I will keep my if statements to one =. The limit switch is still not working here is a link for a picture of it. http://imgur.com/nRB3VqN. We used ground and signal from our pwm and it goes to DIO 4.

Ether 02-04-2015 06:15 PM

Re: Limit Swtich Help
 
Quote:

Originally Posted by curtis0gj (Post 1438398)
So I will keep my if statements to one =.

You did not read my post very carefully.



curtis0gj 02-04-2015 06:19 PM

Re: Limit Swtich Help
 
Quote:

Originally Posted by Ether (Post 1438400)
You did not read my post very carefully.



Oh sorry I miss read that reply I understand now, my bad

Jon Stratis 02-04-2015 06:50 PM

Re: Limit Swtich Help
 
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!

mmaunu 02-04-2015 07:03 PM

Re: Limit Swtich Help
 
Edit: ninja'd by Jon Stratis. I think that we are saying the same basic things. I'll leave it in case...


Quote:

Originally Posted by Ether (Post 1438376)
You don't need "==" to return a Boolean: a single "=" inside an if() statement will indeed return a Boolean. But as you said above, not what the OP was expecting.

Actually, in Java, a single "=" does not return a boolean value. If you are assigning to a boolean value, then you will get the newly assigned value (which would actually be a boolean of course), but if you are assigning to an int or some other type, then the result is actually the value assigned.

For example:
Code:

boolean b = < something >

if( b = true )

The boolean expression evaluates to true in Java because you assigned true to the variable.

A different example:
Code:

int x = < something >

if( x = 8 )

The above will not compile because the value of the expression "x = 8" is actually the integer 8. This is what also allows you to do things like:

Code:

int a = < something >

int b = a = 9;

This gives a the value of 9 and then uses that assigned value to assign 9 to b. (Replace "a = 9" with 9.)

As for the original question/concern, have you tested that the get() method on the limit switch returns true when the limit switch is pushed? Depending on how you wire it (at least the ones that we have used in the past), you can get either true when the switch is triggered and false when it is not OR you can get false when the switch is triggered and true when it is not.

You also never set the limitPressed variable back to false; once set to true, limitPressed seems to remain true until you restart the robot.

Finally, if the limit switch is supposed to cancel the action that the joystick's button is performing, then you might want to change:

Code:

if (xbox.getRawButton(4)) {
                   
                    victor1.set(1);
                    victor2.set(1);
                    buttonPressedForwardVictor = true;
                   
 }

so that it includes a test for the limit switch. Assuming that limitPressed is only true when the limit switch is activated, you could try a condition like:

Code:

if (xbox.getRawButton(4)  &&  limitPressed == false) {
Without that extra condition, your logic is setting the motors to stop (above this line, where you check for the limitPressed value) and then you re-check the button...which re-sets the motors to run at full speed if the button is still engaged. The condition for running the motors needs to check the state of the button and the state of the limit switch. Another option is to use an else-if on the button condition so that you either set the victors to a value of 0 or set them to a value of 1 but not both.

Code:

if(limitPressed = true) {
                   
              victor1.set(0);
              victor2.set(0);
                   
}
           
else if (xbox.getRawButton(4)) {
                   
              victor1.set(1);
              victor2.set(1);
              buttonPressedForwardVictor = true;
                   
}

I apologize if that was a bit scattered. Good luck and ask if anything/everything is unclear.

Ether 02-04-2015 07:15 PM

Re: Limit Swtich Help
 
Quote:

Originally Posted by mmaunu (Post 1438425)
If you are assigning to a boolean value, then you will get the newly assigned value (which would actually be a boolean of course)

That was the context for the discussion.

if (limitPressed = true)

will return a boolean: it will always return "true" regardless of the value of limitPressed prior to the test.



curtis0gj 02-04-2015 09:27 PM

Re: Limit Swtich Help
 
Quote:

Originally Posted by Jon Stratis (Post 1438418)
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! :]

curtis0gj 02-04-2015 10:15 PM

Re: Limit Swtich Help
 
I have setup the limit switch program with the suggestions but I am attempting to setup reverse for the victors. Also I am trying to setup the same if statement layout for my relays does this look okay?
Code:

public class Robot extends SampleRobot {
    RobotDrive robot;
    Joystick stick;
    Joystick xbox;
    Relay spike1;
    Relay spike2;
    Victor victor1;
    Victor victor2;
    DigitalInput limit;
    boolean limitPressed = false;

    public Robot() {
            robot = new RobotDrive(0, 1);
        stick = new Joystick(1);
        xbox = new Joystick(0);
        spike1 = new Relay(0);
        spike2 = new Relay(1);
        limit = new DigitalInput(4);
        victor1 = new Victor(4);
        victor2 = new Victor(5);
    }
    public void operatorControl() {
           
            while (isOperatorControl() && isEnabled()) {
            stick.getThrottle();
            robot.arcadeDrive(stick.getY(), stick.getX());
            limitPressed = limit.get(); //Do I want this out side of my while operator control loop?
            System.out.println("limitPressed=" + limitPressed); //Read the RoboRIO log for some values before you go all out on your motors.
           
            if(limitPressed) {
                    victor1.set(0);
                    victor2.set(0);
            } else if (xbox.getRawButton(4)) {
                    victor1.set(1);
                    victor2.set(1);
            } else {
                    victor1.set(0);
                    victor1.set(0);
            }
            if(xbox.getRawButton(1)) {
                    victor1.set(-1);
                    victor1.set(-1);
            } else {
                    victor1.set(0);
                    victor2.set(0);
            }
            if(xbox.getRawButton(3)) {
                    spike1.set(Relay.Value.kForward);
                    spike2.set(Relay.Value.kForward);
            } else {
                    spike1.set(Relay.Value.kOff);
                    spike2.set(Relay.Value.kOff);                   
            }
            if(xbox.getRawButton(2)) {
                    spike1.set(Relay.Value.kReverse);
                    spike2.set(Relay.Value.kReverse);       
            } else {
                    spike1.set(Relay.Value.kOff);
                    spike2.set(Relay.Value.kOff);
            }   
            }
    }


mmaunu 02-04-2015 10:21 PM

Re: Limit Swtich Help
 
Quote:

Originally Posted by curtis0gj (Post 1438532)
I have setup...
Code:

public class Robot extends SampleRobot {
   
            if(limitPressed) {
                    victor1.set(0);
                    victor2.set(0);
            } else if (xbox.getRawButton(4)) {
                    victor1.set(1);
                    victor2.set(1);
            } else {
                    victor1.set(0);
                    victor1.set(0);
            }
            if(xbox.getRawButton(1)) {
                    victor1.set(-1);
                    victor1.set(-1);
            } else {


A quick glance (I'll look longer later) shows that you have set victor1 twice in several places when you should be setting victor1 and victor2.

Ether: I have learned so much from reading your posts and papers over the years. I didn't intend to sound like I was correcting your post but merely adding explicit context for other readers.

curtis0gj 02-04-2015 10:26 PM

Re: Limit Swtich Help
 
Quote:

Originally Posted by mmaunu (Post 1438538)
A quick glance (I'll look longer later) shows that you have set victor1 twice in several places when you should be setting victor1 and victor2.

Woops I will fix that up thanks.

cstelter 02-04-2015 10:42 PM

Re: Limit Swtich Help
 
Quote:

Originally Posted by mmaunu (Post 1438538)
Ether: I have learned so much from reading your posts and papers over the years. I didn't intend to sound like I was correcting your post but merely adding explicit context for other readers.

I for one appreciated the post. As a C/C++ guy who only recently has been using java (and only WRT FRC), I would have expected

Code:

int x=0;
if(x=8)
{
}

to compile fine. I didn't realize the if statement was strongly typed to boolean in java (kind of would have expected it to be overloaded to many types or that there may be some sort of implicit conversion of int to boolean that would happen transparently.)

Of course had I tried to code it, eclipse/netbeans/whatever would have let me know I was wrong long before I tried compile.

Reminds me of the most 'duh' moment I ever had that IDE's don't tend to flag for us yet. I had coded something along the lines of

int x=0;
...
if(x==4 && something_else==2 && something_else==some_other_thing);
{
//some stuff to do
}

It took me the longest time to figure out why I kept entering the if clause even though I was nearly 100% certain that x was *not* 4 and thus the hypothesis should evaluate to false. Finally I put in a sanity check to print x and confirmed that I was right (x was not 4 and the full expression for the if evaluated to false). Only then was I able to see the error of my ways...

sorry to send this slightly off topic.... I did appreciate the points made thoug.


All times are GMT -5. The time now is 08:42 AM.

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