0
$\begingroup$

What I've tried and works to know the WiringPi library is working on my Raspberry Pi 4.

  • g++ compiling a test cpp script using WiringPi.
  • Terminal gpio commands (gpio -g mode 18 output sets pin 18 to be an output)
  • Physically wiring an output to an input. Writing to the output changed the input correctly
  • All results of changing pinMode and digialWrite were correct on the gpio readall

Properly linking the external WiringPi library into my ROS 2 package, wiringPi.h file in my cpp node, and using the same code as the test cpp script, it doesn't work. Is there something special I need to account for? Is there a ROS 2 background thread preventing WiringPi from functioning?

Code


class MyNode : public rclcpp::Node
{
public:
    MyNode() : Node("my-node")
    {
        wiringPiSetupGpio();
        if (wiringPiSetupGpio() == -1)
        {
            throw std::runtime_error("Failed to initialize WiringPiSetup");
        }
        pinMode(18, OUTPUT);
        pinMode(17, INPUT);
        // Jumper wire connecting pin 18 to 17
        digitalWrite(18, HIGH);
        RCLCPP_INFO(get_logger(), "%d", digitalRead(17);
    }
}

int main(int argc, char **argv)
{
    rclcpp::init(argc, argv);

    try
    {
        auto node = std::make_shared<MyNode>();
        RCLCPP_INFO(node->get_logger(), "Starting node");

        rclcpp::spin(node);
    }
    catch (const std::exception &ex)
    {
        RCLCPP_ERROR_STREAM(rclcpp::get_logger("main"), "Exception during node initialization: " << ex.what());
    }

    rclcpp::shutdown();
    return 0;
}


18 does not change to OUTPUT nor is it set to HIGH. 17 is by default an INPUT.

Reference

$\endgroup$
6
  • 1
    $\begingroup$ Post a minimal example of your ROS node and how you spin it in main(). Without that one can only guess... $\endgroup$
    – JRTG
    Nov 29, 2023 at 18:35
  • $\begingroup$ Updated it with code snippet. $\endgroup$
    – Yato
    Nov 30, 2023 at 17:08
  • $\begingroup$ The only issue I see is that at the start of your question you state "sets pin 17 to be an output" while in the code you set 17 as the input. So maybe you are measuring on the wrong pin and it actually works? $\endgroup$
    – JRTG
    Nov 30, 2023 at 18:16
  • $\begingroup$ No matter what I change, nothing reflects in gpio readall. That was just an example. Let me change it so it's not confusing. $\endgroup$
    – Yato
    Nov 30, 2023 at 18:22
  • $\begingroup$ Do you think it's something to do with how the ROS 2 spins the node? $\endgroup$
    – Yato
    Nov 30, 2023 at 18:43

1 Answer 1

0
$\begingroup$

Put the wiringPiSetup() inside main and before spinning the node (rclcpp::spin(node)). It should look like this.

int main(int argc, char** argv)
{
    rclcpp::init(argc, argv);

    wiringPiSetupGpio();
    if (wiringPiSetupGpio() == -1)
    {
        return -1;
    }

    auto node = std::make_shared<MyNode>();
    RCLCPP_INFO(node->get_logger(), "Starting node");

    rclcpp::spin(node);

    rclcpp::shutdown();
    return 0;
}

Although putting wiringPiSetup() in the constructor doesn't throw any errors, it looks like you want the initialization to be separate from the rest of the ROS Node. I have no references for this solution.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.