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

Addition and subtraction assignment operators #2424

Open
wants to merge 16 commits into
base: rolling
Choose a base branch
from

Conversation

HuaTsai
Copy link
Contributor

@HuaTsai HuaTsai commented Feb 15, 2024

Implement addition/subtraction assignment operators based on existing operators.
Resolves #2399

@HuaTsai
Copy link
Contributor Author

HuaTsai commented Feb 15, 2024

CI problem in TestNodeGraph.

@fujitatomoya fujitatomoya self-assigned this Feb 16, 2024
@fujitatomoya fujitatomoya added the enhancement New feature or request label Feb 16, 2024
Copy link
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm with green CI

@fujitatomoya
Copy link
Collaborator

CI:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Windows Build Status

EXPECT_EQ(time_msg.sec, 1);
EXPECT_EQ(time_msg.nanosec, 200000000u);

time_msg -= pos_duration;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any special handling needed for overflow/underflow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Ryanf55,
Sorry for forgetting to reply.
The overflow test is already in line 133 below.
For the underflow test, please check here: #2419 (comment).

Or let me paraphrase in another way:
I follow what happens when rclcpp::Duration + msg::Time.
It first becomes rclcpp::Duration + rclcpp::Time (implicit conversion).
During the conversion, negative seconds in msg::Time is not allowed.

if (seconds < 0) {
throw std::runtime_error("cannot store a negative time point in rclcpp::Time");
}

IMO this is a small design flaw since it does not match Unix that uses int32 to store time.
But in practice, who cares about time before 1970, so it should remain fine.
After that, positive int32 seconds and uint32 nanoseconds will generate positive int64 full nanoseconds.
A positive int64 nanoseconds add another int64 duration nanoseconds always not underflow.

That's my current understanding and therefore I do (and can) not test underflow.

Copy link

@Ryanf55 Ryanf55 Feb 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it written down anywhere that rclcpp::Time must use Unix time? I have used it in places to store time since boot, in the case that the embedded device does not have a RTC or network connection, and thus had no idea of any time except boot time. Certain unsafe uses of while loops, with timeouts and such can lead to weird bugs.

https://docs.ros2.org/beta3/api/rclcpp/classrclcpp_1_1Time.html

I agree, the likelyhood of underflow while using epoch time is low.

Copy link
Contributor Author

@HuaTsai HuaTsai Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it written down anywhere that rclcpp::Time must use Unix time?

I don't find such description, maybe my statement earlier is too biased.

I have used it in places to store time since boot, in the case that the embedded device does not have a

RTC or network connection, and thus had no idea of any time except boot time.
Yes, this reminds me that the clock source of rclcpp::Time could also be timestamps in rosbag.
Both of them are not using current epoch time.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as overflow/overflow is tested somewhere, seems like a good approach to me.

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

Successfully merging this pull request may close these issues.

operator+ and operator- for builtin_interfaces::msg::Time and rclcpp::Duration
3 participants