analogRead function not working in MATLAB - matlab

I am new to Arduino and I am trying to control it through MATLAB. I have downloaded the arduino IO package and installed it.I am using MATLAB 2014a.I have a LED and I change its brightness by changing the PWM voltage as follows :
a=arduino('COM3');
brightness_step = (5-0)/20;
for i = 1:20
writePWMVoltage(a, 11, i*brightness_step);
val=a.analogRead(0);
display(val)
pause(0.1);
end
for i = 1:20
writePWMVoltage(a, 11, 5-i*brightness_step);
val=a.analogRead(0);
display(val)
pause(0.1);
end
clear a
Everything works great except for the analogRead part which throws the following error :
No appropriate method, property, or field analogRead for class arduino.
What am I doing wrong?

Maybe you can try to use fscanf() like in the following example from AllAboutEE.

I had the same problem with the analogRead when I connected my ArduinoUno R3.
After an hour's struggle, I changed to readVoltage, as Controller replied in 2014, and it works perfectly.

Related

MATLAB R2017a VideoWriter error: invalid properties

I have recently upgraded from MATLAB R2016a to R2017a (also changed from Windows 7, now using Windows 10). Trying to run my existing code (nothing changed) using VideoWriter to create a video object, I'm getting the following error, when I get to the stage of writing the video:
Error using VideoWriter/writeVideo (line 369)
One or more properties of the video is invalid. Ensure 'Quality' and 'FrameRate' property of the object and the frame dimensions are within allowed range.
This is my code to save the video:
v = VideoWriter('vidOutput','MPEG-4');
v.FrameRate = 240;
open(v);
for i = 1:size(data,2)
... code that plots an image frame...
F = getframe;
pause(0.000001)
writeVideo(v,F.cdata);
end
The issue seems to be with the FrameRate property. When it's set to more than 150, the error happens. Anything lower than 150 runs ok. Important to note, that when running on MATLAB R2016a, it did work with FrameRate = 240.
Any idea what might have gone wrong?

How can I visualise results of a simulation in OpenModelica as animation?

I want to know how I can visualise the results of a Modelica simulation in the form of an animation.
Imagine I have a simple simulation as below:
model test
//parameters
parameter Real m_1 = 1;
parameter Real m_2 = 10;
parameter Real K_c = 100000;
//variables
Real x_1;
Real v_1;
Real x_2;
Real v_2;
Real f_1;
Real f_12;
initial equation
x_1 = 0;
v_1 = 0;
x_2 = 0.2;
v_2 = 0;
equation
v_1 = der(x_1);
m_1 * der(v_1) = f_1 - f_12;
v_2 = der(x_2);
m_2 * der(v_2) = f_12;
f_12 = if x_2 >= x_1 then 0 else K_c * (x_1 - x_2);
f_1 = 1;
end test;
(it is actually a very simple elastic collision)
and I have part1.obj and part2.obj (or other possible formats rather than .obj), designed in other CAD software, which I want to import and assign to x_1 and x_2 and then animate. OpenModelica 1.11.0 is already installed on Ubuntu using these instructions.
What I have found so far and the problems I have encountered:
From this page I have learned that Modelica3D should also be installed. However I couldn't find this library using apt or aptitude.
I found the installation instructions for windows and mac on OpenModelica official website, but no Ubuntu (or other Linux distro) one.
I have also found this github repo which claims to be a regularly maintained mirror of the original distribution, however the latest commit is for 2 years ago! Not to mention that the original website is dead.
Do I still need to install this library separately or it has already been integrated into the latest stable OpenModelica release? if yes how can I install it? what other libraries/packages might be needed?
Edit: It seems that Modelica3D has been depreciated (same for OMVisualize). Now the ModelicaServices is being used for animation. more information here
This post gives a very simple example of animating using Wolfram SystemModeller. When I open it into the OpenModelica, it does compile and shows the results. I tried exporting the model as FMU and XML and then use animation icon from the top menus to import the model.
but when tried to import the .mat file I got the error:
Scripting Error
Could not find the visual XML file /tmp/OpenModelica_user/OMEdit/SimpleCarModel_visual.xml.
Is this example compatible with OpenModelica? and if yes what is the problem and how I can solve it? (you can also download the example from this Github gist)
Edit: This example is compatible with OpenModelica. I was doing it wring. I had to choose the "simulate with Animation" instead of "Simulate". I still can't assign an external CAD object to the simulation and a cylinder is being shown instead.
I also tried to learn using OMShell scripting to run the Modelica3D example from this page. However it seems that the code is wrong because I get many errors running it in my OMShell.
One of the linked websites has mentioned that it is possible to use blender for animated results however the author hadn't succeed to do so. I would appreciate if you could help learn this in simple and clear steps.
Visualizing CAD-files is currently only possible for .stl and .dxf.
You can specify the URI of your CAD-file in the shapeType-parameter of your shape. Please use the following notation:
"modelica://packageName/relativePath/To/Your/CADfile.stl"
It is also possible to set the absolute path in the *_visual.xml that has been generated with your simulation results(and should be in the same directory if you want to load simulation from OMEdit).
If you want to move your animation shapes, simply assign variables to i.e. the position vector
r = {x,0,0};

