forked from CentroEPiaggio/kuka-lwr
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlwr_hw_fri.hpp
More file actions
282 lines (235 loc) · 8.39 KB
/
Copy pathlwr_hw_fri.hpp
File metadata and controls
282 lines (235 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#ifndef LWR_HW__LWR_HW_REAL_H
#define LWR_HW__LWR_HW_REAL_H
// lwr hw definition
#include "lwr_hw/lwr_hw.h"
// FRI remote hooks
#include <iostream>
#include <cstdlib>
#include <math.h>
#include <limits.h>
#include <thread>
#include "fri/friudp.h"
#include "fri/friremote.h"
// ToDo: add timeouts to all sync-while's to KRL since the UDP connection might be lost and we will know
namespace lwr_hw
{
class LWRHWFRI : public LWRHW
{
public:
LWRHWFRI() : LWRHW() {}
~LWRHWFRI() { stopKRCComm_ = true; KRCCommThread_.get()->join();}
void setPort(int port){port_ = port; port_set_ = true;};
void setIP(std::string hintToRemoteHost){hintToRemoteHost_ = hintToRemoteHost; ip_set_ = true;};
float getSampleTime(){return sampling_rate_;};
// Init, read, and write, with FRI hooks
bool init()
{
if( !(port_set_) || !(ip_set_) )
{
std::cout << "Did you forget to set the port/ip?" << std::endl << "You must do that before init()" << std::endl << "Exiting..." << std::endl;
return false;
}
// construct a low-level lwr
device_.reset( new friRemote( port_, const_cast<char*>(hintToRemoteHost_.c_str()) ) );
// initialize FRI values
lastQuality_ = FRI_QUALITY_BAD;
lastCtrlScheme_ = FRI_CTRL_OTHER;
std::cout << "Opening FRI Version "
<< FRI_MAJOR_VERSION << "." << FRI_SUB_VERSION << "." <<FRI_DATAGRAM_ID_CMD << "." <<FRI_DATAGRAM_ID_MSR
<< " Interface for LWR ROS server" << std::endl;
std::cout << "Checking if the robot is Stopped..." << std::endl;
if( device_->getState() == FRI_STATE_OFF )
{
std::cout << "Please, start the KRL script now." << std::endl;
}
KRCCommThread_.reset( new std::thread( &LWRHWFRI::KRCCommThreadCallback,this ) );
startFRI();
std::cout << "Ready, FRI has been started!" << std::endl;
std::cout << "FRI Status:\n" << device_->getMsrBuf().intf << std::endl;
sampling_rate_ = device_->getSampleTime();
std::cout << "Sampling Rate: " << sampling_rate_ << std::endl;
return true;
}
void read(ros::Time time, ros::Duration period)
{
for (int j = 0; j < n_joints_; j++)
{
joint_position_prev_[j] = joint_position_[j];
joint_position_[j] = device_->getMsrMsrJntPosition()[j];
joint_position_kdl_(j) = joint_position_[j];
joint_effort_[j] = device_->getMsrJntTrq()[j];
joint_velocity_[j] = filters::exponentialSmoothing((joint_position_[j]-joint_position_prev_[j])/period.toSec(), joint_velocity_[j], 0.2);
joint_stiffness_[j] = joint_stiffness_command_[j];
joint_damping_[j] = joint_damping_command_[j];
}
for(int j = 0; j < 12; j++)
{
cart_pos_[j] = device_->getMsrCartPosition()[j];
}
for(int j = 0; j < 6; j++)
{
cart_stiff_[j] = cart_stiff_command_[j];
cart_damp_[j] = cart_damp_command_[j];
cart_wrench_[j] = cart_wrench_command_[j];
}
return;
}
void write(ros::Time time, ros::Duration period)
{
enforceLimits(period);
float newJntPosition[n_joints_];
float newJntStiff[n_joints_];
float newJntDamp[n_joints_];
float newJntAddTorque[n_joints_];
float newCartPos[12];
float newCartStiff[6];
float newCartDamp[6];
float newAddFT[6];
switch (getControlStrategy())
{
case JOINT_POSITION:
for (int j = 0; j < n_joints_; j++)
{
newJntPosition[j] = joint_position_command_[j];
}
device_->doPositionControl(newJntPosition, false);
break;
case CARTESIAN_IMPEDANCE:
for(int i=0; i < 12; ++i)
{
newCartPos[i] = cart_pos_command_[i];
}
for(int i=0; i < 6; i++)
{
newCartStiff[i] = cart_stiff_command_[i];
newCartDamp[i] = cart_damp_command_[i];
newAddFT[i] = cart_wrench_command_[i];
}
device_->doCartesianImpedanceControl(newCartPos, newCartStiff, newCartDamp, newAddFT, NULL, false);
break;
case JOINT_IMPEDANCE:
for(int j=0; j < n_joints_; j++)
{
newJntPosition[j] = joint_set_point_command_[j];
newJntAddTorque[j] = joint_effort_command_[j];
newJntStiff[j] = joint_stiffness_command_[j];
newJntDamp[j] = joint_damping_command_[j];
}
device_->doJntImpedanceControl(newJntPosition, newJntStiff, newJntDamp, newJntAddTorque, false);
break;
case JOINT_EFFORT:
for(int j=0; j < n_joints_; j++)
{
newJntAddTorque[j] = joint_effort_command_[j];
newJntStiff[j] = 0.0;
}
// mirror the position
device_->doJntImpedanceControl(device_->getMsrMsrJntPosition(), newJntStiff, NULL, newJntAddTorque, false);
break;
case JOINT_STIFFNESS:
for(int j=0; j < n_joints_; j++)
{
newJntPosition[j] = joint_set_point_command_[j];
newJntStiff[j] = joint_stiffness_command_[j];
}
device_->doJntImpedanceControl(newJntPosition, newJntStiff, NULL, NULL, false);
break;
case GRAVITY_COMPENSATION:
device_->doJntImpedanceControl(device_->getMsrMsrJntPosition(), NULL, NULL, NULL, false);
break;
}
return;
}
void doSwitch(const std::list<hardware_interface::ControllerInfo> &start_list, const std::list<hardware_interface::ControllerInfo> &stop_list)
{
// at this point, we now that there is only one controller that ones to command joints
ControlStrategy desired_strategy = JOINT_POSITION; // default
desired_strategy = getNewControlStrategy(start_list,stop_list,desired_strategy);
for (int j = 0; j < n_joints_; ++j)
{
///semantic Zero
joint_position_command_[j] = joint_position_[j];
joint_effort_command_[j] = 0.0;
///call setCommand once so that the JointLimitsInterface receive the correct value on their getCommand()!
try{ position_interface_.getHandle(joint_names_[j]).setCommand(joint_position_command_[j]); }
catch(const hardware_interface::HardwareInterfaceException&){}
try{ effort_interface_.getHandle(joint_names_[j]).setCommand(joint_effort_command_[j]); }
catch(const hardware_interface::HardwareInterfaceException&){}
///reset joint_limit_interfaces
pj_sat_interface_.reset();
pj_limits_interface_.reset();
}
if(desired_strategy == getControlStrategy())
{
std::cout << "The ControlStrategy didn't change, it is already: " << getControlStrategy() << std::endl;
}
else
{
stopFRI();
// send to KRL the new strategy
if( desired_strategy == JOINT_POSITION )
device_->setToKRLInt(0, JOINT_POSITION);
else if( desired_strategy == JOINT_IMPEDANCE)
device_->setToKRLInt(0, JOINT_IMPEDANCE);
else if( desired_strategy == CARTESIAN_IMPEDANCE)
device_->setToKRLInt(0, CARTESIAN_IMPEDANCE);
startFRI();
setControlStrategy(desired_strategy);
std::cout << "The ControlStrategy changed to: " << getControlStrategy() << std::endl;
}
}
private:
// Parameters
int port_;
bool port_set_ = false;
std::string hintToRemoteHost_;
bool ip_set_ = false;
// low-level interface
boost::shared_ptr<friRemote> device_;
// FRI values
FRI_QUALITY lastQuality_;
FRI_CTRL lastCtrlScheme_;
float sampling_rate_;
boost::shared_ptr<std::thread> KRCCommThread_;
bool stopKRCComm_ = false;
void KRCCommThreadCallback()
{
while(!stopKRCComm_)
{
device_->doDataExchange();
}
return;
}
void startFRI()
{
// wait until FRI enters in command mode
// std::cout << "Waiting for good communication quality..." << std::endl;
// while( device_->getQuality() != FRI_QUALITY_OK ){};
device_->setToKRLInt(1, 1);
device_->doDataExchange();
// std::cout << "Waiting for command mode..." << std::endl;
// while ( device_->getFrmKRLInt(1) != 1 )
// {
// std::cout << "device_->getState(): " << device_->getState() << std::endl;
// device_->setToKRLInt(1, 1);
// usleep(1000000);
// }
return;
}
void stopFRI()
{
// wait until FRI enters in command mode
device_->setToKRLInt(1, 0);
std::cout << "Waiting for monitor mode..." << std::endl;
while ( device_->getFrmKRLInt(1) != 0 ){}
// {
// std::cout << "device_->getState(): " << device_->getState() << std::endl;
// std::cout << "Waiting for monitor mode..." << std::endl;
// device_->setToKRLInt(1, 0);
// usleep(1000000);
// }
return;
}
};
}
#endif