|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Re: Limit Swtich Help
Oh sorry I miss read that reply I understand now, my bad
|
|
#2
|
||||
|
||||
|
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;
}
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);
}
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! |
|
#3
|
|||
|
|||
|
Re: Limit Swtich Help
Quote:
![]() |
|
#4
|
|||
|
|||
|
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);
}
}
}
|
|
#5
|
||||
|
||||
|
Re: Limit Swtich Help
Quote:
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. |
|
#6
|
|||
|
|||
|
Re: Limit Swtich Help
Woops I will fix that up thanks.
|
|
#7
|
|||
|
|||
|
Re: Limit Swtich Help
Quote:
Code:
int x=0;
if(x=8)
{
}
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. |
|
#8
|
||||
|
||||
|
Re: Limit Swtich Help
Quote:
Last edited by Ether : 04-02-2015 at 22:55. |
|
#9
|
||||
|
||||
|
Re: Limit Swtich Help
Quote:
Code:
if ()//the conditions you want it to go up
{
//go up
}
else if ()//the conditions you want it to go down
{
//go down
}
else
{
//stop
}
|
|
#10
|
||||
|
||||
|
Re: Limit Swtich Help
Quote:
Code:
xbox.getRawButton(4) && !limitPressed ![]() Reps from Ether! I am showing my students that one!!! You might have a little bit of a fan club here at Team 2485...we seriously owe a lot to you and it is a shame if I haven't said that and "thank you" before (which is probably the case as I am mainly a lurker...but I am working on it). |
|
#11
|
|||
|
|||
|
Re: Limit Swtich Help
Quote:
if(xbox.getRawAxis(I need to check mapping)) { victor 1.set(1); victor2.set(1); } Last edited by curtis0gj : 05-02-2015 at 06:46. |
|
#12
|
||||
|
||||
|
Re: Limit Swtich Help
Good catch mmaunu... I can only blame Sleep deprivation from the build season
![]() Curtis, that looks correct for dealing with two limit switches, good job! If you want to control the victors with axis instead of buttons, you need to pay attention to what the return types are. A button returns a Boolean, true or false, as it only has two states (pressed or not pressed). An axis from a joystick, however, returns a number between -1 and 1, indicating how far it is moved from the center. Treating that number like a Boolean in an if statement won't do what you want. Instead, you have three options: Option 1 - turn the number into a Boolean value by using a comparison. You can say something like "if(axis > 0)" to indicate that you want to do something if the value of the axis is positive. Other handy comparisons are less than (<), equals (previously discussed, ==), and not equals (!=). Option 2 - use the axis to give you variable speed control over the motor. By passing the axis directly into the motor, something like "victor1.set(axis)", the motor will go at a speed proportional to how far you move the joy stick, and it'll go both up and done with that one command! Also, if you let go of the joystick, it'll stop Option 3 - this is kind of a combination of options 1 and 2 by using something called a ternary operator. A ternary operator is kind of like doing an if statement in a single line. An example would be "axis>0?1:0;". You can read this as " if the axis is greater than 0, then use the value 1, otherwise use the value 0". Everything before the question mark is your conditional. Everything between the question mark and the colon is what happens if true, and everything after the colon is what happens when false. So, if we always want the elevator moving at full speed, we could do something like "victor1.set(axis>0?1:axis<0?-1:0);". Here I strung two ternary operators together - if the axis is greater than 0, return 1, otherwise if the axis is less than 0 return -1, otherwise return 0. Many people find ternary operators confusing and non-intuitive, so don't feel bad about going with a more straightforward option! Finally, a word of warning. Often when you let go of a joystick, it will return to something very close to 0, but not quite there. In all the examples above, I used an absolute 0 value for comparisons, which won't work unless the joystick returns to be perfectly centered! It's a lot better to use a "deadband" around 0 - go up if it's greater than 0.05, go down if less than -0.05, otherwise stop. That way if the joystick is close to center you'll stop. |
|
#13
|
|||
|
|||
|
Re: Limit Swtich Help
Quote:
Code:
axis = Xbox.getRawAxis(someaxis); // Do I need float or double?
if(axis == 1) {
victor1.set(1);
victor2.set(1);
}
|
|
#14
|
||||
|
||||
|
Re: Limit Swtich Help
Quote:
Your code is actually really close. Joystick axis values are returned as doubles, but that's a good question! There is often a lot of confusion between float and double, as they appear to be basically the same. In situations like thiszits useful to look at the javadoc For the object your using. A quick search turned up a copy here: http://team2168.org/javadoc/ go there and click on "Joystick" in the class listing on the left. You can then scroll through all of the methods in the joystick class, find the one you're using, get a short description of it, and see its return type. Pretty handy! With the code you posted, the elevator will go up only if you have the joystick pushed full forward so the return value is 1. Pushing it halfway, so you get 0.5, wouldn't move it. Additionally, if you aren't pushing perfectly straight, you won't get a return value of 1! Take a piece of graph paper, draw a set of axis, and then draw a circle with a radius of 1, centered on the origin. Everything inside that circle is a value that can be returned by the joystick. If you push the joystick forward, you could get 0 for the x axis and 1 for the y axis... Or you could get something just a little less than 1 for the y axis and a little greater or less than 0 for the x axis, if you aren't perfectly straight. So the only problem with your code is that it demands perfection from the driver to work. It would be much better to do something like "axis > 0.5" (pick an appropriate number) so you have a range on the joystick where pushing it forward makes it move. That way your driver doesn't have to be perfect! |
|
#15
|
|||
|
|||
|
Re: Limit Swtich Help
Quote:
Will this work? Code:
double axis = Xbox.getRawAxis(*));
if(axis > 0.5) {
victor1.set(1);
victor2.set(1);
}
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|