Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
alsora opened this issue Feb 16, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@alsora
Copy link
Contributor

alsora commented Feb 16, 2023

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;
}
@alsora alsora changed the title Script and SetBlackboard inconsistency when setting float values Floating point values set by Script cause Floating point truncated when passed to BT::InputPort<float> Feb 16, 2023
@facontidavide facontidavide self-assigned this Feb 16, 2023
@facontidavide facontidavide added the bug Something isn't working label Feb 16, 2023
@facontidavide
Copy link
Collaborator

The best course of action is to remove this warning.
Scripting language uses double under the hood for all numeric type to avoid ... killing myself?

facontidavide added a commit that referenced this issue Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants