melfa_hardware_interface.cpp
Go to the documentation of this file.
1 // Copyright 2018 Tokyo Opensource Robotics Kyokai Association (TORK)
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 MelfaHW::MelfaHW (double period)
18  : counter_(0), period_(period),
19  use_joint7_(false), use_joint8_(false),
20  joint7_is_linear_(false), joint8_is_linear_(false)
21 {
22  // initialize UDP socket
23  socket_ = socket (AF_INET, SOCK_DGRAM, 0);
24  if (socket_ < 0)
25  {
26  ROS_ERROR ("Cannot open socket");
27  exit(1);
28  }
29  // set IP and port
30  ros::param::param<std::string>("~robot_ip", robot_ip_, "127.0.0.1");
31  // Usage of additional joints
32  ros::param::param<bool>("~use_joint7", use_joint7_, false);
33  ros::param::param<bool>("~use_joint8", use_joint8_, false);
34  ros::param::param<bool>("~joint7_is_linear", joint7_is_linear_, false);
35  ros::param::param<bool>("~joint8_is_linear", joint8_is_linear_, false);
36 
37  addr_.sin_family = AF_INET;
38  addr_.sin_port = htons (10000);
39  addr_.sin_addr.s_addr = inet_addr (robot_ip_.c_str());
40 
41  memset (&send_buff_, 0, sizeof (send_buff_));
42  memset (&recv_buff_, 0, sizeof (recv_buff_));
43 
44  std::string joint_names[JOINT_NUM] =
45  {
46  "joint1", "joint2", "joint3", "joint4",
47  "joint5", "joint6", "joint7", "joint8"
48  };
49  // connect and register the joint state interface
50  for (int i = 0; i < JOINT_NUM; i++)
51  {
52  // Skip unused joints
53  if (i==6 and not use_joint7_)
54  {
55  continue;
56  }
57  if (i==7 and not use_joint8_)
58  {
59  continue;
60  }
61  hardware_interface::JointStateHandle state_handle (joint_names[i], &pos[i], &vel[i], &eff[i]);
63  }
65 
66  // connect and register the joint position interface
67  for (int i = 0; i < JOINT_NUM; i++)
68  {
69  // Skip unused joints
70  if (i==6 and not use_joint7_)
71  {
72  continue;
73  }
74  if (i==7 and not use_joint8_)
75  {
76  continue;
77  }
78  hardware_interface::JointHandle joint_pos_handle (joint_state_interface.getHandle (joint_names[i]), &cmd[i]);
79  joint_pos_interface.registerHandle (joint_pos_handle);
80  }
82 }
83 
85 {
86  memset (&send_buff_, 0, sizeof (send_buff_));
87  // Get current joint angles in
93  send_buff_.BitTop = 0;
94  send_buff_.BitMask = 0;
95  send_buff_.IoData = 0;
96  send_buff_.CCount = 0;
97 
98  int size = sendto (socket_, (char *) &send_buff_, sizeof (send_buff_), 0, (struct sockaddr *) &addr_, sizeof (addr_));
99  if (size != sizeof (send_buff_))
100  {
101  ROS_ERROR ("Connot send packet to MELFA controller. Check the configuration.");
102  exit (1);
103  }
104 }
105 
106 void MelfaHW::write (void)
107 {
108  // Send MOVE command
109  memset (&send_buff_, 0, sizeof (send_buff_));
116 
119  send_buff_.BitTop = 0;
120  send_buff_.BitMask = 0;
121  send_buff_.IoData = 0;
122  send_buff_.dat.jnt.j1 = cmd[0];
123  send_buff_.dat.jnt.j2 = cmd[1];
124  send_buff_.dat.jnt.j3 = cmd[2];
125  send_buff_.dat.jnt.j4 = cmd[3];
126  send_buff_.dat.jnt.j5 = cmd[4];
127  send_buff_.dat.jnt.j6 = cmd[5];
128  if (joint7_is_linear_)
129  {
130  // Convert unit to mm
131  send_buff_.dat.jnt.j7 = cmd[6] * 1000.0;
132  }
133  else
134  {
135  send_buff_.dat.jnt.j7 = cmd[6];
136  }
137  if (joint8_is_linear_)
138  {
139  // Convert unit to [mm]
140  send_buff_.dat.jnt.j8 = cmd[7] * 1000.0;
141  }
142  else
143  {
144  send_buff_.dat.jnt.j8 = cmd[7];
145  }
147 
148  int size = sendto (socket_, (char *) &send_buff_, sizeof (send_buff_), 0, (struct sockaddr *) &addr_, sizeof (addr_));
149  if (size != sizeof (send_buff_))
150  {
151  ROS_ERROR ("Connot send packet to MELFA controller. Check the configuration.");
152  exit (1);
153  }
154 }
155 
156 void MelfaHW::read (void)
157 {
158  fd_set fds;
159  timeval time;
160 
161  // Record time
164 
165  memset (&recv_buff_, 0, sizeof (recv_buff_));
166 
167  FD_ZERO (&fds);
168  FD_SET (socket_, &fds);
169 
170  time.tv_sec = 0;
171  time.tv_usec = 2 * period_ * 1000000;
172 
173  int status = select (socket_+1, &fds, (fd_set *) NULL, (fd_set *) NULL, &time);
174  if (status < 0)
175  {
176  ROS_ERROR ("Cannot receive packet");
177  }
178  if ((status > 0) && FD_ISSET (socket_, &fds))
179  {
180  int size = recvfrom (socket_, &recv_buff_, sizeof (recv_buff_), 0, NULL, NULL);
181  if (size < 0)
182  {
183  ROS_ERROR ("recvfrom failed");
184  exit (1);
185  }
186  JOINT *joint = (JOINT *) & recv_buff_.dat;
187  pos[0] = joint->j1;
188  pos[1] = joint->j2;
189  pos[2] = joint->j3;
190  pos[3] = joint->j4;
191  pos[4] = joint->j5;
192  pos[5] = joint->j6;
193 
194  if (joint7_is_linear_)
195  {
196  // Convert unit to [m]
197  pos[6] = joint->j7 / 1000.0;
198  }
199  else
200  {
201  pos[6] = joint->j7;
202  }
203 
204  if (joint8_is_linear_)
205  {
206  // Convert unit to [m]
207  pos[7] = joint->j8 / 1000.0;
208  }
209  else
210  {
211  pos[7] = joint->j8;
212  }
213  // Set current position when it starts
214  if (counter_ == 0)
215  {
216  for (int i = 0; i < JOINT_NUM; i++)
217  {
218  cmd[i] = pos[i];
219  }
220  }
221  counter_++;
222  }
223  else
224  {
225  ROS_WARN ("Failed to receive packet.");
226  }
227 }
228 
230 {
231  stat.add ("Counter", counter_);
232 
233  double diff = (time_now_ - time_old_).toSec();
234  // Check over-run
235  if (diff > period_ * 1.2)
236  {
237  stat.summary(diagnostic_msgs::DiagnosticStatus::WARN,
238  "Periodic time exceeds 120%");
239  }
240  else
241  {
242  stat.summary(diagnostic_msgs::DiagnosticStatus::OK,
243  "Periodic time is normal");
244  }
245  stat.add ("Period", diff);
246 }
uint16_t BitTop
Definition: strdef.h:115
#define MXT_TYP_FJOINT
Definition: strdef.h:95
float j3
Definition: strdef.h:30
uint16_t IoData
Definition: strdef.h:117
double pos[JOINT_NUM]
uint16_t RecvType2
Definition: strdef.h:127
double cmd[JOINT_NUM]
uint16_t RecvType1
Definition: strdef.h:120
double vel[JOINT_NUM]
hardware_interface::PositionJointInterface joint_pos_interface
void summary(unsigned char lvl, const std::string s)
void diagnose(diagnostic_updater::DiagnosticStatusWrapper &stat)
Definition: strdef.h:27
float j1
Definition: strdef.h:28
uint16_t SendIOType
Definition: strdef.h:110
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
double eff[JOINT_NUM]
struct sockaddr_in addr_
uint16_t RecvType3
Definition: strdef.h:134
float j6
Definition: strdef.h:33
#define ROS_WARN(...)
#define MXT_CMD_MOVE
Definition: strdef.h:78
uint16_t BitMask
Definition: strdef.h:116
float j5
Definition: strdef.h:32
#define MXT_TYP_FBKCUR
Definition: strdef.h:103
uint16_t RecvType
Definition: strdef.h:81
union enet_rtcmd_str::rtdata dat
#define MXT_TYP_JOINT
Definition: strdef.h:90
#define MXT_IO_NULL
Definition: strdef.h:112
float j2
Definition: strdef.h:29
float j7
Definition: strdef.h:34
float j4
Definition: strdef.h:31
#define MXT_TYP_NULL
Definition: strdef.h:85
void registerHandle(const JointStateHandle &handle)
float j8
Definition: strdef.h:35
MelfaHW(double period)
JointStateHandle getHandle(const std::string &name)
#define MXT_TYP_FB_JOINT
Definition: strdef.h:98
uint16_t Command
Definition: strdef.h:76
uint16_t SendType
Definition: strdef.h:80
static Time now()
uint32_t CCount
Definition: strdef.h:119
void add(const std::string &key, const T &val)
#define ROS_ERROR(...)
void write_first(void)
#define JOINT_NUM
#define MXT_CMD_NULL
Definition: strdef.h:77
uint16_t RecvIOType
Definition: strdef.h:111
std::string robot_ip_
hardware_interface::JointStateInterface joint_state_interface


melfa_driver
Author(s): Ryosuke Tajima
autogenerated on Mon Jun 10 2019 14:04:51