Go to Post Honor is doing the right thing when nobody is watching. - Daniel_LaFleur [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 12-02-2016, 09:09
nlt5 nlt5 is offline
Registered User
FRC #5676
 
Join Date: Feb 2016
Location: Michigan
Posts: 23
nlt5 is an unknown quantity at this point
Help with Code

Thanks to all on this sight for the guidance offered to all teams. We are working hard trying to figure out how to write code and we need some help. below is the code we have written so far. What we want to have happen is for our drive system to be arcade and we are using the left joystick on our xbox controller and we have two shooter motors. Ideally I would like to have the shooter motors turn on and off with one button but I am not sure how to write that so I attempted to start the motors with the press of one button and turn them off with another button. So far nothing works. The drive motors turn in opposite directions, so I attempted to invert the motors. This allowed us to drive forward and reverse but we could not turn. I am not concerned with autonomous at this time just teleop. I would appreciate any insight into problems with this code and suggestions on how to make it work. Thank you in advance. (sorry i'm not sure the best way to attache my code.)

Code:
package org.usfirst.frc.team5676.robot;

import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.buttons.*;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;

/**
 * The VM is configured to automatically run this class, and to call the
 * functions corresponding to each mode, as described in the IterativeRobot
 * documentation. If you change the name of this class or the package after
 * creating this project, you must also update the manifest file in the resource
 * directory.
 */
public class Robot extends IterativeRobot {
    RobotDrive DriveTrain41;
    SpeedController victorsp1;
    SpeedController victorsp2;
    SpeedController victorsp3;
    SpeedController victorsp4;
    SpeedController victorsp5;
    SpeedController victorsp6;
    RobotDrive shootermotors;
    Joystick xboxcontroller;
    JoystickButton Leftjoystick;
    JoystickButton xbutton;
    JoystickButton ybutton;
    int autoLoopCounter;
    
    /**
     * This function is run when the robot is first started up and should be
     * used for any initialization code.
     */
    public void robotInit() {
        DriveTrain41 = new RobotDrive(victorsp1,victorsp2,victorsp3, victorsp4);
        shootermotors = new RobotDrive(victorsp5,victorsp6);
        xboxcontroller = new Joystick(0);
        xbutton = new JoystickButton(xboxcontroller,8);
        ybutton = new JoystickButton(xboxcontroller, 9);
        
    }
    
    /**
     * This function is run once each time the robot enters autonomous mode
     */
    public void autonomousInit() {
        autoLoopCounter = 0;
    }

    /**
     * This function is called periodically during autonomous
     */
    public void autonomousPeriodic() {
        if(autoLoopCounter < 100) //Check if we've completed 100 loops (approximately 2 seconds)
        {
            DriveTrain41.drive(-0.5, 0.0);     // drive forwards half speed
            autoLoopCounter++;
            } else {
            DriveTrain41.drive(0.0, 0.0);     // stop robot
        }
    }
    
    /**
     * This function is called once each time the robot enters tele-operated mode
     */
    public void teleopInit(){
    }

    /**
     * This function is called periodically during operator control
     */
    public void teleopPeriodic() {
        DriveTrain41.arcadeDrive(xboxcontroller);
        shootermotors.setInvertedMotor(RobotDrive.MotorType.kFrontLeft, true);
        
        if(xboxcontroller.getRawButton(8)){
            shootermotors.drive(1,0);
        }
        else if (xboxcontroller.getRawButton(9)){
            shootermotors.drive(0, 0);
        }
   
     
        
        
    }
    
    /**
     * This function is called periodically during test mode
     */
    public void testPeriodic() {
        LiveWindow.run();
    }
    
}

Last edited by nlt5 : 12-02-2016 at 10:16.
Reply With Quote
  #2   Spotlight this post!  
Unread 12-02-2016, 10:49
rich2202 rich2202 is offline
Registered User
FRC #2202 (BEAST Robotics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Wisconsin
Posts: 1,171
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: Help with Code

I'm not a coder, but I stayed at a Holiday Inn Express last night ....


In Public Class, you create the objects for each motor controller (speedcontroller).

In Robotinit, you tell robotdrive which are your 4 drive motors.

Where do you associate the motorcontroller (victorsp1 for example) with a particular channel address (either PWM port, or CAN port)? Something like what you are doing for xboxcontroller where you associate it with Joystick(0), and then with xbutton and y button.

Last edited by rich2202 : 12-02-2016 at 10:58.
Reply With Quote
  #3   Spotlight this post!  
Unread 12-02-2016, 12:11
TimTheGreat's Avatar
TimTheGreat TimTheGreat is offline
ArchdukeTim
FRC #1418 (Vae Victis)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2011
Location: Falls Church
Posts: 234
TimTheGreat has a spectacular aura aboutTimTheGreat has a spectacular aura aboutTimTheGreat has a spectacular aura about
Re: Help with Code

So you're using teleop-periodic. This means that the teleop loop gets called over and over. You can get what you want out of the joystick buttons with this concept.

Code:
    if (getButton)
        shoot.start()
    else
        shoot.stop()
This will stop the motors if the button isn't pressed. You don't need another button to stop
__________________
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.



2012 - Gracious Professionalism - Greater DC
2014 - Regional Finalist - Virginia | Industrial Design - Virginia | Regional Finalist - Greater DC
2015 - Innovation in Control - Greater DC
2016 - District Event Winner - VAHAY | Innovation in Control - VAHAY | District Event Winner - MDBET | Industrial Design - MDBET | District Champion - CHCMP | Innovation in Control - CHCMP
Reply With Quote
  #4   Spotlight this post!  
Unread 12-02-2016, 12:44
nlt5 nlt5 is offline
Registered User
FRC #5676
 
Join Date: Feb 2016
Location: Michigan
Posts: 23
nlt5 is an unknown quantity at this point
Re: Help with Code

I take that to mean that the button will have to be held down in order to turn the motors on. Is there a way that I can turn the motors on when I first press the button then turn them off when I press the button again?
Reply With Quote
  #5   Spotlight this post!  
Unread 12-02-2016, 14:53
TimTheGreat's Avatar
TimTheGreat TimTheGreat is offline
ArchdukeTim
FRC #1418 (Vae Victis)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2011
Location: Falls Church
Posts: 234
TimTheGreat has a spectacular aura aboutTimTheGreat has a spectacular aura aboutTimTheGreat has a spectacular aura about
Re: Help with Code

Quote:
Originally Posted by nlt5 View Post
I take that to mean that the button will have to be held down in order to turn the motors on. Is there a way that I can turn the motors on when I first press the button then turn them off when I press the button again?
Oh I see. So one way you can do it is set a boolean. Then check the boolean to see if it's true.

*Taken from Ether*
Code:
teleop_init
    button_previous = false;
...
teleop_periodic
    button_now = get_button(); // get the button state (pressed or not pressed)

    if (button_now && ! button_previous) // detect rising edge only

    runMotor = ! runMotor; // if rising edge, toggle the direction boolean

    button_previous = button_now; // save the button state for comparison in the next iteration

    if (runMotor)
        motor.set(1)
__________________
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.



2012 - Gracious Professionalism - Greater DC
2014 - Regional Finalist - Virginia | Industrial Design - Virginia | Regional Finalist - Greater DC
2015 - Innovation in Control - Greater DC
2016 - District Event Winner - VAHAY | Innovation in Control - VAHAY | District Event Winner - MDBET | Industrial Design - MDBET | District Champion - CHCMP | Innovation in Control - CHCMP
Reply With Quote
  #6   Spotlight this post!  
Unread 12-02-2016, 15:06
rich2202 rich2202 is offline
Registered User
FRC #2202 (BEAST Robotics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Wisconsin
Posts: 1,171
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: Help with Code

Quote:
Code:
  button_now = get_button(); // get the button state (pressed or not pressed)

    if (button_now && ! button_previous) // detect rising edge only

    runMotor = ! runMotor; // if rising edge, toggle the direction boolean

    button_previous = button_now; // save the button state for comparison in the next iteration

    if (runMotor)
        motor.set(1)
In the past, with that logic, we ran into "bounce" problems with the buttons. As the buttons transition between states (pressed/unpressed), they throw off a lot of state transitions.

In other words, if you actually counted the number of transitions with a single button press, you can get 5 or more transitions. In essence, the 1/2 way point of a button is "undefined", and can be either pressed or not pressed, and it can change between reads.

If it is simply starting a motor when pressed, and stopping a motor when released, you probably won't notice it (the motor controller cycles between off/on really fast). If it is something else (like a toggle), you could end up in an unexpected state.

In order to avoid the problem, you have to set a timer. You reset the timer when a valid transition is detected. If the next detected transition is within 250 milliseconds (1/4 second), you ignore it. Remember to debounce the release too so you don't get an accidental "press" when the button is "released".

Last edited by rich2202 : 12-02-2016 at 15:13.
Reply With Quote
  #7   Spotlight this post!  
Unread 13-02-2016, 14:08
JacobD's Avatar
JacobD JacobD is offline
Registered User
AKA: Jacob
FRC #1672 (Mahwah Robo T-Birds)
Team Role: Leadership
 
Join Date: Jan 2015
Rookie Year: 2013
Location: New Jersey
Posts: 90
JacobD is an unknown quantity at this point
Re: Help with Code

If you are trying to use two motors at the same time. The best way to synchronize them is use a PWM Y splitter. This will eliminate the possibilities of them not being synced.
__________________
2013-2014: Electrical, Mechanical
2014-2017: Team Captain
Reply With Quote
  #8   Spotlight this post!  
Unread 15-02-2016, 16:18
CyberTeam5713 CyberTeam5713 is offline
Registered User
FRC #5713
 
Join Date: Feb 2016
Location: Michigan
Posts: 25
CyberTeam5713 is an unknown quantity at this point
Hey Can anyone help me this is my first year coding.

Down below is my code and we want the robot to drive with arcade with a xbox controller also plz help me to get it to turn. And we are using the standard wheels on the robot



package org.usfirst.frc.team5713.robot;

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



public class Robot extends IterativeRobot {
RobotDrive drive = new RobotDrive(1,2,3,4);
Joystick driveStick = new Joystick(0);
Joystick controlStick = new Joystick(1);
Talon frontLeft = new Talon(1);
Talon rearLeft = new Talon(2);
Talon frontRight = new Talon(3);
Talon rearRight = new Talon(4);

public void robotInit() {
drive = new RobotDrive(1,2,3,4);
driveStick = new Joystick(0);
controlStick = new Joystick(1);
frontLeft = new Talon(1);
frontLeft.enableDeadbandElimination(true);
frontLeft.set(+1.0);
rearLeft = new Talon(2);
rearLeft.enableDeadbandElimination(true);
rearLeft.set(-1.0);
frontRight = new Talon(3);
frontRight.enableDeadbandElimination(true);
frontRight.set(+1.0);
rearRight = new Talon(4);
rearRight.enableDeadbandElimination(true);
rearRight.set(-1.0);

public void teleopInit(){
drive = new RobotDrive(1,2,3,4);
driveStick = new Joystick(0);
controlStick = new Joystick(1);
frontLeft = new Talon(1);
frontLeft.enableDeadbandElimination(true);
frontLeft.set(-1.0);
rearLeft = new Talon(2);
rearLeft.enableDeadbandElimination(true);
rearLeft.set(+1.0);
frontRight = new Talon(3);
frontRight.enableDeadbandElimination(true);
frontRight.set(-1.0);
rearRight = new Talon(4);
rearRight.enableDeadbandElimination(true);
rearRight.set(+1.0);

}




public void teleopPeriodic() {
while (isOperatorControl() && isEnabled()) {
drive.setSafetyEnabled(true);


double joystickLeftY = driveStick.getRawAxis(2);
double joystickLeftX = driveStick.getRawAxis(1);
drive.arcadeDrive(joystickLeftY, joystickLeftX, true);
Timer.delay(0.01); } }
Reply With Quote
  #9   Spotlight this post!  
Unread 15-02-2016, 16:21
CyberTeam5713 CyberTeam5713 is offline
Registered User
FRC #5713
 
Join Date: Feb 2016
Location: Michigan
Posts: 25
CyberTeam5713 is an unknown quantity at this point
Re: Hey Can anyone help me this is my first year coding.

also our middle wheel and back are the only ones connecting to the drive train.
we have two motors on our middle wheel and two on the other middle wheel.
so we can go forward and backward
Reply With Quote
  #10   Spotlight this post!  
Unread 17-02-2016, 14:36
gmnielsen gmnielsen is offline
Registered User
FRC #5265
 
Join Date: Apr 2015
Location: Maine
Posts: 1
gmnielsen is an unknown quantity at this point
Re: Help with Code

Did you confirm that your numbers for controlling the Talons are correct? Go to 10.TE.AM.2 and log into the RoboRio. Enable the lights on Talon(1), and hit save. Verify that your motor, Talon, RobioRio and code all are (1). Then do the same for Talon(2), etc .
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 10:26.

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