How can I send and receive a lot of CAN messages with App designer ?
10 次查看(过去 30 天)
显示 更早的评论
Hi everybody,
I'm a beginner in Matlab/Simulink/App designer and I want to create an entire app which is capable of sending and reading a lot of CAN messages from a Speedgoat calculator.
I tried a lot of differents things and I read a lot of answer on topics inside Mathworks network but I didn't manage to solve my problems.
To sum up my situation a little bit, I create one timer which allows to read CAN messages from Speedgoat calculator every 100 ms and update some informations in the app according to the received messages, here is the code of the timer and its callback function :
% create a timer to receive CAN msgs
app.receiveCANmsgsTimer = timer('Period', 100e-3, 'ExecutionMode', 'fixedRate');
app.receiveCANmsgsTimer.TimerFcn = @(~,~)receiveCANmsgsTimerCallback(app);
% receiveCANmsgsTimerCallback Timer callback function for GUI updating
function receiveCANmsgsTimerCallback(app)
% receive available CAN messages
msg = receive(app.canChannelObj, Inf, 'OutputFormat', 'timetable');
% msg = receive(app.canChannelObj, Inf);
% update OutputModuleStates CAN message data
%newData =
getOutputModuleStatesCANmessage(app, msg);
% Update HOS Output CAN message
%newData1 =
getHOSOutputCANmessage(app, msg);
% Update ESS Gr A Cmd CAN message
%newData2 =
% getESSGrACmdCANmessage(app, msg);
% % Update ESS Gr B Cmd CAN message
% %newData3 =
getESSGrBCmdCANmessage(app, msg);
% % Update ESS Gr C Cmd CAN message
% %newData4 =
getESSGrCCmdCANmessage(app, msg);
% % Update ESS Gr D Cmd CAN message
% %newData5 =
getESSGrDCmdCANmessage(app, msg);
% % Update Cascadia Inverter Cmd CAN message
% %newData6 =
geteDriveCascadiaInverterCmdCANmessage(app, msg);
% % Update eDrive08 Drive Enable CAN message
% %newData7 =
geteDrive08DriveEnableCANmessage(app, msg);
% % Update eDrive07 Drive Enable CAN message
% %newData8 =
geteDrive07DriveEnableCANmessage(app, msg);
% % Update eDrive06 Drive Enable CAN message
% %newData9 =
geteDrive06DriveEnableCANmessage(app, msg);
% % Update eDrive05 Drive Enable CAN message
% %newData10 =
geteDrive05DriveEnableCANmessage(app, msg);
% % Update eDrive04 Drive Enable CAN message
% %newData11 =
geteDrive04DriveEnableCANmessage(app, msg);
% % Update eDrive03 Drive Enable CAN message
% %newData12 =
geteDrive03DriveEnableCANmessage(app, msg);
% % Update eDrive02 Drive Enable CAN message
% %newData13 =
geteDrive02DriveEnableCANmessage(app, msg);
% % Update eDrive01 Drive Enable CAN message
% %newData14 =
geteDrive01DriveEnableCANmessage(app, msg);
end
My canChannelObj is well define and create because I can see all the messages received and sent on the CAN channel.
Following, you will find an example of one of my get "Can Message function", the message and signal name are well created in a dababase file and the database is well linked to the canChannelObj :
function getOutputModuleStatesCANmessage(app, msg)
% extract signals from all OutputModule_States messages
outputModuleStatesSignals = canSignalTimetable(msg, "OutputModule_States");
if ~isempty(outputModuleStatesSignals)
% received new OutputModule_States messages, so create time series from CAN signal data
% Read EMS Mode signal and make the value table to
% display the modes of EMS inside an edit field (text
% area)
EMS_Mode = outputModuleStatesSignals.EMS_Mode;
if EMS_Mode == 0
app.EMSModeEditField_7.Value = "EMS_INIT";
elseif EMS_Mode == 1
app.EMSModeEditField_7.Value = "EMS_WAKEUP";
elseif EMS_Mode == 2
app.EMSModeEditField_7.Value = "EMS_FAILURE_WAKEUP";
elseif EMS_Mode == 3
app.EMSModeEditField_7.Value = "EMS_START_PRECHARGE";
elseif EMS_Mode == 4
app.EMSModeEditField_7.Value = "EMS_WAIT_FOR_HV_ON";
elseif EMS_Mode == 5
app.EMSModeEditField_7.Value = "EMS_PRECHARGE";
elseif EMS_Mode == 6
app.EMSModeEditField_7.Value = "EMS_FAILURE_PRECHARGE";
elseif EMS_Mode == 7
app.EMSModeEditField_7.Value = "EMS_WAIT_FOR_PU_START";
elseif EMS_Mode == 8
app.EMSModeEditField_7.Value = "EMS_FAILURE_PU_START";
elseif EMS_Mode == 9
app.EMSModeEditField_7.Value = "EMS_POWER_ON";
elseif EMS_Mode == 10
app.EMSModeEditField_7.Value = "EMS_FAILURE_POWER_ON";
elseif EMS_Mode == 11
app.EMSModeEditField_7.Value = "EMS_WAIT_FOR_PU_STOP";
elseif EMS_Mode == 12
app.EMSModeEditField_7.Value = "EMS_WAIT_FOR_HV_OFF";
elseif EMS_Mode == 13
app.EMSModeEditField_7.Value = "EMS_STOP";
else
app.EMSModeEditField_7.Value = "EMS_IMPOSSIBLE_VALUE";
end
end % getOutputModuleStatesCANmessage(app,msg)
This is the way I read datas and update the app information according to the CAN signals received by app designer.
One of my problem is that my EMSModeEditField_7.Value isn't update according to the signal value. At the starting of the app, It works but just after the value is blocked and stop to update even if the value of CAN signal changes. For example, at the beginning I see in the app "EMS_WAKEUP" inside the EMSModeEditField_7.Value and stay to this value but inside the CAN message "OutputModule_States" , I see the value 2 inside the signal EMS_Mode but I always have "EMS_WAKEUP" in the app instead of "EMS_FAILURE_WAKEUP".
Someone have some ideas to fix this problem, please ?
I'm really blocked since some days and all things that I tried didn't work.
If you have noticed, in the first code I added in the message, the fact that one of my get function is commented inside the function "receiveCANmsgsTimerCallback" :
%newData2 =
% getESSGrACmdCANmessage(app, msg);
% % Update ESS Gr B Cmd CAN message
If I uncomment this line and if I launch the my application with a USB to CAN connection between my PC and the Speedgoat, I have the following errors message in orange which never stops and continues all the time (my command window stil scroll down indefinitly), I don't have other choice to kill all matlab's windows through the windows tasks manager. I don't understand the link because the errors message seems to be linked to the transmits function that are completly independent compare to the line that I commented. Do you have any ideas on this problem ? 


I hope you have enough informations to help me, if it's not the case, don't hesitate to ask me for more details.
I thank you a lot in advance,
F.G.
回答(1 个)
Yatharth
2023-8-25
Hi,
I understand that you are trying to build an app to communicate with a speed goat calculator via CAN messages. Creating a MATLAB/Simulink/App Designer app to send and read CAN messages from a Speedgoat calculator involves several steps .
However in your implementation I was not able to find the usage of functions like canChannel and canMessage from the MATLAB CAN Communication Toolbox or Simulink CAN blocks like CAN Transmit and CAN Receive. You can refer to the following documentation and re-implement using the inbuilt toolbox of MATLAB or use relevant Simulink block and then use the generated code in your application. You can refer to the simulink documentation here.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!