In ROS2, Path is a set of linear segments. As coordinates of the end of one segment are the same as coordinates of beginning of the next segment, it is convenient to store path as an array of points.
Now, there seems to be no standard about orientation of these points, in other words, we probably shouldn't count on 3rd party algorithm to use or provide them.
Why have I mentioned locations? Think about waypoint following. If the waypoint has orientation (in addition to coordinates), then we can use it to not just arrive to a waypoint, but to face proper direction. After all, we do not want our self-driving car to park across the parking spot, instead of parking along it, right?
As our robot moves along the path, orientation of points can be of use, even if they are not final points of a route. For example, in a rugged terrain, we might cross a slippery area in a particular way...
Nevertheless, in this section I am not going to use orientation; path following code will figure out all the necessary turns based on coordinates only.
This class takes two points (start and end) and provides a straight line path. This is a rather simple algorithm, the only thing that might not be obvious is two possible ways we can use it.
Any class we create, that is a ROS2 style class, can publish messages. Our GlobalPlannerStraightLine class is no exception, it can publish path, using ROS2 predefined Path message type.
In the same time, as this is a code for the single robot (one GlobalPlannerStraightLine serves one robot) and this is a simple single-processor robot (so far), we do not really have to exchange messages: as the object of a class is right here, on the same computer, we can call its functions directly.
So in "main" function that is provided in the same file after the class, I use both approaches (one is commented out, but you can switch comments to use it): I either call functions of a class directly, or I make it a service and call this service.
The second approach works if you want one object of the class to serve multiple robots, or if your robot has more than one processor, in both cases, they are connected over some kind of a network and we use ROS2 messages to pass the path around.