khi_robot_driver.h
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 #ifndef KHI_ROBOT_DRIVER_H
36 #define KHI_ROBOT_DRIVER_H
37 
38 #include <string>
39 #include <khi_robot_msgs/KhiRobotCmd.h>
40 
41 namespace khi_robot_control
42 {
43 #define KHI_MAX_CONTROLLER 8
44 #define KHI_MAX_ARM 2
45 #define KHI_MAX_JOINT 18
46 #define KHI_MAX_SIG_SIZE 512
47 
49 {
50  int jt_num;
51  std::string name[KHI_MAX_JOINT];
53  double cmd[KHI_MAX_JOINT];
54  double pos[KHI_MAX_JOINT];
55  double vel[KHI_MAX_JOINT];
56  double eff[KHI_MAX_JOINT];
58 };
59 
61 {
62  std::string robot_name;
63  int arm_num;
65 };
66 
68 {
69  int state;
71  std::string ip_address;
72  double period;
73 };
74 
76 {
77  STATE_MIN = -1,
90 };
91 const static std::string KhiRobotStateName[STATE_MAX] =
92 {
93  "INIT",
94  "CONNECTING",
95  "INACTIVE",
96  "ACTIVATING",
97  "ACTIVE",
98  "HOLDED",
99  "DEACTIVATING",
100  "DISCONNECTING",
101  "DISCONNECTED",
102  "ERROR",
103  "NOT_REGISTERED"
104 };
105 
107 {
114 };
115 const static std::string KhiRobotStateTriggerName[TRIGGER_MAX] =
116 {
117  "NONE",
118  "HOLD",
119  "RESTART",
120  "QUIT"
121 };
122 
124 {
125 public:
127  {
128  for ( int cno = 0; cno < KHI_MAX_CONTROLLER; cno++ )
129  {
130  cont_info[cno].state = INIT;
131  cont_info[cno].state_trigger = NONE;
132  cont_info[cno].ip_address = "127.0.0.1";
133  }
134 
135  driver_name = __func__;
136  }
137 
138  int getState( const int& cont_no )
139  {
140  if ( ( cont_no < 0 ) || ( cont_no > KHI_MAX_CONTROLLER ) ) { return NOT_REGISTERED; }
141  else { return cont_info[cont_no].state; }
142  }
143 
144  std::string getStateName( const int& cont_no )
145  {
146  int state;
147  std::string name = "";
148 
149  state = getState( cont_no );
150  if ( ( state > STATE_MIN ) && ( state < STATE_MAX ) )
151  {
152  name = KhiRobotStateName[state];
153  }
154 
155  return name;
156  }
157 
158  bool setState( const int& cont_no, const int& state )
159  {
160  if ( !contLimitCheck( cont_no, KHI_MAX_CONTROLLER ) ) { return false; }
161 
162  if ( ( state <= STATE_MIN ) || ( state >= STATE_MAX ) )
163  {
164  return false;
165  }
166  else
167  {
168  if ( cont_info[cont_no].state != state )
169  {
170  infoPrint( "State %d: %s -> %s", cont_no, KhiRobotStateName[cont_info[cont_no].state].c_str(), KhiRobotStateName[state].c_str() );
171  cont_info[cont_no].state = state;
172  }
173  return true;
174  }
175  }
176 
177  bool isTransitionState( const int& cont_no )
178  {
179  int state;
180 
181  state = getState( cont_no );
182  if ( ( state == CONNECTING ) || ( state == ACTIVATING ) || ( state == DEACTIVATING ) || ( state == DISCONNECTING ) )
183  {
184  return true;
185  }
186  else
187  {
188  return false;
189  }
190  }
191 
192  int getStateTrigger( const int& cont_no )
193  {
194  int state_trigger;
195 
196  if ( ( cont_no < 0 ) || ( cont_no > KHI_MAX_CONTROLLER ) ) { return NONE; }
197  else
198  {
199  state_trigger = cont_info[cont_no].state_trigger;
200  cont_info[cont_no].state_trigger = NONE;
201  return state_trigger;
202  }
203  }
204 
205  bool setStateTrigger( const int& cont_no, const int& state_trigger )
206  {
207  if ( !contLimitCheck( cont_no, KHI_MAX_CONTROLLER ) ) { return false; }
208 
209  if ( ( state_trigger <= TRIGGER_MIN ) || ( state_trigger >= TRIGGER_MAX ) )
210  {
211  return false;
212  }
213  else
214  {
215  if ( cont_info[cont_no].state_trigger != NONE )
216  {
217  warnPrint( "State Trigger is already done %d: %s", cont_no, KhiRobotStateTriggerName[state_trigger].c_str() );
218  }
219  infoPrint( "State Trigger %d: %s", cont_no, KhiRobotStateTriggerName[state_trigger].c_str() );
220  cont_info[cont_no].state_trigger = state_trigger;
221  return true;
222  }
223  }
224 
225  bool contLimitCheck( const int& cont_no, const int& limit )
226  {
227  if ( ( cont_no < 0 ) || ( cont_no > KHI_MAX_CONTROLLER ) || ( cont_no > limit ) )
228  {
229  errorPrint( "contLimitCheck ERROR!" );
230  return false;
231  }
232  else
233  {
234  return true;
235  }
236  }
237 
238  void infoPrint( const char* format, ... )
239  {
240  char msg[512] = { 0 };
241  va_list ap;
242 
243  va_start( ap, format );
244  vsnprintf( msg, sizeof(msg), format, ap );
245  va_end( ap );
246  ROS_INFO( "[%s] %s", driver_name.c_str(), msg );
247  }
248 
249  void warnPrint( const char* format, ... )
250  {
251  char msg[512] = { 0 };
252  va_list ap;
253 
254  va_start( ap, format );
255  vsnprintf( msg, sizeof(msg), format, ap );
256  va_end( ap );
257  ROS_WARN( "[%s] %s", driver_name.c_str(), msg );
258  }
259 
260  void errorPrint( const char* format, ... )
261  {
262  char msg[512] = { 0 };
263  va_list ap;
264 
265  va_start( ap, format );
266  vsnprintf( msg, sizeof(msg), format, ap );
267  va_end( ap );
268  ROS_ERROR( "[%s] %s", driver_name.c_str(), msg );
269  }
270 
271  void jointPrint( std::string name, const KhiRobotData& data )
272  {
273  char msg[512] = { 0 };
274  char jt_val[16] = { 0 };
275  const double* value;
276 
277  snprintf( msg, sizeof(msg), "[%s]\t", name.c_str() );
278  for ( int ano = 0; ano < KHI_MAX_ARM; ano++ )
279  {
280  if ( name == std::string("write") ) { value = data.arm[ano].cmd; }
281  else { value = data.arm[ano].pos; }
282  for ( int jt = 0; jt < data.arm[ano].jt_num; jt++ )
283  {
284  snprintf( jt_val, sizeof(jt_val), "%.3lf\t", value[jt] );
285  strcat( msg, jt_val );
286  }
287  }
288  infoPrint( "[SIM]%s", msg );
289  }
290 
291  virtual ~KhiRobotDriver() {};
292  virtual bool initialize( const int& cont_no, const double& period, KhiRobotData& data, const bool in_simulation = false ) = 0;
293  virtual bool open( const int& cont_no, const std::string& ip_address, KhiRobotData& data ) = 0;
294  virtual bool close( const int& cont_no ) = 0;
295  virtual bool activate( const int& cont_no, KhiRobotData& data ) = 0;
296  virtual bool hold( const int& cont_no, const KhiRobotData& data ) = 0;
297  virtual bool deactivate( const int& cont_no, const KhiRobotData& data ) = 0;
298  virtual bool readData( const int& cont_no, KhiRobotData& data ) = 0;
299  virtual bool writeData( const int& cont_no, const KhiRobotData& data ) = 0;
300  virtual bool updateState( const int& cont_no, const KhiRobotData& data ) = 0;
301  virtual bool getPeriodDiff( const int& cont_no, double& diff ) = 0;
302  virtual bool commandHandler( khi_robot_msgs::KhiRobotCmd::Request& req, khi_robot_msgs::KhiRobotCmd::Response& res ) = 0;
303 
304 protected:
306  std::string driver_name;
310 };
311 
312 } // namespace
313 
314 #endif // KHI_ROBOT_DRIVER_H
msg
void errorPrint(const char *format,...)
int getStateTrigger(const int &cont_no)
ROSCONSOLE_DECL void initialize()
#define KHI_MAX_JOINT
data
#define KHI_MAX_CONTROLLER
#define ROS_WARN(...)
KhiRobotArmData arm[KHI_MAX_ARM]
bool isTransitionState(const int &cont_no)
static const std::string KhiRobotStateTriggerName[TRIGGER_MAX]
#define ROS_INFO(...)
int getState(const int &cont_no)
bool setStateTrigger(const int &cont_no, const int &state_trigger)
std::string getStateName(const int &cont_no)
void jointPrint(std::string name, const KhiRobotData &data)
#define KHI_MAX_ARM
bool activate(khi_robot_control::KhiRobotHardwareInterface &robot, struct timespec *tick)
Definition: main.cpp:252
std::string name[KHI_MAX_JOINT]
bool setState(const int &cont_no, const int &state)
void infoPrint(const char *format,...)
#define ROS_ERROR(...)
void warnPrint(const char *format,...)
static const std::string KhiRobotStateName[STATE_MAX]
bool contLimitCheck(const int &cont_no, const int &limit)


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