add test calling method while node spinning
This commit is contained in:
@@ -8,27 +8,30 @@ using namespace std::chrono_literals; // for time measurements
|
|||||||
|
|
||||||
class HeightSensorPublisher : public rclcpp::Node
|
class HeightSensorPublisher : public rclcpp::Node
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HeightSensorPublisher() : Node("height_sensor_publisher")
|
HeightSensorPublisher() : Node("height_sensor_publisher")
|
||||||
{
|
{
|
||||||
publisher_ = this->create_publisher<std_msgs::msg::String>("height_sensor", 10);
|
publisher_ = this->create_publisher<std_msgs::msg::String>("height_sensor", 10);
|
||||||
timer_ = this->create_wall_timer(
|
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");
|
RCLCPP_INFO(this->get_logger(), "Constructor of height sensor publisher");
|
||||||
|
|
||||||
setup_serial_port();
|
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
|
* @brief Timer callback function to publish the height sensor data
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void timer_callback()
|
void timer_callback()
|
||||||
{
|
{
|
||||||
char* readdata = new char[1];
|
char *readdata = new char[1];
|
||||||
serial_port.read(readdata, 1);
|
serial_port.read(readdata, 1);
|
||||||
auto message = std_msgs::msg::String();
|
auto message = std_msgs::msg::String();
|
||||||
message.data = "Height: " + std::to_string((int)readdata[0]);
|
message.data = "Height: " + std::to_string((int)readdata[0]);
|
||||||
@@ -56,10 +59,6 @@ class HeightSensorPublisher : public rclcpp::Node
|
|||||||
serial_port.close();
|
serial_port.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void test()
|
|
||||||
{
|
|
||||||
RCLCPP_INFO(this->get_logger(), "Je moeder is een plopkoek");
|
|
||||||
}
|
|
||||||
rclcpp::TimerBase::SharedPtr timer_;
|
rclcpp::TimerBase::SharedPtr timer_;
|
||||||
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
|
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
|
||||||
std::fstream serial_port; // serial port for reading from device
|
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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
rclcpp::init(argc,argv);
|
rclcpp::init(argc, argv);
|
||||||
|
|
||||||
rclcpp::executors::SingleThreadedExecutor executor;
|
rclcpp::executors::SingleThreadedExecutor executor;
|
||||||
auto node = std::make_shared<HeightSensorPublisher>();
|
rclcpp::Node::SharedPtr node = std::make_shared<HeightSensorPublisher>();
|
||||||
node.get()->setup_serial_port();
|
|
||||||
executor.add_node(node);
|
executor.add_node(node);
|
||||||
executor.spin();
|
executor.spin();
|
||||||
// rclcpp::spin(std::make_shared<HeightSensorPublisher>());
|
// rclcpp::spin(std::make_shared<HeightSensorPublisher>());
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
node->test();
|
||||||
|
}
|
||||||
rclcpp::shutdown();
|
rclcpp::shutdown();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user