Logging Is Delayed

My team is trying to use the pros terminal to output our PID values to debug what our PID does. It logs the time and speed output of the PID along with the P, I, and D values. Occasionally, the program just stops logging and continues logging a few seconds later with the data that it is supposed to log. It has been fine for a while, but this recently started happening. It is supposed to log every 50 milliseconds. Also, it sometimes says that it has trouble decoding bytes. Here is an example of this: WARNING - pros.serial.devices.vex.v5_user_device:read - Could not decode bytes: 6f757431353838342c20362e35333630392c2031312e3736352c202d302e3030303536343537352c202d302c2031312e373634340a.

We have tried increasing the delay between logs and it did the same thing. We have also searched for topics about this on the forum but we were not able to find anything. Does anyone know a way to fix this?

Sounds like you don’t actually have a 50ms delay between logs. Can you send the relevant logging code so that we can see where the issue may lie?
Also, if you plug directly into the brain you won’t have this problem. It is only an issue when plugged into the controller.

1 Like

We can’t plug it into the brain because the robot is moving when we run the PID.

This is the code that runs the PID:

void RobotController::faceAngle(float targetAngle, float minimumSpeed)
{
    turnPID.reset(); // Reset the PID

    float threshTime = 0;
    float last_speed = -1 * sgn(targetAngle - getRotation());
    float max_accel = 127.0 * 8.5;

    while (threshTime < 100)
    {
        float angle = getRotation(); // Get the current angle

        float speed = turnPID.calculate(targetAngle, angle);

        if(speed >= 0 && speed < minimumSpeed){
            speed = minimumSpeed;
        }
        else if(speed < 0 && speed > -minimumSpeed){
            speed = -minimumSpeed;
        }

        if (fabs(speed - last_speed) > max_accel / (1000.0 / 20.0)) {
            speed = last_speed + (max_accel / (1000.0 / 20.0) * sgn(speed - last_speed));
        }

        move(                                     // Update the drivetrain
            0,                                    // No forward velocity
            speed // Turn velocity from the turn PID
        );

        if (abs(turnPID.getError()) < 0.75){
             // Exit if error is low enough
            threshTime += 20;
        }
        else{
            threshTime = 0;
        }

        last_speed = speed;
        pros::delay(50); // Wait so other threads can run
    }

    move(0, 0); // Stop drivetrain
}

These are the functions that do the calculations for the PID and log the values:


float PID::calculateError(float error) {
  this->error = error;

  if(isnan(lastError)){
    lastError = error;
  }
  
  //calculate delta time
  float dT = timer.millis().convert(okapi::millisecond) - lastTime;
  //abort if dt is too small
  if(dT < 10) return output;
  
  //calculate proportional
  proportional = (error * kP);

  //calculate derivative
  derivative = (error - lastError) / dT;

  total_error += error;
  
  //calculate output
  output = proportional + (derivative * kD) + (total_error *  kI);

  // std::cout << fmin(m_error, 127) << ", " << fmin(m_proportional, 127) << ", " << fmin(m_derivative * m_kD, 127) << ", " << fmin(m_output, 127) << std::endl;
  std::cout << lastTime + dT << ", " << error << ", " << proportional << ", " << derivative * kD << ", " << total_error * kI << ", " << output << std::endl;

  //limit output
  if(std::abs(output) > 127) output = sgn(output) * 127;

  //save values
  lastTime = timer.millis().convert(okapi::millisecond);
  lastError = error;
  
  return output;
}

float PID::calculate(float target, float current) {
  return calculateError(target - current);
}

We just tried this code and the logging was also delayed:

while(true){
    if (master.get_digital(DIGITAL_L1))
    {
        std::cout << "pressed" << std::endl;
    }
    else
    {
        std::cout << "not pressed" << std::endl;
    }

    pros::delay(50);
}

This means that the problem is probably not caused by our code.

I have run into this problem before and I fixed it by plugging directly into the robot. However, it seems you are unable to do this so an alternative would be to log the information onto a microSD card and then look at the logged info afterward.