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 03-12-2015, 18:51
simon1636 simon1636 is offline
Registered User
FRC #4501
 
Join Date: Dec 2015
Location: California
Posts: 9
simon1636 is an unknown quantity at this point
Spike Code Problems

Hi! My robotics class is using cRIO FRC 2014 on Netbeans with Java. The code we are using does not seem to work. We are coding a Spike relay as a kind of intake system, yet each time we upload it to try and control it, nothing happens! If any of you could take a look at the code to see if you notice any problems that would be great! Thanks!

Code:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.Timer;


public class RobotTemplate extends SimpleRobot {
    RobotDrive myDrive;
    Joystick moveStick, rotateStick;
    Talon backRight, backLeft, frontRight, frontLeft;
    Relay relay;

    public void robotInit(){
                frontLeft = new Talon(1);
                backLeft = new Talon(2);
                backRight = new Talon(3);
                frontRight = new Talon(4);
                
                relay=new Relay(1);
                    
                        
       
		
                //myDrive = new RobotDrive(frontLeft, backLeft, frontRight, backRight);
                myDrive = new RobotDrive(backLeft, backRight, frontLeft, frontRight);
		
                moveStick = new Joystick(2);
		rotateStick = new Joystick(1);
          
       
        
              
        
        
    }
    /**
     * This function is called once each time the robot enters autonomous mode.
     */
   

    /**
     * This function is called once each time the robot enters operator control.
     */
    public void operatorControl() {
   //   
        //while(true &&isOperatorControl() &&isEnabled()){
        //myDrive.setInvertedMotor(RobotDrive.MotorType.kRearRight, true);
        //myDrive.setInvertedMotor(RobotDrive.MotorType.kRearLeft, true);
        
        
        while (isOperatorControl() && isEnabled()) {
      
            myDrive.mecanumDrive_Cartesian(moveStick.getY(), rotateStick.getX(), moveStick.getX(), 0);
           
            
            }
        
        if (rotateStick.getTrigger(GenericHID.Hand.kLeft)&& isEnabled()){
            relay.set(Relay.Value.kOff);
            relay.setDirection(Relay.Direction.kForward);
        }
         
                    //mecanumDrive_Polar(moveStick.getY(), moveStick.getX(), rotateStick.getX());
           Timer.delay(0.1);
        }

    }
    
    /**
     * This function is called once each time the robot enters test mode.
     */
Reply With Quote
  #2   Spotlight this post!  
Unread 03-12-2015, 22:48
GeeTwo's Avatar
GeeTwo GeeTwo is online now
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,654
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Spike Code Problems

As I read this, the only time that the trigger is read and the relay is set is at the beginning of teleop (OperatorControl). This polling and setting needs to be in the function which is called repeatedly while in that state. Not being familiar with SimpleRobot (or SampleRobot, as I thought it had become), I don't know the name of that method.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #3   Spotlight this post!  
Unread 04-12-2015, 10:26
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 103
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
Re: Spike Code Problems

operatorControl is only called once, at the start of teleop. So you have to have all of your teleop code in the loop.

This is how your teleop method flows right now:

1. Check if teleop is enabled and the robot is enabled
2. If so, update drivebase inputs
3. Repeat 1 and 2
4. Loop exits -- teleop is over or the robot is disabled
5. Check if the left trigger is pulled and the robot is enabled
6. If so, set the relay
7. Delay 0.1 seconds
8. Exit operatorControl

Move everything after the loop into the loop, then it should work.

Also, you should format your code (alt+shift+F in Netbeans). The indenting is messed up and that can lead to some headache trying to understand the code
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #4   Spotlight this post!  
Unread 04-12-2015, 19:37
simon1636 simon1636 is offline
Registered User
FRC #4501
 
Join Date: Dec 2015
Location: California
Posts: 9
simon1636 is an unknown quantity at this point
Re: Spike Code Problems

So I updated my code, but nothing seems to be happening! Here's the updated version:

Code:
package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.Timer;

public class RobotTemplate extends SimpleRobot {

    RobotDrive myDrive;
    Joystick moveStick, rotateStick;
    Talon backRight, backLeft, frontRight, frontLeft;
    Relay relay;

