Chief Delphi

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

GoldenGollem 04-10-2016 07:54 AM

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.

engunneer 04-10-2016 08:02 AM

Re: Limit Switch Programming
 
Which program template are you using?

Can you post an example?

GoldenGollem 04-10-2016 08:52 AM

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() {
   
    }
   
}


engunneer 04-10-2016 10:14 AM

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);
}


Ether 04-10-2016 10:23 AM

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.




GoldenGollem 04-10-2016 12:56 PM

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.

GoldenGollem 04-10-2016 01:00 PM

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

Ether 04-10-2016 02:41 PM

Re: Limit Switch Programming
 
Quote:

Originally Posted by GoldenGollem (Post 1570301)
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.



engunneer 04-10-2016 03:41 PM

Re: Limit Switch Programming
 
Quote:

Originally Posted by GoldenGollem (Post 1570298)
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?

rich2202 04-10-2016 04:02 PM

Re: Limit Switch Programming
 
Quote:

Originally Posted by GoldenGollem (Post 1570298)
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)?

Brian Ho 04-10-2016 08:42 PM

Re: Limit Switch Programming
 
Quote:

Originally Posted by GoldenGollem (Post 1570298)
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.

engunneer 04-10-2016 09:32 PM

Re: Limit Switch Programming
 
Quote:

Originally Posted by Brian Ho (Post 1570546)
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.


All times are GMT -5. The time now is 07:24 AM.

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