Go to Post If you want to change the culture sometimes you need to change a little yourself - Koko Ed [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
  #1   Spotlight this post!  
Unread 11-02-2012, 23:33
joelg236 joelg236 is offline
4334 Retired Mentor & Alumni
AKA: Joel Gallant
no team
Team Role: Mentor
 
Join Date: Dec 2011
Rookie Year: 2012
Location: Calgary
Posts: 733
joelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond reputejoelg236 has a reputation beyond repute
Smile How is this looking?

I'd love to hear some feedback from people who actually know about this stuff (My team is first year and nobody is really above me in terms of java knowledge - or frc strategies in code)
Code:
/*
 
THIS CODE IS MADE BY THE BEST PEOPLE IN THE WORLD, DO NOT STEAL.........please >_<.

*/
package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.*;
 
public class Robot4334 extends SimpleRobot {  //DECLARE ALL METHODS IN INIT AND SIMPLEROBOT
                                                
    public RobotDrive drivetrain;
    public Joystick XBoxController;
    public Gyro gyro;
    public Timer timer;
    public Encoder leftEncoder;
    public Encoder rightEncoder;
    public static final double DRIVE_STRAIGHT = 0;
    public static final double DRIVE_FULL_SPEED = 1;
    public static final int LeftX = 1;
    public static final int LeftY = 2;
    public static final int Triggers = 3; //(Each trigger = 0 to 1, axis value = right - left)
    public static final int RightX = 4;
    public static final int RightY = 5;
    public static final int DPad = 6;
    public boolean brakeOn ;
    public float leftSpeed;
    public float rightSpeed;
    
    public void log(String theMessage) { // Logger -System.out.println function
        System.out.println(theMessage);
    }
    
    public void BRAKE() { //Brake method - stops motors
        drivetrain.drive(0.0,0.0);
        drivetrain.stopMotor();
    }
    
    public void DRIVE() {
        while(getBrake() == false && getEmergency() == false)
        {
            returnTurn();
            drivetrain.tankDrive(leftSpeed , rightSpeed);
        }
  
        if (getActualSpeed('L') > (leftSpeed + 0.15) || getActualSpeed('L') < (leftSpeed - 0.15)) {
            if(getActualSpeed('R') > (rightSpeed + 0.15) || getActualSpeed('R') < (rightSpeed - 0.15)) {
                log("Speed and encoder do not match");
            }
        }
    }
    
    public void EMERGENCY() { //ADD ALL FUNCTIONS HERE TO STOP ON ROBOT DURING EMERGENCY
        
        drivetrain.drive(0.0,0.0);
        timerControl("Start");
        while(getTimer()<10)
        {
            int time = (int) getTimer();
            log("EMERGENCY   :::   "+time+" SECONDS UNTIL RESTART");
        }
    }
    
    public void RESET() {
        
        gyro.reset();
        leftEncoder.reset();
        rightEncoder.reset();
        timer.reset();
        drivetrain.stopMotor();
    }
    
    public void runMotorFor(double seconds, double power, double turn) {
        
        timerControl("Start");
        while(getTimer()<seconds)
        drivetrain.drive(power,turn);
    }
    
    public void timerControl(String args) {
        if(args.equals("stopAndReset")) {
            timer.stop();
            timer.reset();     
        }else if(args.equals("Start")) {
            timer.start();
        }else if(args.equals("stopAndReset")) {
            timer.stop();
            timer.reset();
        }
    }
    
    public boolean getEmergency() {
        
        if(XBoxController.getRawButton(5)) {
            EMERGENCY();
            boolean emergency = true;
            return emergency;
        }
        else {
            return false;
        }
    }    
    
    public double getActualSpeed(char LorR) {
        double speed = 0;
        if(LorR == 'L') {
            speed = leftEncoder.getRate();
        }
        else if(LorR == 'R') {
            speed = rightEncoder.getRate();
        }
        //INSERT CODE TO CONVERT RATE INTO -1 to +1
        return speed;
    }
    
    public boolean getBrake() {
        
        brakeOn = false;
        if(XBoxController.getRawButton(2))
        {
            BRAKE();
            brakeOn = true;
        }
        return brakeOn;
    }
    
    public double getTimer() {
        double time = timer.get();
        return time;
    }
    
    public double getEncoder(char LorR) {
        double distance = 0;
        if(LorR == 'L') {
            distance = leftEncoder.getDistance();
        }else if(LorR =='R') {
            distance = rightEncoder.getDistance();
        }
        return distance;
    }
    
    public double getGyro() {
        return gyro.getAngle();
    }
    
    public double getAndLoopTimer() {
        double time = timer.get();
        timer.stop();
        timer.reset();
        timer.start();
        return time;
    }    
    
    public float returnTriggerSpeed() { // Get the speed value from the xbox controller triggers
            double speed = XBoxController.getRawAxis(Triggers);
            // speed = exp* growth on trigger getRawAxis value
            return (float)speed;
    }    
    
    public void returnTurn() {
        
        // Check if triggers are on use for control if not use joysticks
        boolean triggersOn = XBoxController.getRawAxis(Triggers)>0.1 || XBoxController.getRawAxis(Triggers)<0.1;
            if(triggersOn) {
                
                rightSpeed = returnTriggerSpeed();
                leftSpeed = rightSpeed ; 
            } else if(triggersOn == false) {
                
                if(XBoxController.getRawAxis(LeftY)<= 0.05 && XBoxController.getRawAxis(LeftY)>= -0.05) {
                    
                    leftSpeed = 0;
                } else {
                   
                    leftSpeed = (float) XBoxController.getRawAxis(LeftY);
                }
                
                if(XBoxController.getRawAxis(RightY)<= 0.05 && XBoxController.getRawAxis(RightY)>= -0.05) {
                    
                    rightSpeed = 0;
                } else {
                    
                    leftSpeed = (float) XBoxController.getRawAxis(RightY);
                }
                
            }
            
            //press button one for fine control 25% speed
            if(XBoxController.getRawButton(1)) {
                
                rightSpeed *= 0.25;
                leftSpeed *= 0.25;
            }
            
            // Imma goin fast again :D !!!!!!
            // press button 3 to use 100% speed
            if(XBoxController.getRawButton(3)) {
                
                rightSpeed *= 2;
                leftSpeed *= 2;              
            }
            
            //clayton wants to use this button for 75% control
            if(XBoxController.getRawButton(4)) {
                
                rightSpeed *= 0.75;
                leftSpeed *= 0.75;
            }  
            
            // take right and left speed value calculate for exp* growth  
            rightSpeed = (float) (0.94*(rightSpeed*rightSpeed)+0.05); 
            leftSpeed = (float) (0.94*(leftSpeed*leftSpeed)+0.05);
            
            // default to 50% speed 
            rightSpeed *= 0.5;
            leftSpeed *= 0.5;
    }
      
    public void robotInit() { //REMEMBER TO INITIALIZE EVERYTHING
        
        drivetrain = new RobotDrive(1, 3, 2, 4);
        XBoxController = new Joystick(1);
        gyro = new Gyro(1 , 1); 
        timer = new Timer();
        leftEncoder = new Encoder(2,1,2,2);
        rightEncoder = new Encoder(2,3,2,4); 
    }
    
    public void autonomous() { 
        
        while(isAutonomous()&&isEnabled()){ //Makes sure in autonomous.
            log("Autonomous control enabled");
        }
    }
    
    public void operatorControl() {
        
        while (isOperatorControl() && isEnabled()) { //Make sure in driver
            
            log("Operator control enabled");
            DRIVE();
        }
    }
    
    public void disabled() {
        RESET();
        log("Is disabled");
    }
}

/*
1: A
2: B
3: X
4: Y
5: Left Bumper
6: Right Bumper
7: Back
8: Start
9: Left Joystick
10: Right Joystick
 
The axis on the controller follow this mapping
(all output is between -1 and 1)
1: Left Stick X Axis
-Left:Negative ; Right: Positive
2: Left Stick Y Axis
-Up: Negative ; Down: Positive
3: Triggers
-Left: Positive ; Right: Negative
4: Right Stick X Axis
-Left: Negative ; Right: Positive
5: Right Stick Y Axis
-Up: Negative ; Down: Positive
6: Directional Pad (Not recommended, buggy)
*/
Thanks
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 08:48.

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