- The use of "close all" and "clc" in the "startupFcn" method is generally not recommended because it can close all figures and clear the command window which might not be the desired behaviour.
- You are deleting the serial object in the "startupFcn" and "Time000secSliderValueChanging" functions, which could lead to issues if the serial port does not open properly or if there is an error in communication.
- You are using "fscanf" to read data from the serial port, but you are not checking if the data is read correctly. This could lead to errors if the data is not received in the expected format.
Why I am not receiving serial data from arduino? I am using app designer
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, I am trying to use this GUI with serial data from arduino, but when I run it nothing happens.
classdef dinvu < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
FlightInstrumentsFlightDataPlaybackUIFigure matlab.ui.Figure
PiperPA24ComancheFlightDataDisplayLabel matlab.ui.control.Label
Time000secSlider matlab.ui.control.Slider
Time000secSliderLabel matlab.ui.control.Label
ClimbIndicator Aero.ui.control.ClimbIndicator
HeadingIndicator Aero.ui.control.HeadingIndicator
TurnCoordinator Aero.ui.control.TurnCoordinator
Altimeter Aero.ui.control.Altimeter
ArtificialHorizon Aero.ui.control.ArtificialHorizon
AirspeedIndicator Aero.ui.control.AirspeedIndicator
Image matlab.ui.control.Image
end
properties (Access = public)
a % Saved flight data [time X Y Z phi theta psi]
animObj % Aero.Animation object
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
close all;
clc;
delete(instrfindall);
NumMues=10;
y=zeros(1,NumMues);
delete(instrfind({"Port"},{"COM4"}));
micro=serial("COM4");
micro.BaudRate=9600;
warning("off","MATLAB:serial:fscanf:unsuccesfulRead");
fopen(micro);
% Load saved flight status data
savedData = fscanf(micro,"%s");
a = strsplit(savedData, ',');
yaw = str2double(a(7));
yaw(yaw<0) = yaw(yaw<0)+2*pi; % unwrap yaw angles
% Create animation object to visualize aircraft flight dynamics corresponding with saved data over time
app.animObj = Aero.Animation;
app.animObj.createBody('pa24-250_orange.ac','Ac3d'); % Piper PA-24 Comanche geometry
app.animObj.Bodies{1}.TimeseriesSourceType = 'Array6DoF'; % [time X Y Z phi theta psi]
app.animObj.Bodies{1}.TimeSeriesSource = str2double(a(1));
app.animObj.Camera.PositionFcn = @staticCameraPosition;
app.animObj.Figure.Position = [app.FlightInstrumentsFlightDataPlaybackUIFigure.Position(1)+625 app.FlightInstrumentsFlightDataPlaybackUIFigure.Position(2) app.FlightInstrumentsFlightDataPlaybackUIFigure.Position(3) app.FlightInstrumentsFlightDataPlaybackUIFigure.Position(4)];
%app.animObj.updateBodies(app.a(1,1)); % Initialize animation window at t=0
%app.animObj.updateCamera(app.a(1,1));
app.animObj.show();
end
% Value changing function: Time000secSlider
function Time000secSliderValueChanging(app, event)
delete(instrfindall);
NumMues=200;
y=zeros(1,NumMues);
delete(instrfind({"Port"},{"COM4"}));
micro=serial("COM4");
micro.BaudRate=9600;
warning("off","MATLAB:serial:fscanf:unsuccesfulRead");
fopen(micro);
contador=1;
while contador <=NumMues
savedData = fscanf(micro,"%s");
% Display current time in slider component
t = event.Value;
app.Time000secSliderLabel.Text = sprintf('Time: %.1f sec', t);
% Find corresponding time data entry
k = find(strsplit(savedData, ',')<=t);
k = k(end);
nData{k,1} = str2double(t(1));
nData{k,2} = str2double(t(2));
nData{k,3} = str2double(t(3));
nData{k,4} = str2double(t(4));
nData{k,5} = str2double(t(5));
nData{k,6} = str2double(t(6));
nData{k,2:4}=str2double(t(2:4));
nData{k-1,2:4}=str2double(t(2:4));
nData{k,5:7}=str2double(t(5:7));
nData{k-1,5:7}=str2double(t(5:7));
app.Altimeter.Altitude = convlength(-nData{k,4}, 'm', 'ft');
app.HeadingIndicator.Heading = convang(nData{k,7},'rad','deg');
app.ArtificialHorizon.Roll = convang(nData{k,5},'rad','deg');
app.ArtificialHorizon.Pitch = convang(nData{k,6},'rad','deg');
if k>1
% Estimate velocity and angular rates
Vel = (nData{k,2:4}-nData{k-1,2:4})/(nData{k,1}-nData{k-1,1});
rates = (nData{k,5:7}-nData{k-1,5:7})/(nData{k,1}-nData{k-1,1});
app.AirspeedIndicator.Airspeed = convvel(sqrt(sum(Vel.^2)),'m/s','kts');
app.ClimbIndicator.ClimbRate = convvel(-Vel(3),'m/s','ft/min');
% Estimate turn rate and slip behavior
app.TurnCoordinator.Turn = convangvel(rates(1)*sind(30) + rates(3)*cosd(30),'rad/s','deg/s');
app.TurnCoordinator.Slip = 1/(2*pi)*convang(atan(rates(3)*sqrt(sum(Vel.^2))/9.81)-nData{k,5},'rad','deg');
else
% time = 0
app.ClimbIndicator.ClimbRate = 0;
app.AirspeedIndicator.Airspeed = 0;
app.TurnCoordinator.Slip = 0;
app.TurnCoordinator.Turn = 0;
end
end
delete(micro)
%% Update animation window display
app.animObj.updateBodies(app.a(k,1));
app.animObj.updateCamera(app.a(k,1));
end
% Close request function:
% FlightInstrumentsFlightDataPlaybackUIFigure
function FlightInstrumentsFlightDataPlaybackUIFigureCloseRequest(app, event)
% Close animation figure with app
delete(app.animObj);
delete(app);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create FlightInstrumentsFlightDataPlaybackUIFigure and hide until all components are created
app.FlightInstrumentsFlightDataPlaybackUIFigure = uifigure('Visible', 'off');
app.FlightInstrumentsFlightDataPlaybackUIFigure.AutoResizeChildren = 'off';
app.FlightInstrumentsFlightDataPlaybackUIFigure.Color = [0.2706 0.2706 0.2784];
app.FlightInstrumentsFlightDataPlaybackUIFigure.Position = [100 100 620 550];
app.FlightInstrumentsFlightDataPlaybackUIFigure.Name = 'Flight Instruments - Flight Data Playback';
app.FlightInstrumentsFlightDataPlaybackUIFigure.Resize = 'off';
app.FlightInstrumentsFlightDataPlaybackUIFigure.CloseRequestFcn = createCallbackFcn(app, @FlightInstrumentsFlightDataPlaybackUIFigureCloseRequest, true);
% Create Image
app.Image = uiimage(app.FlightInstrumentsFlightDataPlaybackUIFigure);
app.Image.Position = [8 -2 606 577];
app.Image.ImageSource = 'appdesignerInstrumentPanel.png';
% Create AirspeedIndicator
app.AirspeedIndicator = uiaeroairspeed(app.FlightInstrumentsFlightDataPlaybackUIFigure);
app.AirspeedIndicator.Limits = [25 250];
app.AirspeedIndicator.ScaleColorLimits = [0 60;50 200;200 225;225 250];
app.AirspeedIndicator.Position = [22 317 185 185];
% Create ArtificialHorizon
app.ArtificialHorizon = uiaerohorizon(app.FlightInstrumentsFlightDataPlaybackUIFigure);
app.ArtificialHorizon.Position = [219 317 185 185];
% Create Altimeter
app.Altimeter = uiaeroaltimeter(app.FlightInstrumentsFlightDataPlaybackUIFigure);
app.Altimeter.Position = [416 317 185 185];
% Create TurnCoordinator
app.TurnCoordinator = uiaeroturn(app.FlightInstrumentsFlightDataPlaybackUIFigure);
app.TurnCoordinator.Position = [22 70 185 185];
% Create HeadingIndicator
app.HeadingIndicator = uiaeroheading(app.FlightInstrumentsFlightDataPlaybackUIFigure);
app.HeadingIndicator.Position = [219 70 185 185];
% Create ClimbIndicator
app.ClimbIndicator = uiaeroclimb(app.FlightInstrumentsFlightDataPlaybackUIFigure);
app.ClimbIndicator.MaximumRate = 8000;
app.ClimbIndicator.Position = [416 70 185 185];
% Create Time000secSliderLabel
app.Time000secSliderLabel = uilabel(app.FlightInstrumentsFlightDataPlaybackUIFigure);
app.Time000secSliderLabel.HorizontalAlignment = 'right';
app.Time000secSliderLabel.FontSize = 11.5;
app.Time000secSliderLabel.FontColor = [1 1 1];
app.Time000secSliderLabel.Position = [267 3 80 22];
app.Time000secSliderLabel.Text = 'Time: 00.0 sec';
% Create Time000secSlider
app.Time000secSlider = uislider(app.FlightInstrumentsFlightDataPlaybackUIFigure);
app.Time000secSlider.Limits = [0 49.833333333333];
app.Time000secSlider.MajorTicks = [0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 49.833333333333];
app.Time000secSlider.MajorTickLabels = {'0', '2', '4', '6', '8', '10', '12', '14', '16', '18', '20', '22', '24', '26', '28', '30', '32', '34', '36', '38', '40', '42', '44', '46', '48', '50'};
app.Time000secSlider.ValueChangingFcn = createCallbackFcn(app, @Time000secSliderValueChanging, true);
app.Time000secSlider.MinorTicks = [];
app.Time000secSlider.FontSize = 11.5;
app.Time000secSlider.FontColor = [1 1 1];
app.Time000secSlider.Position = [50 55 520 3];
% Create PiperPA24ComancheFlightDataDisplayLabel
app.PiperPA24ComancheFlightDataDisplayLabel = uilabel(app.FlightInstrumentsFlightDataPlaybackUIFigure);
app.PiperPA24ComancheFlightDataDisplayLabel.BackgroundColor = [0.8 0.8 0.8];
app.PiperPA24ComancheFlightDataDisplayLabel.HorizontalAlignment = 'center';
app.PiperPA24ComancheFlightDataDisplayLabel.FontName = 'Courier New';
app.PiperPA24ComancheFlightDataDisplayLabel.FontSize = 14;
app.PiperPA24ComancheFlightDataDisplayLabel.FontWeight = 'bold';
app.PiperPA24ComancheFlightDataDisplayLabel.Position = [141 515 347 22];
app.PiperPA24ComancheFlightDataDisplayLabel.Text = 'Piper PA-24 Comanche Flight Data Display';
% Show the figure after all components are created
app.FlightInstrumentsFlightDataPlaybackUIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = dinvu
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.FlightInstrumentsFlightDataPlaybackUIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.FlightInstrumentsFlightDataPlaybackUIFigure)
end
end
end
댓글 수: 0
답변 (1개)
Nivedita
2023년 12월 4일
I understand you are facing issues with receiving serial data from arduino in you rapp created using MATLAB app designer. I figures some of these issues could be responsible for such behaviour, you could try to modify your code according to these suggestions:
This code snippet could help you with the modifications:
function startupFcn(app)
% Initialize serial communication
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
end
app.micro = serial("COM4", 'BaudRate', 9600);
fopen(app.micro);
% Assuming the rest part of the code is correct`
end
% Value changing function: Time000secSlider
function Time000secSliderValueChanging(app, event)
% Read data from serial port
savedData = fscanf(app.micro, "%s");
if isempty(savedData)
return; % No data read, exit the function
end
a = strsplit(savedData, ',');
if length(a) < 7
return; % Not enough data, exit the function
end
% Convert the string data to numbers
numericData = str2double(a);
% Update the flight instruments using numericData
% ...
end
Do remember to properly handle the creation and deletion of the serial object to avoid issues with the COM port being left open or in an undefined state. Additionally, ensure that the data received from the serial port is in the correct format and that you handle any possible communication errors.
I hope this helps!
Regards,
Nivedita.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!