io_service.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017, 2018 Simon Rasmussen (refactor)
3  *
4  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <ros/ros.h>
22 #include <ur_msgs/SetIO.h>
23 #include <ur_msgs/SetIORequest.h>
24 #include <ur_msgs/SetIOResponse.h>
25 #include <ur_msgs/SetPayload.h>
26 #include <ur_msgs/SetPayloadRequest.h>
27 #include <ur_msgs/SetPayloadResponse.h>
28 #include "ur_modern_driver/log.h"
30 
31 class IOService
32 {
33 private:
38 
39  bool setIO(ur_msgs::SetIORequest& req, ur_msgs::SetIOResponse& resp)
40  {
41  LOG_INFO("setIO called with [%d, %d]", req.fun, req.pin);
42  bool res = false;
43  bool flag = req.state > 0.0 ? true : false;
44  switch (req.fun)
45  {
46  case ur_msgs::SetIO::Request::FUN_SET_DIGITAL_OUT:
47  res = commander_.setDigitalOut(req.pin, flag);
48  break;
49  case ur_msgs::SetIO::Request::FUN_SET_ANALOG_OUT:
50  res = commander_.setAnalogOut(req.pin, req.state);
51  break;
52  case ur_msgs::SetIO::Request::FUN_SET_TOOL_VOLTAGE:
53  res = commander_.setToolVoltage(static_cast<uint8_t>(req.state));
54  break;
55  case ur_msgs::SetIO::Request::FUN_SET_FLAG:
56  res = commander_.setFlag(req.pin, flag);
57  break;
58  default:
59  LOG_WARN("Invalid setIO function called (%d)", req.fun);
60  }
61 
62  return (resp.success = res);
63  }
64 
65  bool setPayload(ur_msgs::SetPayloadRequest& req, ur_msgs::SetPayloadResponse& resp)
66  {
67  LOG_INFO("setPayload called");
68  // TODO check min and max payload?
69  return (resp.success = commander_.setPayload(req.payload));
70  }
71 
72 public:
73  IOService(URCommander& commander)
74  : commander_(commander)
75  , io_service_(nh_.advertiseService("ur_driver/set_io", &IOService::setIO, this))
76  , payload_service_(nh_.advertiseService("ur_driver/set_payload", &IOService::setPayload, this))
77  {
78  }
79 };
virtual bool setDigitalOut(uint8_t pin, bool value)=0
bool setPayload(ur_msgs::SetPayloadRequest &req, ur_msgs::SetPayloadResponse &resp)
Definition: io_service.h:65
bool setIO(ur_msgs::SetIORequest &req, ur_msgs::SetIOResponse &resp)
Definition: io_service.h:39
IOService(URCommander &commander)
Definition: io_service.h:73
ServiceServer advertiseService(ros::NodeHandle n, std::string name, boost::function< bool(const typename ActionSpec::_action_goal_type::_goal_type &, typename ActionSpec::_action_result_type::_result_type &result)> service_cb)
URCommander & commander_
Definition: io_service.h:35
bool setToolVoltage(uint8_t voltage)
Definition: commander.cpp:47
bool setPayload(double value)
Definition: commander.cpp:65
ros::ServiceServer io_service_
Definition: io_service.h:36
bool setFlag(uint8_t pin, bool value)
Definition: commander.cpp:58
#define LOG_INFO(format,...)
Definition: log.h:35
ros::ServiceServer payload_service_
Definition: io_service.h:37
virtual bool setAnalogOut(uint8_t pin, double value)=0
ros::NodeHandle nh_
Definition: io_service.h:34
#define LOG_WARN(format,...)
Definition: log.h:34


ur_modern_driver
Author(s): Thomas Timm Andersen, Simon Rasmussen
autogenerated on Fri Jun 26 2020 03:37:00