Hey guys I just finished my autonomous program on bag night and the next day realised how long and messy it was. So I got some help and made functions but I can’t test them :mad: . So if anyone is able to help me here’s the github link https://github.com/curtis0gj/5033-2014-15?files=1.
FYI you can build them to search for syntax errors… like this
private static void wait(Robot r, double waittime) {
while (true) {
if (!.risAutonomous() || !r.isEnabled()) return;
r.Timer.delay(waittime); //r or just timer?
return;
}
private static void reset(Robot r) {
missing a } and i assume you mean “”!r.is…" not “!.ris…” (also, its just Timer.delay)
passing in Robot for everything is ugh-ly. just make Robot a singleton… or use a static initializer
while (true) {
double distance = r.encoder.get();
double angle = r.gyro.getAngle();
if (!r.isAutonomous() || !r.isEnabled()) return;
r.robot.drive(-0.25, angle * r.Kp);
//r.robot.drive(-0.40, 0);
if (distance < -d) {
reset(r);
return;
}
}
the closed loop structure of this will hurt the robot, periodic ms timers are necessary
r.robot.drive(-.40, -1)
not Java-standard formatting
Boolean maxarmlimit = r.limit4.get();
capital boolean (also that name scheme is bad, both on the variable and the limit)
r.armmotor.set(Defines.ARM_SPEED);
r.leftarmwheel.set(Relay.Value.kForward);
r.rightarmwheel.set(Relay.Value.kReverse);
non-standard formatting (camelcase, descriptive names)
if (angle > deg) {
comparing a double and an int
r.robot.drive(-.40, -1);
constant loop? atleast a P loop, preferably PID
your “turn” and “turn2” functions are quite ineffective, basic angle averaging schemes should be used
other than that I’m losing my focus, but there are alot more issues
Alright thanks I am going to make those changes.
should distance be an int or a double and how should I add the periodic timer?
don’t double post, use the edit button.
for distance, i usually go with doubles for most things so I don’t accidentally mess data up with division.
don’t know what the second thing you asked is in reference to. If it’s the closed loop, than just do a Timer.delay(0.02) (20ms, standard 50hz operating cycle) inside the while(true) loop
Alright I added in some delays inside the while(true) loop. I just put the delay as the first line in the loop I don’t know if it matters.
Preferably it would be the last line (with it at the first, theres a 20ms delay before any actions are performed)
Your indenting for everything after “wait” is now wrong
AUTOS should not be capitalized (and should be descriptive: “AutonMode”, “SelectedAutonMode”, “AutonEnum”, etc)
turn and turn2 still need to be turned into feedback loops but for now rename them to turnLeft and turnRight and make one function- turn- that if gyro < desired than turnLeft(angle) else turnRight(angle)
capitalization schemes are wrong in Robot.java (http://java.about.com/od/javasyntax/a/nameconventions.htm)
if (autoMethod == Defines.AUTOS.AUTO_ONE) {
return;
} else if (autoMethod == Defines.AUTOS.AUTO_MOVE) {
return;
} else if (autoMethod == Defines.AUTOS.AUTO_TWORED) {
return;
} else if (autoMethod == Defines.AUTOS.AUTO_TWOBLUE) {
return;
}
this block of code is rather… erm…
Why is your wait function inside of a while(true) loop?
lol I did a very lousy job of naming things, sorry for that and I will change the names to be more descriptive and properly cased. Anyway how can I get the waits to work again? This is the function,
public static void turn(Robot r, double deg) {
while (true) {
double angle = r.gyro.getAngle();
if (!r.isAutonomous() || !r.isEnabled()) return;
if(angle < 70) {
turnRight(r, 70);
} else {
turnLeft(r, -70);
reset(r);
break;
}
}
I’m not sure how to program it, sorry I’m still fairly new to programming.
here’s what I’d do
public static void turn(Robot r, double deg) {
if(deg < gyro.getAngle()){ //this is not right, need to have a function to handle rollovers
//example for such a function: if Math.abs(deg-gyro.getAngle()) < 90 || Math.abs(deg-gyro.getAngle()) > 270
//i dont know, just thinking to myself. see here for further instructions -> http://stackoverflow.com/questions/1878907/the-smallest-difference-between-2-angles
turnLeft(r,deg);
}else{
turnRight(r,deg);
}
}
public static void turnRight(Robot r, double deg){
while(true){
//turn right
if(distanceBetweenAngles(getGyro(),deg) <= MAX_ROTATE_ERROR){
return; //or break; whatever
}
Timer.delay(0.02);
}
}
except without the broken formatting since I made that in this text editor and not a code editor
E/ misunderstood the point of the code, fixed.
So what will this whole function be used for calculating turns?
hm?
note that I edited that post like 5 times.
Anyway, heres a (maybe) working function
public static double angleError(double setpointDegressZeroToThreeSixty, double experimentalDegrees){
double err = setpointDegressZeroToThreeSixty - experimentalDegrees; //0 360
if(err < -180){
err += 360;
}else if(err > 180){
err -= 360;
}
return err;
}
double kp_rotate = 0.01;
double MAX_ERROR = 5;
public static void turn(double deg){
while(true){
double deltaAngle = angleError(deg, gyro.getAngle());
if(Math.abs(deg-deltaAngle) < MAX_ERROR){
break;
}else{
robot.drive(0, deltaAngle*kp_rotate); //drive taking a move and a rotate value
}
Timer.delay(0.02);
}
}
Does this turn function replace left turn and right turn?
The new turn function does, yes, since the error will be positive or negative depending on where the setpoint is in comparison to the experimental and thus doesn’t need seperate cases depending on left or right.
Should I change the static voids to private and add Robot r. Also I wanted to know how I can make the wait function work so the robot waits between certain functions.
apologies, wrapped up in poe.
Should I change the static voids to private and add Robot r.
For now, yeah. End game shouldn’t have that “Robot r” parameter
Also I wanted to know how I can make the wait function work so the robot waits between certain functions.
hm?
Timer.delay will wait the desired time. For the new loops, they automatically wait untill finished so this isn’t needed. If you wanted to add an extra wait, just throw in a Timer.delay() before turn, drive, etc. calls
Alright thanks, to replace the Robot r parameter could I extend my public class auto to robot or is there another way to do it?
Something like this
static Robot r
public static void setRobot(Robot rob){
r = rob;
}
//in Robot.java
AutonManager.setRobot(this);
or
Robot robot
public AutonManager(Robot r){
this.robot =r;
}
//in Robot.java
AutonManager autonManager;
public void robotInit(){
autonManager = new AutonManager(this);
}
Would this be okay?
public class Auto {
static Robot r
public static void setRobot(Robot rob) {
r = rob;
}
And when I have this in do I need to put r in front of everything still?
yeah thats fine. If you don’t want to put r in front of everything, you can either
- rename r to robot (atleast it looks more professional…)
public class Auto {
static Robot r
static RobotDrive drive
static DigitalInput upperLimitSwitch
//and so on for other stuff
public static void setRobot(Robot rob) {
r = rob;
drive = r.drive;
upperLimitSwitch = r.limit4;
}
Also I was wondering how I can call the new turn function you showed me do I just call turn(r, deg that I want to turn???); And you also said there was an ugly block in robot will it be an issue?
public void autonomous() {
autoMethod = (Defines.Autos) autoChooser.getSelected();
Auto.run(this, autoMethod);
if (autoMethod == Defines.Autos.AUTO_GRAB_ONE_BIN_BLUE_SIDE) {
return;
} else if (autoMethod == Defines.Autos.AUTO_GRAB_ONE_BIN_RED_SIDE) {
return;
} else if (autoMethod == Defines.Autos.AUTO_MOVE_TO_ZONE) {
return;
} else if (autoMethod == Defines.Autos.AUTO_GRAB_TWO_BINS_RED_SIDE) {
return;
} else if (autoMethod == Defines.Autos.AUTO_GRAB_TWO_BINS_BLUE_SIDE) {
return;
}
}
public static void angleError(double setpointDegressZeroToThreeSixty, double experimentalDegrees) {
double err = setpointDegressZeroToThreeSixty - experimentalDegrees; // 0 TO 360!
if(err < -180) {
err += 360;
} else if(err > 180) {
err -= 360;
}
return err;
}
double kp_rotate = 0.01;
double MAX_ERROR = 5;
public static void turn(Robot r, double deg) {
while(true) {
double deltaAngle = angleError(deg, gyro.getAngle());
if(Math.abs(deg - deltaAngle) < MAX_ERROR) {
break;
} else {
r.robot.drive(0, deltaAngle * kp_rotate);
}
Timer.delay(0.02);
}
}