Go to Post Eating at Pappy's is the only St Louis activity for me. - Cory [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 05-06-2012, 17:23
2185Bilal's Avatar
2185Bilal 2185Bilal is offline
Driver, Ld. Programmer, Electrical
AKA: Bilal Majeed
FRC #2185 (Ramazoidz)
Team Role: Programmer
 
Join Date: Feb 2012
Rookie Year: 2011
Location: Toronto, Canada
Posts: 110
2185Bilal will become famous soon enough
Post Code and Deployment Help

Hello with the help of Youtube and CD I was finally able to make my first java program for the robot.

Here it is:

The main class:
Code:
// ***************** THIS CODE WAS WRITTEN BY BILAL MAJEED ***************** //   

package bilal.robotics.code;

// ***************** TO BE USED COMPONENTS ***************** //
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Timer;

public class Team2185Main extends IterativeRobot {
    
    Jaguar leftMotor1 = new Jaguar(1);  // define the 1st left motor 
    Jaguar leftMotor2 = new Jaguar(2);  // define the 2nd left motor
    
    Jaguar rightMotor1 = new Jaguar(3); // define the 1st right motor 
    Jaguar rightMotor2 = new Jaguar(4); // define the 2nd right motor 
    
        
    RobotDrive drive = new RobotDrive (leftMotor1, leftMotor2, rightMotor1, rightMotor2);   // define the robot drive system
    
    Joystick controller = new Joystick(1);  // define the joystick as controller 
    final int buttonA = 1;                  // define button 1 as buttonA
    
    Shooter shooter;
    
    
// ***************** INITIALIZATION CODE ***************** //    
    public void robotInit() 
    {
        shooter = new Shooter(); // call the shooter class
    }
    
        
// ***************** AUTONOMOUS METHODS ***************** //  
    public void autonomousInit() 
    {
        for(int i=0; i<4; i++){
            drive.drive(0.5,0.0);    // drive forward at 1/2 speed
            Timer.delay(2.0);        // wait 2 sec
            drive.drive(0.0, 0.75);  // drive with 0% speed and 75% turn
            Timer.delay(0.75);       // wait for the 90 degree turn to complete
        }
        drive.drive(0.0,0.0); 
        
        
            
                  
    }

    public void autonomousPeriodic()
    {
        
    }
    
    public void autonomousContinuous()
    {
        
    }
    
        
// ***************** TELOP METHODS ***************** //    
    public void teleopInit()
    {
       while(isOperatorControl() && isEnabled())
        {
            drive.tankDrive(controller, 2, controller, 5);  // drive the robot according to joystick
            
            if (controller.getRawButton(buttonA))   // if button 1 is pressed on the controller 
            {                                       // then make the robot drive forward
                leftMotor1.set(1);                          
                leftMotor2.set(1);
                rightMotor1.set(1);
                rightMotor2.set(1);
            }
        }
    }
    
    public void telopPeriodic()
    {
        shooter.shoot();    // every 20mS call the shooting class 
    }
    
    public void telopContinuous()
    {
        
    }
    
        
// ***************** DISABLED METHODS ***************** //   
    public void disabledInit() 
    {
        
    }
    
    public void disabledPeriodic()
    {
        
    }
    
    public void disabledContinuous()
    {
        
    }
       
}
And the shooter Class:
Code:
package bilal.robotics.code;

import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Joystick.ButtonType;


public class Shooter 
{
    Jaguar shooter = new Jaguar(5);
    
    Joystick controller = new Joystick(2);
    
    final int buttonA = 1;
    
       
    
    public Shooter()
    {
        
    }
    
    public void shoot(){
        if(controller.getRawButton(buttonA)) {
            shooter.set(controller.getRawAxis(2));
        }
    }
    
}
So please tell me anything i did wrong, or a better way to do it.
Thanks

Also how would i deploy this code on to the robot.


Thanks
__________________
RAMAZOIDZ

2009 Toronto West Regional - Regional Winners
2009 Waterloo Regional - Regional Winners
Reply With Quote
  #2   Spotlight this post!  
Unread 08-06-2012, 14:52
daniel_dsouza daniel_dsouza is offline
does what needs to be done.
FRC #2449 (Out of Orbit Robotics)
Team Role: Alumni
 
