Hello, I am trying to implement a delay in my PID loop so that I don’t do a burnout on the start of the program. I seem to have a problem with it though, as it does not slow down as it is supposed to with my “time code”. Here is the code. Any ideas?
#pragma region VEXcode Generated Robot Configuration
// Make sure all required headers are included.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include "vex.h"
using namespace vex;
// Brain should be defined by default
brain Brain;
// START V5 MACROS
#define waitUntil(condition) \
do { \
wait(5, msec); \
} while (!(condition))
#define repeat(iterations) \
for (int iterator = 0; iterator < iterations; iterator++)
// END V5 MACROS
// Robot configuration code.
motor Motor1 = motor(PORT1, ratio18_1, false);
motor Motor2 = motor(PORT2, ratio18_1, true);
sonar RangeFinderA = sonar(Brain.ThreeWirePort.A);
#pragma endregion VEXcode Generated Robot Configuration
#include "vex.h"
using namespace vex;
// A global instance of competition
competition Competition;
void usercontrol(void) {
// User control code here, inside the loop
while (1) {
Brain.Screen.clearScreen();
// display the ultrasonic distance in in on the screen
Brain.Screen.printAt(1, 40, "distance: %f in", RangeFinderA.distance(distanceUnits::in));
//Ints
int Target = 24;
int TotalError = 0;
int PreventError = 0;
int Derivative;
//Double
//double kP = .25;
//double kI = .5;
//double kD = .01;
//double kP = .40;
//double kI = .10;
//double kD = .10;
//Zero osolation, .27 of target(19);
double kP;
double kI = .10;
double kD = .10;
int timer;
if (timer<10) {
timer++;
kP = 0.000001;
kI = .0001;
kD = .00001;
wait(1,seconds);
}
else {
kP = .40;
}
double RangeValue = RangeFinderA.distance(inches);
int Error;
//Potential Error
Error = RangeValue - Target ;
//Derivative
Derivative = Error - PreventError;
//integral
TotalError += Error;
//defines motor power
double motorPower = Error * kP + Derivative * kI + TotalError * kD;
Motor1.spin(reverse, motorPower, volt);
Motor2.spin(reverse, motorPower, volt);
PreventError = Error;
wait(20, msec); // Sleep the task for a short amount of time to
// prevent wasted resources.
}
}
int main() {
// Set up callbacks for driver control period
Competition.drivercontrol(usercontrol);
// Prevent main from exiting with an infinite loop.
while (true) {
wait(100, msec);
}
}