HapticLib  0.7
Haptic Feedback Library for embedded systems
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
impact.m
Go to the documentation of this file.
1 %%
2  % Copyright (c) 2012, Silvio Vallorani <silvio.vallorani@studio.unibo.it>
3  %
4  % Permission to use, copy, modify, and/or distribute this
5  % software for any purpose with or without fee is hereby
6  % granted, provided that the above copyright notice and this
7  % permission notice appear in all copies.
8  %
9  % THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
10  % ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
11  % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
12  % EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13  % INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  % WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
15  % WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  % TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
17  % USE OR PERFORMANCE OF THIS SOFTWARE.
18  %
19  %%
20 %%
21 % @file impact.m
22 %
23 % Here 256 samples for impactPatterns are generated accordingly to
24 %
25 % Taku Hachisu et al. (2011)
26 % Pseudo-Haptic Feedback Augmented with Visual and Tactile Vibrations
27 %
28 % Q(t) = A(v) * e^(-B*t) * sin(2*pi*f*t) (1)
29 %
30 % where Amplitude is function of velocity but quasi-linearity implies
31 % we can semplify in
32 %
33 % Q(t) = A * v * e^(-B*t) * sin(2*pi*f*t)
34 %
35 % Use this formula as Haptic Pattern Sample Generator needs some other
36 % manipulations:
37 %
38 % PS(t) = abs(A) * v * [ 2*e^(-B*t) * (1 + sin(2*pi*f*t) ]
39 %
40 % This approch ensures non negative samples mainteining continuity.
41 % Samples are also scaled to remain in (1/3) * PMN_LEVELS range (slow).
42 %
43 % @author Silvio Vallorani
44 % @version v0.1
45 % @date 2012
46 %
47 %%
48 
49 close all; clear all; clc;
50 
51 % Generic Parameters
52 
53 PWM_LEVELS = 65000; % number of PWM levels that can be used in HapticLib
54 
55 t = ( 0 : 0.00035 : 0.1 ); % ensure impact transitory estinguish
56  % in 256 samples
57 
58 V1 = 1; V2 = 2; V3 = 3; % Three different impact velocity magnitude are
59  % defined.
60  % V1 => SLOW
61  % V2 => NORMALW
62  % V3 => FAST
63  %
64  % Samples must be generated using V1.
65 
66 
67 %% ALUMINUM
68 
69 A_Aluminum = - 300; A_Aluminum = abs ( A_Aluminum );
70 B_Aluminum = 90;
71 F_Aluminum = 300;
72 
73 S_Aluminum = 1 + sin( 2 * pi * F_Aluminum * t );
74 E_Aluminum = 2 * exp( - B_Aluminum * t );
75 
76 H_Aluminum = A_Aluminum * S_Aluminum .* E_Aluminum;
77 SCALE_FACTOR = ceil( max( H_Aluminum ) );
78 H_Aluminum = floor ( H_Aluminum / SCALE_FACTOR * (PWM_LEVELS/3) );
79 H_Aluminum_V1 = V1 * H_Aluminum;
80 H_Aluminum_V2 = V2 * H_Aluminum;
81 H_Aluminum_V3 = V3 * H_Aluminum;
82 
83 subplot( 3, 3, 7 );
84 plot( t, H_Aluminum_V1 ), title( 'ALUMINUM @ SLOW' ), axis( [0 t(256) 0 PWM_LEVELS ] )
85 subplot( 3, 3, 8 );
86 plot( t, H_Aluminum_V2 ), title( 'ALUMINUM @ NORMAL' ), axis( [0 t(256) 0 PWM_LEVELS ] )
87 subplot( 3, 3, 9 );
88 plot( t, H_Aluminum_V3 ), title( 'ALUMINUM @ FAST' ), axis( [0 t(256) 0 PWM_LEVELS ] )
89 
90 %% RUBBER
91 
92 A_Rubber = - 240; A_Rubber = abs ( A_Rubber );
93 B_Rubber = 60;
94 F_Rubber = 30;
95 
96 S_Rubber = 1 + sin( 2 * pi * F_Rubber * t );
97 E_Rubber = 2 * exp( - B_Rubber * t);
98 
99 H_Rubber = A_Rubber * S_Rubber .* E_Rubber;
100 H_Rubber = floor ( H_Rubber / SCALE_FACTOR * (PWM_LEVELS/3) );
101 H_Rubber_V1 = V1 * H_Rubber;
102 H_Rubber_V2 = V2 * H_Rubber;
103 H_Rubber_V3 = V3 * H_Rubber;
104 
105 subplot( 3, 3, 1 );
106 plot( t, H_Rubber_V1 ), title( 'RUBBER @ SLOW' ), axis( [0 t(256) 0 PWM_LEVELS ] )
107 subplot( 3, 3, 2 );
108 plot( t, H_Rubber_V2 ), title( 'RUBBER @ NORMAL' ), axis( [0 t(256) 0 PWM_LEVELS ] )
109 subplot( 3, 3, 3 );
110 plot( t, H_Rubber_V3 ), title( 'RUBBER @ FAST' ), axis( [0 t(256) 0 PWM_LEVELS ] )
111 
112 %% WOOD
113 
114 A_Wood = - 150; A_Wood = abs ( A_Wood );
115 B_Wood = 80;
116 F_Wood = 100;
117 
118 S_Wood = 1 + sin( 2 * pi * F_Wood * t );
119 E_Wood = 2 * exp( - B_Wood * t );
120 
121 H_Wood = A_Wood * S_Wood .* E_Wood;
122 H_Wood = floor ( H_Wood / SCALE_FACTOR * (PWM_LEVELS/3) );
123 H_Wood_V1 = V1 * H_Wood;
124 H_Wood_V2 = V2 * H_Wood;
125 H_Wood_V3 = V3 * H_Wood;
126 
127 subplot( 3, 3, 4 );
128 plot( t, H_Wood_V1 ), title( 'WOOD @ SLOW' ), axis( [0 t(256) 0 PWM_LEVELS ] )
129 subplot( 3, 3, 5 );
130 plot( t, H_Wood_V2 ), title( 'WOOD @ NORMAL' ), axis( [0 t(256) 0 PWM_LEVELS ] )
131 subplot( 3, 3, 6 );
132 plot( t, H_Wood_V3 ), title( 'WOOD @ FAST' ), axis( [0 t(256) 0 PWM_LEVELS ] )
133 
134 % WRITE RESULTS ON TXT FILE
135 
136 fileID = fopen('impact.txt', 'w', 'n');
137 formatPattern = '%5d,\t%5d,\t%5d,\t%5d,\t%5d,\t%5d,\t%5d,\t%5d,\t%5d,\t%5d,\t\t\\\n';
138 
139 fprintf(fileID, 'const uint8_t rubberImpactPattern[256] = {\t\t\t\t\t\t\t\t\t\t\t\\\n');
140 fprintf(fileID, formatPattern, H_Rubber_V1(1:255));
141 fprintf(fileID, '%5d\t\t\t\t\t\t\t\t\t\t};\n\n',0);
142 
143 fprintf(fileID, 'const uint8_t woodImpactPattern[256] = {\t\t\t\t\t\t\t\t\t\t\t\\\n');
144 fprintf(fileID, formatPattern, H_Wood_V1(1:255));
145 fprintf(fileID, '%5d\t\t\t\t\t\t\t\t\t\t};\n\n',0);
146 
147 fprintf(fileID, 'const uint8_t aluminumImpactPattern[256] = {\t\t\t\t\t\t\t\t\t\t\\\n');
148 fprintf(fileID, formatPattern, H_Aluminum_V1(1:255));
149 fprintf(fileID, '%5d\t\t\t\t\t\t\t\t\t\t};\n\n',0);
150 fclose(fileID);
151 
152 fprintf('\nText file with Samples generated.\n')
153 
154 %% PRINT THE MAX
155 fprintf('\nBiggest Sample values exracted from FAST Patterns\n\n')
156 fprintf('Biggest Rubber Sample: %5d\n',max(H_Rubber_V3))
157 fprintf('Biggest Wood Sample: %5d\n',max(H_Wood_V3))
158 fprintf('Biggest Aluminum Sample: %5d\n',max(H_Aluminum_V3))
159 fprintf('\n\n')