khi_robot_client.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019, Kawasaki Heavy Industries, LTD.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #include <stdio.h>
36 #include <boost/thread/thread.hpp>
37 #include <khi_robot_client.h>
38 #include <khi_robot_krnx_driver.h>
39 
40 namespace khi_robot_control
41 {
43 {
44  if ( driver == NULL ) return;
45 
46  ROS_INFO( "[KhiRobotCommandService] Start" );
47 
48  ros::AsyncSpinner spinner(boost::thread::hardware_concurrency()-1);
49  ros::NodeHandlePtr node = boost::make_shared<ros::NodeHandle>();
50  ros::ServiceServer service = node->advertiseService( "khi_robot_command_service", &KhiRobotDriver::commandHandler, driver );
51  spinner.start();
53 }
54 
55 bool KhiRobotClient::open( const std::string& ip, const double& period, KhiRobotData& data, const bool in_simulation )
56 {
57  cont_no = 0;
58 
59  /* select driver */
60  driver = new KhiRobotKrnxDriver();
61  if ( !driver->initialize( cont_no, period, data, in_simulation ) ) { return false; }
62 
63  /* open */
64  if ( !driver->open( cont_no, ip, data ) ) { return false; }
65 
67 
68  return true;
69 }
70 
72 {
73  if ( driver == NULL ) { return false; }
74 
75  return driver->activate( cont_no, data );
76 }
77 
79 {
80  if ( driver == NULL ) { return false; }
81 
82  return driver->hold( cont_no, data );
83 }
84 
86 {
87  if ( driver == NULL ) { return; }
88 
89  driver->deactivate( cont_no, data );
90 }
91 
93 {
94  if ( driver == NULL ) { return; }
95 
96  driver->close( cont_no );
97  delete driver;
98 }
99 
101 {
102  if ( driver == NULL ) { return; }
103 
104  driver->writeData( cont_no, data );
105 }
106 
108 {
109  if ( driver == NULL ) { return; }
110 
111  driver->readData( cont_no, data );
112 }
113 
115 {
116  if ( driver == NULL ) { return NOT_REGISTERED; }
117 
118  return driver->updateState( cont_no, data );
119 }
120 
122 {
123  if ( driver == NULL ) { return NONE; }
124 
125  return driver->getStateTrigger( cont_no );
126 }
127 
128 bool KhiRobotClient::getPeriodDiff( double& diff )
129 {
130  if ( driver == NULL ) { return false; }
131 
132  return driver->getPeriodDiff( cont_no, diff );
133 }
134 
136 {
137  if ( driver == NULL ) { return; }
138 
139  boost::thread thread_srv( KhiCommandService, driver );
140 }
141 
142 } // end of khi_robot_control namespace
virtual bool hold(const int &cont_no, const KhiRobotData &data)=0
int getStateTrigger(const int &cont_no)
bool hold(const KhiRobotData &data)
virtual bool writeData(const int &cont_no, const KhiRobotData &data)=0
virtual bool readData(const int &cont_no, KhiRobotData &data)=0
bool activate(KhiRobotData &data)
void deactivate(const KhiRobotData &data)
int updateState(const KhiRobotData &data)
void spinner()
virtual bool deactivate(const int &cont_no, const KhiRobotData &data)=0
virtual bool getPeriodDiff(const int &cont_no, double &diff)=0
#define ROS_INFO(...)
virtual bool close(const int &cont_no)=0
bool open(const std::string &ip, const double &period, KhiRobotData &data, const bool in_simulation=false)
virtual bool updateState(const int &cont_no, const KhiRobotData &data)=0
void write(const KhiRobotData &data)
void KhiCommandService(KhiRobotDriver *driver)
virtual bool commandHandler(khi_robot_msgs::KhiRobotCmd::Request &req, khi_robot_msgs::KhiRobotCmd::Response &res)=0
virtual bool open(const int &cont_no, const std::string &ip_address, KhiRobotData &data)=0
virtual bool activate(const int &cont_no, KhiRobotData &data)=0
virtual bool initialize(const int &cont_no, const double &period, KhiRobotData &data, const bool in_simulation=false)=0
ROSCPP_DECL void waitForShutdown()


khi_robot_control
Author(s): nakamichi_d
autogenerated on Fri Mar 26 2021 02:34:21