Go to Post So Love your robot, and it will love you. - Not2B [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Events   CD-Media   CD-Spy   FRC-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 01-30-2012, 08:50 PM
joelg236 joelg236 is offline
Registered User
AKA: Joel Gallant
no team
Team Role: Alumni
 
Join Date: Dec 2011
Rookie Year: 2012
Location: Calgary
Posts: 678
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
New to FRC programming... How does this look?

I am completely new at FRC programming, and I'm not sure what really is the best way to go about it. Anyone care to give suggestions?
Code:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.*;

public class RobotTemplate extends IterativeRobot {

    public RobotDrive drivetrain;
    public Joystick leftStick;
    public Joystick rightStick;
    public Joystick KTrigger;
    public Gyro gyro; //gyro
    public Watchdog getWatchdog;
    
    

    public void robotInit(){
        drivetrain = new RobotDrive(1, 2, 3, 4);  
        leftStick = new Joystick(1);    
        rightStick = new Joystick(2);
        KTrigger = new Joystick(3);
    }


    public void autonomousPeriodic(){
        getWatchdog.kill();
        int x = 1;
        do{    
            gyro.reset();

            int i=0;
            while(i<4){    
                drivetrain.drive(0.5,0.0); //START GOING STRAIGHT
                Timer.delay(4.0);
                drivetrain.drive(0.0,0.0); //STOP
                Timer.delay(4.0);
                gyro.reset();
                double angle = 0;
                do {
                    drivetrain.drive(0.0, 0.15); //TURN 
                    Timer.delay(0.5);
                    angle += gyro.getAngle(); //
                    gyro.reset();
                    log("Gyro reads "+angle);
                }while(angle<90); //Keep going in increments of .15 until gyro returns more than 90 degrees
                drivetrain.drive(0.0,0.0); //STOP
                Timer.delay(4.0);
                i++;
            }
        }while(x>0);
    } 

    public void teleopPeriodic() {
        getWatchdog().kill();
        while (isOperatorControl() && isEnabled()){
        Timer.delay(0.002);
                while(KTrigger.getTrigger()){
                    drivetrain.drive(0.0,0.0);
                    Timer.delay(1.0);
                }
            double leftYAxis = leftStick.getAxis(Joystick.AxisType.kY);
            double leftXAxis = leftStick.getAxis(Joystick.AxisType.kX);
            double rightYAxis = rightStick.getAxis(Joystick.AxisType.kY);
            double rightXAxis = rightStick.getAxis(Joystick.AxisType.kX);
            double powerValue = ((leftXAxis+rightXAxis)/2); // FIND RIGHT CALCULATION FOR 0-100% (0.0-1.0)
            double turnValue = ((leftYAxis+rightYAxis)/2);//FIND RIGHT CALCULATION FOR 0.0-1.0
            drivetrain.drive(powerValue,turnValue);
        }
    }
    public static String log(String aMessage){
        System.out.println(aMessage);
        return aMessage;
    }
}
Reply With Quote
  #2   Spotlight this post!  
Unread 01-30-2012, 09:19 PM
Djur's Avatar
Djur Djur is offline
Vision tracking is fun!
AKA: Sam Carlberg
FRC #1687
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2009
Location: Massachusetts
Posts: 169
Djur will become famous soon enough
Re: New to FRC programming... How does this look?

Quote:
Originally Posted by joelg236 View Post
I am completely new at FRC programming, and I'm not sure what really is the best way to go about it. Anyone care to give suggestions?
  • You need to initialize all used objects in RobotInit() -- You're calling data from the gyro, but it's never been initialized, so you'll get Null Pointers from that; same for your Watchdog.
  • You don't need to use the watchdog class.
  • During teleopPeriodic: Timer.delay(0.002) only slows down the robot, same for Timer.delay(1.0) -- it makes that thread sleep and you can't do anything until it wakes up. You'd be better off using if(KTrigger.getTrigger())drivetrain.drive(0,0);} -- Since teleopPeriodic is looped anyway, the "while" loop is redundant.

Just some quick suggestions
__________________
"Wow." - Dean Kamen on 2084's Rebound Rumble 'bot.
Finalists in the 2012 Boston Regional with 125 and 78
LiveWindow, RobotBuilder, and WPILibJ developer.

Last edited by Djur : 01-31-2012 at 08:07 AM. Reason: Misread robotInit()
Reply With Quote
  #3   Spotlight this post!  
Unread 01-31-2012, 06:17 PM
joelg236 joelg236 is offline
Registered User
AKA: Joel Gallant
no team
Team Role: Alumni
 
Join Date: Dec 2011
Rookie Year: 2012
Location: Calgary
Posts: 678
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
Re: New to FRC programming... How does this look?

Thanks so much for the help
Look better?

Code:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.*;
 
public class RobotTemplate extends IterativeRobot {
 