Join Date: May 2011
Rookie Year: 2011
Location: Chandler, AZ
Posts: 231
daniel_dsouza has a spectacular aura aboutdaniel_dsouza has a spectacular aura about
Re: Code and Deployment Help

I'm assuming that you are using netbeans, and that all your constants/fields are correct. I can't really diagnose problems from here without seeing your setup.

The only issue I see in your code is that your teleopPeriodic might never get called, because of your infinite loop in your teleopInit. I would suggest moving just your driving code (not the loop) to your teleopPeriodic. For future reference, avoid loops unless they are escapable.

As for deploying, make sure your computer can communicate with your cRIO (ping it), and then push the "green arrow" or push "F6" or go to the "run" menu and select "run main project".

Good Luck!
Reply With Quote
  #3   Spotlight this post!  
Unread 15-06-2012, 20:00
2185Bilal's Avatar
2185Bilal 2185Bilal is offline
Driver, Ld. Programmer, Electrical
AKA: Bilal Majeed
FRC #2185 (Ramazoidz)
Team Role: Programmer
 
Join Date: Feb 2012
Rookie Year: 2011
Location: Toronto, Canada
Posts: 110
2185Bilal will become famous soon enough
Post Re: Code and Deployment Help

Thank you for the help, I made the adjustments to the code as you instructed and this is how it looks like:
Code:
package bilal.eci.code.robotics;

import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Timer;


public class ExhibtionMainClass extends IterativeRobot {
    
    Jaguar leftMotor1 = new Jaguar(1);  // define the 1st left motor 
    Jaguar leftMotor2 = new Jaguar(2);  // define the 2nd left motor
    
    Jaguar rightMotor1 = new Jaguar(3); // define the 1st right motor 
    Jaguar rightMotor2 = new Jaguar(4); // define the 2nd right motor 
    
        
    RobotDrive drive = new RobotDrive (leftMotor1, leftMotor2, rightMotor1, rightMotor2);   // define the robot drive system
    
    Joystick controller = new Joystick(1);
    
    public void robotInit() {
        
    }

    public void autonomousInit() {
        
        for(int i=0; i<4; i++){
            drive.drive(0.5,0.0);    // drive forward at 1/2 speed
            Timer.delay(2.0);        // wait 2 sec
            drive.drive(0.0, 0.75);  // drive with 0% speed and 75% turn
            Timer.delay(0.75);       // wait for the 90 degree turn to complete
            }
    }
    
    public void autonomousContinuous() {
        
    }
    public void autonomousPeriodic() {

    }

    public void telopInit(){
        while(isOperatorControl() && isEnabled())
        {
           
            
        }       
    }
    
    public void telopContinuous() {
        
    }
    
    public void teleopPeriodic() {
         drive.tankDrive(controller, 2, controller, 5);  // drive the robot according to joystick        
    }
    
    public void disabledInit() {
        drive.drive(0.0,0.0);        
    }
    
    public void disabledPeriodic() {
        
    }
    
    public void disabledContinuous() {
        
    }
}
Please do let me know if that is what you meant. Thank You Very Much

ps. I deleted the use of the button and i also deleted the shooter class
__________________
RAMAZOIDZ

2009 Toronto West Regional - Regional Winners
2009 Waterloo Regional - Regional Winners

Last edited by 2185Bilal : 15-06-2012 at 20:01. Reason: to add additional info
Reply With Quote
  #4   Spotlight this post!  
Unread 15-06-2012, 23:15
Djur's Avatar
Djur Djur is offline
WPILib
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2009
Location: Massachusetts
Posts: 182
Djur will become famous soon enough
Re: Code and Deployment Help

There are a few things that I see in your code: empty statements (not a bad thing to have, but very superfluous) and an infinite loop (definitely a bad thing).

The code you posted looks like this; I added the comments.

Quote:
Originally Posted by 2185Bilal View Post
Code:
package bilal.eci.code.robotics;

import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Timer;


public class ExhibitionMainClass extends IterativeRobot {
    
    Jaguar leftMotor1 = new Jaguar(1);  // define the 1st left motor 
    Jaguar leftMotor2 = new Jaguar(2);  // define the 2nd left motor
    
    Jaguar rightMotor1 = new Jaguar(3); // define the 1st right motor 
    Jaguar rightMotor2 = new Jaguar(4); // define the 2nd right motor 
    
        
    RobotDrive drive = new RobotDrive (leftMotor1, leftMotor2, rightMotor1, rightMotor2);   // define the robot drive system
    
