Skip to content

Instantly share code, notes, and snippets.

@umhan35
Last active July 23, 2024 19:43
Show Gist options
  • Save umhan35/f7dfd3ae935af0c849e1338a62c2f3e6 to your computer and use it in GitHub Desktop.
Save umhan35/f7dfd3ae935af0c849e1338a62c2f3e6 to your computer and use it in GitHub Desktop.

Revisions

  1. umhan35 revised this gist Jul 23, 2024. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions a.cpp
    Original file line number Diff line number Diff line change
    @@ -4,11 +4,12 @@

    ros::NodeHandle nh;

    void messageCb(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res) {
    bool messageCb(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res) {
    digitalWrite(LED_BUILTIN, HIGH-digitalRead(LED_BUILTIN)); // blink the led

    nh.loginfo("LED is ON");


    return true;
    }

    ros::ServiceServer<std_srvs::Empty::Request, std_srvs::Empty::Response> blinkServer("/led/blink", &messageCb);
    @@ -23,8 +24,8 @@ void setup()
    void loop()
    {
    digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
    delay(1000); // wait for a second
    delay(10); // wait for a second
    digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
    delay(1000); // wait for a second
    delay(10); // wait for a second
    nh.spinOnce();
    }
  2. umhan35 created this gist Jul 23, 2024.
    30 changes: 30 additions & 0 deletions a.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #include <ros.h>
    #include <std_msgs/String.h>
    #include <std_srvs/Empty.h>

    ros::NodeHandle nh;

    void messageCb(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res) {
    digitalWrite(LED_BUILTIN, HIGH-digitalRead(LED_BUILTIN)); // blink the led

    nh.loginfo("LED is ON");

    }

    ros::ServiceServer<std_srvs::Empty::Request, std_srvs::Empty::Response> blinkServer("/led/blink", &messageCb);

    void setup()
    {
    pinMode(LED_BUILTIN, OUTPUT);
    nh.initNode();
    nh.advertiseService(blinkServer);
    }

    void loop()
    {
    digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
    delay(1000); // wait for a second
    digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
    delay(1000); // wait for a second
    nh.spinOnce();
    }