    public RobotDrive drivetrain;
    public Joystick joystick;
    public Joystick KTrigger;
    public Gyro gyro; //gyro
   
   
 
    public void robotInit(){
        drivetrain = new RobotDrive(1, 2, 3, 4); 
        joystick = new Joystick(1);   
        KTrigger = new Joystick(1);
        gyro = new Gyro(1);
    }
 
 
    public void autonomousContinous(){
        int x = 1;
        do{   
            gyro.reset();
 
            int i=0;
            while(i<4){   
                drivetrain.drive(0.5,0.0); //START GOING STRAIGHT
                Timer.delay(4.0);
                drivetrain.drive(0.0,0.0); //STOP
                Timer.delay(4.0);
                gyro.reset();
                double angle = 0;
                do {
                    drivetrain.drive(0.0, 0.15); //TURN
                    Timer.delay(0.5);
                    angle += gyro.getAngle(); //
                    gyro.reset();
                    log("Gyro reads "+angle);
                }while(angle<90); //Keep going in increments of .15 until gyro returns more than 90 degrees
                drivetrain.drive(0.0,0.0); //STOP
                Timer.delay(4.0);
                i++;
            }
        }while(x>0);
    }
 
    public void teleopContinous() {
        while (isOperatorControl() && isEnabled()){
            if(KTrigger.getTrigger())
                {drivetrain.drive(0,0);
                log("Brake enabled");}
            double YAxis = joystick.getAxis(Joystick.AxisType.kY);
            double XAxis = joystick.getAxis(Joystick.AxisType.kX);
            drivetrain.drive(YAxis,XAxis);
        }
    }
    public static String log(String aMessage){
        System.out.println(aMessage);
        return aMessage;
    }
}
Reply With Quote
  #4   Spotlight this post!  
Unread 02-01-2012, 10:53 AM
eddie12390's Avatar
eddie12390 eddie12390 is offline
FRC 3260 - SHARP
AKA: Eddie
FRC #3260 (SHARP)
Team Role: Programmer
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Pittsburgh
Posts: 231
eddie12390 is just really niceeddie12390 is just really niceeddie12390 is just really niceeddie12390 is just really niceeddie12390 is just really nice
Re: New to FRC programming... How does this look?

This may just be one of the things that I do and find useful, but creating constants for port numbers can be extremely useful for someone who has not read your code and is not using your robot.

Things like
Code:
private static final int DRIVE_LEFT_FRONT = 1;
Jaguar frontLeftDrive = new Jaguar(DRIVE_LEFT_FRONT);
will be a lot easier for others to understand. (And when you want to make a change you can just find your constants and type in a new number)

Also, to find the x axis/y axis value just type joystick.getX() or joystick.getY(). You also do not need to set the value of the joystick to a double before supplying it to the drive() method.

Code:
    public static String log(String aMessage){
        System.out.println(aMessage);
        return aMessage;
    }
The above would probably just be better off as a void where you don't have to bother with the return type. You'll just be telling yourself something that you already know.

Last edited by eddie12390 : 02-01-2012 at 10:56 AM.
Reply With Quote
  #5   Spotlight this post!  
Unread 02-02-2012, 09:12 AM
omalleyj omalleyj is offline
Registered User
AKA: Jim O'Malley
FRC #1279 (Cold Fusion)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2008
Location: New Jersey
Posts: 127
omalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to beholdomalleyj is a splendid one to behold
Re: New to FRC programming... How does this look?

Code:
double angle = 0;

do {
     drivetrain.drive(0.0, 0.15); //TURN
     drivetrainTimer.delay(0.5);
     angle += gyro.getAngle(); //
     gyro.reset();
     log("Gyro reads "+angle);
}while(angle<90); //Keep going in increments of .15 until gyro
I don't use the standard drive() methods, but IIRC the first value is the forward speed and the second the turning offset speed-wise, not an angle. So unless you know 0.5 second increments is going to end up close to 90, you could be over by a bit.
And only reset your gyro when you need to, and not when its moving. You want to stop and wait a little so that you have a steady zero value.

Last edited by omalleyj : 02-02-2012 at 09:17 AM.
Reply With Quote
  #6   Spotlight this post!  
Unread 02-02-2012, 09:36 AM
neal's Avatar
neal neal is offline
Neal
FRC #1777 (Viking Robotics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2009
Location: United States
Posts: 56
neal is an unknown quantity at this point
Re: New to FRC programming... How does this look?

Like omalleyj said, resetting gyro in the loop isn't a good way, especially when it's moving.

I would do something simple like this:

Code:
// Reset gyro before the main loop, or with a button.
double angle = gyro.getAngle();
if (angle < 90) {
    drivetrain.drive(0.0, 0.15); // I don't know how turn works, I've only done tank drive and mecanum drive in the past.
}
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 01:15 PM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi