6 #include <dynamic-graph/factory.h>
8 #include <sot/core/debug.hh>
9 #include <sot/core/stop-watch.hh>
16 namespace dg = ::dynamicgraph;
18 using namespace dg::command;
21 #define CALIBRATION_FILE_NAME "/opt/imu_calib.txt"
23 #define PROFILE_IMU_OFFSET_COMPENSATION_COMPUTATION \
24 "ImuOffsetCompensation computation"
26 #define INPUT_SIGNALS m_accelerometer_inSIN << m_gyrometer_inSIN
27 #define OUTPUT_SIGNALS m_accelerometer_outSOUT << m_gyrometer_outSOUT
35 "ImuOffsetCompensation");
42 CONSTRUCT_SIGNAL_IN(accelerometer_in,
dynamicgraph::Vector),
44 CONSTRUCT_SIGNAL_OUT(accelerometer_out,
dynamicgraph::Vector,
45 m_accelerometer_inSIN),
46 CONSTRUCT_SIGNAL_OUT(gyrometer_out,
dynamicgraph::Vector,
48 m_initSucceeded(false),
50 m_update_cycles_left(0),
52 m_a_gyro_DC_blocker(1.0f)
65 docCommandVoid1(
"Initialize the entity.",
66 "Timestep in seconds (double)")));
71 docCommandVoid1(
"Update the IMU offsets.",
72 "Duration of the update phase in seconds (double)")));
74 "setGyroDCBlockerParameter",
76 docCommandVoid1(
"Set DC Blocker filter parameter.",
85 if (dt <= 0.0)
return SEND_MSG(
"Timestep must be positive", MSG_TYPE_ERROR);
86 m_dt =
static_cast<float>(dt);
92 if (!infile.is_open())
93 return SEND_MSG(
"Error trying to read calibration results from file " +
106 return SEND_MSG(
"Error trying to read gyro offset from file " +
108 ". Not enough values: " + toString(i),
113 while (infile >> z) {
121 return SEND_MSG(
"Error trying to read acc offset from file " +
123 ". Not enough values: " + toString(i),
127 SEND_MSG(
"Offset read finished:\n* acc offset: " +
134 if (alpha > 1.0 || alpha <= 0.0)
135 return SEND_MSG(
"GyroDCBlockerParameter must be > 0 and <= 1",
142 return SEND_MSG(
"Duration must be greater than the time step",
149 const dynamicgraph::Vector& accelerometer = m_accelerometer_inSIN(iter);
150 const dynamicgraph::Vector& gyrometer = m_gyrometer_inSIN(iter);
156 Vector3 g, new_acc_offset, new_gyro_offset;
163 "Offset computation finished:" +
164 (
"\n* old acc offset: " + toString(
m_acc_offset.transpose())) +
165 "\n* new acc offset: " + toString(new_acc_offset.transpose()) +
166 "\n* old gyro offset: " + toString(
m_gyro_offset.transpose()) +
167 "\n* new gyro offset: " + toString(new_gyro_offset.transpose()),
175 return SEND_MSG(
"Error trying to save calibration results on file " +
179 for (
unsigned long int i = 0; i < 3; i++) aof <<
m_gyro_offset[i] <<
" ";
181 for (
unsigned long int i = 0; i < 3; i++) aof <<
m_acc_offset[i] <<
" ";
195 if (!m_initSucceeded) {
196 SEND_WARNING_STREAM_MSG(
197 "Cannot compute signal accelerometer before initialization!");
201 if (m_update_cycles_left > 0) update_offset_impl(iter);
203 const dynamicgraph::Vector& accelerometer = m_accelerometer_inSIN(iter);
204 if (s.size() != 3) s.resize(3);
205 s = accelerometer - m_acc_offset;
210 if (!m_initSucceeded) {
211 SEND_WARNING_STREAM_MSG(
212 "Cannot compute signal gyrometer before initialization!");
215 const dynamicgraph::Vector& gyrometer = m_gyrometer_inSIN(iter);
216 if (s.size() != 3) s.resize(3);
219 if (m_a_gyro_DC_blocker != 1.0)
220 m_gyro_offset = m_gyro_offset * m_a_gyro_DC_blocker +
221 (1. - m_a_gyro_DC_blocker) * gyrometer;
222 s = gyrometer - m_gyro_offset;
231 os <<
"ImuOffsetCompensation " << getName();
233 getProfiler().report_all(3, os);
234 }
catch (ExceptionSignal e) {
void update_offset(const double &duration)
float m_dt
true if the entity has been successfully initialized
Vector3 m_gyro_sum
accelerometer offset
void init(const double &dt)
EIGEN_MAKE_ALIGNED_OPERATOR_NEW ImuOffsetCompensation(const std::string &name)
double m_a_gyro_DC_blocker
total number of update cycles to perform
virtual void display(std::ostream &os) const
compensated gyrometer data
int m_update_cycles
number of update cycles left
void setGyroDCBlockerParameter(const double &alpha)
Vector3 m_acc_offset
gyrometer offset
void update_offset_impl(int iter)
int m_update_cycles_left
sampling time in seconds
#define CALIBRATION_FILE_NAME
DEFINE_SIGNAL_OUT_FUNCTION(u, dynamicgraph::Vector)
AdmittanceController EntityClassName
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceController, "AdmittanceController")