Skip to content

Floating point values set by Script cause Floating point truncated when passed to BT::InputPort<float> #506

@alsora

Description

@alsora

Hi, I'm using BehaviorTree.cpp version 4.0.1.
According to the docs

If you are migrating from BT.CPP 3.X, Script is a drop-in replacement for SetBlackboard, which is now discouraged.

However, I'm noticing a different behavior between the Script and SetBlackboard nodes when setting floating point values.
In particular, it looks like the following two blocks yield different results

<Action ID="SetBlackboard" output_key="data" value="0.1"/>
<Script code=" data:=0.1 "/>

Passing data to a BT::InputPort<float> results in a Floating point truncated error if using the script, while it works if using the set blackboard.
It looks like the script sets the value to a double (indeed if I change the input port to be double, the exception goes away).

Is this behavior expected? Should there be a way to specify floats using the scripting language?

See a minimal reproducible example

class CheckFloat : public BT::SyncActionNode
{
public:
  CheckFloat(const std::string& name, const BT::NodeConfig& config)
    : BT::SyncActionNode(name, config)
  { }

  static BT::PortsList providedPorts()
  {
    return { BT::InputPort<float>("in_value") };
  }

  BT::NodeStatus tick() override
  {
    auto data = getInput<float>("in_value");
    if (!data) {
      throw BT::RuntimeError("missing required input: ", data.error());
    }
    std::cout << "Robot says: " << data.value() << std::endl;
    return BT::NodeStatus::SUCCESS;
  }
};

int main()
{
    auto my_factory = std::make_unique<BT::BehaviorTreeFactory>();
    my_factory->registerNodeType<CheckFloat>("CheckFloat");

    std::stringstream bt_tree_text;
    bt_tree_text << R"(
    <root BTCPP_format="4">
    <BehaviorTree ID="MainTree">
    <Sequence>
        <Script code=" data:=0.1 "/>
        <Action ID="CheckFloat" in_value="{data}"/>
    </Sequence>
    </BehaviorTree>
    </root>)";

    auto tree = my_factory->createTreeFromText(bt_tree_text.str());
    tree.tickWhileRunning();

    return 0;
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions