Flatbuffers编码然后解码C ++双精度数组+表+联合返回垃圾 [英] Flatbuffers encoding then decoding C++ double array + table + union returns junk

查看:368
本文介绍了Flatbuffers编码然后解码C ++双精度数组+表+联合返回垃圾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在填写一些Flatbuffer消息,但是当我编码然后解码它们时,我会被淘汰.我没有包含完整的消息以避免不必要的信息,但是我能够成功提取联合组件的枚举值.但是,当我提取由枚举标识的类型时,我打印出的double数组包含垃圾,如下所示.

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:

输入/输出:

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:

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:

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

推荐答案

您不会垃圾,数据实际上是正确的.该错误在以下语句中:std::cout << i << " " << movearm->goal()->position()->Get(i);

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

如果您写的是这样的内容: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 :)

这篇关于Flatbuffers编码然后解码C ++双精度数组+表+联合返回垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