epson_imu_driver_node.cpp
Go to the documentation of this file.
1 extern "C" {
2 #include "hcl.h"
3 #include "hcl_gpio.h"
4 #include "hcl_uart.h"
5 #include "sensor_epsonCommon.h"
6 }
7 #include <ros/ros.h>
8 #include <sensor_msgs/Imu.h>
9 #include <termios.h>
10 #include <string>
11 #include <iostream>
12 
13 using namespace std;
14 
15 int comPort;
16 string serial_port;
18 
19 //=========================================================================
20 //------------------------ IMU Initializaion ------------------------------
21 //=========================================================================
22 
23 bool init(struct EpsonOptions options){
24  ROS_INFO("Initializing HCL layer...");
25  if (!seInit()){
26  ROS_ERROR("Error: could not initialize the Seiko Epson HCL layer. Exiting...");
27  return false;
28  }
29 
30  ROS_INFO("Initializing GPIO interface...");
31  if (!gpioInit()){
32  ROS_ERROR("Error: could not initialize the GPIO layer. Exiting...");
33  return false;
34  }
35 
36  ROS_INFO("Initializing UART interface...");
38  if(comPort == -1){
39  ROS_ERROR("Error: could not initialize UART interface. Exiting...");
40  return false;
41  }
42 
43  ROS_INFO("Checking sensor NOT_READY status...");
44  if(!sensorPowerOn()){
45  ROS_ERROR("Error: failed to power on Sensor. Exiting...");
47  return -1;
48  }
49  printf("...done.");
50 
51  ROS_INFO("Initializing Sensor...");
52  if(!sensorInitOptions(options)){
53  ROS_ERROR("Error: could not initialize Epson Sensor. Exiting...");
55  return -1;
56  }
57 
58  ROS_INFO("...Epson IMU initialized.");
59 }
60 
61 //=========================================================================
62 //----------------------- Timestamp Correction ----------------------------
63 //=========================================================================
64 
66 private:
67  int MAX_COUNT;
71 
72  int32_t count_corrected;
73  int32_t count_old;
74  int32_t count_diff;
75  int32_t time_current;
76  int32_t time_old;
79  bool rollover;
81 public:
83  ros::Time get_stamp(int);
84 };
85 
87  MAX_COUNT = 1397861550;
88  ALMOST_ROLLOVER = 1300000000;
89  ONE_SEC_NSEC = 1000000000;
90  HALF_SEC_NSEC = 500000000;
91 
92  count_corrected = 0;
93  count_old = 0;
94  count_diff = 0;
95  time_current = 0;
96  time_old = 0;
97  time_nsec_current = 0;
98  count_corrected_old = 0;
99  rollover = false;
100  flag_imu_lead = false;
101 }
102 
104  //converting count into time
105  time_current = ros::Time::now().toSec();
106  time_nsec_current = ros::Time::now().nsec;
107 
108  count_diff = count - count_old;
109  if(count > ALMOST_ROLLOVER){
110  rollover = 1;
111  }
112  else if(count_diff<0){
113  if(rollover){
114  count_diff = count + (MAX_COUNT - count_old);
115  }
116  else {
117  count_diff = count;
118  count_corrected = 0;
119  std::cout.precision(20);
120  std::cout << "reset at: " << ros::Time::now().toSec() << std::endl;
121  }
122  rollover = 0;
123  }
124  count_corrected = (count_corrected + count_diff) % ONE_SEC_NSEC;
125  if(time_current != time_old && count_corrected > HALF_SEC_NSEC){
126  time_current = time_current - 1;
127  }
128  else if((count_corrected-count_corrected_old) < 0 && time_nsec_current > HALF_SEC_NSEC){
129  time_current = time_current + 1;
130  flag_imu_lead = 1;
131  }
132  else if(flag_imu_lead && time_nsec_current > HALF_SEC_NSEC){
133  time_current = time_current + 1;
134  }
135  else{
136  flag_imu_lead = 0;
137  }
138 
139  ros::Time time;
140  time.nsec = count_corrected;
141  time.sec = time_current;
142  time_old=time_current;
143  count_old = count;
144  count_corrected_old = count_corrected;
145  return time;
146 }
147 
148 //=========================================================================
149 //------------------------------ Main -------------------------------------
150 //=========================================================================
151 
152 int main(int argc, char** argv){
153  ros::init(argc, argv, "epson_imu_driver_node");
154  ros::NodeHandle nh;
155  ros::NodeHandle np("~");
156 
157  np.param<string>("port", serial_port, "/dev/ttyUSB0");
158 
159  struct EpsonOptions options;
160  int time_correction = 0;
161 
162  np.param("enable_xgyro", options.enable_xgyro, 0);
163  np.param("enable_ygyro", options.enable_ygyro, 0);
164  np.param("enable_zgyro", options.enable_zgyro, 0);
165  np.param("enable_xaccel", options.enable_xaccel, 1);
166  np.param("enable_yaccel", options.enable_yaccel, 1);
167  np.param("enable_zaccel", options.enable_zaccel, 1);
168  np.param("enable_temp", options.enable_temp, 1);
169  np.param("enable_xgyro_delta", options.enable_xgyro_delta, 0);
170  np.param("enable_ygyro_delta", options.enable_ygyro_delta, 0);
171  np.param("enable_zgyro_delta", options.enable_zgyro_delta, 0);
172  np.param("enable_xaccel_delta", options.enable_xaccel_delta, 0);
173  np.param("enable_yaccel_delta", options.enable_yaccel_delta, 0);
174  np.param("enable_zaccel_delta", options.enable_zaccel_delta, 0);
175 
176  np.param("ext_sel", options.ext_sel, 1);
177  np.param("ext_pol", options.ext_pol, 0);
178  np.param("drdy_on", options.drdy_on, 0);
179  np.param("drdy_pol", options.drdy_pol, 0);
180  np.param("flash_test", options.flash_test, 0);
181  np.param("self_test", options.self_test, 0);
182 
183  np.param("dout_rate", options.dout_rate, 3);
184 
185  np.param("filter_sel", options.filter_sel, 5);
186 
187  np.param("baud_rate", options.baud_rate, 0);
188  np.param("auto_start", options.auto_start, 0);
189  np.param("uart_auto", options.uart_auto, 1);
190 
191  np.param("flag_out", options.flag_out, 1);
192  np.param("temp_out", options.temp_out, 1);
193  np.param("gyro_out", options.gyro_out, 1);
194  np.param("accel_out", options.accel_out, 1);
195  np.param("gyro_delta_out", options.gyro_delta_out, 0);
196  np.param("accel_delta_out", options.accel_delta_out, 0);
197  np.param("gpio_out", options.gpio_out, 0);
198  np.param("count_out", options.count_out, 1);
199  np.param("checksum_out", options.checksum_out, 1);
200 
201  np.param("temp_bit", options.temp_bit, 1);
202  np.param("gyro_bit", options.gyro_bit, 1);
203  np.param("accel_bit", options.accel_bit, 1);
204  np.param("gyro_delta_bit", options.gyro_delta_bit, 1);
205  np.param("accel_delta_bit", options.accel_delta_bit, 1);
206 
207  np.param("invert_xgyro", options.invert_xgyro, 0);
208  np.param("invert_ygyro", options.invert_ygyro, 0);
209  np.param("invert_zgyro", options.invert_zgyro, 0);
210  np.param("invert_xaccel", options.invert_xaccel, 0);
211  np.param("invert_yaccel", options.invert_yaccel, 0);
212  np.param("invert_zaccel", options.invert_zaccel, 0);
213 
214  np.param("dlt_ovf_en", options.dlt_ovf_en, 0);
215  np.param("dlt_range_ctrl", options.dlt_range_ctrl, 8);
216 
217  np.param("time_correction", time_correction, 1);
218 
219  if(options.baud_rate == 0)
220  baudrate = B460800;
221  else
222  baudrate = B230400;
223 
224  init(options);
225  sensorStart();
226 
227  struct EpsonData epson_data;
228  TimeCorrection tc;
229 
230  sensor_msgs::Imu imu_msg;
231  for(int i = 0; i < 9; i++){
232  imu_msg.orientation_covariance[i] = 0;
233  imu_msg.angular_velocity_covariance[i] = 0;
234  imu_msg.linear_acceleration_covariance[i] = 0;
235  }
236  imu_msg.orientation_covariance[0] = -1;
237 
238  ros::Publisher imu_pub = nh.advertise<sensor_msgs::Imu>("epson_imu", 1);
239 
240  while(ros::ok()){
241  if(sensorDataReadBurstNOptions(options, &epson_data)){
242  imu_msg.header.frame_id = "epson";
243  if(!time_correction)
244  imu_msg.header.stamp = ros::Time::now();
245  else
246  imu_msg.header.stamp = tc.get_stamp(epson_data.count);
247  imu_msg.angular_velocity.x = epson_data.gyro_x;
248  imu_msg.angular_velocity.y = epson_data.gyro_y;
249  imu_msg.angular_velocity.z = epson_data.gyro_z;
250  imu_msg.linear_acceleration.x = epson_data.accel_x;
251  imu_msg.linear_acceleration.y = epson_data.accel_y;
252  imu_msg.linear_acceleration.z = epson_data.accel_z;
253  imu_pub.publish(imu_msg);
254  }
255  }
256 
257  sensorStop();
259  gpioRelease();
260  seRelease();
261 
262  return 0;
263 }
ros::Time get_stamp(int)
void publish(const boost::shared_ptr< M > &message) const
int sensorDataReadBurstNOptions(struct EpsonOptions, struct EpsonData *)
bool init(struct EpsonOptions options)
int baudrate
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int sensorInitOptions(struct EpsonOptions)
int seInit(void)
Definition: hcl_linux.c:28
string serial_port
int gpioInit(void)
Definition: hcl_gpio.c:29
int uartInit(const char *comPortPath, int baudRate)
Definition: hcl_uart.c:222
void sensorStop(void)
int gpioRelease(void)
Definition: hcl_gpio.c:41
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int uartRelease(ComPortHandle comPort)
Definition: hcl_uart.c:236
void sensorStart(void)
int main(int argc, char **argv)
int seRelease(void)
Definition: hcl_linux.c:42
static Time now()
int sensorPowerOn(void)
#define ROS_ERROR(...)


epson_g364_imu_driver
Author(s):
autogenerated on Mon Jun 10 2019 13:12:32