DAC pin output lag

Hi everyone,

I'm new to arduino and circuitry alike, and I have a quick question. I noticed that there was a good half second output lag time with the DAC pins on the due. Long story short, I didn't have the micro usb plug to power it via laptop, but i did have an uno board and usb for that so i took the 5v out from the uno to power the due. I know it was under powered, and I was wondering if that is the reason for the lag?

Thanks!

What evidence do you have for that astonishing delay value? (ie where's the sketch and your methodology - the delay won't be in the DAC, clearly).

[my quick experiments show the delay from a digitalWrite to an analogWrite on a DAC pin to be about 3us, and the rise/fall time of the DAC pin to be about 250ns. Part of the 3us delay is in digitalWrite, part in analogWrite and part is the true delay of the hardware DAC. This is in 12 bit mode.

I’m using the DAC to send a voltage signal to some motor controllers for an electric go kart. I was basically reading a throttle pot value, a steering pot value, doing some calculations, and sending it to the motor controllers.
(Kelly KBL http://kellycontroller.com/kbl96351e24-96v350abldc-controllerwith-regen-p-1258.html )

Here’s a rough draft of the sketch

#include <math.h>;     //include math library
const int potPin = A0;
const int throttlePin = A1;
const int leftMotor = DAC0;
const int rightMotor = DAC1;


void setup()
{

  Serial.begin(9600);  //just for now
  analogReadResolution(12);  //set to 12 bits
  analogWriteResolution(12);
  pinMode(potPin, INPUT);
  pinMode(throttlePin, INPUT);
  pinMode(rightMotor, OUTPUT);
  pinMode(leftMotor, OUTPUT);
}

void loop()
{
  float potVal = analogRead(potPin);  //read the pot pin
  float velRead = analogRead(throttlePin);
  
  float leftTheta = map(potVal, 0, 2028, 45, 0);    //read pot value and 
  float rightTheta = map(potVal, 2068, 4095, 0, 45);  // and map from 0 to 45 deg
  //constrain (leftTheta, 0, 511);    //hopefully contrains values to ignore anything
  float velInitial = map(velRead, 940, 563, 0, 4095);
  //constrain (rightTheta, 512, 1023);  //values on opposite side turning
  
  float leftRadian = (leftTheta * 3.14159 / 180); //convert deg to rad since tan func
                                                     //only works in rad.
  float rightRadian = (rightTheta * 3.14159 / 180);
  
  if (potVal <= 2028)   //if the pot is showing left turn, calculate both arcs & vel increase
    {
  
      float leftTurnRad = (60 / (tan (leftRadian)));  //60 will be front to back wheel base
  
      float rightTurnRad = 40 + leftTurnRad;  //40 will be back wheel base
  
      float arcInside = leftRadian * leftTurnRad;  //calc arc length of inside
      float arcOutside = leftRadian * rightTurnRad;  //calc arc length of outside
      //double arcDiff = arcOutside - arcInside;        //calc difference of arc lengths
      float arcRatio = arcOutside / arcInside;    //calc ratio of arc lengths
      //int velIncrease;  //vel increase i think needs to be int
      //if (arcRatio > 1)  // sets the min threshhold at 1
      //{
      float velIncrease = arcRatio * velInitial;  //this should calc speed increase, deal with else later
      //}
   //  else
     // {
      //velIncrease = velInitial;    //i think its getting corn-fizzled here
     // }

  Serial.print(velInitial);
  Serial.print("   ");
  Serial.println(velIncrease);
  delay(500);
  
    analogWrite(DAC0, velInitial);
    analogWrite(DAC1, velIncrease);
    }
  else if (potVal >= 2068)    
    {
  
    float rightTurnRad2 = (60 / (tan (rightRadian)));  //60 will be front to back wheel base
  
    float leftTurnRad2 = 40 + rightTurnRad2;  //40 will be back wheel base
  
    float arcInside2 = rightRadian * rightTurnRad2;  //calc arc length of inside
    float arcOutside2 = rightRadian * leftTurnRad2;  //calc arc length of outside
    //double arcDiff = arcOutside - arcInside;        //calc difference of arc lengths
    float arcRatio2 = arcOutside2 / arcInside2;    //calc ratio of arc lengths
    //int velIncrease2;  //vel increase i think needs to be int
    //if (arcRatio2 > 1)  // sets the min threshhold at 1deg 
      //{
    float velIncrease2 = arcRatio2 * velInitial;
      //}
     // else
      //  {
     //   velIncrease2 = velInitial;
      //  }
    Serial.print(velIncrease2);
    Serial.print("   ");
    Serial.println(velInitial);
    delay(500);
    analogWrite(DAC1, velInitial);
    analogWrite(DAC0, velIncrease2);
    }
  else //if (potVal > 510 && potVal < 512)    //this should take care of the 1 to 1 ratio issues
  {
  Serial.print(velInitial);
  Serial.print("   ");
  Serial.print(velInitial);
  Serial.print("   ");
  Serial.println("Straight Condition");
  delay(500);
  analogWrite(DAC1, velInitial);
  analogWrite(DAC0, velInitial);
  }

  
  //double insideSpeed = velInitial;
  //double outsideSpeed = velInitial + velIncrease;
  
  //Serial.print(velInitial);
  //Serial.print("   ");
  //Serial.println(velIncrease);
  //delay(500);
  
  //analogWrite(rightLed, rightTurnRad);
  
  
}

I noticed the lag when I turned the pot to accelerate the motors, and it took a good half second. Serial values and calculations all seem okay.

  delay(500);

is what's causing your delay.

 delay(500);
    analogWrite(DAC0, velInitial);
    analogWrite(DAC1, velIncrease);

You’re wondering about a delay of 500mS and you have delay(500) in your code three times…just a guess but personally I’d start looking there.


Rob

Oh jeeze... I had that in there for reading the serial values earlier in testing. Totally forgot about it! Thanks guys!