Reading multiple signed digits from serial port
2 次查看(过去 30 天)
显示 更早的评论
I'm using simulink support package for arduino to read serial data from port2 in Arduino due. My plan is to read signed integers (-415 for example) representing motor speed and feed it to the pid controllers as in the image.
from the far end i'm sending delimited data in the following shape . The matlab function in sumlink is supposed to read the received ASCII characters and add them to a variable until it reaches the end character '>'. I'm using the following simple code just to give the output on both the Right and Left to check if I'm receiving the correct data, However I'm not.
function [Right ,Left] = fcn(data,status)
SON = '<';
EON = '>';
persistent receivedNumber;
receivedNumber = 0;
persistent negative;
negative = false;
if(status ==1)
switch(data)
case EON
if (negative)
receivedNumber = -1*receivedNumber;
else
receivedNumber = 1*receivedNumber;
end
case SON
receivedNumber = 0;
negative = false;
case {'0','1','2','3','4','5','6','7','8','9'}
receivedNumber = 10*receivedNumber;
receivedNumber = receivedNumber + double((data - 48));
case '-'
negative = true;
end
end
Right = receivedNumber;
Left = receivedNumber;
end
Can anybody tells if there are any other approaches to read multiple signed digits in simulink? Taking into consideration that I have to use the support package for Arduino since my pid controllers are already configured in Simulink and interfaced with port2 in Arduino (which will be connected to BeagleBone black)
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Run on Target Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!