Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Autonomous Help (http://www.chiefdelphi.com/forums/showthread.php?t=133182)

notmattlythgoe 23-01-2015 13:24

Re: Autonomous Help
 
Quote:

Originally Posted by curtis0gj (Post 1432785)
None in the ds error box but I don't know how to check the roborio error log.

The riolog is in eclipse. IF you don't see it as a tab near the bottom then go yo Window -> Show View -> Riolog.

curtis0gj 23-01-2015 13:40

Re: Autonomous Help
 
Quote:

Originally Posted by notmattlythgoe (Post 1432787)
The riolog is in eclipse. IF you don't see it as a tab near the bottom then go yo Window -> Show View -> Riolog.

I don't see anything bad in the riolog or ds log so idk whats going on with that.

curtis0gj 23-01-2015 15:47

Re: Autonomous Help
 
Hey guys I finally got the encoder going, my electrical guy said it was grounding on something. Anyway I am getting a nice reading on smart db however, when I run autonomous mode the robot turns about 180 stops and turns again and this never ends. I think I am stuck in an if loop and it's not entering the straight driving portion of the program...

Code:

public class Robot extends IterativeRobot {
       
        RobotDrive robot;
    Joystick stick;
    Encoder encoder;       
    Gyro gyro1;
   
    static final double Kp = 0.05;
   
    public void robotInit() {
           
            robot = new RobotDrive(0, 1);
        stick = new Joystick(0);
        gyro1 = new Gyro(0);
        gyro1.reset();
        gyro1.initGyro();
        encoder = new Encoder(0, 1, false, EncodingType.k4X);
     
       
        encoder.setDistancePerPulse(1);
     
        encoder.getDistance();
       

    }

 
    public void autonomousPeriodic() {

            gyro1.reset();
               
            while(isAutonomous() && isEnabled())  {
                   
                    double angle = gyro1.getAngle();
                    double distance = encoder.get();
                   
                    SmartDashboard.putNumber("angle", angle);
                    SmartDashboard.putNumber("distance", distance);
                   
                    robot.drive(-0.15, -1);
                   
                    if(angle > 150) {
                           
                            robot.drive(0,0);
                            gyro1.reset();
                            encoder.reset();
                           
                            Timer.delay(0.5);

                            break;
                           
                            }
                   
                            Timer.delay(0.1);
            }
           
                                while (isAutonomous() && isEnabled()) {

                                                double angle2 = gyro1.getAngle();
                                                double distance2 = encoder.get();
                                               
                                                SmartDashboard.putNumber("distance", distance2);
                                                SmartDashboard.putNumber("angle", angle2);
                                               
                                                robot.drive(-0.20, -angle2 * Kp);
                                       
                                                if(encoder.getDistance() < 100)  {
                                                        robot.drive(0,-0);
                                                       
                                                        break;
                                                }
                                                Timer.delay(0.1);       
                                }
                               
                            }


notmattlythgoe 23-01-2015 15:53

Re: Autonomous Help
 
Quote:

Originally Posted by curtis0gj (Post 1432882)
Hey guys I finally got the encoder going, my electrical guy said it was grounding on something. Anyway I am getting a nice reading on smart db however, when I run autonomous mode the robot turns about 180 stops and turns again and this never ends. I think I am stuck in an if loop and it's not entering the straight driving portion of the program...

Code:

public class Robot extends IterativeRobot {
       
        RobotDrive robot;
    Joystick stick;
    Encoder encoder;       
    Gyro gyro1;
   
    static final double Kp = 0.05;
   
    public void robotInit() {
           
            robot = new RobotDrive(0, 1);
        stick = new Joystick(0);
        gyro1 = new Gyro(0);
        gyro1.reset();
        gyro1.initGyro();
        encoder = new Encoder(0, 1, false, EncodingType.k4X);
     
       
        encoder.setDistancePerPulse(1);
     
        encoder.getDistance();
       

    }

 
    public void autonomousPeriodic() {

            gyro1.reset();
               
            while(isAutonomous() && isEnabled())  {
                   
                    double angle = gyro1.getAngle();
                    double distance = encoder.get();
                   
                    SmartDashboard.putNumber("angle", angle);
                    SmartDashboard.putNumber("distance", distance);
                   
                    robot.drive(-0.15, -1);
                   
                    if(angle > 150) {
                           
                            robot.drive(0,0);
                            gyro1.reset();
                            encoder.reset();
                           
                            Timer.delay(0.5);

                            break;
                           
                            }
                   
                            Timer.delay(0.1);
            }
           
                                while (isAutonomous() && isEnabled()) {

                                                double angle2 = gyro1.getAngle();
                                                double distance2 = encoder.get();
                                               
                                                SmartDashboard.putNumber("distance", distance2);
                                                SmartDashboard.putNumber("angle", angle2);
                                               
                                                robot.drive(-0.20, -angle2 * Kp);
                                       
                                                if(encoder.getDistance() < 100)  {
                                                        robot.drive(0,-0);
                                                       
                                                        break;
                                                }
                                                Timer.delay(0.1);       
                                }
                               
                            }


What is the reported angle during the second part of the autonomous routine? My guess is that it is not scaled enough and it is correcting it in the wrong direction.

curtis0gj 23-01-2015 16:04

Re: Autonomous Help
 
Quote:

Originally Posted by notmattlythgoe (Post 1432887)
What is the reported angle during the second part of the autonomous routine? My guess is that it is not scaled enough and it is correcting it in the wrong direction.

Unfortunatly, I can't check what angle it's reading until monday. However I can fix the correcting in the wrong direction would that just be a different < or change my -1 to 1?

Ether 23-01-2015 16:09

Re: Autonomous Help
 
Quote:

Originally Posted by curtis0gj (Post 1432882)
Hey guys I finally got the encoder going, my electrical guy said it was grounding on something.

That's why you should turn the encoder shaft slowly and observe the hi/lo quadrature behavior of A & B when testing with a multimeter...



notmattlythgoe 23-01-2015 16:14

Re: Autonomous Help
 
Quote:

Originally Posted by curtis0gj (Post 1432893)
Unfortunatly, I can't check what angle it's reading until monday. However I can fix the correcting in the wrong direction would that just be a different < or change my -1 to 1?

I'm guessing you need to change it to positive, but that will all depend on the angle value.

curtis0gj 23-01-2015 16:37

Re: Autonomous Help
 
Quote:

Originally Posted by notmattlythgoe (Post 1432899)
I'm guessing you need to change it to positive, but that will all depend on the angle value.

Okay will this fix the issue where it's not driving straight?

notmattlythgoe 23-01-2015 16:40

Re: Autonomous Help
 
Quote:

Originally Posted by curtis0gj (Post 1432915)
Okay will this fix the issue where it's not driving straight?

I don't know. I need to know what the reported angle values are to answer that.

curtis0gj 23-01-2015 16:47

Re: Autonomous Help
 
Quote:

Originally Posted by notmattlythgoe (Post 1432922)
I don't know. I need to know what the reported angle values are to answer that.

Okay I will figure this out on Monday thanks so much for the help with everything, it's really helped our team a tremendous amount. :D

mmaunu 23-01-2015 17:31

Re: Autonomous Help
 
Quote:

Originally Posted by curtis0gj (Post 1432882)
Hey guys I finally got the encoder going, my electrical guy said it was grounding on something. Anyway I am getting a nice reading on smart db however, when I run autonomous mode the robot turns about 180 stops and turns again and this never ends. I think I am stuck in an if loop and it's not entering the straight driving portion of the program...

Code:

public class Robot extends IterativeRobot {
       
        RobotDrive robot;
    Joystick stick;
    Encoder encoder;       
    Gyro gyro1;
   
    static final double Kp = 0.05;
   
    public void robotInit() {
           
            robot = new RobotDrive(0, 1);
        stick = new Joystick(0);
        gyro1 = new Gyro(0);
        gyro1.reset();
        gyro1.initGyro();
        encoder = new Encoder(0, 1, false, EncodingType.k4X);
     
       
        encoder.setDistancePerPulse(1);
     
        encoder.getDistance();
       

    }

 
    public void autonomousPeriodic() {

            gyro1.reset();
               
            while(isAutonomous() && isEnabled())  {
                   
                    double angle = gyro1.getAngle();
                    double distance = encoder.get();
                   
                    SmartDashboard.putNumber("angle", angle);
                    SmartDashboard.putNumber("distance", distance);
                   
                    robot.drive(-0.15, -1);
                   
                    if(angle > 150) {
                           
                            robot.drive(0,0);
                            gyro1.reset();
                            encoder.reset();
                           
                            Timer.delay(0.5);

                            break;
                           
                            }
                   
                            Timer.delay(0.1);
            }
           
                                while (isAutonomous() && isEnabled()) {

                                                double angle2 = gyro1.getAngle();
                                                double distance2 = encoder.get();
                                               
                                                SmartDashboard.putNumber("distance", distance2);
                                                SmartDashboard.putNumber("angle", angle2);
                                               
                                                robot.drive(-0.20, -angle2 * Kp);
                                       
                                                if(encoder.getDistance() < 100)  {
                                                        robot.drive(0,-0);
                                                       
                                                        break;
                                                }
                                                Timer.delay(0.1);       
                                }
                               
                            }



I hope that it is ok to jump in here...

It looks like you started off with a Robot that extended SampleRobot and have now migrated to a class that extends IterativeRobot. The way that your autonomous code will be executed is very different in these two cases.

SampleRobot
When you extend SampleRobot, your autonomous method will get called for you once. You need to write loops in your code that keep running until you have completed your task. This is why you have the while loop:
while( isAutonomous() and isEnabled() )
{
//stuff
}
You also put your own calls to Timer.delay() in this type of robot because nothing is pausing your code (nothing is creating a timed structure for you).

IterativeRobot
When you extend IterativeRobot, your autonomousPeriodic() method will be called every 20 ms. If you put a loop in this method that runs for longer than 20 ms...well, that is bad. You also don't want to put calls to Timer.delay() in here as this will slow down the execution cycle. Since the code in autonomousPeriodic() is being executed every 20 ms, you want to avoid loops. Put the body of your loops in the method; since it is being executed every 20 ms, it is being looped for you. Also, be careful about resetting values in autonomousPeriodic() without checking a condition. Since the code gets executed repeatedly, you will be continuously resetting the value (e.g. the call to reset the gyro at the top of the method is constantly resetting the gyro).

Proposed new method:

Code:


//a new instance variable for the class...true until the initial turn is done...only false when you want to drive straight
boolean doingInitialTurn;

public void autonomousInit() {
    gyro1.reset();
    doingInitialTurn = true;
}

public void autonomousPeriodic() {
       
                    double angle = gyro1.getAngle();
                    double distance = encoder.getDistance();                    //NOTE: changed to getDistance()
                   
                    SmartDashboard.putNumber("angle", angle);
                    SmartDashboard.putNumber("distance", distance);
                   
              // if you are just starting and doing the first turn...turn code
                if( angle <= 150 && doingInitialTurn )
                          robot.drive(-0.15, -1);
                   
                // otherwise, if the angle is now greater than 150 but you are just finishing the turn...stop turning
                    else if( doingInitialTurn ) {
                           
                            robot.drive(0,0);
                        doingInitialTurn = false;
                            gyro1.reset();
                            encoder.reset();
                           

                    }
                   
                    // if you are completely done with the turn...drive forward instead
                else
                {
                        // you can reuse the original angle and distance values from the top of the method...
                        // they get updated every 20 ms
                        //double angle2 = gyro1.getAngle();
                        //double distance2 = encoder.getDistance();
                       
                        // You also don't need to put them to the SmartDashboard again since the lines at the
                        // top of the method do that for you every 20 ms                       
                        //SmartDashboard.putNumber("distance", distance);
                        //SmartDashboard.putNumber("angle", angle);
                       
                       
                        robot.drive(-0.20, -angle * Kp);
                                       
                        if(distance > 100)
                                robot.drive(0,-0);
                                                       
                                                       
                }
                               
}

Also, the line of code that checks for when to stop driving forward had reversed logic; if the distance traveled was less than 100, you stopped driving. You want to stop driving only after the distance traveled is greater than 100. You had:
Quote:

Code:

if(encoder.getDistance() < 100)  {
        robot.drive(0,-0);
                                                       
        break;
}


Lastly, you do not want to set the distance per pulse to be 1. The distance per pulse is the number of inches (or other unit) that the robot actually moves every time the encoder registers a "pulse", which is 250 times per revolution of the encoder (or 1000 times per revolution if you are using 4x encoding?). The Encoder's get() method returns the count (the same as the number of pulses?) since the last call to reset. The getDistance() method returns the count multiplied by the number specified in setDistancePerPulse(). If you set the distance per pulse to 1, then you will need to travel a great "distance" in your code.

To find the correct value for setDistancePerPulse(), we typically:
  1. Reset the encoder in teleopInit(). This code will only run when you first enable teleop.
  2. In teleopPeriodic(), get the count with encoder.get() and print it or put it up on the SmartDashboard. This code will run every 20 ms when teleop mode is enabled.
  3. Manually move the robot forward 10 feet (as close to 120 inches as you can).
  4. Divide 120 inches by the count from the encoder...that is your distance per pulse.

Good luck and let me know if this did or didn't work...unfortunately, I didn't test the code :(

curtis0gj 23-01-2015 18:04

Re: Autonomous Help
 
Quote:

Originally Posted by mmaunu (Post 1432952)
I hope that it is ok to jump in here...

It looks like you started off with a Robot that extended SampleRobot and have now migrated to a class that extends IterativeRobot. The way that your autonomous code will be executed is very different in these two cases.

SampleRobot
When you extend SampleRobot, your autonomous method will get called for you once. You need to write loops in your code that keep running until you have completed your task. This is why you have the while loop:
while( isAutonomous() and isEnabled() )
{
//stuff
}
You also put your own calls to Timer.delay() in this type of robot because nothing is pausing your code (nothing is creating a timed structure for you).

IterativeRobot
When you extend IterativeRobot, your autonomousPeriodic() method will be called every 20 ms. If you put a loop in this method that runs for longer than 20 ms...well, that is bad. You also don't want to put calls to Timer.delay() in here as this will slow down the execution cycle. Since the code in autonomousPeriodic() is being executed every 20 ms, you want to avoid loops. Put the body of your loops in the method; since it is being executed every 20 ms, it is being looped for you. Also, be careful about resetting values in autonomousPeriodic() without checking a condition. Since the code gets executed repeatedly, you will be continuously resetting the value (e.g. the call to reset the gyro at the top of the method is constantly resetting the gyro).

Proposed new method:

Code:


//a new instance variable for the class...true until the initial turn is done...only false when you want to drive straight
boolean doingInitialTurn;

public void autonomousInit() {
    gyro1.reset();
    doingInitialTurn = true;
}

public void autonomousPeriodic() {
       
                    double angle = gyro1.getAngle();
                    double distance = encoder.getDistance();                    //NOTE: changed to getDistance()
                   
                    SmartDashboard.putNumber("angle", angle);
                    SmartDashboard.putNumber("distance", distance);
                   
              // if you are just starting and doing the first turn...turn code
                if( angle <= 150 && doingInitialTurn )
                          robot.drive(-0.15, -1);
                   
                // otherwise, if the angle is now greater than 150 but you are just finishing the turn...stop turning
                    else if( doingInitialTurn ) {
                           
                            robot.drive(0,0);
                        doingInitialTurn = false;
                            gyro1.reset();
                            encoder.reset();
                           

                    }
                   
                    // if you are completely done with the turn...drive forward instead
                else
                {
                        // you can reuse the original angle and distance values from the top of the method...
                        // they get updated every 20 ms
                        //double angle2 = gyro1.getAngle();
                        //double distance2 = encoder.getDistance();
                       
                        // You also don't need to put them to the SmartDashboard again since the lines at the
                        // top of the method do that for you every 20 ms                       
                        //SmartDashboard.putNumber("distance", distance);
                        //SmartDashboard.putNumber("angle", angle);
                       
                       
                        robot.drive(-0.20, -angle * Kp);
                                       
                        if(distance > 100)
                                robot.drive(0,-0);
                                                       
                                                       
                }
                               
}

Also, the line of code that checks for when to stop driving forward had reversed logic; if the distance traveled was less than 100, you stopped driving. You want to stop driving only after the distance traveled is greater than 100. You had:


Lastly, you do not want to set the distance per pulse to be 1. The distance per pulse is the number of inches (or other unit) that the robot actually moves every time the encoder registers a "pulse", which is 250 times per revolution of the encoder (or 1000 times per revolution if you are using 4x encoding?). The Encoder's get() method returns the count (the same as the number of pulses?) since the last call to reset. The getDistance() method returns the count multiplied by the number specified in setDistancePerPulse(). If you set the distance per pulse to 1, then you will need to travel a great "distance" in your code.

To find the correct value for setDistancePerPulse(), we typically:
  1. Reset the encoder in teleopInit(). This code will only run when you first enable teleop.
  2. In teleopPeriodic(), get the count with encoder.get() and print it or put it up on the SmartDashboard. This code will run every 20 ms when teleop mode is enabled.
  3. Manually move the robot forward 10 feet (as close to 120 inches as you can).
  4. Divide 120 inches by the count from the encoder...that is your distance per pulse.

Good luck and let me know if this did or didn't work...unfortunately, I didn't test the code :(

Thanks for the feed back! I have been receiving so much positive feed back it's wonderful. Any who, the reason I switch from SampleRobot to IterativeRobot is because I made a new project later on just to try different autonomous programs so I did not muck up my original project (Also because when I made a new project I did not know what the difference was between the two...). However, now that I have a good idea of what my autonomous is going to be should I switch back to SampleRobot? I would like to keep the loops so I am guessing I need to switch, let me know what you think. Also thanks for the setDistanceperPulse tuning I will do that on Monday to get it nice and tuned. :)

mmaunu 23-01-2015 18:38

Re: Autonomous Help
 
Quote:

Originally Posted by curtis0gj (Post 1432970)
Thanks for the feed back! I have been receiving so much positive feed back it's wonderful. Any who, the reason I switch from SampleRobot to IterativeRobot is because I made a new project later on just to try different autonomous programs so I did not muck up my original project (Also because when I made a new project I did not know what the difference was between the two...). However, now that I have a good idea of what my autonomous is going to be should I switch back to SampleRobot? I would like to keep the loops so I am guessing I need to switch, let me know what you think. Also thanks for the setDistanceperPulse tuning I will do that on Monday to get it nice and tuned. :)

As for which template to use (Sample or Iterative), that is up to you. The SampleRobot template gets complicated quickly if you add any complexity to your code. If you like the loops, then you will need to use SampleRobot.

If the code above makes sense, though, I would encourage you to explore the IterativeRobot template. The teleop code works in a similar fashion: teleopInit() gets called once when teleop is enabled and teleopPeriodic() gets called every 20 ms for you (so avoid loops that don't exit quickly and do not use Timer.delay() to pause your code). Also, the 20 ms is just a great approximation...the loop timing is reallllly close to every 20 ms (certainly close enough for what we're doing).

If you do switch back to SampleRobot, then you will need the loops and the calls to Timer.delay().

Keep plugging away at it and keep asking questions. ChiefDelphi is full of people that want to help :)

curtis0gj 23-01-2015 23:28

Re: Autonomous Help
 
Quote:

Originally Posted by mmaunu (Post 1432993)
As for which template to use (Sample or Iterative), that is up to you. The SampleRobot template gets complicated quickly if you add any complexity to your code. If you like the loops, then you will need to use SampleRobot.

If the code above makes sense, though, I would encourage you to explore the IterativeRobot template. The teleop code works in a similar fashion: teleopInit() gets called once when teleop is enabled and teleopPeriodic() gets called every 20 ms for you (so avoid loops that don't exit quickly and do not use Timer.delay() to pause your code). Also, the 20 ms is just a great approximation...the loop timing is reallllly close to every 20 ms (certainly close enough for what we're doing).

If you do switch back to SampleRobot, then you will need the loops and the calls to Timer.delay().

Keep plugging away at it and keep asking questions. ChiefDelphi is full of people that want to help :)

Sweet maybe after the build season I will explore the Iterative robot but I would like to try and keep it basic this year seeing as I am new to this whole thing.

Fauge7 24-01-2015 01:49

Re: Autonomous Help
 
Quote:

Originally Posted by curtis0gj (Post 1433117)
Sweet maybe after the build season I will explore the Iterative robot but I would like to try and keep it basic this year seeing as I am new to this whole thing.

Honestly your better off with the command based structure, its easier to manage, more people use it, and its logically broken out into easier to find places. Need a constant? RobotMap...Need a joystick? OI, need a command for autonomous? Autonomous command Group, need a motor? SubsystemA...if you need help with it I am willing to Skype you and mentor you through converting it from a sample Robot to a command based robot. Send me a pm! This applies to anybody having trouble with any of the templates!


All times are GMT -5. The time now is 10:30.

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