arduino / Arduino

Arduino IDE 1.x
https://www.arduino.cc/en/software
Other
14.12k stars 7k forks source link

Due: issues about Wire plus SPI, DueTimer + Scheduler + pwm + PID_v1 #4301

Closed shiftleftplusone closed 7 years ago

shiftleftplusone commented 8 years ago

is it possible that there are still unresolved issues about the DUE using Wire plus SPI, DueTimer + Scheduler +pwm + PID_v1 ? I'm always runing into issues for SD init (either if SD.h or SdFat) when running those different other libs additionally, simultaneously - when outcommenting SD init, everything runs fine though.

see, e.g. http://forum.arduino.cc/index.php?topic=365405.msg2520360#msg2520360

shiftleftplusone commented 8 years ago

the whole pin setup is

// pins   0, 1       : Serial
// pins   2, 3       : ...
// SPI_SS 4          : (reserve)
// pins   5, 6       : ...
// pins   7,8,9,10   : motor pwm
// pins   11 - 13    : ...
// pins   22 - 37    : motor dir + encoder
// pins   38 - 46    : keypad pins
// pins   47 - 50    : bumper
// TFT_DC      51
// TFT_CS      52
// SD_CS       53

the setup procedure is the following:


void setup()
{
  int32_t i;
  char    sbuf[128];

   // SD_CS       53
   // TFT_CS      52
   // TFT_DC      51

   //------------------------------------------------------------------------------------
   // Serial terminal window

      i=115200;
      Serial.begin(i);
      sprintf(sbuf, "Serial started, baud=%ld \n");
      Serial.println(sbuf);
      // lcdprint(sbuf);     
      Serial.println("[done.] \n");

   //------------------------------------------------------------------------------------
   // TFT LCD

      Serial.println();
      LCDTYPE = _ILI9341due_;
      Serial.print("init LCD... \n");     
      initLCD(1);   
      lcdcls();
      sprintf(sbuf, "LCD=%d wi%dxhi%d Font %dx%d \n",LCDTYPE,LCDmaxX,LCDmaxY,fontwi,fonthi);
      Serial.println(sbuf);
      Serial.println();
      lcdcls(); lcdprint(sbuf);
      Serial.println("[done.] \n");   

   //------------------------------------------------------------------------------------
   // init i2c devices

      sprintf(sbuf, "i2c init \n");
      lcdprint(sbuf);
      Wire.begin();       
      // i2c CMPS11: addr=0x60
      sprintf(sbuf, "CMPS11 Example V: %d \n", IMU_soft_ver()); // software version of the CMPS11
      Serial.println(sbuf);
      lcdprint(sbuf);
      Serial.println("[done.] \n");

   //------------------------------------------------------------------------------------
   // motor settings
   // setup for L293D motor driver

      for(i=0; i<MAXMOTORS; ++i) {motenc[i]=0; oldenc[i]=0; }

      // motor 0
      pinMode(pinenc0A, INPUT_PULLUP);  // enc0A    yellow
      pinMode(pinenc0B, INPUT_PULLUP);  // enc0B    blue
      pinMode(pinmot0d1, OUTPUT);        // dir0-1   
      pinMode(pinmot0d2, OUTPUT);        // dir0-2   
      pinMode(pinmot0pwm ,OUTPUT);       // enpwm0

      // motor 1
      pinMode(pinenc1A, INPUT_PULLUP);  // enc1A    yellow
      pinMode(pinenc1B, INPUT_PULLUP);  // enc1B    blue
      pinMode(pinmot1d1, OUTPUT);        // dir1-1   
      pinMode(pinmot1d2, OUTPUT);        // dir1-2
      pinMode(pinmot1pwm, OUTPUT);       // enpwm1

      // motor 2
      pinMode(pinenc2A, INPUT_PULLUP);  // enc2A    yellow
      pinMode(pinenc2B, INPUT_PULLUP);  // enc2B    blue
      pinMode(pinmot2d1, OUTPUT);        // dir2-1
      pinMode(pinmot2d2, OUTPUT);        // dir2-2   
      pinMode(pinmot2pwm, OUTPUT);       // enpwm2

      // motor 3
      pinMode(pinenc3A, INPUT_PULLUP);  // enc3A     yellow
      pinMode(pinenc3B, INPUT_PULLUP);  // enc3B     blue
      pinMode(pinmot3d1, OUTPUT);        // dir3-1   
      pinMode(pinmot3d2, OUTPUT);        // dir3-2
      pinMode(pinmot3pwm, OUTPUT);       // enpwm3

      sprintf(sbuf, "GPIOs motor 0: enc=%2d %2d di=r%2d %2d pwm=%2d \n", pinenc0A,pinenc0B,pinmot0d1,pinmot0d2,pinmot0pwm);
      Serial.println(sbuf);   
      lcdprint(sbuf);
      sprintf(sbuf, "GPIOs motor 1: enc=%2d %2d dir=%2d %2d pwm=%2d \n", pinenc1A,pinenc1B,pinmot1d1,pinmot1d2,pinmot1pwm);
      Serial.println(sbuf);   
      lcdprint(sbuf);
      sprintf(sbuf, "GPIOs motor 2: enc=%2d %2d dir=%2d %2d pwm=%2d \n", pinenc2A,pinenc2B,pinmot2d1,pinmot2d2,pinmot2pwm);
      Serial.println(sbuf);   
      lcdprint(sbuf);
      sprintf(sbuf, "GPIOs motor 3: enc=%2d %2d dir=%2d %2d pwm=%2d \n", pinenc3A,pinenc3B,pinmot3d1,pinmot3d2,pinmot3pwm);
      Serial.println(sbuf);   
      lcdprint(sbuf);

      sprintf(sbuf, "\nGPIOs OK ! \n");   
      Serial.println(sbuf);   
      lcdprint(sbuf);
      Serial.println("[done.] \n");

   //------------------------------------------------------------------------------------
   // SD card

      sprintf(sbuf, "SD init... ");   Serial.println(sbuf);
      i = initSD();
      if( i==fileIO_ERR_SDCARD ) sprintf(sbuf, "SD failed! ERROR ! ");
      else sprintf(sbuf, "SD OK ! ");   
      Serial.println(sbuf);   
      lcdprint(sbuf);
      Serial.println("[done.] \n");   

   //------------------------------------------------------------------------------------
   // attach Due Timer

      i=100;   // 0.1 ms
      Timer.getAvailable().attachInterrupt(encHandler).start(i);
      sprintf(sbuf, "motor encoder Timer IRQ started: %f ms \n", (float)i/1000.0);
      Serial.println(sbuf);
      lcdprint(sbuf);
      delay(50);

      Serial.println("[done.] \n");

  //------------------------------------------------------------------------------------
  // attach Scheduler tasks

     Scheduler.startLoop(Task_PollSensors_Slow);
     sprintf(sbuf, "Scheduler task 1: Task_PollSensors_Slow started \n");
     Serial.println(sbuf);
     lcdprint(sbuf);

     Scheduler.startLoop(Task_IMU);
     delay(100);                  // wait for IMU sensor fusion to initialize
     sprintf(sbuf, "Scheduler task 2: Task_IMU started \n");
     Serial.println(sbuf);
     lcdprint(sbuf);

     Scheduler.startLoop(Task_Navigator);
     sprintf(sbuf, "Scheduler task 3: Task_Navigator started \n");
     Serial.println(sbuf);
     lcdprint(sbuf);

     Serial.println("[done.] \n");

  //------------------------------------------------------------------------------------
  // reset PID parameters

  for (int16_t i = 0; i < MAXMOTORS; ++i) {
     PIDs[ i].SetMode(AUTOMATIC);
     PIDs[ i].SetOutputLimits(PID_REGMIN, PID_REGMAX);
     PIDs[ i].SetSampleTime(PID_REGTIME_MS);
     PIDsetpoint[ i] = 0;                      // set target,
     OUTregstate[ i] = OUT_REGSTATE_NUL;       // switch the PID on to motor[i]
     motoroff(i);
  }

     sprintf(sbuf, "PID parameters reset \n");
     Serial.println(sbuf);
     lcdprint(sbuf);

     Serial.println("[done.] \n");

  //------------------------------------------------------------------------------------

  sprintf(sbuf, "all systems running! \n");
  Serial.println(sbuf);   
  lcdprint(sbuf);

  sprintf(sbuf, "setup(): [done.] \n\n");
  Serial.println(sbuf);   
  lcdprint(sbuf);

}

