Flatbuffers encoding then decoding C++ double array + table + union returns junk

ⅰ亾dé卋堺 提交于 2020-01-15 22:26:08

问题


I'm filling out some flatbuffer messages but when I encode then decode them I get junk back out. I've not included the full message to avoid extraneous information, but I'm able to extract the enum value of the union component successfully. However, when I go to extract the type identified by the enum the double array I print out contains junk as illustrated below.

Here are the important parts of the buffers:

Input/Output:

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;
}

encode:

          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...
}

decode:

          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:


You don't get junk out, the data is actually correct. The bug is in this statement: std::cout << i << " " << movearm->goal()->position()->Get(i);

If instead you wrote something like: std::cout << i << "=" << movearm->goal()->position()->Get(i) << ", "; it be more readable :)



来源:https://stackoverflow.com/questions/35924332/flatbuffers-encoding-then-decoding-c-double-array-table-union-returns-junk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!