WDDcat / vex-Robot-Control

用于记录我所研究出来用于vex机器人控制的函数This repository is to record some functions I wrote while I did research on vex robot control
4 stars 2 forks source link

share code #8

Open WDDcat opened 5 years ago

WDDcat commented 5 years ago

bool turnRightWithGyroL(int power, float target, float timeLimit, bool fullTime, float P, float I, float D){ ResetMotor(); float cur = GyroGetAngle(); float err = target - cur, last_err = 0, total_err = 0, delta_err = 0, OUT; Brain.resetTimer(); while (Brain.timer(msec) < timeLimit) { cur = GyroGetAngle(); last_err = err; err = target - cur; if(cur / target > KI_TURN_START_PERCENT) total_err += err; delta_err = err - last_err; if(err <= 0.6) { if(!fullTime) return true; else if(err >= -0.6) return true; }

OUT = P * err + I * total_err + D * delta_err;
if(fabs(OUT) < power * 0.1){
  if(OUT > 0) OUT = power * 0.1;
  else        OUT = -(power * 0.1);
}

LeftMotor1.spin(fwd, 3 * OUT, rpm);
LeftMotor2.spin(fwd, 3 * OUT, rpm);
RightMotor1.stop(hold);
RightMotor2.stop(hold);

sleep(10);

} Stop(hold); return false; }

bool turnLeftWithGyroR(int power, float target, float timeLimit, bool fullTime, float P, float I, float D){ ResetMotor(); float cur = GyroGetAngle(); float err = target - cur, last_err = 0, total_err = 0, delta_err = 0, OUT; Brain.resetTimer(); while (Brain.timer(msec) < timeLimit) { cur = GyroGetAngle(); last_err = err; err = target - cur; if(cur / target > KI_TURN_START_PERCENT) total_err += err; delta_err = err - last_err; if(err <= 0.6) { if(!fullTime) return true; else if(err >= -0.6) return true; }

OUT = P * err + I * total_err + D * delta_err;
if(fabs(OUT) < power * 0.1){
  if(OUT > 0) OUT = power * 0.1;
  else        OUT = -(power * 0.1);
}

RightMotor1.spin(fwd, -(3 * OUT), rpm);
RightMotor2.spin(fwd, -(3 * OUT), rpm);
LeftMotor1.stop(hold);
LeftMotor2.stop(hold);

sleep(10);

} Stop(hold); return false; }

WDDcat commented 5 years ago

`bool turnRightWithGyroL(int power, float target, float timeLimit, bool fullTime, float P, float I, float D){ ResetMotor(); float cur = GyroGetAngle(); float err = target - cur, last_err = 0, total_err = 0, delta_err = 0, OUT; Brain.resetTimer(); while (Brain.timer(msec) < timeLimit) { cur = GyroGetAngle(); last_err = err; err = target - cur; if(cur / target > KI_TURN_START_PERCENT) total_err += err; delta_err = err - last_err; if(err <= 0.6) { if(!fullTime) return true; else if(err >= -0.6) return true; }

OUT = P * err + I * total_err + D * delta_err;
if(fabs(OUT) < power * 0.1){
  if(OUT > 0) OUT = power * 0.1;
  else        OUT = -(power * 0.1);
}

LeftMotor1.spin(fwd, 3 * OUT, rpm);
LeftMotor2.spin(fwd, 3 * OUT, rpm);
RightMotor1.stop(hold);
RightMotor2.stop(hold);

sleep(10);

} Stop(hold); return false; }

bool turnLeftWithGyroR(int power, float target, float timeLimit, bool fullTime, float P, float I, float D){ ResetMotor(); float cur = GyroGetAngle(); float err = target - cur, last_err = 0, total_err = 0, delta_err = 0, OUT; Brain.resetTimer(); while (Brain.timer(msec) < timeLimit) { cur = GyroGetAngle(); last_err = err; err = target - cur; if(cur / target > KI_TURN_START_PERCENT) total_err += err; delta_err = err - last_err; if(err <= 0.6) { if(!fullTime) return true; else if(err >= -0.6) return true; }

OUT = P * err + I * total_err + D * delta_err;
if(fabs(OUT) < power * 0.1){
  if(OUT > 0) OUT = power * 0.1;
  else        OUT = -(power * 0.1);
}

RightMotor1.spin(fwd, -(3 * OUT), rpm);
RightMotor2.spin(fwd, -(3 * OUT), rpm);
LeftMotor1.stop(hold);
LeftMotor2.stop(hold);

sleep(10);

} Stop(hold); return false; }`