Go Down

Topic: Problem interfacing Arduino UNO with Matlab (Read 1 time) previous topic - next topic

I've been using Matlab to send messages to Arduino Duemilanove to control two PMDC Motors. The system was working fine. But when I changed my board to Arduino UNO, there seems to be some sort of time out error that keeps occuring and Matlab hangs after some time. The problem is only with UNO. Can anyone help?

robtillaart


Please post your code so we can verify (recreate) the problem.
Did you change from IDE version too? if so you should check the releasenotes as the latest releases some small but essential things changed.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

This is the code that I'm using to send data to the Arduino from Matlab.

MATLAB

freeduinoStringArray = ['xx'; 'FW'; 'RV'; 'TL'; 'TR';'BR'];

freeduinoSerial = serial('COM18');
set(freeduinoSerial, 'BaudRate', 9600, 'Terminator', 'CR/LF', 'TimeOut', 100);

fopen(freeduinoSerial);

fprintf(freeduinoSerial, '%s', freeduinoString); //freeduinoSerial contains one of the values in freeduinoStringArray

This code runs inside the main function and is called over and over again as new input is obtained by MATLAB from an image capture.

The following is the code which is used to control the motors with Arduino

/*
  HERCULES Motor Controller Code

  PWM control

  for reverse the direction pin should be changed in the motor driver of hercules

  BRAKE      -  BR
  FORWARD    -  FW
  REVERSE    -  RV
  TURN LEFT  -  TL
  TURN RIGHT -  TR
  IGNORE     -  xx
  TEST       -  TT

/* 
  // Set pin 9's PWM frequency to 3906 Hz (31250/8 = 3906)
// Note that the base frequency for pins 3, 9, 10, and 11 is 31250 Hz
setPwmFrequency(9, 8 );

// Set pin 6's PWM frequency to 62500 Hz (62500/1 = 62500)
// Note that the base frequency for pins 5 and 6 is 62500 Hz
setPwmFrequency(6, 1 );

// Set pin 10's PWM frequency to 31 Hz (31250/1024 = 31)
setPwmFrequency(10, 1024) ;

Please keep in mind that changing the PWM frequency changes the Atmega's timers and disrupts the normal operation of many functions that rely on time (delay(), millis(), Servo library).

/**
* Divides a given PWM pin frequency by a divisor.
*
* The resulting frequency is equal to the base frequency divided by
* the given divisor:
*   - Base frequencies:
*      o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
*      o The base frequency for pins 5 and 6 is 62500 Hz.
*   - Divisors:
*      o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
*        256, and 1024.
*      o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
*        128, 256, and 1024.
*
* PWM frequencies are tied together in pairs of pins. If one in a
* pair is changed, the other is also changed to match:
*   - Pins 5 and 6 are paired on timer0
*   - Pins 9 and 10 are paired on timer1
*   - Pins 3 and 11 are paired on timer2
*
* Note that this function will have side effects on anything else
* that uses timers:
*   - Changes on pins 3, 5, 6, or 11 may cause the delay() and
*     millis() functions to stop working. Other timing-related
*     functions may also be affected.
*   - Changes on pins 9 or 10 will cause the Servo library to function
*     incorrectly.
*
*/




void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x07; break;
      default: return;
    }
    TCCR2B = TCCR2B & 0b11111000 | mode;
  }
}


float fadeValue1=0;
float fadeValue2=0;

int x=0;
int y=0;
int z=0;


const int commandLength = 2;

int  stepCount = 0;
int readIndex = 0;
char receivedCommand[] = {'x'  ,  'x'};
char prevreceivedCommand[] = {'x'  ,  'x'};

