|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#76
|
|||||
|
|||||
|
Re: Autonomous Help
The riolog is in eclipse. IF you don't see it as a tab near the bottom then go yo Window -> Show View -> Riolog.
|
|
#77
|
|||
|
|||
|
Re: Autonomous Help
I don't see anything bad in the riolog or ds log so idk whats going on with that.
|
|
#78
|
|||
|
|||
|
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);
}
}
|
|
#79
|
|||||
|
|||||
|
Re: Autonomous Help
Quote:
|
|
#80
|
|||
|
|||
|
Re: Autonomous Help
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?
|
|
#81
|
||||
|
||||
|
Re: Autonomous Help
Quote:
|
|
#82
|
|||||
|
|||||
|
Re: Autonomous Help
I'm guessing you need to change it to positive, but that will all depend on the angle value.
|
|
#83
|
|||
|
|||
|
Re: Autonomous Help
Okay will this fix the issue where it's not driving straight?
|
|
#84
|
|||||
|
|||||
|
Re: Autonomous Help
I don't know. I need to know what the reported angle values are to answer that.
|
|
#85
|
|||
|
|||
|
Re: Autonomous Help
Quote:
![]() |
|
#86
|
||||
|
||||
|
Re: Autonomous Help
Quote:
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);
}
}
Quote:
To find the correct value for setDistancePerPulse(), we typically:
Good luck and let me know if this did or didn't work...unfortunately, I didn't test the code ![]() |
|
#87
|
|||
|
|||
|
Re: Autonomous Help
Quote:
![]() |
|
#88
|
||||
|
||||
|
Re: Autonomous Help
Quote:
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 ![]() |
|
#89
|
|||
|
|||
|
Re: Autonomous Help
Quote:
|
|
#90
|
|||
|
|||
|
Re: Autonomous Help
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!
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|