Skip to content

Blackboard's set function seems to set variable to type string #974

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

Open
sven-hoek opened this issue May 13, 2025 · 1 comment
Open

Blackboard's set function seems to set variable to type string #974

sven-hoek opened this issue May 13, 2025 · 1 comment

Comments

@sven-hoek
Copy link
Contributor

sven-hoek commented May 13, 2025

What I do

After creating a tree, I set blackboard entries from data read in from a JSON string, something like this

{
  "goal_position": 0.0,
  "goal_tolerance": 0.01,
  "serial_port": "/dev/ttyUSB0",
}

I currently restrict to primitive values, setting the parameters with something like this:

const auto parameters = nlohmann::json::parse(json_string);
for (const auto& [key, value] : parameters.items()) {
    if (value.is_string()) {
        // Necessary to avoid having quotes in the string-value
        blackboard->set(key, value.get<std::string>());
    } else if (value.is_primitive()) {
        blackboard->set(key, value.dump());
    } else {
        // discard
    }
}

In my tree, I want to set values according to those set blackboard entries, like so:

<Script code="goal_range_min := goal_position - goal_tolerance" />

What I expect

I expect the script node to succeed, resulting in a blackboard entry goal_range_min, which contains the difference of goal_position and goal_tolerance. In this example, it would be -0.01.

What I get

Error in script [goal_range_min := goal_position - goal_tolerance]

I get a bit more info when I do the calculation in two steps, i.e.

<Script code="goal_range_min := goal_position" />
<Script code="goal_range_min -= goal_tolerance" />

which will result in the following error:

Error in script [goal_range_min -= goal_tolerance]
Operator not supported for strings

Or, when I add a Script node at the beginning of the tree to set/overwrite an entry, i.e.

<Script code="goal_tolerance := 0.03" />

I get

Error in script [goal_tolerance := 0.03]
Error assigning a value to entry [goal_tolerance] with type [std::string]. 
The right operand has type [double] and can't be converted to [std::string]

However, when I delete the goal_tolerance entry from the JSON and only set it through the Script Node, while still setting the goal_position from the JSON, it seems to work.

Thoughts

This hints me that the blackboard entries have the type information to be a string. However, I would expect them to be without type information, according to this line and following:

// if a new generic port is created with a string, it's type should be AnyTypeAllowed

So, are mathematical operations between two AnyTypeAlloweds not possible? I assume if both are of unknown type, it is unknown what the operation is supposed to be (addition? string concatenation?).

But is there any solution? I could infer the data type from the JSON entry and use the right template instantiation of Blackboard::set<T>. Any other suggestion?

@viprob-ai
Copy link

Hi @sven-hoek , incorrect type

if you want arthimetic operation to be performed just correct this blackboard->set<T>(key,T value)

const auto parameters = nlohmann::json::parse(json_string);
for (const auto& [key, value] : parameters.items()) {
    if (value.is_string()) {
        // Necessary to avoid having quotes in the string-value
        blackboard->set(key, value.get<std::string>());
    } else if (value.is_number_float()()) {   // <=========================== @sven-hoek 
        blackboard->set(key, value.get<float>());
    } else {
        // discard
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants