diff --git a/data/kansei.json b/data/kansei.json deleted file mode 100755 index d8639df..0000000 --- a/data/kansei.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "accel_limit": { - "accel_back_limit": 620.0, - "accel_front_limit": 445.0, - "accel_right_limit": 570.0, - "accel_left_limit": 400.0 - }, - "orientation_limit": { - "pitch_back_limit": 0.0, - "pitch_front_limit": 0.0, - "roll_right_limit": 0.0, - "roll_left_limit": 0.0 - } -} diff --git a/include/kansei/measurement/sensor/mpu.hpp b/include/kansei/measurement/sensor/mpu.hpp index af05167..734129e 100755 --- a/include/kansei/measurement/sensor/mpu.hpp +++ b/include/kansei/measurement/sensor/mpu.hpp @@ -39,6 +39,7 @@ class MPU : public MeasurementUnit bool connect(); void update_rpy() override; + static void *start(void *object); void reset_orientation() override; void set_orientation_to(const keisan::Angle & target_orientation) override; @@ -46,9 +47,11 @@ class MPU : public MeasurementUnit private: int socket_fd; std::string port_name; + pthread_t thread; - keisan::Angle oreintation_compensation; - keisan::Angle oreintation_error; + keisan::Angle raw_orientation; + keisan::Angle orientation_compensation; + keisan::Angle orientation_error; }; } // namespace kansei::measurement diff --git a/src/check_rpy_main.cpp b/src/check_rpy_main.cpp index 7fb3fd2..d1192c9 100755 --- a/src/check_rpy_main.cpp +++ b/src/check_rpy_main.cpp @@ -27,6 +27,8 @@ #include "kansei/measurement/measurement.hpp" #include "keisan/keisan.hpp" +using namespace std::chrono_literals; + int main(int argc, char * argv[]) { std::string port_name = "/dev/ttyUSB1"; @@ -45,8 +47,8 @@ int main(int argc, char * argv[]) return 0; } + rclcpp::Rate loop_rate(8ms); while (true) { - mpu.update_rpy(); keisan::Euler rpy = mpu.get_orientation(); @@ -54,6 +56,8 @@ int main(int argc, char * argv[]) std::cout << "Pitch: " << rpy.pitch.degree() << std::endl; std::cout << "Yaw: " << rpy.yaw.degree() << std::endl; std::cout << "\033c"; + + loop_rate.sleep(); } return 0; diff --git a/src/kansei/measurement/node/measurement_node.cpp b/src/kansei/measurement/node/measurement_node.cpp index 612c44d..4f342b0 100755 --- a/src/kansei/measurement/node/measurement_node.cpp +++ b/src/kansei/measurement/node/measurement_node.cpp @@ -75,7 +75,6 @@ MeasurementNode::MeasurementNode( void MeasurementNode::update(double seconds) { if (std::dynamic_pointer_cast(measurement_unit)) { - measurement_unit->update_rpy(); publish_status(); } else if (std::dynamic_pointer_cast(measurement_unit)) { diff --git a/src/kansei/measurement/sensor/mpu.cpp b/src/kansei/measurement/sensor/mpu.cpp index 78926ef..0117d62 100755 --- a/src/kansei/measurement/sensor/mpu.cpp +++ b/src/kansei/measurement/sensor/mpu.cpp @@ -41,7 +41,7 @@ namespace kansei::measurement { MPU::MPU(const std::string & port_name) -: socket_fd(-1), oreintation_error(0_deg), oreintation_compensation(0_deg) +: socket_fd(-1), orientation_error(0_deg), orientation_compensation(0_deg), raw_orientation(0_deg) { set_port_name(port_name); } @@ -78,6 +78,8 @@ bool MPU::connect() tcflush(socket_fd, TCIFLUSH); tcsetattr(socket_fd, TCSANOW, &newtio); + pthread_create(&thread, NULL, &start, this); + return true; } else { fprintf(stderr, "Can not connect MPU on %s\n", port_name.c_str()); @@ -168,15 +170,23 @@ void MPU::update_rpy() rpy.roll = keisan::make_degree(roll); rpy.pitch = keisan::make_degree(pitch); - rpy.yaw = keisan::make_degree(yaw) + oreintation_error + oreintation_compensation; - break; + raw_orientation = keisan::make_degree(yaw); + rpy.yaw = raw_orientation + orientation_error + orientation_compensation; + } else { usart_status = 0; } } } +void *MPU::start(void *object) +{ + reinterpret_cast(object)->update_rpy(); + + return 0; +} + void MPU::set_port_name(const std::string & port_name) { this->port_name = port_name; @@ -184,13 +194,14 @@ void MPU::set_port_name(const std::string & port_name) void MPU::reset_orientation() { - oreintation_error = -rpy.yaw; - oreintation_compensation = 0_deg; + orientation_error = -raw_orientation; + orientation_compensation = 0_deg; } void MPU::set_orientation_to(const keisan::Angle & target_orientation) { - oreintation_compensation = target_orientation; + orientation_error = -raw_orientation; + orientation_compensation = target_orientation; } } // namespace kansei::measurement