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

Subscription callback integer_value type is incorrect #1025

Open
PierreSachot opened this issue Jan 29, 2025 · 1 comment
Open

Subscription callback integer_value type is incorrect #1025

PierreSachot opened this issue Jan 29, 2025 · 1 comment
Labels

Comments

@PierreSachot
Copy link

PierreSachot commented Jan 29, 2025

Description
When subscribing to a parameter event in Node.js, if the data type for the parameter is set to Integer but the assigned value exceeds 9999999999999999 (15 digits), its type changes unexpectedly from 'number' to 'string'. This behavior causes the Parameter.fromParameterMessage function to fail because it expects an integer, not a string.

This issue seems to be related to the following previously reported issues:

Environment Details:

  • Library Version: 0.28.1
  • ROS Version: ROS 2 Humble
  • Platform/OS: Ubuntu 22.04

Steps To Reproduce

  1. Create a Python file with the following code to declare a parameter:
 import rclpy
  import rclpy.node
  
  class MinimalParam(rclpy.node.Node):
      def __init__(self):
          super().__init__('minimal_param_node')
  
          self.declare_parameter('my_parameter', 1000)
  
  
  def main():
      rclpy.init()
      node = MinimalParam()
      rclpy.spin(node)
  
  if __name__ == '__main__':
      main()
  1. Create the following Node.js code that subscribes to parameter events:
// Create ROS Node
const node = new Node('TestNode', '', Context.defaultContext(), options);
node.spin();

node.createSubscription(
  'rcl_interfaces/msg/ParameterEvent',
  '/parameter_events',
  { qos: QoS.profileParameterEvents },
  (buff: any) => {
    const msg = buff as ParameterEvent;


    if (msg.changed_parameters.length !== 0) {
      console.log('Received parameter event from node: ', msg.node);
      console.log('msg.value = ', msg.changed_parameters[0].value.integer_value, typeof msg.changed_parameters[0].value.integer_value);
    }
  }
  1. Start the Python node and the Node.js code in separate terminals
  2. In a third terminal, update the parameter value to 9999999999999999 using the following command:
ros2 param set /minimal_param_node my_parameter 9999999999999999

You should in Node.js terminal that the parameter integer_value type is number, next type:

  1. Now, update the parameter value to 10000000000000000 with the following command:
ros2 param set /minimal_param_node my_parameter 10000000000000000

Now, the parameter integer_value type is string


Actual Behavior
When the parameter value exceeds 9999999999999999, its data type transitions from number to string in Node.js. This type mismatch causes the Parameter.fromParameterMessage function to fail, as it expects the value to remain an integer.

A possible workaround to handle this issue could look like the following code snippet:

try {
  convertedMsg = Parameter.fromParameterMessage(msg);
  return convertedMsg;
} catch (error) {
  // get type
  const parameterType = msg.value.type;
  // handle array errors
  if (parameterType === ParameterType.INTEGER) {
    msg.value.integer_value = Number(msg.value.integer_value);
    convertedMsg = Parameter.fromParameterMessage(msg);
  }
}

This workaround allows the application to recover from the type mismatch by explicitly casting the integer_value back to a number before processing the parameter message. However, it only addresses the symptom of the issue and does not resolve the root cause.

Expected Behavior
The casting behavior (converting a string back to number when ParameterType.INTEGER is detected) should be directly detected and implemented in the fromParameterMessage function. This would eliminate the need for external type-handling workarounds and ensure consistency regardless of the parameter value.

@PierreSachot PierreSachot changed the title Subscription callback integer type is incorrect if integer_value > 9999999999999999 Subscription callback integer_value type is incorrect Jan 29, 2025
@minggangw
Copy link
Member

Hi @PierreSachot thanks for your investigation, as you said, it's due to the limitation of JavaScript when representing integer, BigInt may work for this case (suggested by #836), I haven't check with it. BTW, you are welcome to submit a PR to fix it.

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

No branches or pull requests

2 participants