Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. //
  2. // This code has been transcribed from
  3. // http://elinux.org/Interfacing_with_I2C_Devices
  4. // for TRC3000, S2 2016
  5. //
  6.  
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <linux/i2c-dev.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <math.h>
  17. #include <time.h>
  18. #include "opencv2/highgui/highgui.hpp"
  19. #include "opencv2/imgproc/imgproc.hpp"
  20.  
  21. #define PI 3.1415926
  22.  
  23.  
  24.  
  25. int main(int argc, char** argv ) {
  26. int file;
  27. char filename[40];
  28. unsigned char mode_change_a[2] = {0x00, 0x00}; // buffer for reg addr/value
  29. unsigned char mode_change_b[2] = {0x00, 0x00}; // buffer for reg addr/value
  30. unsigned char mode_change_c[2] = {0x00, 0x00}; // buffer for reg addr/value
  31. unsigned char mode_change_d[2] = {0x00, 0x00}; // buffer for reg addr/value
  32. unsigned char read_buffer[7] = {0x00};
  33. unsigned char start_addr = 0x28;//0x28
  34.  
  35.  
  36. int addr = 0x1E; // The I2C address of the LSM6DS33
  37. int idx;
  38. short Ax, Ay, Az;
  39.  
  40. sprintf(filename,"/dev/i2c-1"); // using i2c-1 bus
  41. if ((file = open(filename,O_RDWR)) < 0) {
  42. printf("Failed to open the bus.");
  43. /* ERROR HANDLING; you can check errno to see what went wrong */
  44. exit(1);
  45. }
  46. else
  47. printf("%s is now open successfully\n", filename);
  48.  
  49. if (ioctl(file,I2C_SLAVE,addr) < 0) {
  50. printf("Failed to acquire bus access and/or talk to slave.\n");
  51. /* ERROR HANDLING; you can check errno to see what went wrong */
  52. exit(1);
  53. }
  54. else
  55. printf("I2C bus access to LSM6DS33 is acquired\n");
  56.  
  57. mode_change_a[0] = 0x20; // CTRL1_XL address
  58. mode_change_a[1] = 0x70; // Reg value for 1.66KHz sampling
  59. mode_change_b[0] = 0x21; // CTRL1_XL address
  60. mode_change_b[1] = 0x00; // Reg value for 1.66KHz sampling
  61. mode_change_c[0] = 0x22; // CTRL1_XL address
  62. mode_change_c[1] = 0x00; // Reg value for 1.66KHz sampling
  63. mode_change_d[0] = 0x23; // CTRL1_XL address
  64. mode_change_d[1] = 0x0C; // Reg value for 1.66KHz sampling
  65.  
  66. if (write(file,mode_change_a,2) != 2) {
  67. /* ERROR HANDLING: i2c transaction failed */
  68. printf("Failed to mode change.\n");
  69. exit(1);
  70. }
  71. else if (write(file,mode_change_b,2) != 2) {
  72. /* ERROR HANDLING: i2c transaction failed */
  73. printf("Failed to mode change.\n");
  74. exit(1);
  75. }
  76. else if (write(file,mode_change_c,2) != 2) {
  77. /* ERROR HANDLING: i2c transaction failed */
  78. printf("Failed to mode change.\n");
  79. exit(1);
  80. }
  81. else if (write(file,mode_change_d,2) != 2) {
  82. /* ERROR HANDLING: i2c transaction failed */
  83. printf("Failed to mode change.\n");
  84. exit(1);
  85. }
  86. else
  87. printf("Accelerometer enabled at 1.66KHz mode.\n");
  88.  
  89. usleep(100000); // waiting for the sensor to boot up
  90.  
  91. while (1)
  92. {
  93.  
  94. if (write(file,&start_addr,1) != 1) {
  95. /* ERROR HANDLING: i2c transaction failed */
  96. printf("Failed to change address pointer.\n");
  97. exit(1);
  98. }
  99.  
  100. if (read(file, read_buffer, 6) !=6 )
  101. {
  102. printf("Something's wrong, received data length is not 6\n");
  103. }
  104. else
  105. {
  106. // Assemble data into short integer
  107. Ax = (short)(read_buffer[1] << 8 | read_buffer[0]);
  108. Ay = (short)(read_buffer[3] << 8 | read_buffer[2]);
  109. Az = (short)(read_buffer[5] << 8 | read_buffer[4]);
  110. double myNum = 57.29578*atan(((float)Ax)/((float)Ay));
  111. printf("%6d %6d %8d %6.3f\n", Ax, Ay, Az, myNum);
  112.  
  113. }
  114.  
  115. usleep(200); // optional delay
  116.  
  117. } // end of for
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement