{"id":1266,"date":"2011-12-15T19:04:35","date_gmt":"2011-12-15T18:04:35","guid":{"rendered":"http:\/\/blogs.ukoln.ac.uk\/ukolndev\/?p=1266"},"modified":"2011-12-15T19:04:35","modified_gmt":"2011-12-15T18:04:35","slug":"detecting-motion-dlp-tilt-2-axis-accelerometer","status":"publish","type":"post","link":"https:\/\/www.emmatonkin.com\/ukolndev\/2011\/12\/15\/detecting-motion-dlp-tilt-2-axis-accelerometer\/","title":{"rendered":"Detecting motion: DLP-TILT 2-axis accelerometer"},"content":{"rendered":"<h2>Using an accelerometer to assess musculoskeletal stresses<\/h2>\n<p>Accelerometers are helpful for characterising device usage and detecting the mode of use. Most of these devices have an internal accelerometer, but it is convenient for our purposes to use an external device (requires less programming and does not require a jailbroken device).<\/p>\n<h2>Setup<\/h2>\n<p>In order to use this device, you must first set up the serial port with a line like the following:<\/p>\n<pre>stty -F \/dev\/ttyUSB0 raw ispeed 38400 ospeed 38400 cs8 -ignpar -cstopb -echo<\/pre>\n<h2>Code<\/h2>\n<p>The code probably won&#8217;t tell you all that much, but here&#8217;s one way to make your life much easier: assuming you&#8217;re running this on Linux, set your device ID as \/dev\/dlptilt, then change the line &#8216;\/dev\/ttyUSB0&#8217; to read &#8216;\/dev\/dlptilt&#8217; in the code before compiling.<\/p>\n<pre>#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include \n\n#define BAUDRATE B38400\n#define MODEMDEVICE \"\/dev\/ttyS1\"\n#define _POSIX_SOURCE 1         \/\/POSIX compliant source\n#define FALSE 0\n#define TRUE 1\n#define DEBUG 0\n\nvolatile int STOP=FALSE;\n\nvoid signal_handler_IO (int status);    \/\/definition of signal handler\nint wait_flag=TRUE;                     \/\/TRUE while no signal received\nchar devicename[80];\nlong Baud_Rate = 38400;         \/\/ default Baud Rate (110 through 38400)\nlong BAUD;                      \/\/ derived baud rate from command line\nlong DATABITS;\nlong STOPBITS;\nlong PARITYON;\nlong PARITY;\nint Data_Bits = 8;              \/\/ Number of data bits\nint Stop_Bits = 1;              \/\/ Number of stop bits\nint Parity = 0;                 \/\/ Parity as follows:\n                  \/\/ 00 = NONE, 01 = Odd, 02 = Even, 03 = Mark, 04 = Space\nint Format = 4;\nFILE *input;\nFILE *output;\nint status;\n\nmain(int Parm_Count, char *Parms[])\n{\n\n   int fd, tty, c, res, i, error;\n   char In1, Key;\n   struct termios oldtio, newtio;\n   struct termios oldkey, newkey;\n   struct sigaction saio;\n   char buf[255];\n   char message[90];\n\n   \/\/ set device name here\n   strcpy(devicename,\"\/dev\/ttyUSB0\"); \n\n   newkey.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;\n   newkey.c_iflag = IGNPAR;\n   newkey.c_oflag = 0;\n   newkey.c_lflag = 0;\n   newkey.c_cc[VMIN]=1;\n   newkey.c_cc[VTIME]=0;\n   BAUD=38400;\n   DATABITS=CS8;\n   STOPBITS=CSTOPB;\n   PARITYON=0;\n   PARITY=0;\n   fd = open(devicename, O_RDWR | O_NOCTTY | O_NONBLOCK);\n   if (fd &lt; 0)\n   {\n   \tperror(devicename);\n   \texit(-1);\n   }\n\n   saio.sa_handler = signal_handler_IO;\n   sigemptyset(&amp;saio.sa_mask);\n   saio.sa_flags = 0;\n   saio.sa_restorer = NULL;\n   sigaction(SIGIO,&amp;saio,NULL);\n\n   fcntl(fd, F_SETOWN, getpid());\n   fcntl(fd, F_SETFL, FASYNC);\n\n   tcgetattr(fd,&amp;oldtio);\n   newtio.c_cflag = BAUD | CRTSCTS | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;\n   newtio.c_iflag = IGNPAR;\n   newtio.c_oflag = 0;\n   newtio.c_lflag = 0;\n   newtio.c_cc[VMIN]=1;\n   newtio.c_cc[VTIME]=0;\n   tcflush(fd, TCIFLUSH);\n   tcsetattr(fd,TCSANOW,&amp;newtio);\n   write(fd,\"T\",1);\n   while (STOP==FALSE) {\n       status=1;\n       if (status==1) \/\/ so redundant...\n\t\t{\n\tif(DEBUG){\n   \t\twrite(fd,\"P\",1);\n\t\tprintf(\"Printed Pn\");\n\t} else {\n\t\t\/\/ 7 - single a d conversion to host on current settings\n\t\t\/\/ z 3 bytes (both channels x and y of accelerometer)\n\t\t\/\/ s - 7 bytes (all 7 channels)\n\t\t\/\/ 8 - streaming a\/d conversion data using current settings\n   \t\twrite(fd,\"z\",1);\n\t}\n\t} \n\n         if (wait_flag==FALSE)  \/\/if output is available\n\t{\n\t\tres = read(fd,buf,255);\n\t\tif (res&gt;0)\n\t\t{\n\n\t\t\t\/\/ Print serial output\n\t\t\tfor (i=0; i<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using an accelerometer to assess musculoskeletal stresses Accelerometers are helpful for characterising device usage and detecting the mode of use. Most of these devices have an internal accelerometer, but it is convenient for our purposes to use an external device (requires less programming and does not require a jailbroken device). Setup In order to use [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1393,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19,2,21,4,9,14,17],"tags":[175,31,167,40,41,45,78],"_links":{"self":[{"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/posts\/1266"}],"collection":[{"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/comments?post=1266"}],"version-history":[{"count":0,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/posts\/1266\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/media\/1393"}],"wp:attachment":[{"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/media?parent=1266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/categories?post=1266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.emmatonkin.com\/ukolndev\/wp-json\/wp\/v2\/tags?post=1266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}