Error opening a text file using fopen

disp('Setting up serial communication...');
% Determine which COM port is for microcontroller and change
a = arduino('COM4','Uno');
% Open serial COM port for communication
fopen(a);
set(a,'Timeout',10);
prev_left = [0 0 0 0];
prev_right = [0 0 0 0];
prev_left_flag=0;
prev_right_flag =0;
prev_flag = 2;
check_left = 0;
check_right = 0;
check_straight_right = 0;
check_straight_left = 0;
check_left_right = 0;
check_right_left = 0;

2 Comments

Stephen23
Stephen23 on 27 Sep 2017
Edited: Stephen23 on 27 Sep 2017
a is not a text file, it is an Arduino object.

Sign in to comment.

 Accepted Answer

It is not necessary to fopen() an arduino object: the arduino() function does any necessary opening of the device.

13 Comments

so for arduino does not any necessary? am i right?
sorry for disturbing you sir. sir can you help me for correcting my coding because my driver motor is not moving.
The code you posted does not read anything from the device and does not send anything to the device.
%%Initialization of variables and serial communication
clc;
clear all;
close all;
delete(instrfind);
% Set up the COM port for serial communication
disp('Setting up serial communication...');
% Determine which COM port is for microcontroller and change
s = serial('COM4','Parity','none','FlowControl','none','BaudRate',9600);
% Open serial COM port for communication
fopen(s);
set(s,'Timeout',10);
prev_left = [0 0 0 0];
prev_right = [0 0 0 0];
prev_left_flag=0;
prev_right_flag =0;
prev_flag = 2;
check_left = 0;
check_right = 0;
check_straight_right = 0;
check_straight_left = 0;
check_left_right = 0;
check_right_left = 0;
%%Image Capture and Eye Detection
vid=videoinput('winvideo',1); start(vid)
while(vid.FramesAcquired<=25)
I = getsnapshot(vid);
I = rgb2gray(I);
I = imadjust(I);
I = adapthisteq(I);
I = imrotate(I,180);
faceDetector = vision.CascadeObjectDetector('LeftEyeCART');
j=0;
left_flag=0;
right_flag=0;
bboxes = step(faceDetector, I);
[m,n] = size(bboxes);
IFaces = insertObjectAnnotation(I, 'rectangle', bboxes, 'Eye');
imshow(IFaces), title('Detected eye');
%%Image Processing
TF = isempty(bboxes);
if (TF==1)
disp('nothing');
k=1;
else
k=0;
end
for i=1:1:m
if (bboxes(i,3) < 150)
% display('invalid eye');
elseif (bboxes(i,3) > 300)
% display('invalid eye');
else
j=j+1;
eye(j,:) = bboxes(i,:);
end
end
if (j>0)
for (i=1:1:j)
if(eye(i,1)>300) && (eye(i,1)<600)
left_eye = eye(i,:);
disp('Left:');
disp(left_eye);
left_flag=1;
elseif(eye(i,1)>600) && (eye(i,1)<900)
right_eye = eye(i,:);
disp('Right:');
disp(right_eye);
right_flag=1;
end
end
%%Movement Detection
if((left_flag==1)&& (prev_left_flag ==1))
prev_left_flag = 1;
if((left_eye(1,1) - prev_left(1,1))>50)
flag = 0;
display('moved left');
elseif ((left_eye(1,1) - prev_left(1,1))<-50)
flag = 1;
display('moved right');
else
flag = 2;
display('stayed');
end
prev_left = left_eye;
elseif((right_flag==1)&&(prev_right_flag==1))
prev_right_flag = 1;
if((right_eye(1,1) - prev_right(1,1))>50)
flag = 0;
display('moved left');
elseif ((right_eye(1,1) - prev_right(1,1))<-50)
flag = 1;
display('moved right');
else
flag = 2;
display('stayed');
end
prev_right = right_eye;
elseif(left_flag==1)
prev_left_flag = 1;
if((left_eye(1,1) - prev_left(1,1))>50)
flag = 0;
display('moved left');
elseif ((left_eye(1,1) - prev_left(1,1))<-50)
flag = 1;
display('moved right');
else
flag = 2;
display('stayed');
end
prev_left = left_eye;
elseif(right_flag==1)
prev_right_flag = 1;
if((right_eye(1,1) - prev_right(1,1))>50)
flag = 0;
display('moved left');
elseif ((right_eye(1,1) - prev_right(1,1))<-50)
flag = 1;
display('moved right');
else
flag = 2;
display('stayed');
end
prev_right = right_eye;
end
if (left_flag == 0)
prev_left_flag=0;
elseif (right_flag == 0)
prev_right_flag=0;
end
%%Motor Control Signals
if ((prev_flag == 0) && (flag == 1)) % straight movement
display('motor moved straight');
move = 2;
check_straight_right = 1;
check_left_right = 1;
elseif ((prev_flag ==1) && (flag == 0))
move = 2;
display('motor moved straight');
check_straight_left = 1;
check_right_left = 1;
elseif ((prev_flag == 0) && (flag == 2)) % left movement
if ((check_right == 1) || (check_straight_right == 1)||(check_right_left))
move = 3;
display('motor stays');
check_right = 0;
check_straight_right = 0;
check_right_left = 0;
else
move = 0;
display('motor moved left');
check_left = 1;
end
elseif ((prev_flag == 1) && (flag == 2)) % right movement
if ((check_left == 1) || (check_straight_left == 1) || (check_left_right))
move = 3;
display('motor stays');
check_left = 0;
check_straight_left = 0;
check_left_right = 0;
else
move = 1;
display('motor moved right');
check_right = 1;
end
else % no movement
move = 3;
display('motor stays');
end
prev_flag = flag;
%%Serial Transmission
fprintf(s,'%1d\n',move);
disp('done');
end
hold on;
end
stop(vid);
flushdata(vid);
pause(0.04);
fclose(s);
delete(s);
clear s;
clear all;
sir this is my full coding. i want change this coding by using arduino because mt project using arduino uno. but when i change serial to arduino error fopen. and motor driver cannot move.
Arduino can be used in two ways.
1) You could have a program ("sketch") running on the arduino that is actively reading the serial port, examining the data received, and dealing with external hardware all on its own, in a way that is hidden from the host program. That is the style that the current program is written for, where the program just sends out an encoded value of '1' (move right) or '2' (move left) or '3' (stay), and the device receiving the information is responsible for figuring out what to do what that data. If you have such a program running on the arduino, then all you should need to do is change the port number to the one that the arudino is on: you would continue to use serial() and so on. There would be almost no change to the program you show.
2) You can instead have the host program set up connections to particular hardware devices like digital pins or i2c sensors, and the host would use calls such as writeDigitalPin or writeRegister to send more direct hardware instructions as to exactly what to do at the hardware level. The program you show would have to change to set up the hardware and would have to change in how it sent hardware instructions. It is only this form of using the arduino that would use the arduino() call.
sir for the first way. i cant understand about program ("sketch"). can you explain or give me link about this.
Arduino "sketches" (programs) are written in C or C++.
You can find examples about controlling bidirectional motors. See for example http://www.instructables.com/id/Control-DC-and-stepper-motors-with-L298N-Dual-Moto/
//test for Serial COM with matlab
#define F_CPU 16000000UL
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <math.h>
#include <util/delay.h>
// serial communication library
#include "uart.h"
#include "uart.c"
double counter;
// UART file descriptor
// putchar and getchar are in uart.c
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
void delay (void);
void initialize(void);
int main(void)
{
int move;
// 0 - left, 1- right, 2- straight, 3 - stop
initialize();
while(1){
fscanf(stdin,"%d",&move); // receive data
switch (move)
{
case 0:
PORTA = 0x01; // move left
_delay_ms (1000);
break;
case 1:
PORTA = 0x04; // move right
_delay_ms (1000);
break;
case 2:
PORTA = 0x05; // move straight
_delay_ms (1000);
break;
case 3:
PORTA = 0x00; // halt
_delay_ms (1000);
break;
default:
PORTA = 0x00; // halt
_delay_ms (1000);
break;
}
}
}
void initialize(void)
{
DDRA =0xFF;
//init the UART -- uart_init() is in uart.c
uart_init();
stdout = stdin = stderr = &uart_str;
fprintf(stdout,"Starting...\n\r");
}
sir coding above is coding c. This coding can not be run. is this coding error?
The use of // as a comment delimiter requires C99 or later.
Writing to a port using something like PORTA = 0x04; would require..... ummm, I would have to research to see if it is possible at all. More typical would be *PORTA = 0x04; which is valid for memory mapped ports.
It is uncommon for motors to handle bytes of commands like that. Much more common would be pulse width modulation schemes, or rotary pulse schemes.
thank you sir. sir did you know how to compile matlab software and code block?
I have gone back and checked, and I find that on some systems, it is possible to write to a port using code such as PORTA = 0x04; The conditions for doing this is that the linker needs to be instructed to place the variable at a particular memory location (on a device that supports that.) See the description of scatter.txt in http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3750.html
"did you know how to compile matlab software and code block?"
I know some things; I would prefer if you asked specific questions. Are you talking about MATLAB Compiler product (create .exe), or MATLAB Coder product (create C/C++ code), or MATLAB Compiler SDK (create .dll); or are you talking about Simulink Coder (generate C/C++ code from Simulink models); or are you talking about code generation from Simulink models for specific hardware targets such as Arduino ?
If you are trying to use MATLAB Coder to generate C/C++ code for Arduino, the task is a bit harder, as MATLAB Coder does not know anything about Arduino as a target and so has no libraries to talk to Arduino operating system or hardware -- Simulink does know about Arduino as a target.
If you want to move your entire MATLAB program onto Arduino, then you have the problem that MATLAB Coder, and Simulink, and the Support Package for Arduino, do not know how to create graphics on Arduino using imshow() or any other direct graphics. Simulink does have an Arduino video display block, which has to be sent complete images -- requiring that you do all of the drawing into an array and then send the array to be displayed as an image. The Computer Vision routines such as shape inserter and text inserter are very useful for those purposes.

Sign in to comment.

More Answers (0)

Categories

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!