Go to Post I'm a programmer by training and inclination, so my mechanical ideas are sometimes rather, um, loosely constrained by physical reality. - Alan Anderson [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

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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
 


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 11:39.

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