//=====================================================================================
File     SDfile;
#define  sd_cs    53   

char     _fname_[64];
int16_t  _fnameext_;

// SD file IO error

#define fileIO_OK            +1
#define fileIO_ERR_CREATE    -1
#define fileIO_ERR_OPEN      -2
#define fileIO_ERR_REMOVE    -3
#define fileIO_ERR_WRITE     -4
#define fileIO_ERR_READ      -5
#define fileIO_ERR_IMPLAUS   -6
#define fileIO_ERR_NAME      -8
#define fileIO_ERR_SDCARD   -16

//=====================================================================================
// SD init
//=====================================================================================

int16_t initSD() {
   char sbuf[128];
   uint32_t  tstamp;
   int16_t   ior=0;

   tstamp = clock();
   ior = SD.begin(sd_cs);  // success==1==true; else 0==false
   while( !ior) {     
      sprintf(sbuf, "#: ...SD not found... ");
      curlf(); lcdprint("#: ...SD not found... ");
      Serial.println(sbuf);
      delay(1000);   
      ior=SD.begin(sd_cs);
      if (clock()-tstamp>20000) {Serial.println("#: ...break!"); break; }
   }
  if(!ior) return fileIO_ERR_SDCARD;     // SD ioresult==0 => error = -16
  return fileIO_OK ;                     // SD ioresult==1 => ok = 1
}
peabo7 commented 8 years ago

How many times do you have to be told that this is the wrong place to post non-IDE issues?

Now you have copy-pasted someone else's non-IDE issue here.

Reading your messages is an astounding waste of time.

shiftleftplusone commented 8 years ago

no, they are my own (aka ArthurD) code issues, and it seems its caused by Arduino IDE and wire lib and timer (or whatever) incompatibilities. So please read closely before you are offending and insulting me. (You'd be well advised to apologize...!)

agdl commented 7 years ago

This issue was moved to arduino/ArduinoCore-sam#15