Simulink and arduino serial comunication

I’ve been trying to make a path planner for an omidirectional robot using Simulink and arduino.
The model calculates the wheel velocities from 0 to 255 plus a sings byte and sends them over to an arduino via Xbee to go into a PID (or PI or PD not quite sure yet). The problem is I’m not getting the right data on the arduino. I’m slowing down the Simulink model using the “Real time pacer” library.
The arduino right now waits for all 205 bytes to be transmitted (I know they are 205 using a terminal and checking the Rx total byte count) and then sends them back just to be readable using Serial mointor.
I don’t know if anybody has some tips as to get it to work.
Any help will be much apreciated.
I leave you the arduino code

int F1[41],F2[41],F3[41],F4[41],Sig[41],j,contador,F[41],mood,signo[41];
void setup()
{
Serial.begin(9600);
}
void loop()
{
for(contador=0;contador<205;contador++){  
do{
mood=contador%5;
j=contador/5;
} while(!Serial.available());
F[contador]=Serial.read();
switch(mood){
case 0:
F1[j]=F[contador];
break;
case 1:
F2[j]=F[contador];
break;
case 2:
F3[j]=F[contador];
break;
case 3:
F4[j]=F[contador]; 
break;
case 4:
Sig[j]=F[contador];
break;}
}
for(long int i=1;i<41;i++)
{
 signo[i]=Sig[i];
  if(Sig[i]%2==0)
  {
  F1[i]=-F1[i];
  }
  Sig[i]=Sig[i]>>1;
  if(Sig[i]%2==0)
  {
    F2[i]=-F2[i];
  }
  Sig[i]=Sig[i]>>1;
  if(Sig[i]%2==0)
  {
    F3[i]=-F3[i];
  }
   Sig[i]=Sig[i]>>1;
  if(Sig[i]%2==0)
  {
    F4[i]=-F4[i];
  }
  Serial.print(F1[i]);
Serial.print(" ");
  Serial.print(F2[i]);
    Serial.print(" ");
  Serial.print(F3[i]);
    Serial.print(" ");
  Serial.println(F4[i]);
}
}

vitoovalle:
I leave you the arduino code

My Eyes!

Do yourself and everyone else a favor and run that thing through the Auto-Format tool before posting it. In the Arduino's IDE this is located under Tools > Auto Format or with the shortcut of Ctrl+T.

I’m sorry about that, I haven’t really done this forum thing :sweat_smile: I’ll be sure to do that from now on

vitoovalle:
I'm sorry about that, I haven't really done this forum thing :sweat_smile: I'll be sure to do that from now on

If you're expecting help with the current issue, it would be a good idea to do that with this current post as well.

Arrch:
If you’re expecting help with the current issue, it would be a good idea to do that with this current post as well.

I’m not quite sure what you meant by that :frowning: but anyhow I’ve done just what you suggested and now I’ll post the formated code

int F1[41],F2[41],F3[41],F4[41],Sig[41],j,contador,F[41],mood,signo[41];
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  for(contador=0;contador<205;contador++){  
    do{
      mood=contador%5;
      j=contador/5;
    } 
    while(!Serial.available());
    F[contador]=Serial.read();
    switch(mood){
    case 0:
      F1[j]=F[contador];
      break;
    case 1:
      F2[j]=F[contador];
      break;
    case 2:
      F3[j]=F[contador];
      break;
    case 3:
      F4[j]=F[contador]; 
      break;
    case 4:
      Sig[j]=F[contador];
      break;
    }
  }
  for(long int i=1;i<41;i++)
  {
    signo[i]=Sig[i];
    if(Sig[i]%2==0)
    {
      F1[i]=-F1[i];
    }
    Sig[i]=Sig[i]>>1;
    if(Sig[i]%2==0)
    {
      F2[i]=-F2[i];
    }
    Sig[i]=Sig[i]>>1;
    if(Sig[i]%2==0)
    {
      F3[i]=-F3[i];
    }
    Sig[i]=Sig[i]>>1;
    if(Sig[i]%2==0)
    {
      F4[i]=-F4[i];
    }
    Serial.print(F1[i]);
    Serial.print(" ");
    Serial.print(F2[i]);
    Serial.print(" ");
    Serial.print(F3[i]);
    Serial.print(" ");
    Serial.println(F4[i]);
  }
}