Serial Comunnication between matlab and arduino

I am trying to send data from MATLAB to ARDUINO using the following code for arduino and the second one for the matlab. Both code work fine and when i press 1 led lights up and when press 2 led become off. But actually what i am trying to do is when matlab run code it automatically send 1 to arduino and the led become on. i have tried may changes but cant able to do. when iam trying to run the third code (given below) the arduino status led blink that show it receive some thing but my actual led which is connected to pin 13 still off.
int ledPin=13;
int matlabData;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0) // if there is data to read
{
matlabData=Serial.read(); // read data
if(matlabData==1)
digitalWrite(ledPin,HIGH); // turn light on
else if(matlabData==2)
digitalWrite(ledPin,LOW); // turn light off
}
}
(MATLAB)
1.
clear all
2.clc
3.
4.answer=1; % this is where we'll store the user's answer
5.arduino=serial('COM4','BaudRate',9600); % create serial communication object on port COM4
6.
7.fopen(arduino); % initiate arduino communication
8.
9.while answer
10. fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
11. answer=input('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): '); % ask user to enter value for variable answer
12.end
13.
14.fclose(arduino); % end communication with arduino
(MY edit code)
1.
clear all
2.clc
3.
4.answer=1; % this is where we'll store the user's answer
5.arduino=serial('COM4','BaudRate',9600); % create serial communication object on port COM4
6.
7.fopen(arduino); % initiate arduino communication
8.
9.%while answer
10. fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
11. answer='1'%('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): '); % ask user to enter value for variable answer
12.%end
13.
14.fclose(arduino); % end communication with arduino
The difference is the following:
answer = input('bla')
yields an answer that is a number, i.e. answer is of type double. In your third case, you wrote answer='1' which is a char, so in fact, the variable answer is different in both cases. Try and change the code in the third part to this here:
answer = 1;
or change the fprintf command to
fprintf(arduino, '%s', str2num(answer));

Compile Matlab code for Arduino

I understand there is a way to compile and upload MATLAB Simulink code to an Arduino so you don't have to have the Arduino connected to your computer. Is there a way to compile and upload a MATLAB script file of code that I have written myself to the Arduino so it can work independently from being plugged in. i.e. could I upload something like:
a = arduino('COM3')
a.pinMode(13,'output');
i = 0
while i>0
a.digitalWrite(13,1)
pause(1)
a.digitalWrite(13,0)
pause(1)
end
how would I do this and does it still require code lines like:
a = arduino
and putting the a. in front of the lines of code

I want to record video from webcam

I am using Matlab code for that.
But when I run it, It shows errors.
Code :
fig=figure;
set(fig,'DoubleBuffer','on');
set(gca,'xlim',[-80 80],'ylim',[-80 80],'NextPlot','replace','Visible','off');
mov = avifile('movie.avi','compression','cinepak');
mov.quality=90;
i=imread('white.jpg');
for k=1:1000
imshow(i);
F = getframe(gca);
mov = addframe(mov,F);
end
mov = close(mov);
Errors :
??? Error using ==> capturescreen
Figure destroyed during getframe
Error in ==> getframe at 35
x=capturescreen(varargin{:});
Error in ==> a at 9
F = getframe(gca);
Please help me with this.
Thank you.
Works fine here, although I had to change compression to 'None' since RLE and MSVC won't won't work with the truecolor image I was using and indeo/cinepak wouldn't work with x64. (Not either version of indeo, oddly)
My guess was that you needed a drawnow after your imshow command to clear the buffer so you have an image to copy. I produced a different error the first time I ran it, which seemed to confirm that, but I couldn't reproduce that one. It definitely works with the drawnow; in.
Update: For anyone running win7 x64, you should be able to use 'i420' as your codec. Using that the above code works fine for me either with drawnow in after imshow or not.