add test calling method while node spinning

This commit is contained in:
Sem van der Hoeven
2023-03-03 13:37:31 +01:00
parent 4df0d69b53
commit b88d8beb39

View File

@@ -8,27 +8,30 @@ using namespace std::chrono_literals; // for time measurements
class HeightSensorPublisher : public rclcpp::Node
{
public:
public:
HeightSensorPublisher() : Node("height_sensor_publisher")
{
publisher_ = this->create_publisher<std_msgs::msg::String>("height_sensor", 10);
timer_ = this->create_wall_timer(
1ms, std::bind(&HeightSensorPublisher::timer_callback, this));
500ms, std::bind(&HeightSensorPublisher::timer_callback, this));
RCLCPP_INFO(this->get_logger(), "Constructor of height sensor publisher");
setup_serial_port();
}
private:
void test()
{
RCLCPP_INFO(this->get_logger(), "Je moeder is een plopkoek");
}
private:
/**
* @brief Timer callback function to publish the height sensor data
*
*/
void timer_callback()
{
char* readdata = new char[1];
char *readdata = new char[1];
serial_port.read(readdata, 1);
auto message = std_msgs::msg::String();
message.data = "Height: " + std::to_string((int)readdata[0]);
@@ -56,10 +59,6 @@ class HeightSensorPublisher : public rclcpp::Node
serial_port.close();
}
void test()
{
RCLCPP_INFO(this->get_logger(), "Je moeder is een plopkoek");
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
std::fstream serial_port; // serial port for reading from device
@@ -67,17 +66,19 @@ class HeightSensorPublisher : public rclcpp::Node
int main(int argc, char *argv[])
{
rclcpp::init(argc,argv);
rclcpp::init(argc, argv);
rclcpp::executors::SingleThreadedExecutor executor;
auto node = std::make_shared<HeightSensorPublisher>();
node.get()->setup_serial_port();
rclcpp::Node::SharedPtr node = std::make_shared<HeightSensorPublisher>();
executor.add_node(node);
executor.spin();
// rclcpp::spin(std::make_shared<HeightSensorPublisher>());
while (true)
{
node->test();
}
rclcpp::shutdown();
return 0;
}