sensors.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <stdio.h>
  2. #include <sys/time.h>
  3. #include <webots/supervisor.h>
  4. #include "sensors.h"
  5. #define M_PI 3.14159265358979323846
  6. #define M_PI2 6.28318530718
  7. /*
  8. https://discuss.ardupilot.org/t/copter-x-y-z-which-is-which/6823/2
  9. Copy pasted what’s important:
  10. NED Coordinate System:
  11. The x axis is aligned with the vector to the north pole (tangent to meridians).
  12. The y axis points to the east side (tangent to parallels)
  13. The z axis points to the center of the earth
  14. There is also Body Fixed Frame:
  15. Body Fixed Frame (Attached to the aircraft)
  16. The x axis points in forward (defined by geometry and not by movement) direction. (= roll axis)
  17. The y axis points to the right (geometrically) (= pitch axis)
  18. The z axis points downwards (geometrically) (= yaw axis)
  19. In order to convert from Body Frame to NED what you need to call this function:
  20. copter.rotate_body_frame_to_NE(vel_vector.x, vel_vector.y);
  21. */
  22. /*
  23. returns: "yaw":_6.594794831471518e-05,"pitch":_-0.0005172680830582976,"roll":_0.022908752784132957}}
  24. */
  25. void getInertia (const WbDeviceTag inertialUnit, const double *northDirection, char *buf)
  26. {
  27. const double *inertial_directions = wb_inertial_unit_get_roll_pitch_yaw (inertialUnit);
  28. if (northDirection[0] == 1)
  29. {
  30. sprintf(buf,"\"roll\": %f,\"pitch\": %f,\"yaw\": %f",inertial_directions[0], inertial_directions[1], inertial_directions[2]);
  31. }
  32. else
  33. {
  34. sprintf(buf,"\"roll\": %f,\"pitch\": %f,\"yaw\": %f",inertial_directions[1], -inertial_directions[0], inertial_directions[2]);
  35. }
  36. return ;
  37. }
  38. /*
  39. returns: "magnetic_field":_[23088.669921875,_3876.001220703125,_-53204.57421875]
  40. */
  41. void getCompass (const WbDeviceTag compass, const double *northDirection, char *buf)
  42. {
  43. const double *north3D = wb_compass_get_values(compass);
  44. if (northDirection[0] == 1)
  45. {
  46. sprintf(buf,"[%f, %f, %f]",north3D[0], north3D[2], north3D[1]);
  47. }
  48. else
  49. {
  50. sprintf(buf,"[%f, %f, %f]",north3D[2], -north3D[0], north3D[1]);
  51. }
  52. return ;
  53. }
  54. /*
  55. returns: "vehicle.gps":{"timestamp":_1563301031.055164,"x":_5.5127296946011484e-05,"y":_-0.0010968948481604457,"z":_0.037179552018642426},
  56. */
  57. void getGPS (const WbDeviceTag gps, const double *northDirection, char *buf)
  58. {
  59. const double *north3D = wb_gps_get_values(gps);
  60. if (northDirection[0] == 1)
  61. {
  62. sprintf(buf,"\"x\": %f,\"y\": %f,\"z\": %f", north3D[0], north3D[2], north3D[1]);
  63. }
  64. else
  65. {
  66. sprintf(buf,"\"x\": %f,\"y\": %f,\"z\": %f", north3D[2], -north3D[0], north3D[1]);
  67. }
  68. return ;
  69. }
  70. /*
  71. returns: "linear_acceleration": [0.005074390675872564, 0.22471477091312408, 9.80740737915039]
  72. */
  73. void getAcc (const WbDeviceTag accelerometer, const double *northDirection, char *buf)
  74. {
  75. //SHOULD BE CORRECT
  76. const double *a = wb_accelerometer_get_values(accelerometer);
  77. if (northDirection[0] == 1)
  78. {
  79. sprintf(buf,"[%f, %f, %f]",a[0], a[2], a[1]);
  80. }
  81. else
  82. {
  83. sprintf(buf,"[%f, %f, %f]",a[0], a[2], a[1]);
  84. }
  85. //sprintf(buf,"[0.0, 0.0, 0.0]");
  86. return ;
  87. }
  88. /*
  89. returns: "angular_velocity": [-1.0255117643964695e-07, -8.877226775894087e-08, 2.087078510015772e-09]
  90. */
  91. void getGyro (const WbDeviceTag gyro, const double *northDirection, char *buf)
  92. {
  93. const double *g = wb_gyro_get_values(gyro);
  94. if (northDirection[0] == 1)
  95. {
  96. sprintf(buf,"[%f, %f, %f]",g[0], g[2], g[1]);
  97. }
  98. else
  99. {
  100. sprintf(buf,"[%f, %f, %f]",g[0], g[2], g[1]);
  101. }
  102. return ;
  103. }
  104. void getLinearVelocity (WbNodeRef nodeRef, const double *northDirection, char * buf)
  105. {
  106. const double * vel = wb_supervisor_node_get_velocity (nodeRef);
  107. if (vel != NULL)
  108. {
  109. if (northDirection[0] == 1)
  110. {
  111. sprintf (buf,"[%f, %f, %f]", vel[0], vel[2], vel[1]);
  112. }
  113. else
  114. {
  115. sprintf (buf,"[%f, %f, %f]", vel[2], -vel[0], vel[1]);
  116. }
  117. }
  118. }
  119. void getAllSensors (char *buf, const double *northDirection, WbDeviceTag gyro, WbDeviceTag accelerometer, WbDeviceTag compass, const WbDeviceTag gps, WbDeviceTag inertial_unit)
  120. {
  121. /*
  122. {"timestamp": 1563544049.2840538,
  123. "vehicle.imu": {"timestamp": 1563544049.2673872,
  124. "angular_velocity": [-2.0466000023589004e-06, 3.1428675129063777e-07, -6.141597896913709e-09],
  125. "linear_acceleration": [0.005077465437352657, 0.22471386194229126, 9.807389259338379],
  126. "magnetic_field": [23088.71484375, 3875.498046875, -53204.48046875]},
  127. "vehicle.gps": {
  128. "timestamp": 1563544049.2673872,
  129. "x": 4.985610576113686e-05, "y": -0.0010973707539960742, "z": 0.037179529666900635},
  130. "vehicle.velocity": {"timestamp": 1563544049.2673872,
  131. "linear_velocity": [-3.12359499377024e-10, -1.3824124955874595e-08, -6.033386625858839e-07],
  132. "angular_velocity": [-2.0466000023589004e-06, 3.1428675129063777e-07, -6.141597896913709e-09],
  133. "world_linear_velocity": [0.0, 0.0, -6.034970851942489e-07]},
  134. "vehicle.pose": {"timestamp": 1563544049.2673872,
  135. "x": 4.985610576113686e-05, "y": -0.0010973707539960742, "z": 0.037179529666900635, "yaw": 8.899446402210742e-05, "pitch": -0.0005175824626348913, "roll": 0.022908702492713928}
  136. }
  137. */
  138. static char compass_buf [150];
  139. static char acc_buf [150];
  140. static char gyro_buf [150];
  141. static char gps_buf [150];
  142. static char inertial_buf [150];
  143. static char linear_velocity_buf [150];
  144. char szTime[21];
  145. double time = wb_robot_get_time(); // current simulation time in [s]
  146. sprintf(szTime,"%f", time);
  147. getGyro(gyro, northDirection, gyro_buf);
  148. getAcc(accelerometer, northDirection, acc_buf);
  149. getCompass(compass, northDirection, compass_buf);
  150. getGPS(gps, northDirection, gps_buf);
  151. getInertia (inertial_unit, northDirection, inertial_buf);
  152. getLinearVelocity(wb_supervisor_node_get_self(), northDirection, linear_velocity_buf);
  153. sprintf (buf,"{\"ts\": %s,\"vehicle.imu\": {\"av\": %s,\"la\": %s,\"mf\": %s,\"vehicle.gps\":{%s},\"vehicle.velocity\":{\"wlv\": %s},\"vehicle.pose\":{%s,%s}}\r\n"
  154. , szTime, gyro_buf, acc_buf, compass_buf, gps_buf, linear_velocity_buf, gps_buf, inertial_buf );
  155. }