Controllo PID

Adesso il pid funziona e anche se non ho ancora settato kd e ki il robottino prova con delle "botte" a bilanciarsi... che carino XD

    double kp=35; //100 botta forte
    double ki=0;                  
    double kd=0;                  
  
    double ITerm, lastInput;
    unsigned long lastTime;
    unsigned long SampleTime;							

void iniz(){
      SampleTime = 100;							
      lastTime = millis()-SampleTime;	
  }


int updatePid(float mySetpoint, float x){
   iniz();
   
   unsigned long now = millis();
   unsigned long timeChange = (now - lastTime);
   if(timeChange>=SampleTime)
   {
      /*Compute all the working error variables*/
      float error = mySetpoint - x;
      ITerm += ki*(error * (SampleTime/1000));
      float dInput = (x - lastInput);
 
      /*Compute PID Output*/
      int output = (kp * error) + ITerm - (kd * dInput);
      	  
      /*Remember some variables for next time*/
      lastInput = x;
      lastTime = now;

                                        
                if(output>=255){output=255;}
                if(output<=0)  {output=0;}
                if(output <= 2 && output > 0) {vel = 0;}
                return output;
   }
   else return -1;
}

il problema che ho è che il robot comincia a correggere a circa 70-80(troppo tardi) su 255 e quindi cade.
FINO A 70-80 I DUE MOTORI LEGO NON SI MUOVONO ED EMETTONO UNO STRANO BEEP NEVROTICO!
qualcuno avete mai riscontrato di questi problemi?
ho cercato su google ma niente... perchè un motore con encoder(che x ora nn uso) deve fare quel suono... troppi comandi che gli arrivano?? escluderei falsi contatti..
ps smacchinando e spulciando in giro con il kit lego nxt ho letto che hanno previsto una funzione tipo NXTmotor.WaitFor();
che attende che il mattoncino esegua l'istruzione. Ora che arduino ha preso il posto del brick non vorrei che mancasse qualcosa di hardware/software... :disappointed_relieved:

HO TROVATO QUALCOSA:

    I'm controlling two motors, and when their movement has stopped, the motors make a strange high-pitched noise…
        This is a feature of the NXT firmware. The sound occurs when working with two synchronized motors, e.g. for driving robots. Once the movement has stopped (using old-fashioned SendMotorSettings command from toolbox version 2.*, or using ActionAtTachoLimit = 'Coast' with NXTMotor in toolbox version 4.*, or DirectMotorCommand), the motors are still synced. If you move one motor, the other will move, too. This by design, and this is when you hear the high-pitched sound from the motors. To disable this noise, just send a .Stop() command from NXTMotor or use StopMotor(). The next motor command should clear / override the synchronization anyway.

ragazzi come potete vedere dalle foto io uso arduino uno quindi non c'è traccia di nxt ne firmware.... sono allibito
mi spieghi chi + di me sa, per favore...

copia.ino (63 KB)