void setup()
{
  setPwmFrequency(5, 8 );
  setPwmFrequency(6, 8 );
//  TCCR2B=((TCCR2B & 0xF8) |  2);
   pinMode(8, OUTPUT);
pinMode(13, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(8, HIGH);
  Serial.begin(9600);
  Serial.println("READY");
}

void loop()
{
  if(Serial.available())
  {
    prevreceivedCommand[0,1]=receivedCommand[0,1];
    for( ; readIndex < commandLength; readIndex++)
    {
      receivedCommand[readIndex] = Serial.read();
      Serial.println(receivedCommand[readIndex]);
    }

    Serial.println("Command received.");

    switch (receivedCommand[0])
    {
      case 'B'  :
        if(receivedCommand[1] == 'R')
        {
          Serial.println("Received BRAKE.");

          while(Serial.available() == 0)
          {
            relayBrake();
          }
        }
        break;

      case 'F'  :
        if(receivedCommand[1] == 'W')
        {
          Serial.println("Received FORWARD.");
           {
            if (prevreceivedCommand[0,1] != receivedCommand[0,1])
            {relayBrake();
          }
          while(Serial.available() == 0)

            {

            digitalWrite(12, HIGH);
            digitalWrite(13, HIGH);
            relayForward();         
          }
        }
        }
        break;

      case 'R'  :
        if(receivedCommand[1] == 'V')
        {
          Serial.println("Received REVERSE.");
           {
            if (prevreceivedCommand[0,1] != receivedCommand[0,1])
            {relayBrake();
          ;}
          while(Serial.available() == 0)
       {

            digitalWrite(12, LOW);
            digitalWrite(13, LOW);
            relayReverse();         
          }
        }
        }
        break;

      case 'T'  :
        if(receivedCommand[1] == 'L')
        {
          Serial.println("Received TURN LEFT.");
          {
            if (prevreceivedCommand[0,1] != receivedCommand[0,1])
            {relayBrake();
          ;}
          while(Serial.available() == 0)
          {
            digitalWrite(12, LOW);
            digitalWrite(13, HIGH);
            relayTurnLeft();         
          }
        }
        }
        if(receivedCommand[1] == 'R')
        {
            if (prevreceivedCommand[0,1] != receivedCommand[0,1])
            {relayBrake();
          }
          Serial.println("Received TURN RIGHT.");
          while(Serial.available() == 0)
           {       
            digitalWrite(12, HIGH);
            digitalWrite(13, LOW);
            relayTurnRight();         
          }
        }   
        break;

      case 'x'  :
        if(receivedCommand[1] == 'x')
        {
          Serial.println("Ignored.");
          while(Serial.available() == 0)
          {
            relayBrake();         
          }
        }
        break;
    }
  } 
  else
  {
    if(Serial.available() == 0)
    {
      Serial.println("READY");
    }
  } 

   readIndex = 0; 



void relayBrake()
{

    for(; fadeValue1 >= 0 ||  fadeValue2 >= 0 ; )
  {
    if(fadeValue1 >= 0)
    {
    analogWrite(5, fadeValue1); 
    fadeValue1 -=0.005;   
    }
    if(fadeValue2 >= 0)
  {
    analogWrite(6, fadeValue2);
    fadeValue2 -=0.005;     
  } 
  }
}

void relayReverse()
{

for( ; fadeValue1 <= 60 || fadeValue2 <= 60; )
      {
        if (fadeValue1 <= 60)
        {
    analogWrite(5, fadeValue1);
    fadeValue1 +=0.005; 
  }

      if (fadeValue2 <= 60)
  {
    analogWrite(6, fadeValue2);
    fadeValue2 +=0.005;   
  }     
}
}

void relayForward()
{

     for( ; fadeValue1 <= 60 || fadeValue2 <= 60; )
      {
       if (fadeValue1 <= 60)
       {
      fadeValue1 +=0.005;
      analogWrite(5, fadeValue1);
       }
     if (fadeValue2 <= 60)
     {
    fadeValue2 +=0.005;
    analogWrite(6, fadeValue2);   
     }
  }
}
void relayTurnLeft()
{

  for(; fadeValue1 <= 60 || fadeValue2 >=0; )
  {
    if (fadeValue1 <= 60)
    {
    fadeValue1 +=0.001;
      analogWrite(5, fadeValue1);   
    }
  if (fadeValue2 >=0)
    {
    fadeValue2 -=0.001;
      analogWrite(6, fadeValue2);
  } 
  }
  } 

void relayTurnRight()
{

    for(; fadeValue1 >= 0 || fadeValue2 <=60; )
  {
    if (fadeValue1 >= 0)
    {
    fadeValue1 -=0.001;
      analogWrite(5, fadeValue1); 

    }
  if (fadeValue2 <=60)
    {
    fadeValue2 +=0.001;
    analogWrite(6, fadeValue2);
     } 
  }
  }



Thanks for the help!  :)

robtillaart

#3
Dec 28, 2012, 09:21 am Last Edit: Dec 28, 2012, 09:24 am by robtillaart Reason: 1
Please modify your post above, select the source code and press the # button above the smileys. IT will tag your code properly so it becomes more readable

Thanks

Code: [Select]
 if(Serial.available())
 {
   prevreceivedCommand[0,1]=receivedCommand[0,1];
   for( ; readIndex < commandLength; readIndex++)
   {
     receivedCommand[readIndex] = Serial.read();
     Serial.println(receivedCommand[readIndex]);
   }


You read more than one character but you did not test if there are at least two available. For a uProcessor it takes 1000's of statements to transport one byte.

try to change it this way - also on the other spots ...
Code: [Select]
 if(Serial.available() >= 2)
 {
   prevreceivedCommand[0,1]=receivedCommand[0,1];
   for( ; readIndex < commandLength; readIndex++)
   {
     receivedCommand[readIndex] = Serial.read();
     Serial.println(receivedCommand[readIndex]);
   }


This is still not enough to be robust as you don't know if you got the first or second char of a command or a line terminator etc but that is the next step. Encapsulating commands with start and stop bytes ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Code: [Select]
This is the code that I'm using to send data to the Arduino from Matlab.

MATLAB

[code]
freeduinoStringArray = ['xx'; 'FW'; 'RV'; 'TL'; 'TR';'BR'];

freeduinoSerial = serial('COM18');
set(freeduinoSerial, 'BaudRate', 9600, 'Terminator', 'CR/LF', 'TimeOut', 100);

fopen(freeduinoSerial);

fprintf(freeduinoSerial, '%s', freeduinoString); //freeduinoSerial contains one of the values in freeduinoStringArray


This code runs inside the main function and is called over and over again as new input is obtained by MATLAB from an image capture.

The following is the code which is used to control the motors with Arduino

Code: [Select]
/*
  HERCULES Motor Controller Code

  PWM control

  for reverse the direction pin should be changed in the motor driver of hercules

  BRAKE      -  BR
  FORWARD    -  FW
  REVERSE    -  RV
  TURN LEFT  -  TL
  TURN RIGHT -  TR
  IGNORE     -  xx
  TEST       -  TT

/* 
  // Set pin 9's PWM frequency to 3906 Hz (31250/8 = 3906)
// Note that the base frequency for pins 3, 9, 10, and 11 is 31250 Hz
setPwmFrequency(9, 8 );

// Set pin 6's PWM frequency to 62500 Hz (62500/1 = 62500)
// Note that the base frequency for pins 5 and 6 is 62500 Hz
setPwmFrequency(6, 1 );

// Set pin 10's PWM frequency to 31 Hz (31250/1024 = 31)
setPwmFrequency(10, 1024) ;

Please keep in mind that changing the PWM frequency changes the Atmega's timers and disrupts the normal operation of many functions that rely on time (delay(), millis(), Servo library).

/**
* Divides a given PWM pin frequency by a divisor.
*
* The resulting frequency is equal to the base frequency divided by
* the given divisor:
*   - Base frequencies:
*      o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
*      o The base frequency for pins 5 and 6 is 62500 Hz.
*   - Divisors:
*      o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
*        256, and 1024.
*      o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
*        128, 256, and 1024.
*
* PWM frequencies are tied together in pairs of pins. If one in a
* pair is changed, the other is also changed to match:
*   - Pins 5 and 6 are paired on timer0
*   - Pins 9 and 10 are paired on timer1
*   - Pins 3 and 11 are paired on timer2
*
* Note that this function will have side effects on anything else
* that uses timers:
*   - Changes on pins 3, 5, 6, or 11 may cause the delay() and
*     millis() functions to stop working. Other timing-related
*     functions may also be affected.
*   - Changes on pins 9 or 10 will cause the Servo library to function
*     incorrectly.
*
*/




void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x07; break;
      default: return;
    }
    TCCR2B = TCCR2B & 0b11111000 | mode;
  }
}


