I am encountering a problem with my Arduino Uno. It is supposed to transmit data from a Novint Falcon to Dspace and receive sensor data from Dspace to be sent back to the Falcon. Dspace is programmed with Simulink.
My Arduino code runs like this:
- Read 6 bytes from laptop using Serial.
- Write 8 bytes to Dspace using SoftwareSerial.
- Read 3 bytes from Dspace using SoftwareSerial.
- Write those 3 bytes back to laptop.
In Dspace, the procedure is:
- Read 8 bytes from Arduino using Serial Receive.
- Send 3 bytes back to Arduino with Serial Transmit.
The problem I have now is, the Arduino hangs after about 1s when Dspace is turned on (on my laptop, I do actually see the 3 bytes read from Dspace). However, this problem does not occur when I only send ONE byte from Dspace. Anything more and it does not work.
The Baudrate of the serial ports is 115200. The Simulink model has a step time of 2ms.
It looks as though this has something to do with a buffer overflow in the Arduino, but I'm not sure. Can anybody please help me or offer suggestions as to how to pinpoint the root of the problem?? Been stuck on this for some time now.. thanks!
#include <SoftwareSerial.h>
char Axispos[6] ={0,0,0,0,0,0};
SoftwareSerial mySerial(7,6); //RX, TX
void setup() {
Serial.begin(115200);
Serial.flush();
mySerial.begin(115200);
}
void loop(){
if ( Serial.available() >= 6)
{
char Fx=0;
char Fy=0;
char Fz=0;
Serial.readBytes(Axispos,6);
mySerial.write(33);
mySerial.write(22);
mySerial.write(Axispos);
Fx = mySerial.read();
Fy = mySerial.read();
Fz = mySerial.read();
Serial.write(Fx);
Serial.write(Fy);
Serial.write(Fz);
}
}
C-code in simulink:
function [xPos, yPos, zPos, Force] = fcn(Axispos, NumRX)
persistent xPer yPer zPer count output;
Force=uint8(zeros(1,3));
if isempty(xPer)
xPer = int16(0);
yPer = int16(0);
zPer = int16(0);
count = 0;
output = uint8(0);
end
StartByte1 = char(Axispos(1));
StartByte2 = char(Axispos(2));
xh = uint8(Axispos(3));
xl = uint8(Axispos(4));
yh = uint8(Axispos(5));
yl = uint8(Axispos(6));
zh = uint8(Axispos(7));
zl = uint8(Axispos(8));
if (NumRX >=8 && StartByte1 == 33 && StartByte2 == 22)
xwert = uint16(bitshift(uint16(xh),8,16) + bitand(uint16(xl),uint16(255)));
ywert = uint16(bitshift(uint16(yh),8,16) + bitand(uint16(yl),uint16(255)));
zwert = uint16(bitshift(uint16(zh),8,16) + bitand(uint16(zl),uint16(255)));
var1 = typecast(uint16(xwert),'int16');
var2 = typecast(uint16(ywert),'int16');
var3 = typecast(uint16(zwert),'int16');
xPer = var1(1);
yPer = var2(1);
zPer = var3(1);
end
xPos = xPer;
yPos = yPer;
zPos = zPer;
Force(1) = uint8(10);
Force(2) = uint8(20);
Force(3) = uint8(30);