The problem is I'm not getting the right data on the arduino.

Which begs the question, what data are you getting, and what are you expecting?

well when the comunication starts I should get the following response:
102 77 102 77
113 60 113 60
121 41 121 41
126 22 126 22
127 2 127 2
126 18 126 18
122 37 122 37
but there are some issuses.
To begin with, the first to or three iterations return nothing but garbage (I can't copy it, i assume it's because the caracters are control ones wich don't allow it) and I seem to be loosing one byte always at the fifteenth iteration, which I am reciving from Simulink, I checked on a terminal and all the info is there. I'm going to try and change the baud rate, see if it any how makes it better/worse.

vitoovalle:
To begin with, the first to or three iterations return nothing but garbage (I can't copy it, i assume it's because the caracters are control ones wich don't allow it)

Try printing the data in HEX, so that you know exactly what the values are. and compare them with what you expect. It would also help if you posted a copy/paste or a screenshot of what you see and what you expect to see the contrast.

What I should get (Simulink+terminal)
36 36 36 36 0F 66 4D 66 4D 0A 71 3C 71 3C 0A 79 29 79 29 0A 7E 16 7E 16 0A 7F 02 7F 02 0A 7E 12 7E 12 00 7A 25 7A 25 00 72 38 72 38 00 68 49 68 49 00 5C 59 5C 59 00 4D 66 4D 66 00 3C 71 3C 71 00 29 79 29 79 00 16 7E 16 7E 00 02 7F 02 7F 00 12 7E 12 7E 05 25 7A 25 7A 05 38 72 38 72 05 49 68 49 68 05 59 5C 59 5C 05 66 4D 66 4D 05 71 3C 71 3C 05 79 29 79 29 05 7E 16 7E 16 05 7F 02 7F 02 05 7E 12 7E 12 0F 7A 25 7A 25 0F 72 38 72 38 0F 68 49 68 49 0F 5C 59 5C 59 0F 4D 66 4D 66 0F 3C 71 3C 71 0F 29 79 29 79 0F 16 7E 16 7E 0F 02 7F 02 7F 0F 12 7E 12 7E 0A 25 7A 25 7A 0A 38 72 38 72 0A 49 68 49 68 0A FF FF FF FF 0A
What I get(Simulink+Arduino)
00 12 00 7E 00 05 00 25 00 7A 00 25 00 7A 00 05 00 38 00 72 00 38 00 72 00 05 36 36 36 0F 66 4D 66 4D 0A 71 3C 71 3C 0A 79 29 79 29 0A 7E 16 7E 16 0A 7F 02 7F 02 0A 7E 12 7E 12 00 7A 25 7A 25 00 72 00 72 38 00 68 49 68 49 00 5C 59 5C 59 00 4D 66 4D 66 00 3C 71 3C 71 00 29 79 29 79 00 16 7E 16 7E 00 00 02 7F 02 7F 00 12 7E 12 7E 05 25 7A 25 7A 05 38 72 38 72 05 49 68 49 68 05 59 5C 59 5C 05 66 4D 66 4D 05 71 3C 71 3C 05 79 29 79 29 05 7E 16 7E 16 05 7F 02 7F 02 05 7E 12 7E 12 0F 7A 25 7A 25 0F 72 38 72 38 0F 68 49 68 49 0F 5C 59 5C 59 0F 4D 66 4D 66 0F 3C 71 3C 71 0F 29 79 29 79 0F 16 7E 16 7E 0F 02 7F 02 7F 0F 12 7E 12 7E 0A 25 7A 25 7A 0A 38 72 38 72 0A 49 68 49 68 0A FF FF FF FF
the underscore is where my good data actually stats

I have already found the solution :smiley:
The array F is made of 41 elements, when in reality it should be 205 elements long