ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Call service from arduino?

asked 2018-07-30 15:37:30 -0600

Droter gravatar image

Hi,

I am trying to call a ROS service to turn on/off relays from an arduino. The service works from the command line and also from a python service client call. How do I call a service from an arduino?

The relay service message is:

int32 channel
bool  state
---
bool success

I made the ros_lib library and the new service is listed.

rosrun rosserial_client make_libraries .

How do you call the service?

#include <WProgram.h>
#include "ros.h"
#include <ros/service_client.h>
#include "turtlebot/relayCmd.h"


ros::NodeHandle nh;
ros::ServiceClient<turtlebot::relayCmd::Request, turtlebot::relayCmd::Response> client("/relay_cmd");
turtlebot::relayCmd::Request relay_request;
turtlebot::relayCmd::Response response;

void setup() {
  nh.initNode();

  nh.getHardware()->setBaud(57600);

  while (!nh.connected())
  {
      nh.spinOnce();
  }
  nh.loginfo("Teensy is connected");
  delay(1);
}

void loop() {
    relay_request.channel = 2;
    relay_request.state = 1;

    response.success = false;

    nh.loginfo("Calling relay server");

    if ( client.call(relay_request, response) ) {
      nh.loginfo("service callback true");
    }
    delay(1000);
}

Error Message during compiling:

   Compiling .pioenvs/teensy31/src/main.cpp.o
src/main.cpp: In function 'void loop()':
src/main.cpp:42:21: error: could not convert 'client.ros::ServiceClient<MReq, MRes>::call<turtlebot::relayCmdRequest, turtlebot::relayCmdResponse>(relay_request, response)' from 'void' to 'bool'
if ( client.call(relay_request, response) ) {
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-07-30 17:20:36 -0600

Droter gravatar image

Followed the format on this post: https://answers.ros.org/question/3871...

It is working now.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-07-30 15:37:30 -0600

Seen: 1,994 times

Last updated: Jul 30 '18