    public void robotInit() {
        frontLeft = new Talon(1);
        backLeft = new Talon(2);
        backRight = new Talon(3);
        frontRight = new Talon(4);

        relay = new Relay(1);

        //myDrive = new RobotDrive(frontLeft, backLeft, frontRight, backRight);
        myDrive = new RobotDrive(backLeft, backRight, frontLeft, frontRight);

        moveStick = new Joystick(2);
        rotateStick = new Joystick(1);

    }

    /**
     * This function is called once each time the robot enters autonomous mode.
     */
    /**
     * This function is called once each time the robot enters operator control.
     */
    public void operatorControl() {

        while (isOperatorControl() && isEnabled()) {

            myDrive.mecanumDrive_Cartesian(moveStick.getY(), rotateStick.getX(), moveStick.getX(), 0);

            if (rotateStick.getTrigger(GenericHID.Hand.kLeft) && isEnabled()) {
                relay.set(Relay.Value.kOff);
                relay.setDirection(Relay.Direction.kForward);
            }

        }

        //mecanumDrive_Polar(moveStick.getY(), moveStick.getX(), rotateStick.getX());
        Timer.delay(0.1);
    }

}

/**
 * This function is called once each time the robot enters test mode.
 */
Reply With Quote
  #5   Spotlight this post!  
Unread 04-12-2015, 20:46
GeeTwo's Avatar
GeeTwo GeeTwo is online now
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,654
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Spike Code Problems

Quote:
Originally Posted by simon1636 View Post
So I updated my code, but nothing seems to be happening! Here's the updated version:

Code:
.
.
.
    public void operatorControl() {

        while (isOperatorControl() && isEnabled()) {

            myDrive.mecanumDrive_Cartesian(moveStick.getY(), rotateStick.getX(), moveStick.getX(), 0);

            if (rotateStick.getTrigger(GenericHID.Hand.kLeft) && isEnabled()) {
                relay.set(Relay.Value.kOff);
                relay.setDirection(Relay.Direction.kForward);
            }

        }

        //mecanumDrive_Polar(moveStick.getY(), moveStick.getX(), rotateStick.getX());
        Timer.delay(0.1);
    }

}
.
.
.
I see two issues:
First, you called relay.set(Relay.Value.kOff); This essentially disables the relay; it won't let either the forward or re reverse direction be enabled. The relay.set is usually called in the initialize method, unless the way in which it operates changes for different parts of the program.

Second, you don't have any way to turn the relay off. The if statement that wraps the relay.setDirection() should have an else case that does this.

Further, this if statement probably doesn't need the isEnabled() as part of the condition, as it was checked just a few microseconds earlier at the top of the while loop.

Edit: One more thing - didn't you mean to include the delay within the loop?
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.

Last edited by GeeTwo : 04-12-2015 at 21:33.
Reply With Quote
  #6   Spotlight this post!  
Unread 05-12-2015, 15:26
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,572
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Spike Code Problems

Quote:
Originally Posted by GeeTwo View Post
I see two issues:
First, you called relay.set(Relay.Value.kOff); This essentially disables the relay; it won't let either the forward or re reverse direction be enabled. The relay.set is usually called in the initialize method, unless the way in which it operates changes for different parts of the program.

Second, you don't have any way to turn the relay off. The if statement that wraps the relay.setDirection() should have an else case that does this.
I think you're making the same mistake the OP did. setDirection() is called once (if at all) to initialize the allowable directions, while set changes the physical state of the relay.
Reply With Quote
  #7   Spotlight this post!  
Unread 05-12-2015, 22:43
GeeTwo's Avatar
GeeTwo GeeTwo is online now
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,654
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Spike Code Problems

No, not quite the same mistake; I made my own mistake of switching the two as I moved back and forth between windows.

To correct my previous post: within the initialization method, setDirection() should be once, with either kForward_val or kReverse_val.

set() should be called in both branches of an if-then-else in the while loop in operatorControl(), on with kOff_val, the other with kOn_val.

If the system winds up running backward, just change the call to setDirection.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
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:50.

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