1 #ifndef GRL_KUKA_JAVA_DRIVER 2 #define GRL_KUKA_JAVA_DRIVER 12 #include <boost/asio.hpp> 13 #include <boost/circular_buffer.hpp> 14 #include <boost/exception/all.hpp> 15 #include <boost/config.hpp> 16 #include <boost/lexical_cast.hpp> 18 #include <boost/range/algorithm/copy.hpp> 19 #include <boost/range/algorithm/transform.hpp> 20 #include <boost/chrono/include.hpp> 21 #include <boost/chrono/duration.hpp> 24 #include <sys/types.h> 25 #include <sys/socket.h> 27 #include <sys/select.h> 28 #include <netinet/in.h> 32 #ifdef BOOST_NO_CXX11_ATOMIC_SMART_PTR 33 #include <boost/thread.hpp> 47 inline std::ostream& operator<<(std::ostream& out, std::vector<T>& v)
50 size_t last = v.size() - 1;
51 for(
size_t i = 0; i < v.size(); ++i) {
62 template<
typename T, std::
size_t U>
63 inline std::ostream& operator<<(std::ostream& out, boost::container::static_vector<T,U>& v)
66 size_t last = v.size() - 1;
67 for(
size_t i = 0; i < v.size(); ++i) {
77 namespace grl {
namespace robot {
namespace arm {
123 return std::make_tuple(
125 "KUKA_LBR_IIWA_14_R820" ,
186 BOOST_LOG_TRIVIAL(trace) <<
"KukaLBRiiwaRosPlugin: Connecting UDP Socket from " <<
187 std::get<LocalUDPAddress> (params_) <<
":" << std::get<LocalUDPPort> (params_) <<
" to " <<
188 std::get<RemoteUDPAddress> (params_);
191 socket_local = socket(AF_INET, SOCK_DGRAM, 0);
192 if (socket_local < 0) {
193 BOOST_THROW_EXCEPTION(std::runtime_error(
"KukaJAVAdriver Error opening socket. Check that the port is available, and that all the cables are connected tightly. If you have no other options try restarting your computer."));
196 port = boost::lexical_cast<
int>( std::get<LocalUDPPort> (params_));
198 inet_pton(AF_INET, std::get<LocalUDPAddress>(params_).c_str(), &(local_sockaddr.sin_addr));
199 local_sockaddr.sin_family = AF_INET;
200 local_sockaddr.sin_port = htons(port);
206 if (bind(socket_local, (
struct sockaddr *)&local_sockaddr,
sizeof(local_sockaddr)) < 0) {
207 printf(
"Error binding sr_joint!\n");
208 BOOST_THROW_EXCEPTION(std::runtime_error(
"KukaJAVAdriver Error opening socket. Check that the port is available, and that all the cables are connected tightly. If you have no other options try restarting your computer."));
212 FD_ZERO(&dummy_mask);
213 FD_SET(socket_local, &mask);
216 }
catch( boost::exception &e) {
217 e <<
errmsg_info(
"KukaLBRiiwaRosPlugin: Unable to connect to UDP Socket from " +
218 std::get<LocalUDPAddress> (params_) +
" to " +
219 std::get<RemoteUDPAddress> (params_));
251 bool haveNewData =
false;
259 std::shared_ptr<flatbuffers::FlatBufferBuilder> fbbP;
260 fbbP = std::make_shared<flatbuffers::FlatBufferBuilder>();
262 boost::lock_guard<boost::mutex> lock(jt_mutex);
264 double duration = boost::chrono::high_resolution_clock::now().time_since_epoch().count();
267 auto basename = std::get<RobotName>(params_);
269 auto bns = fbbP->CreateString(basename);
271 flatbuffers::Offset<flatbuffer::ArmControlState> controlState;
274 switch (armControlMode_) {
283 auto armPositionBuffer = fbbP->CreateVector(armState_.commandedPosition_goal.data(),armState_.commandedPosition_goal.size());
284 auto commandedTorque = fbbP->CreateVector(armState_.commandedTorque.data(),armState_.commandedTorque.size());
288 std::cout <<
"\nKukaJAVAdriver sending armposition command:" <<armState_.commandedPosition_goal<<
"\n";
312 std::cerr <<
"KukaJAVAdriver unsupported use case: " << armControlMode_ <<
"\n";
315 auto name = fbbP->CreateString(std::get<RobotName>(params_));
321 auto kukaiiwaStateVec = fbbP->CreateVector(&kukaiiwastate, 1);
327 flatbuffers::Verifier verifier(fbbP->GetBufferPointer(),fbbP->GetSize());
335 std::cout <<
"re-extracted " << movearm->
goal()->
position()->size() <<
" joint angles: ";
336 for(std::size_t i = 0; i < movearm->goal()->position()->size(); ++i)
338 std::cout << i <<
"=" << movearm->goal()->position()->Get(i) <<
", ";
345 ret = sendto(socket_local, fbbP->GetBufferPointer(), fbbP->GetSize(), 0, (
struct sockaddr *)&dst_sockaddr,
sizeof(dst_sockaddr));
347 if (static_cast<long>(ret) !=
static_cast<long>(fbbP->GetSize())) printf(
"Error sending packet to KUKA iiwa: ret = %d, len = %u\n", ret, fbbP->GetSize());
365 num = select(FD_SETSIZE, &temp_mask, &dummy_mask, &dummy_mask, &tv);
370 if (FD_ISSET(socket_local, &temp_mask))
372 static const std::size_t udp_size = 1400;
373 unsigned char recbuf[udp_size];
374 static const int flags = 0;
376 ret = recvfrom(socket_local, recbuf,
sizeof(recbuf), flags, (
struct sockaddr *)&dst_sockaddr, &dst_sockaddr_len);
377 if (ret <= 0) printf(
"Receive Error: ret = %d\n", ret);
381 if(debug_) std::cout <<
"received message size: " << ret <<
"\n";
384 auto rbPstart =
static_cast<const uint8_t *
>(recbuf);
386 auto verifier = flatbuffers::Verifier(rbPstart, ret);
392 auto bufff =
static_cast<const void *
>(rbPstart);
393 if(debug_) std::cout <<
"Succeeded in verification. " <<
"\n";
396 auto wrench = fbKUKAiiwaStates->states()->Get(0)->monitorState()->CartesianWrench();
398 armState_.wrenchJava.clear();
399 armState_.wrenchJava.push_back(wrench->force().x());
400 armState_.wrenchJava.push_back(wrench->force().y());
401 armState_.wrenchJava.push_back(wrench->force().z());
402 armState_.wrenchJava.push_back(wrench->torque().x());
403 armState_.wrenchJava.push_back(wrench->torque().y());
404 armState_.wrenchJava.push_back(wrench->torque().z());
408 std::cout <<
"Failed verification. bufOk: " << bufOK <<
"\n";
446 template<
typename Range>
448 boost::lock_guard<boost::mutex> lock(jt_mutex);
449 armState_.clearCommands();
450 boost::copy(range, std::back_inserter(armState_.commandedPosition));
451 boost::copy(range, std::back_inserter(armState_.commandedPosition_goal));
458 boost::lock_guard<boost::mutex> lock(jt_mutex);
459 commandInterface_ = cif;
466 boost::lock_guard<boost::mutex> lock(jt_mutex);
467 commandInterface_ = mif;
484 template<
typename TimeDuration>
486 boost::lock_guard<boost::mutex> lock(jt_mutex);
487 armState_.goal_position_command_time_duration = duration_to_goal_command;
501 boost::lock_guard<boost::mutex> lock(jt_mutex);
502 return armState_.timestamp;
518 template<
typename Range>
520 boost::lock_guard<boost::mutex> lock(jt_mutex);
521 armState_.clearCommands();
547 template<
typename Range>
549 boost::lock_guard<boost::mutex> lock(jt_mutex);
550 armState_.clearCommands();
551 std::copy(range,armState_.commandedCartesianWrenchFeedForward);
557 boost::lock_guard<boost::mutex> lock(jt_mutex);
562 { boost::lock_guard<boost::mutex> lock(jt_mutex);
564 if (!armState_.wrenchJava.empty()) {
573 armControlMode_ = armControlMode;
580 struct sockaddr_in dst_sockaddr;
581 socklen_t dst_sockaddr_len =
sizeof(dst_sockaddr);
582 struct sockaddr_in local_sockaddr;
584 fd_set mask, temp_mask, dummy_mask;
613 boost::mutex jt_mutex;
615 int64_t sequenceNumber;
cartesian_state wrenchJava
flatbuffers::Offset< KUKAiiwaState > CreateKUKAiiwaState(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset< flatbuffers::String > name=0, flatbuffers::Offset< flatbuffers::String > destination=0, flatbuffers::Offset< flatbuffers::String > source=0, double timestamp=0, uint8_t setArmControlState=0, flatbuffers::Offset< grl::flatbuffer::ArmControlState > armControlState=0, uint8_t setArmConfiguration=0, flatbuffers::Offset< KUKAiiwaArmConfiguration > armConfiguration=0, uint8_t hasMonitorState=0, flatbuffers::Offset< KUKAiiwaMonitorState > monitorState=0, uint8_t hasMonitorConfig=0, flatbuffers::Offset< KUKAiiwaMonitorConfiguration > monitorConfig=0)
std::tuple< JointScalar, JointScalar, JointScalar, JointScalar, JointScalar, TransformationMatrices, JointStateTag > State
volatile std::size_t m_attemptedCommunicationConsecutiveSuccessCount
std::vector< TransformationMatrix > TransformationMatrices
Internal implementation class for driver use only, stores all the kuka state data in a simple object...
flatbuffers::Offset< StopArm > CreateStopArm(flatbuffers::FlatBufferBuilder &_fbb)
std::chrono::time_point< std::chrono::high_resolution_clock > time_point_type
void construct(Params params)
volatile std::size_t m_attemptedCommunicationCount
bool destruct()
shuts down the arm
void getWrench(KukaState &state)
OutputIterator copy(std::string model, OutputIterator it, grl::revolute_joint_velocity_open_chain_state_constraint_tag)
copy vector of joint velocity limits in radians/s
std::vector< double > JointScalar
flatbuffers::Offset< MoveArmJointServo > CreateMoveArmJointServo(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset< grl::flatbuffer::JointState > goal=0)
std::array< double, 12 > TransformationMatrix
data for changing a system's physical state, such as joint angles to be sent to a robot arm...
bool VerifyKUKAiiwaStatesBuffer(flatbuffers::Verifier &verifier)
boost::error_info< struct tag_errmsg, std::string > errmsg_info
the time duration over which a command should be completed
KukaJAVAdriver(Params params=defaultParams())
const Params & getParams()
volatile std::size_t m_attemptedCommunicationConsecutiveFailureCount
static const Params defaultParams()
flatbuffers::Offset< StartArm > CreateStartArm(flatbuffers::FlatBufferBuilder &_fbb)
std::tuple< std::string, std::string, std::string, std::string, std::string, std::string, std::string, std::string, std::string, std::string, std::string > Params
flatbuffers::Offset< ArmControlState > CreateArmControlState(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset< flatbuffers::String > name=0, int64_t sequenceNumber=0, double timeStamp=0, ArmState state_type=ArmState_NONE, flatbuffers::Offset< void > state=0)
flatbuffers::Offset< KUKAiiwaStates > CreateKUKAiiwaStates(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset< flatbuffers::Vector< flatbuffers::Offset< KUKAiiwaState >>> states=0)
void FinishKUKAiiwaStatesBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset< grl::flatbuffer::KUKAiiwaStates > root)
flatbuffers::Offset< PauseArm > CreatePauseArm(flatbuffers::FlatBufferBuilder &_fbb)
const grl::flatbuffer::KUKAiiwaStates * GetKUKAiiwaStates(const void *buf)
bool run_one()
SEND COMMAND TO ARM. Call this often Performs the main update spin once.
flatbuffers::Offset< TeachArm > CreateTeachArm(flatbuffers::FlatBufferBuilder &_fbb)
volatile std::size_t m_haveReceivedRealDataCount
identifies data representing the state of a sytem
flatbuffers::Offset< JointState > CreateJointState(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset< flatbuffers::Vector< double >> position=0, flatbuffers::Offset< flatbuffers::Vector< double >> velocity=0, flatbuffers::Offset< flatbuffers::Vector< double >> acceleration=0, flatbuffers::Offset< flatbuffers::Vector< double >> torque=0)
single point in time relative to some start point
flatbuffers::Offset< KUKAiiwaArmConfiguration > CreateKUKAiiwaArmConfiguration(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset< flatbuffers::String > name=0, KUKAiiwaInterface commandInterface=KUKAiiwaInterface_Disabled, KUKAiiwaInterface monitorInterface=KUKAiiwaInterface_Disabled, EClientCommandMode clientCommandMode=EClientCommandMode_NO_COMMAND_MODE, EOverlayType overlayType=EOverlayType_NO_OVERLAY, EControlMode controlMode=EControlMode_POSITION_CONTROL_MODE, flatbuffers::Offset< SmartServo > smartServoConfig=0, flatbuffers::Offset< FRI > FRIConfig=0, flatbuffers::Offset< flatbuffers::Vector< flatbuffers::Offset< grl::flatbuffer::LinkObject >>> tools=0, flatbuffers::Offset< flatbuffers::Vector< flatbuffers::Offset< ProcessData >>> processData=0, flatbuffers::Offset< flatbuffers::String > currentMotionCenter=0, uint8_t requestMonitorProcessData=0)