HapticLib  0.7
Haptic Feedback Library for embedded systems
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
impact.c
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  */
139 #include "hapticLib.h"
140 
152 const uint16_t rubberImpactPattern[256] = { \
153  9369, 9779, 10165, 10527, 10861, 11167, 11445, 11692, 11909, 12095, \
154 12249, 12372, 12463, 12524, 12553, 12552, 12521, 12462, 12374, 12259, \
155 12118, 11953, 11763, 11552, 11319, 11067, 10798, 10511, 10210, 9896, \
156  9569, 9233, 8887, 8535, 8177, 7815, 7450, 7084, 6719, 6354, \
157  5993, 5635, 5283, 4937, 4598, 4267, 3946, 3634, 3333, 3043, \
158  2765, 2500, 2247, 2008, 1782, 1570, 1372, 1188, 1018, 862, \
159  720, 592, 477, 375, 287, 211, 148, 97, 57, 28, \
160  9, 0, 1, 10, 28, 53, 85, 124, 168, 217, \
161  271, 329, 390, 455, 521, 589, 658, 727, 797, 867, \
162  935, 1003, 1069, 1133, 1195, 1254, 1310, 1363, 1413, 1459, \
163  1501, 1540, 1574, 1605, 1631, 1653, 1670, 1684, 1693, 1698, \
164  1699, 1696, 1688, 1677, 1663, 1644, 1623, 1598, 1570, 1539, \
165  1506, 1470, 1432, 1391, 1349, 1305, 1260, 1214, 1166, 1118, \
166  1069, 1020, 970, 921, 871, 822, 774, 726, 679, 633, \
167  588, 544, 501, 460, 421, 383, 346, 312, 279, 248, \
168  219, 191, 166, 143, 121, 101, 84, 68, 53, 41, \
169  30, 22, 14, 8, 4, 1, 0, 0, 1, 3, \
170  6, 10, 15, 21, 27, 34, 42, 50, 59, 68, \
171  77, 86, 96, 105, 115, 124, 133, 142, 151, 159, \
172  167, 175, 182, 189, 196, 201, 207, 212, 216, 219, \
173  223, 225, 227, 228, 229, 229, 229, 228, 227, 225, \
174  223, 220, 217, 213, 209, 204, 200, 195, 189, 184, \
175  178, 172, 165, 159, 152, 146, 139, 132, 126, 119, \
176  112, 106, 99, 93, 87, 81, 75, 69, 63, 58, \
177  53, 48, 43, 38, 34, 30, 26, 23, 20, 17, \
178  14, 11, 9, 7, 6, 0 };
179 
191 const uint16_t woodImpactPattern[256] = { \
192  5855, 6936, 7894, 8683, 9269, 9626, 9745, 9624, 9278, 8728, \
193  8006, 7149, 6200, 5204, 4205, 3245, 2364, 1593, 958, 479, \
194  163, 14, 24, 181, 465, 851, 1312, 1818, 2338, 2844, \
195  3309, 3709, 4026, 4246, 4361, 4368, 4269, 4073, 3791, 3438, \
196  3033, 2595, 2145, 1701, 1283, 906, 585, 329, 145, 35, \
197  0, 33, 129, 278, 468, 685, 917, 1149, 1370, 1568, \
198  1732, 1857, 1936, 1967, 1949, 1885, 1780, 1639, 1469, 1280, \
199  1079, 877, 682, 501, 342, 210, 108, 40, 5, 2, \
200  30, 84, 159, 251, 352, 457, 560, 655, 738, 804, \
201  852, 878, 883, 866, 829, 774, 705, 624, 536, 446, \
202  356, 270, 193, 126, 73, 33, 9, 0, 5, 22, \
203  51, 88, 131, 178, 225, 270, 311, 345, 371, 389, \
204  396, 394, 383, 362, 335, 301, 264, 223, 182, 143, \
205  106, 73, 45, 24, 9, 1, 0, 5, 15, 29, \
206  47, 68, 89, 110, 129, 146, 160, 170, 176, 178, \
207  175, 168, 158, 144, 128, 110, 92, 74, 57, 41, \
208  27, 16, 7, 2, 0, 0, 3, 9, 16, 25, \
209  34, 44, 53, 61, 68, 74, 78, 80, 79, 77, \
210  73, 68, 61, 54, 46, 38, 30, 22, 15, 9, \
211  5, 2, 0, 0, 0, 2, 5, 9, 13, 17, \
212  21, 25, 29, 32, 34, 35, 36, 35, 34, 32, \
213  29, 26, 22, 19, 15, 12, 8, 5, 3, 1, \
214  0, 0, 0, 0, 1, 3, 4, 6, 8, 10, \
215  12, 13, 14, 15, 16, 16, 15, 15, 14, 12, \
216  11, 9, 7, 6, 4, 3, 2, 1, 0, 0, \
217  0, 0, 0, 1, 1, 0 };
218 
231 const uint16_t aluminumImpactPattern[256] = { \
232 11711, 18304, 21647, 20434, 15299, 8439, 2627, 41, 1417, 5832, \
233 11188, 15131, 16034, 13609, 8947, 3986, 673, 165, 2408, 6234, \
234  9903, 11848, 11302, 8563, 4809, 1560, 40, 696, 3063, 6008, \
235  8234, 8819, 7567, 5045, 2304, 423, 66, 1236, 3315, 5355, \
236  6481, 6247, 4790, 2738, 923, 34, 340, 1606, 3224, 4478, \
237  4848, 4205, 2842, 1329, 264, 25, 633, 1761, 2894, 3543, \
238  3452, 2678, 1556, 545, 27, 164, 841, 1728, 2434, 2664, \
239  2335, 1600, 766, 163, 8, 323, 934, 1562, 1936, 1906, \
240  1496, 884, 320, 20, 79, 439, 926, 1322, 1463, 1296, \
241  900, 440, 100, 2, 164, 495, 843, 1057, 1052, 835, \
242  501, 188, 14, 37, 229, 495, 718, 803, 719, 506, \
243  253, 61, 0, 83, 262, 454, 577, 580, 466, 284, \
244  110, 10, 17, 119, 265, 389, 440, 398, 284, 145, \
245  37, 0, 42, 138, 245, 315, 320, 259, 161, 64, \
246  6, 8, 62, 141, 211, 241, 221, 159, 83, 22, \
247  0, 21, 73, 132, 171, 176, 144, 91, 37, 4, \
248  3, 32, 75, 114, 132, 122, 89, 47, 13, 0, \
249  10, 38, 71, 93, 97, 80, 51, 21, 3, 1, \
250  16, 40, 62, 72, 67, 50, 27, 8, 0, 5, \
251  20, 38, 51, 53, 44, 29, 12, 1, 0, 8, \
252  21, 33, 39, 37, 28, 15, 4, 0, 2, 10, \
253  20, 27, 29, 24, 16, 7, 1, 0, 4, 11, \
254  18, 21, 20, 15, 8, 2, 0, 1, 5, 11, \
255  15, 16, 13, 9, 4, 0, 0, 2, 6, 9, \
256  11, 11, 8, 5, 1, 0, 0, 2, 5, 8, \
257  8, 7, 5, 2, 0, 0 };
258 
273 {
274 
275  /*
276  * Create a handy reference for userParameters and statusParameters
277  * Note:
278  * - userParams data reside on the user program module. (main.c)
279  * - statusParams data reside on SystemDesc variable.
280  */
281  impactUserParameters *userParams = &pattern->userParams->impact;
282  impactStatusParameters *statusParams = &pattern->statusParams.impact;
283 
284  uint16_t j = statusParams->progress++;
285 
286  switch (userParams->material)
287  {
288  case Rubber:
289  #ifdef HL_DEBUG
290  if( j == 0 )
291  {
292  send_string("Rubber Impact Pattern at v = \0");
293  send_int( userParams->velocity );
294  send_string("\r\n\0");
295  send_string("\r\nRubber Pattern: \r\n\r\n\0");
296  }
297  #endif
299  pattern->activeHaptorList->id );
300  #ifdef HL_DEBUG
301  if ( j<10 || j>245 ) {
302  send_string("Sample number \0");
303  send_int( j+1 );
304  send_string(": \0");
305  send_int( userParams->velocity * aluminumImpactPattern[j] );
306  send_string("\r\n\0");
307  }
308  if( j == 0 )
309  {
310  send_string("\r\n\0");
311  }
312  #endif
313  break;
314  case Wood:
315  #ifdef HL_DEBUG
316  if( j == 0 )
317  {
318  send_string("Wood Impact Pattern at v = \0");
319  send_int( userParams->velocity );
320  send_string("\r\n\0");
321  send_string("\r\nWood Pattern: \r\n\r\n\0");
322  }
323  #endif
325  pattern->activeHaptorList->id );
326  #ifdef HL_DEBUG
327  if ( j<10 || j>245 ) {
328  send_string("Sample number \0");
329  send_int( j+1 );
330  send_string(": \0");
331  send_int( userParams->velocity * aluminumImpactPattern[j] );
332  send_string("\r\n\0");
333  }
334  if( j == 0 )
335  {
336  send_string("\r\n\0");
337  }
338  #endif
339  break;
340  case Aluminum:
341  #ifdef HL_DEBUG
342  if( j == 0 )
343  {
344  send_string("Aluminum Impact Pattern at v = \0");
345  send_int( userParams->velocity );
346  send_string("\r\n\0");
347  send_string("\r\nAluminum Pattern: \r\n\r\n\0");
348  }
349  #endif
351  pattern->activeHaptorList->id );
352  #ifdef HL_DEBUG
353  if ( j<10 || j>245 ) {
354  send_string("Sample number \0");
355  send_int( j+1 );
356  send_string(": \0");
357  send_int( userParams->velocity * aluminumImpactPattern[j] );
358  send_string("\r\n\0");
359  }
360  if( j == 0 )
361  {
362  send_string("\r\n\0");
363  }
364  #endif
365  break;
366  default :
367  #ifdef HL_DEBUG
368  send_string("ERROR: Impact pattern unknown...\r\n\0");
369  #endif
370  break;
371  }
372 
373  if( j == 255 )
374  {
375  // Ensure finish without vibrations
377 
378  // Removing pattern from scheduling
379  // and freeing all the haptors
380 
381  cleanList(pattern->activeHaptorList);
382  pattern->name = Null;
383  pattern->userParams = NULL;
384  pattern->continuator = NULL;
385  pattern->activeHaptorList = NULL;
386 
387  return 0;
388  }
389  return 0;
390 }
391 
432 {
433 
434  /*
435  * Create a handy reference for userParameters and statusParameters
436  * Note:
437  * - userParams data reside on the user program module. (main.c)
438  * - statusParams data reside on SystemDesc variable.
439  */
440  impactUserParameters *userParams = &pattern->userParams->impact;
441  impactStatusParameters *statusParams = &pattern->statusParams.impact;
442 
443 
444  /* User Parameters Validation */
445  if( userParams->velocity > 3 ) {
446 #ifdef HL_DEBUG
447  send_string("ERROR: Velocity must be 1, 2, 3\r\n\0");
448 #endif
449  return 1;
450  }
451 
452  /* Status initialization */
453  statusParams->progress = 0;
454 
455 
456  /* Ready to schedule the pattern */
457  pattern->continuator = &impactContinuator;
458 
459  return 0;
460 }