Matlab simulate quadcopter - matlab

I want to simulate a quadcopter flight (x,y,z,roll,pitch,yaw).
I need a simplest option to simulate/visualization - maybe 'X' / cross sign.
like this video: in 2:00 : https://www.youtube.com/watch?v=dvNzxVqqgnw
given (x,y,z,roll,pitch,yaw) , how can I simulate/visualization using the simplest way ?
I know how to plot 3d point with 'X' sign (using plot3)- but this thing doesn't control angles (roll,pitch,yaw) - is there a similar function that takes (x,y,z,roll,pitch,yaw) ??
Thanks

I am going to assume that you have the flight data ((x,y,z,roll,pitch,yaw) and simply want to display it.
Plot3 plots a single 3d point, which in itself cannot have pitch, roll, or yaw. It sounds like you will need to design a shape that can represent your quadcopter (a triangle of 3 points would do - anything with more than 1 dimension will. A square would probably be easiest), and then write a function that can compute the 3d coords of each point, given the coords of one of one of the points, and the pitch roll yaw data. Then use the plot3 to plot each point individually (and maybe add lines between them or something)
I realize this may not be helpful, but your question is extremely broad.

Related

MATLAB STL 3D modelling from IMU Data

I am trying to upload an STL file to MATLAB and be able to manipulate it but can't find the best way to do it.
What I am trying to do is import an STL a file of a hand tool and be able to rotate the 3D image by giving it roll, pitch and yaw angles. The whole system will involve a live read out from an IMU which calculates these angles (going to use a 9 axis IMU - 9250 and hope to incorporate space movement into this but that's progress for another day) which will feed into a function which alters the orientation of the model made from the STL to show in real time how the body is moving. Its important to note the body is fixed so no points can move relative to each other (simplifying the problem).
Currently I have not got far but have modeled the STL fixed in space:
model = createpde(3);
importGeometry(model,'Test_model.stl');
pdegplot(model);
This will plot the STL file. The model is made up of a certain number of faces and vertices which can be plotted but I cannot see a way of manipulating these. I figure that there should be some way of converting this to a 3D matrix of points in x,y,z which I can mulitply by a rotation vector to give a new position rotated by the three angles.
Rx = rotx(psi);
Ry = roty(theta);
Rz = rotz(phi);
R = Rx*Ry*Rz;
Then multiply the model by this and update the plot.
I will also need a way of offsetting all points by certain values to be able to change the point of rotation (where the IMU is placed). I figure once I get the coordinates in a matrix then I can offset them all by certain values in each direction x, y and z.
Can anyone help with this, I have been looking for similar projects but I have not been able to find anything with good code explanations as of yet. The way I am proposing is only my idea, if there is an easier method then please say. Thanks!
I do not have comment privileges so this may not seem like a complete answer.
I've done exactly this type of thin in MATLAB for other research but had to write my own data parsers as I do not have any tool boxes, or importGeometry() didn't exist at the time. The STL is structured as a list of triangles each with a normal and three vertices. I'd ask you, after importing STL what is the data format? An array of positions, a struct or object? Also, what s/w was used to make it. The gmsh format is easier to work with as it gives you a reduced list of points and lists of connections between them based on what simplex contains the points.
If the output of importGeometry is a struct with the full data set then you will have repeated data and need to (1) parse the struct, (2) delete duplicates, (3) stack the results in a 3-by-N or N-by-3 matrix, then operate on this result with the rotation matrix and update plots.
You haven't really asked a specific question but I hope that my comments were helpful.

Getting a cross-section from power-spectrum

I am currently trying to use spectral-methods to analyse topographic landscapes.
When i FFT the landscape and plot the power-spectrum. From the power-spectrum an orientation of the structures in the landscape can be found.
2D power-spectrum:-
In this power-spectrum, i would like to make a cross-section.
This is easy when the peak amplitude orientation is along the x or y-axis.
But for this area (and others), this is not the case.
Cross-section from another area - orientated along the y-axis:-
My problem is i want to make a cross-section along the peaks in 1, and i just cant seem to figure it out how.
If anyone could point me towards some solution for this. Been stuck here for a couple of days now.
Edit 1
I would like the cross-section, to be a line along the peak orientation.
Edit 2
Improved the first image to show where i want my cross-section
My solution was, as GameOfThrows suggested:
Pick 2 (or more) points on the orientation i want
used Least squares on the points to create the line
Setup a meshgrid for the interpolation.
Use the interp2 function on the new line.
define a proper axis for the section
In my final cross section i ended up having multiple lines in it, that way i was sure to hit the max amplitudes.
i was a little with the answer to my question, but i have been busy :)
You can use ginput built-in matlab function to store 2 (x,y) coordinates of your power spectrum and then use this values to delimit a profile to be interpolated.

MATLAB: Digitizing a plot with multiple variables and implementing the data

I have 8 plots which I want to implement in my Matlab code. These plots originate from several research papers, hence, I need to digitize them first in order to be able to use them.
An example of a plot is shown below:
This is basically a surface plot with three different variables. I know how to digitize a regular plot with just X and Y coordinates. However, how would one digitize a graph like this? I am quite unsure, hence, the question.
Also, If I would be able to obtain the data from this plot. How would you be able to utilize it in your code? Maybe with some interpolation and extrapolation between the given data points?
Any tips regarding this topic are welcome.
Thanks in advance
Here is what I would suggest:
Read the image in Matlab using imread.
Manually find the pixel position of the left bottom corner and the upper right corner
Using these pixels values and the real numerical value, it is simple to determine the x and y value of every pixel. I suggest you use meshgrid.
Knowing that the curves are in black, then remove every non-black pixel from the image, which leaves you only with the curves and the numbers.
Then use the function bwareaopen to remove the small objects (the numbers). Don't forget to invert the image to remove the black instead of the white.
Finally, by using point #3 and the result of point #6, you can manually extract the data of the graph. It won't be easy, but it will be feasible.
You will need the data for the three variables in order to create a plot in Matlab, which you can get either from the previous research or by estimating and interpolating values from the plot. Once you get the data though, there are two functions that you can use to make surface plots, surface and surf, surf is pretty much the same as surface but includes shading.
For interpolation and extrapolation it sounds like you might want to check out 2D interpolation, interp2. The interp2 function can also do extrapolation as well.
You should read the documentation for these functions and then post back with specific problems if you have any.

Constructing voxels of a 3D cube in MATLAB

I want to construct a 3D cube in MATLAB. I know that the units of any 3D shape are voxels not pixels. Here is what I want to do,
First, I want to construct a cube with some given dimensions x, y, and z.
Second, according to what I understand from different image processing tutorials, this cube must consists of voxels (3D pixels). I want to give every voxel an initial color value, say gray.
Third, I want to access every voxel and change its color, but I want to distinguish the voxels that represent the faces of the cube from those that represent the internal region. I want to axis every voxel by its position x,y, z. At the end, we will end up with a cube that have different colors regions.
I've searched a lot but couldn't find a good way to implement that, but the code given here seems very close in regard to constructing the internal region of the cube,
http://www.mathworks.com/matlabcentral/fileexchange/3280-voxel
But it's not clear to me how it performs the process.
Can anyone tell me how to build such a cube in MATLAB?
Thanks.
You want to plot voxels! Good! Lets see how we can do this stuff.
First of all: yeah, the unit of 3D shapes may be voxels, but they don't need to be. You can plot an sphere in 3D without it being "blocky", thus you dont need to describe it in term of voxels, the same way you don't need to describe a sinusoidal wave in term of pixels to be able to plot it on screen. Look at the figure below. (same happens for cubes)
If you are interested in drawing voxels, I generally would recommend you to use vol3D v2 from Matlab's FEX. Why that instead of your own?
Because the best (only?) way of plotting voxels is actually plotting flat square surfaces, 6 for each cube (see answer here for function that does that). This flat surfaces will also create some artifacts for something called z-fighting in computer graphics. vol3D actually only plots 3 surfaces, the ones looking at you, saving half of the computational time, and avoiding ugly plotting artifacts. It is easy to use, you can define colors per voxel and also the alpha (transparency) of each of them, allowing you to see inside.
Example of use:
% numbers are arbitrary
cube=zeros(11,11,11);
cube(3:9,3:9,3:9)=5; % Create a cube inside the region
% Boring: faces of the cube are a different color.
cube(3:9,3:9,3)=2;
cube(3:9,3:9,9)=2;
cube(3:9,3,3:9)=2;
cube(3:9,9,3:9)=2;
cube(3,3:9,3:9)=2;
cube(9,3:9,3:9)=2;
vold3d('Cdata',cube,'alpha',cube/5)
But yeah, that still looks bad. Because if you want to see the inside, voxel plotting is not the best option. Alphas of different faces stack one on top of the other and the only way of solving this is writing advanced computer graphics ray tracing algorithms, and trust me, that's a long and tough road to take.
Very often one has 4D data, thus data that contains 3D location and a single data for each of the locations. One may think that in this case, you really want voxels, as each of them have a 3D +color, 4D data. Indeed! you can do it with voxels, but sometimes its better to describe it in some other ways. As an example, lets see this person who wanted to highlight a region in his/hers 4D space (link). To see a bigger list I suggest you look at my answer in here about 4D visualization techniques.
Lets try wits a different approach than the voxel one. Lets use the previous cube and create isosurfaces whenever the 4D data changes of value.
iso1=isosurface(cube,1);
iso2=isosurface(cube,4);
p1=patch(iso1,'facecolor','r','facealpha',0.3,'linestyle','none');
p2=patch(iso2,'facecolor','g','facealpha',1,'linestyle','none');
% below here is code for it to look "fancy"
isonormals(cube,p1)
view(3);
axis tight
axis equal
axis off
camlight
lighting gouraud
And this one looks way better, in my opinion.
Choose freely and good plotting!

how can i trace the finger movement on touch for drawing smooth curves?

like what i want is if i move my finger fast on the iphone screen , then i want like something that it make a proper curve using quartz 2d or opengl es whatever.
i want to draw a path in curve style......
i had seen that GLPaint(OpenglES) example ,but it will not help me alot , considering if your finger movement is fast.....
something like making a smooth curve.....
any one have some kind of example please tell me
thanks
Edit: Moved from answer below:
thanks to all.......
but i had tried the bezier curve algo with two control points but problem is first how to calculate the control points whether there is no predefined points....
as i mentioned my movement of finger is fast...... so most of the time i got straight line instead of curve, due to getting less number of touch points.......
now as mark said piecewise fashion, ihad tried it like considering first four touch points and render them on screen , then remove the first point then again go for next four points ex. step 1: 1,2,3,4 step 2: 2,3,4,5 like that where as in that approach i got an overlap , which is not the issue actually , but didn't get smooth curve........
but for fast movement of finger i have to find something else?????
Depending on the number of sample points you are looking at, there are two approaches that I would recommend:
Simple Interpolation
You can simply sample the finger location at set intervals and then interpolate the sample points using something like a Catmull-Rom spline. This is easier than it sounds since you can easily convert a Catmull-Rom spline into a series of cubic Bezier curves.
Here's how. Say you have four consecutive sample points P0, P1, P2 and P3, the cubic Bezier curve that connects P1 to P2 is defined by the following control points:
B0 = P1
B1 = P1 + (P2 - P0)/6
B3 = P2 + (P1 - P3)/6
B4 = P2
This should work well as long as your sample points aren't too dense and it's super easy. The only problem might be at the beginning and end of your samples since the first and last sample point aren't interpolated in an open curve. One common work-around is to double-up your first and last sample point so that you have enough points for the curve to pass through each of the original samples.
To get an idea of how Catmull-Rom curves look, you can try out this Java applet demonstrating Catmull-Rom splines.
Fit a curve to your samples
A more advance (and more difficult) approach would be to do a Least Squares approximation to your sample points. If you want try this, the procedure looks something like the following:
Collect sample points
Define a NURBS curve (including its knot vector)
Set up a system of linear equations for the samples & curve
Solve the system in the Least Squares sense
Assuming you can pick a reasonable NURBS knot vector, this will give you a NURBS curve that closely approximates your sample points, minimizing the squared distance between the samples and your curve. The NURBS curve can even be decomposed into a series of Bezier curves if needed.
If you decide to explore this approach, then the book "Curves and Surfaces for CAGD" by Gerald Farin, or a similar reference, would be very helpful. In the 5th edition of Farin's book, section 9.2 deals specifically with this problem. Section 7.8 shows how to do this with a Bezier curve, but you'd probably need a high-degree curve to get a good fit.
Naaff gives a great overview of the NURBS technique. Unfortunately, I think generating a smooth bezier on-the-fly might be too much for the iPhone. I write drawing apps, and getting a large number of touchesMoved events per second is quite a challenge to begin with. You really need to optimize your drawing code just to get good performance while recording individual points - much less constructing a bezier path.
If you end up going with a bezier or NURBS curve representation - you'll probably have to wait until the user has finished touching the screen to compute the smoothed path. Doing the math continuously as the user moves their finger (and then redrawing the entire recomputed path using Quartz) is not going to give you a high enough data collection rate to do anything useful...
Good luck!
Do something like Shadow suggested. Get the position of the touch with some frequency and then make a Bézier curve out of it. This is how paths are drawn with a mouse (or tablet) in programs like Illustrator.