forked from CentroEPiaggio/kuka-lwr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlwr_hw_fril.hpp
233 lines (190 loc) · 7.22 KB
/
lwr_hw_fril.hpp
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
#ifndef LWR_HW__LWR_HW_REAL_H
#define LWR_HW__LWR_HW_REAL_H
// lwr hw definition
#include "lwr_hw/lwr_hw.h"
// FRIL remote hooks
#include <FastResearchInterface.h>
#define NUMBER_OF_CYCLES_FOR_QUAULITY_CHECK 2000
#define EOK 0
namespace lwr_hw
{
class LWRHWFRIL : public LWRHW
{
public:
LWRHWFRIL() : LWRHW() {}
~LWRHWFRIL() {}
void stop(){return;};
void set_mode(){return;};
void setInitFile(std::string init_file){init_file_ = init_file; file_set_ = true;};
// Init, read, and write, with FRI hooks
bool init()
{
if( !(file_set_) )
{
std::cout << "Did you forget to set the init file?" << std::endl
<< "You must do that before init()" << std::endl
<< "Exiting..." << std::endl;
return false;
}
// construct a low-level lwr
device_.reset( new FastResearchInterface( init_file_.c_str() ) );
ResultValue = device_->StartRobot( FRI_CONTROL_POSITION );
if (ResultValue != EOK)
{
std::cout << "An error occurred during starting up the robot...\n" << std::endl;
return false;
}
return true;
}
void read(ros::Time time, ros::Duration period)
{
float msrJntPos[n_joints_];
float msrJntTrq[n_joints_];
device_->GetMeasuredJointPositions( msrJntPos );
device_->GetMeasuredJointTorques( msrJntTrq );
for (int j = 0; j < n_joints_; j++)
{
joint_position_prev_[j] = joint_position_[j];
joint_position_[j] = (double)msrJntPos[j];
joint_position_kdl_(j) = joint_position_[j];
joint_effort_[j] = (double)msrJntTrq[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];
}
return;
}
void write(ros::Time time, ros::Duration period)
{
enforceLimits(period);
// ensure the robot is powered and it is in control mode, almost like the isMachineOk() of Standford
if ( device_->IsMachineOK() )
{
device_->WaitForKRCTick();
switch (getControlStrategy())
{
case JOINT_POSITION:
// Ensure the robot is in this mode
if( (device_->GetCurrentControlScheme() == FRI_CONTROL_POSITION) )
{
float newJntPosition[n_joints_];
for (int j = 0; j < n_joints_; j++)
{
newJntPosition[j] = (float)joint_position_command_[j];
}
device_->SetCommandedJointPositions(newJntPosition);
}
break;
case CARTESIAN_IMPEDANCE:
break;
case JOINT_IMPEDANCE:
// Ensure the robot is in this mode
if( (device_->GetCurrentControlScheme() == FRI_CONTROL_JNT_IMP) )
{
float newJntPosition[n_joints_];
float newJntStiff[n_joints_];
float newJntDamp[n_joints_];
float newJntAddTorque[n_joints_];
// WHEN THE URDF MODEL IS PRECISE
// 1. compute the gracity term
// f_dyn_solver_->JntToGravity(joint_position_kdl_, gravity_effort_);
// 2. read gravity term from FRI and add it with opposite sign and add the URDF gravity term
// newJntAddTorque = gravity_effort_ - device_->getF_DYN??
for(int j=0; j < n_joints_; j++)
{
newJntPosition[j] = (float)joint_position_command_[j];
newJntAddTorque[j] = (float)joint_effort_command_[j];
newJntStiff[j] = (float)joint_stiffness_command_[j];
newJntDamp[j] = (float)joint_damping_command_[j];
}
device_->SetCommandedJointStiffness(newJntStiff);
device_->SetCommandedJointPositions(newJntPosition);
device_->SetCommandedJointDamping(newJntDamp);
device_->SetCommandedJointTorques(newJntAddTorque);
}
break;
case GRAVITY_COMPENSATION:
break;
}
}
return;
}
void doSwitch(const std::list<hardware_interface::ControllerInfo> &start_list, const std::list<hardware_interface::ControllerInfo> &stop_list)
{
ResultValue = device_->StopRobot();
if (ResultValue != EOK)
{
std::cout << "An error occurred during stopping the robot, couldn't switch mode...\n" << std::endl;
return;
}
// at this point, we now that there is only one controller that ones to command joints
ControlStrategy desired_strategy = JOINT_POSITION; // default
// If any of the controllers in the start list works on a velocity interface, the switch can't be done.
for ( std::list<hardware_interface::ControllerInfo>::const_iterator it = start_list.begin(); it != start_list.end(); ++it )
{
if( it->hardware_interface.compare( std::string("hardware_interface::PositionJointInterface") ) == 0 )
{
std::cout << "Request to switch to hardware_interface::PositionJointInterface (JOINT_POSITION)" << std::endl;
desired_strategy = JOINT_POSITION;
break;
}
else if( it->hardware_interface.compare( std::string("hardware_interface::EffortJointInterface") ) == 0 )
{
std::cout << "Request to switch to hardware_interface::EffortJointInterface (JOINT_IMPEDANCE)" << std::endl;
desired_strategy = JOINT_IMPEDANCE;
break;
}
}
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 changed, it is already: " << getControlStrategy() << std::endl;
}
else
{
switch( desired_strategy )
{
case JOINT_POSITION:
ResultValue = device_->StartRobot( FRI_CONTROL_POSITION );
if (ResultValue != EOK)
{
std::cout << "An error occurred during starting the robot, couldn't switch to JOINT_POSITION...\n" << std::endl;
return;
}
break;
case JOINT_IMPEDANCE:
ResultValue = device_->StartRobot( FRI_CONTROL_JNT_IMP );
if (ResultValue != EOK)
{
std::cout << "An error occurred during starting the robot, couldn't switch to JOINT_IMPEDANCE...\n" << std::endl;
return;
}
break;
}
// if sucess during the switch in FRI, set the ROS strategy
setControlStrategy(desired_strategy);
std::cout << "The ControlStrategy changed to: " << getControlStrategy() << std::endl;
}
}
private:
// Parameters
std::string init_file_;
bool file_set_ = false;
// low-level interface
boost::shared_ptr<FastResearchInterface> device_;
int ResultValue = 0;
};
}
#endif