1
+ """
2
+ MIT LICENSE
3
+
4
+ Copyright (C) 2023-25 Murilo Marques Marinho (www.murilomarinho.info)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ """
24
+ import math
25
+
26
+ import rclpy
27
+ from rclpy .action import ActionServer
28
+ from rclpy .node import Node
29
+
30
+ from geometry_msgs .msg import Point
31
+ from package_with_interfaces .action import MoveStraightIn2D
32
+
33
+
34
+ class MoveStraightIn2DActionServerNode (Node ):
35
+ """A ROS2 Node with an Action Server for MoveStraightIn2D."""
36
+
37
+ def __init__ (self ):
38
+ super ().__init__ ('move_straight_in_2d_action_server' )
39
+
40
+ self .current_position = Point ()
41
+
42
+ self .action_server = ActionServer (
43
+ self ,
44
+ MoveStraightIn2D ,
45
+ 'move_straight_in_2d' ,
46
+ self .execute_callback )
47
+
48
+ def get_error_norm (self , desired_position ):
49
+ print ("TODO" )
50
+
51
+ def execute_callback (self , goal : MoveStraightIn2D .ActionGoalHandle ) -> MoveStraightIn2D .Result :
52
+ desired_position = goal .request .desired_position
53
+
54
+ self .get_logger ().info (f'Target set to { desired_position } ...' )
55
+
56
+ feedback_msg = Fibonacci .Feedback ()
57
+ feedback_msg .partial_sequence = [0 , 1 ]
58
+
59
+ for i in range (1 , goal_handle .request .order ):
60
+ feedback_msg .partial_sequence .append (
61
+ feedback_msg .partial_sequence [i ] + feedback_msg .partial_sequence [i - 1 ])
62
+ self .get_logger ().info ('Feedback: {0}' .format (feedback_msg .partial_sequence ))
63
+ goal_handle .publish_feedback (feedback_msg )
64
+ time .sleep (1 )
65
+
66
+ goal .succeed ()
67
+
68
+ result = MoveStraightIn2D .Result ()
69
+ result .final_position = self .current_position
70
+ return result
71
+
72
+
73
+ def main (args = None ):
74
+ """
75
+ The main function.
76
+ :param args: Not used directly by the user, but used by ROS2 to configure
77
+ certain aspects of the Node.
78
+ """
79
+ try :
80
+ rclpy .init (args = args )
81
+
82
+ node = MoveStraightIn2DActionServerNode ()
83
+
84
+ rclpy .spin (node )
85
+ except KeyboardInterrupt :
86
+ pass
87
+ except Exception as e :
88
+ print (e )
89
+
90
+
91
+ if __name__ == '__main__' :
92
+ main ()
0 commit comments