    Joystick controller = new Joystick(1);
    
    public void robotInit() { //empty method - you don't need this
        
    }

    public void autonomousInit() {
        
        for(int i=0; i<4; i++){
            drive.drive(0.5,0.0);    // drive forward at 1/2 speed
            Timer.delay(2.0);        // wait 2 sec
            drive.drive(0.0, 0.75);  // drive with 0% speed and 75% turn
            Timer.delay(0.75);       // wait for the 90 degree turn to complete
            }
    }
    
    public void autonomousContinuous() { //empty method - you don't need this
        
    }
    public void autonomousPeriodic() { //empty method - you don't need this

    }

    public void telopInit(){ //all this method does is freeze your robot - get rid of it
        while(isOperatorControl() && isEnabled()) //infinite loop = not good!
        {
           
            
        }       
    }
    
    public void telopContinuous() { //empty method - you don't need this
        
    }
    
    public void teleopPeriodic() {
         drive.tankDrive(controller, 2, controller, 5);  // drive the robot according to joystick        
    }
    
    public void disabledInit() {
        drive.drive(0.0,0.0);        
    }
    
    public void disabledPeriodic() { //empty method - you don't need this
        
    }
    
    public void disabledContinuous() { //empty method - you don't need this
        
    }
}

You have an infinite loop in teleopInit(), so your program will never progress beyond that because it is always enabled and under operator control during teleop. If you're not going to do anything with a method, leave it out entirely - this goes for the robotInit(), autonomousPeriodic(), autonomousContinuous(), teleopInit(), and teleopContinuous() methods in the code you posted.

Better code would look like this:

Code:
package bilal.eci.code.robotics;

import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Timer;

public class ExhibitionMainClass extends IterativeRobot {

    Jaguar leftMotor1   = new Jaguar(1);
    Jaguar leftMotor2   = new Jaguar(2);
    Jaguar rightMotor1 = new Jaguar(3);
    Jaguar rightMotor2 = new Jaguar(4);

    RobotDrive drive = new RobotDrive (leftMotor1, leftMotor2, rightMotor1, rightMotor2);   // define the robot drive system
    
    Joystick controller = new Joystick(1);

    public void autonomousInit() {        
        for(int i=0; i<4; i++){
                drive.drive(0.5,0.0);    //drive forward at 1/2 speed
                Timer.delay(2.0);        //wait 2 sec
                drive.drive(0.0, 0.75); //drive with 0% speed and 75% turn
                Timer.delay(0.75);      //wait for the 90 degree turn to complete
            }
    }

    public void teleopPeriodic() {
         drive.tankDrive(controller, 2, controller, 5);  // drive the robot according to joystick        
    }
    
    public void disabledInit() {
        drive.drive(0.0,0.0);        
    }

}
You may also want to declare your objects final to make it clear that you're not going to reassign new values, but that's just my personal preference.
__________________
WPILib dev (RobotBuilder, SmartDashboard, GRIP)
Reply With Quote
  #5   Spotlight this post!  
Unread 16-06-2012, 14:59
2185Bilal's Avatar
2185Bilal 2185Bilal is offline
Driver, Ld. Programmer, Electrical
AKA: Bilal Majeed
FRC #2185 (Ramazoidz)
Team Role: Programmer
 
Join Date: Feb 2012
Rookie Year: 2011
Location: Toronto, Canada
Posts: 110
2185Bilal will become famous soon enough
Question Re: Code and Deployment Help

Quote:
Originally Posted by Djur View Post
You may also want to declare your objects final to make it clear that you're not going to reassign new values, but that's just my personal preference.
Wow..thank you very much. And what was the thing you told me about the "final"

ps. I pretty new to Java, and i think "final" is like a variable type (string, integer), so could you please tell what is a final.

Thanks
__________________
RAMAZOIDZ

2009 Toronto West Regional - Regional Winners
2009 Waterloo Regional - Regional Winners
Reply With Quote
  #6   Spotlight this post!  
Unread 17-06-2012, 12:02
Djur's Avatar
Djur Djur is offline
WPILib
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2009
Location: Massachusetts
Posts: 182
Djur will become famous soon enough
Re: Code and Deployment Help

Quote:
Originally Posted by 2185Bilal View Post
Wow..thank you very much. And what was the thing you told me about the "final"

