berndporr / iir1

DSP IIR realtime filter library written in C++
http://berndporr.github.io/iir1/
MIT License
637 stars 140 forks source link

ros-callback #58

Closed REAL-LXJ closed 1 year ago

REAL-LXJ commented 1 year ago

I want to use ros callback to filter imu msg real time, but the result is not good. double filter(double ax) { double axf; const float fs = 200; const float cutoff = 10; Iir::Butterworth::LowPass<4> lowpass; lowpass.setup(fs, cutoff); axf = lowpass.filter(ax); return axf; } void imu_callback(const sensor_msgs::Imu &imu_msg) { double ax = imu_msg.linear_acceleration.x; double axf = filter(ax); std::cout << "ax = " << ax << std::endl; std::cout << "axf = " << axf << std::endl; }

berndporr commented 1 year ago

You are creating a new filter instance at every time step and then calling the filter design at every time-step. This will wipe out the memory of the filter. You need to create the instance only once and call setup only once. Only lowpass.filter(ax) should be called in the callback. Hope you find the feedback useful. Sorry, won't have time to comment more on your code. Best of luck with the ROS coding. Stating "the result is not good" is not helpful. For future reference always provide a reproducible test-case and a description what you'd expect and what is the actual result.