float fadeValue1=0;
float fadeValue2=0;

int x=0;
int y=0;
int z=0;


const int commandLength = 2;

int  stepCount = 0;
int readIndex = 0;
char receivedCommand[] = {'x'  ,  'x'};
char prevreceivedCommand[] = {'x'  ,  'x'};

void setup()
{
  setPwmFrequency(5, 8 );
  setPwmFrequency(6, 8 );
//  TCCR2B=((TCCR2B & 0xF8) |  2);
   pinMode(8, OUTPUT);
pinMode(13, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(8, HIGH);
  Serial.begin(9600);
  Serial.println("READY");
}

void loop()
{
  if(Serial.available())
  {
    prevreceivedCommand[0,1]=receivedCommand[0,1];
    for( ; readIndex < commandLength; readIndex++)
    {
      receivedCommand[readIndex] = Serial.read();
      Serial.println(receivedCommand[readIndex]);
    }

    Serial.println("Command received.");

    switch (receivedCommand[0])
    {
      case 'B'  :
        if(receivedCommand[1] == 'R')
        {
          Serial.println("Received BRAKE.");

          while(Serial.available() == 0)
          {
            relayBrake();
          }
        }
        break;

      case 'F'  :
        if(receivedCommand[1] == 'W')
        {
          Serial.println("Received FORWARD.");
           {
            if (prevreceivedCommand[0,1] != receivedCommand[0,1])
            {relayBrake();
          }
          while(Serial.available() == 0)

            {

            digitalWrite(12, HIGH);
            digitalWrite(13, HIGH);
            relayForward();         
          }
        }
        }
        break;

      case 'R'  :
        if(receivedCommand[1] == 'V')
        {
          Serial.println("Received REVERSE.");
           {
            if (prevreceivedCommand[0,1] != receivedCommand[0,1])
            {relayBrake();
          ;}
          while(Serial.available() == 0)
       {

            digitalWrite(12, LOW);
            digitalWrite(13, LOW);
            relayReverse();         
          }
        }
        }
        break;

      case 'T'  :
        if(receivedCommand[1] == 'L')
        {
          Serial.println("Received TURN LEFT.");
          {
            if (prevreceivedCommand[0,1] != receivedCommand[0,1])
            {relayBrake();
          ;}
          while(Serial.available() == 0)
          {
            digitalWrite(12, LOW);
            digitalWrite(13, HIGH);
            relayTurnLeft();         
          }
        }
        }
        if(receivedCommand[1] == 'R')
        {
            if (prevreceivedCommand[0,1] != receivedCommand[0,1])
            {relayBrake();
          }
          Serial.println("Received TURN RIGHT.");
          while(Serial.available() == 0)
           {       
            digitalWrite(12, HIGH);
            digitalWrite(13, LOW);
            relayTurnRight();         
          }
        }   
        break;

      case 'x'  :
        if(receivedCommand[1] == 'x')
        {
          Serial.println("Ignored.");
          while(Serial.available() == 0)
          {
            relayBrake();         
          }
        }
        break;
    }
  } 
  else
  {
    if(Serial.available() == 0)
    {
      Serial.println("READY");
    }
  } 

   readIndex = 0; 



void relayBrake()
{

    for(; fadeValue1 >= 0 ||  fadeValue2 >= 0 ; )
  {
    if(fadeValue1 >= 0)
    {
    analogWrite(5, fadeValue1); 
    fadeValue1 -=0.005;   
    }
    if(fadeValue2 >= 0)
  {
    analogWrite(6, fadeValue2);
    fadeValue2 -=0.005;     
  } 
  }
}

void relayReverse()
{

for( ; fadeValue1 <= 60 || fadeValue2 <= 60; )
      {
        if (fadeValue1 <= 60)
        {
    analogWrite(5, fadeValue1);
    fadeValue1 +=0.005; 
  }

      if (fadeValue2 <= 60)
  {
    analogWrite(6, fadeValue2);
    fadeValue2 +=0.005;   
  }     
}
}

void relayForward()
{

     for( ; fadeValue1 <= 60 || fadeValue2 <= 60; )
      {
       if (fadeValue1 <= 60)
       {
      fadeValue1 +=0.005;
      analogWrite(5, fadeValue1);
       }
     if (fadeValue2 <= 60)
     {
    fadeValue2 +=0.005;
    analogWrite(6, fadeValue2);   
     }
  }
}
void relayTurnLeft()
{

  for(; fadeValue1 <= 60 || fadeValue2 >=0; )
  {
    if (fadeValue1 <= 60)
    {
    fadeValue1 +=0.001;
      analogWrite(5, fadeValue1);   
    }
  if (fadeValue2 >=0)
    {
    fadeValue2 -=0.001;
      analogWrite(6, fadeValue2);
  } 
  }
  } 

void relayTurnRight()
{

    for(; fadeValue1 >= 0 || fadeValue2 <=60; )
  {
    if (fadeValue1 >= 0)
    {
    fadeValue1 -=0.001;
      analogWrite(5, fadeValue1); 

    }
  if (fadeValue2 <=60)
    {
    fadeValue2 +=0.001;
    analogWrite(6, fadeValue2);
     } 
  }
  }
[/code]

I made the changes you had suggested. I'm still experiencing the same problem, but there is a difference. I'd tried printing the serial object while the program was running. Before I made the change, the system would hang after sending about 180 values. After I made the change, the system was still hanging, but after sending about 700 values.

robtillaart


You still not test if there are enough bytes received by the serial ...
The for loop has no initialized readINdex.
...
Code: [Select]
  if(Serial.available())
  {
    prevreceivedCommand[0,1]=receivedCommand[0,1];
    for( ; readIndex < commandLength; readIndex++)
    {
      receivedCommand[readIndex] = Serial.read();
      Serial.println(receivedCommand[readIndex]);
    }




What is this command supposed to do?
both arrays are 1 dimensional !
Code: [Select]
    prevreceivedCommand[0,1]=receivedCommand[0,1];
Have you read the tutorial on Arrays?

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up