ps. I pretty new to Java, and i think "final" is like a variable type (string, integer), so could you please tell what is a final.

Thanks
"final" is a modifier to a variable or Object (variables being integers, doubles; Objects being Strings, Jaguars, Joysticks, etc). If you put "final" in front of a variable/Object, that means that it cannot be changed by any method in any class of your code. Basically, "final" means "constant."

For example:
Code:
Jaguar jaguar = new Jaguar(1);
jagaur = new Jaguar(2);

void changeJag(){
    jaguar = new Jaguar(3);
}
A non-final object can be reassigned values, like I showed above. The Jaguar is not "final," so you can change it's value anywhere in your code.

Code:
final Jaguar jaguar = new Jaguar(1);
jaguar = new Jaguar(2);

void changeJag(){
    jaguar = new Jaguar(3);
}
The above code will throw an error while compiling because the Jaguar object is declared "final." If you declare an object "final," it cannot be assigned a new value. Declaring your objects "final" means that you do not want them to have values other than the ones you give them. For example, if you have a Jaguar controlling your front-left wheel, you do not want to give it a new value in the code so that your robot thinks that your front-left wheel is the rear-left wheel.
__________________
WPILib dev (RobotBuilder, SmartDashboard, GRIP)
Reply With Quote
  #7   Spotlight this post!  
Unread 19-06-2012, 23:26
2185Bilal's Avatar
2185Bilal 2185Bilal is offline
Driver, Ld. Programmer, Electrical
AKA: Bilal Majeed
FRC #2185 (Ramazoidz)
Team Role: Programmer
 
Join Date: Feb 2012
Rookie Year: 2011
Location: Toronto, Canada
Posts: 110
2185Bilal will become famous soon enough
Post Re: Code and Deployment Help

WOW...thank you very much

So i have made the adjustments you told me to, and here how it looks like:

Code:
// This code was created on Friday June 15, 2012 by Bilal Majeed
// This code is meant to be used on the exhibtion robot

package bilal.eci.code.robotics;

// OBJECTS // 
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Timer;

// ROBOT CODE (AUTONOMOUS AND TELOP) // 
public class ExhibtionMainClass extends IterativeRobot {
    
    // DEFINING THE JAGUARS // 
    final Jaguar leftMotor1 = new Jaguar(1); 
    final Jaguar leftMotor2 = new Jaguar(2);  
    final Jaguar rightMotor1 = new Jaguar(3);  
    final Jaguar rightMotor2 = new Jaguar(4);     
        
    // DEFINING THE ROBOT DRIVE SYSTEM (4 WHEEL, TANK DRIVE) // 
    final RobotDrive drive = new RobotDrive (leftMotor1, leftMotor2, rightMotor1, rightMotor2);       
    
    // DEFINING THE JOYSTICK (USB1) // 
    final Joystick controller = new Joystick(1);
    
    // AUTONOMOUS CODE // 
    public void autonomousInit() {
        
        for(int i=0; i<4; i++){
            drive.drive(0.5,0.0);    
            Timer.delay(2.0);        
            drive.drive(0.0, 0.75);  
            Timer.delay(0.75);       
            }
    }
    
    // TELOPERATED PERIOD CODE // 
    public void teleopPeriodic() {
         drive.tankDrive(controller, 2, controller, 5);        
    }
    
    // DISABLED ROBOT CODE // 
    public void disabledInit() {
        drive.drive(0.0,0.0);        
    }
}
ps. Code is for our exhibition or display robot during the off-season, and it only includes a drive train

Let me know if i did anything wrong or differently, and also any tips or advice will be greatly appreciated, thank you

__________________
RAMAZOIDZ

2009 Toronto West Regional - Regional Winners
2009 Waterloo Regional - Regional Winners
Reply With Quote
  #8   Spotlight this post!  
Unread 22-06-2012, 14:32
daniel_dsouza daniel_dsouza is offline
does what needs to be done.
FRC #2449 (Out of Orbit Robotics)
Team Role: Alumni
 
Join Date: May 2011
Rookie Year: 2011
Location: Chandler, AZ
Posts: 231
daniel_dsouza has a spectacular aura aboutdaniel_dsouza has a spectacular aura about
Re: Code and Deployment Help

looks good. In the future, when you write your own classes where things might change, you will not need to use the "final" keyword. I usually only use it for variables where I am sure nothing will change.
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 13:33.

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