MATLAB Answers

Receiving messages from the CAN via CANcase using Vehicle Network Toolbox(R2010b) and working with it in realtime.

8 views (last 30 days)
Karthik Suresh
Karthik Suresh on 8 Jul 2011
Commented: Jorge Garza on 7 Sep 2016
I would want to receive a few CAN messages in real time using VNT and give it as an input to my algorithm running in MATLAB. But the CAN communication takes longer time than real time. I am currently using a timer that calls the timer function callback every 100ms. I need to receive CAN messages from two CAN channels and process the data within this 100ms. I am sure that the process is not slower due to the algorithm which processes the CAN messages itself. I am also attaching the piece of codes which i used for CAN interface. Please tell me where i went wrong?
% Accesing the database file
db1 = canDatabase('PT_CAN.dbc');
db2 = canDatabase('LWS_CAN.dbc');
% Initializing the Recieving Channels
rxcanch1=canChannel('Vector','CANcaseXL 1',1);
rxcanch2=canChannel('Vector','CANcaseXL 1',2);
% Assigning DBC to CAN channel
rxcanch1.Database = db1;
rxcanch2.Database = db2;
% Filter out messages needed from the channel
filterAcceptRange(rxcanch1, 206, 206)
filterAcceptRange(rxcanch1, 304, 304)
filterAcceptRange(rxcanch1, 760, 764)
filterAcceptRange(rxcanch2, 194, 194);
% Make receiving channel available
start(rxcanch1);
start(rxcanch2);
% Timer to receive CAN messages
timer_can = timer('StartDelay',.1,'ExecutionMode','fixedRate','Period',0.1,'BusyMode','queue','TasksToExecute',600);
timer_can.TimerFcn=@timerfcn_callback;
start(timer_can);
And the Timer function callback is as follows.
function timerfcn_callback(obj,event)
message1 = receive(rxcanch1,Inf);% PT_CAN
message2 = receive(rxcanch2,Inf);% LWS_CAN
if ~isempty(message1)
msgOut1=extractAll(message1,760,false);
if ~isempty(msgOut1)
signals1=[msgOut1.Signals];
DISP_MN=[signals1.DISP_MN];
Time_min=[Time_min,DISP_MN];
DISP_HR=[signals1.DISP_HR];
Time_hr=[Time_hr,DISP_HR];
end
msgOut2=extractAll(message1,762,false);
if ~isempty(msgOut2)
signals2=[msgOut2.Signals];
SW_BLTB_DR=[signals2.SW_BLTB_DR];
Driver_door=[Driver_door,SW_BLTB_DR];
end
msgOut3=extractAll(message1,764,false);
if ~isempty(msgOut3)
signals3=[msgOut3.Signals];
ST_DSW_DRD=[signals3.ST_DSW_DRD];
Seat_belt=[Seat_belt,ST_DSW_DRD];
end
msgOut4=extractAll(message1,206,false);
if ~isempty(msgOut4)
signals4=[msgOut4.Signals];
V_WHL_FLH=[signals4.V_WHL_FLH];
vveh_fl=[vveh_fl,V_WHL_FLH];
V_WHL_FRH=[signals4.V_WHL_FRH];
vveh_fr=[vveh_fr,V_WHL_FRH];
V_WHL_RLH=[signals4.V_WHL_RLH];
vveh_rl=[vveh_rl,V_WHL_RLH];
V_WHL_RRH=[signals4.V_WHL_RRH];
vveh_rr=[vveh_rr,V_WHL_RRH];
end
msgOut6=extractAll(message1,304,false);
if ~isempty(msgOut6)
signals6=[msgOut6.Signals];
ST_KL_15=[signals6.ST_KL_15];
ignition=[ignition,ST_KL_15];
end
end
if ~isempty(message2)
msgOut5=extractAll(message2,194,false);
if ~isempty(msgOut5)
signals5=[msgOut5.Signals];
ANGLE=[signals5.ANGLE];
SWA=[SWA,ANGLE];
end
end
end
The CAN communication consumes the most part of the running time of the algorithm. Since I am new to VNT I could'nt find the solution myself.
  2 Comments
Jorge Garza
Jorge Garza on 7 Sep 2016
Hi, just wanted to update my question. I can read CAN values not real time but using a while loop, the loop uses a pause with the message transmision rate. The value reading works fine, now there is still to see if while values are being read the algorithm processing doesnt ruin the script performance and CAN messages/sensor info get loss.

Sign in to comment.

Answers (3)

Gerd
Gerd on 8 Jul 2011
Hi Karthik,
realtime behaviour with the VNT is not easy to accomplish. Looking at your code you might be able to use the 'MessageReceive Callback' from your CAN channels. Everytime a filtered CAN message is available this callback function is executed.
But my experience is that this functionality don't work in realtime also. The Mathworks support had only a workaround.
You have to increase the sample time for the CAN message update function and have a second timer for your algorithm. I think the CANTool which Mathworks is providing works the same.
Please contact the Mathworks support, hopefully they can make the performance in a future Release better.
Gerd

Shankar Subramanian
Shankar Subramanian on 22 Jul 2011
Hi Karthik,
A few thoughts on this:
  1. How many messages are you getting from the network every 100ms? This is more to know network load/traffic.
  2. Receiving messages in CAN channel is Asynchronous process. Have you checked to see the timestamps on the received messages? You can use canTool to see if it is able to keep up with your network bursts.
  3. As suggested, using MessageReceivedFcn and MessageReceivedFcnCount might be better.
  4. When you use the extractAll method, use the second output remainder for subsequent extractAll calls. You can see the doc here for the signature of the function.
Again, at this point, since I do not know much about your network, it is tough to comment on the timing/speed aspect. Above information might be a good point to start the investigation.
Thanks,
Shankar

Jaremy
Jaremy on 25 Jul 2011
Hello,
A few additional thoughts are:
1. If you only need the latest information received to feed your algorithm every 100 ms, then try using ExtractRecent instead of ExtractAll.
2. As Shankar mentioned, the Extract functions also return a remainder argument. You can use that in each subsequent Extract call for faster processing. It will avoid reparsing the same messages again and again.
3. You can try putting tic/toc calls in your timer function. This would help to understand how long that processing is taking. Put the tic at the top and the toc at the end. You will see a screen print of the time expired during the function.
Thanks, Jaremy

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!