Flatbuffers编码然后解码C ++ double array + table + union返回垃圾

时间:2016-03-10 18:22:51

标签: c++ flatbuffers

我填写了一些flatbuffer消息,但是当我编码然后解码它们时我得到了垃圾消息。我没有包含完整的消息以避免无关的信息,但我能够成功提取union组件的枚举值。但是,当我去提取由枚举标识的类型时,我打印出来的双数组包含垃圾,如下图所示。

以下是缓冲区的重要部分:

输入/输出:

KukaJAVAdriver sending armposition command:[1, 0, 0, 0, 0, 0, 1]
re-extracted 7 joint angles: 0 11 02 03 04 05 06 1

JointState.fbs:

table JointState {
  // @todo consider adding name string
  position:[double]; // angle in radians
  velocity:[double]; // velocity in radians/second
  acceleration:[double]; // acceleration in radians/(second^2)
  torque:[double]; // Newton Meters (N*m)
}

ArmControlState.fbs:

include "JointState.fbs";
include "Geometry.fbs";

namespace grl.flatbuffer;

table StartArm {
}

table StopArm {
}

table PauseArm {
}

table TeachArm {
}

table ShutdownArm {
}

table MoveArmTrajectory {
  traj:[JointState];
}

table MoveArmJointServo {
  goal:JointState;
}

table MoveArmCartesianServo {
  parent:string; // Object/Frame/Coordinate System to move wrt. Empty default is robot base
  goal:Pose;
}

union ArmState { StartArm, StopArm, PauseArm, ShutdownArm, TeachArm, MoveArmTrajectory, MoveArmJointServo, MoveArmCartesianServo }

table ArmControlState {
  name:string;   // entity to move
  sequenceNumber:long;
  timeStamp:double;
  state:ArmState;
}

编码:

          flatbuffers::Offset<flatbuffer::ArmControlState> controlState;


          switch (armControlMode_) {

              case flatbuffer::ArmState::ArmState_StartArm: {
                 controlState = flatbuffer::CreateArmControlState(*fbbP,bns,sequenceNumber++,duration,armControlMode_,flatbuffer::CreateStartArm(*fbbP).Union());
                 break;
              }
              case flatbuffer::ArmState::ArmState_MoveArmJointServo: {

                /// @todo when new
                JointScalar                          armPosVelAccelEmpty;
                auto armPositionBuffer = fbbP->CreateVector(armPosition_.data(),armPosition_.size());
                auto goalJointState = grl::flatbuffer::CreateJointState(*fbbP,armPositionBuffer);
                auto moveArmJointServo = grl::flatbuffer::CreateMoveArmJointServo(*fbbP,goalJointState);
                controlState = flatbuffer::CreateArmControlState(*fbbP,bns,sequenceNumber++,duration,armControlMode_,moveArmJointServo.Union());
                std::cout << "KukaJAVAdriver sending armposition command:" <<armPosition_<<"\n";
                 break;
              }
//...snip...
}

解码:

          auto states = flatbuffer::CreateKUKAiiwaStates(*fbbP,kukaiiwaStateVec);

          grl::flatbuffer::FinishKUKAiiwaStatesBuffer(*fbbP, states);

          flatbuffers::Verifier verifier(fbbP->GetBufferPointer(),fbbP->GetSize());
          BOOST_VERIFY(grl::flatbuffer::VerifyKUKAiiwaStatesBuffer(verifier));


          if(armControlMode_ == flatbuffer::ArmState::ArmState_MoveArmJointServo)
          {
              auto states2 = flatbuffer::GetKUKAiiwaStates(fbbP->GetBufferPointer());
              auto movearm = static_cast<const flatbuffer::MoveArmJointServo*>(states2->states()->Get(0)->armControlState()->state());
              std::cout << "re-extracted " << movearm->goal()->position()->size() << " joint angles: ";
              for(std::size_t i = 0; i <  movearm->goal()->position()->size(); ++i)
              {
                std::cout << i << " " << movearm->goal()->position()->Get(i);
              }
              std::cout << "\n";
          }

          kukaJavaDriverP->async_send_flatbuffer(fbbP);

1 个答案:

答案 0 :(得分:1)

你没有垃圾,数据实际上是正确的。该错误在此声明中:std::cout << i << " " << movearm->goal()->position()->Get(i);

如果你写了类似的东西:std::cout << i << "=" << movearm->goal()->position()->Get(i) << ", ";它更具可读性:)