Quantcast
Channel: Eric Cox » Uncategorized
Viewing all articles
Browse latest Browse all 13

Joystick Matlab Visualizer

$
0
0

This is a simple script I wrote to visualize a joystick inputs into Matlab. Not a big deal, but maybe helpful to someone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
% This script plots the joystick orientation in 3D-space
% Requires a joystick
% Created March 2014 by Eric Cox
% http://ericcox.me

% NOTE: To stop the program, hit ctrl+c.
% Otherwise, it will run indefinitely.

% Define joystick ID (for only 1 joystick, this is usually '1')
ID = 1
% Create joystick variable
joy=vrjoystick(ID);

% Select the number of time steps to rememeber
% (i.e., trailing tail length)
n=100;

% Preallocate vectors
kz=zeros(1,n);
ky=zeros(1,n);
kx=zeros(1,n);

% loop until ctrl+c
while true
Y=axis(joy, 1); % X-axis is joystick axis 1
X=axis(joy, 2); % Y-axis is joystick axis 2
y=-sin(Y*pi/6); % Correlate axis to angular orientation of joystick
x=-sin(X*pi/6); % See above.
z=sqrt(1-x^2-y^2); % Vertical height of joystick by Pythagoras

% Define vector representing current joystick orientation
z_v = [0 1]*z;
y_v = [0 1]*y;
x_v = [0 1]*x;

% Define vectors representing joystick orientation history
kz(end+1)=z;
kz=kz(2:n+1);
ky(end+1)=y;
ky=ky(2:n+1);
kx(end+1)=x;
kx=kx(2:n+1);

% Plot marker to define end of joystick
plot3(x,y,z,'ro','MarkerSize',10,'MarkerFaceColor','r')
hold on

% Plot current orientation of joystick
plot3(x_v,y_v,z_v,'-r','LineWidth',3)
hold on

% Plot Y-reference vector (it can be hard to tell where things are
% in 3D plots)
plot3([.5 -.5],[y y],[z z],'--k')
hold on

% Plot X-reference vector
plot3([x x],[.5 -.5],[z z],'--k')
hold on

% Plot historical data (if n>0)
plot3(kx,ky,kz,':r');
hold on

% Plot box around plot perimeter for altitude reference
plot3([-.5 .5 .5 -.5 -.5],[-.5 -.5 .5 .5 -.5],[z z z z z],'-k')
hold off

% define plot axes
axis([-.5 .5 -.5 .5 0 1])

% force the plot
drawnow;

% delay between plots
pause(.05)
end

Viewing all articles
Browse latest Browse all 13

Latest Images

Trending Articles





Latest Images