-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
string location | ||
--- | ||
bool success | ||
--- | ||
string status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
40 changes: 40 additions & 0 deletions
40
navigation/packages/nav_main/scripts/move_action_client.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import rclpy | ||
from rclpy.action import ActionClient | ||
from rclpy.node import Node | ||
|
||
from nav_main.action import Move | ||
|
||
|
||
class MoveActionClient(Node): | ||
|
||
def __init__(self): | ||
super().__init__('Move_action_client') | ||
self._action_client = ActionClient(self, Move, 'move') | ||
|
||
def send_goal(self, location): | ||
goal_msg = Move.Goal() | ||
goal_msg.location = location | ||
|
||
self.get_logger().info(f'Sending goal to move to: {location}') | ||
self._action_client.wait_for_server() | ||
future = self._action_client.send_goal_async(goal_msg) | ||
future.add_done_callback(self._on_result) | ||
|
||
def _on_result(self, future): | ||
result = future.result().result | ||
self.get_logger().info(f"Result: {'Success' if result.success else 'Failed'}, Message: '{result.message}'") | ||
|
||
|
||
def main(args=None): | ||
rclpy.init(args=args) | ||
|
||
action_client = MoveActionClient() | ||
|
||
test_location = "Table" | ||
|
||
action_client.send_goal(test_location) | ||
|
||
rclpy.spin(action_client) | ||
|
||
if __name__ == '__main__': | ||
main() |
61 changes: 61 additions & 0 deletions
61
navigation/packages/nav_main/scripts/move_action_server.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import rclpy | ||
from rclpy.action import ActionServer | ||
from rclpy.node import Node | ||
import json | ||
|
||
from nav_main.action import Move | ||
|
||
class MoveActionServer(Node): | ||
|
||
def __init__(self): | ||
super().__init__('move_action_server') | ||
|
||
# Cargar locaciones desde un archivo JSON | ||
with open('areas.json', 'r') as file: | ||
self.locations = json.load(file) | ||
|
||
self._action_server = ActionServer( | ||
self, | ||
Move, | ||
'Move', | ||
self.execute_callback) | ||
|
||
def execute_callback(self, goal_handle): | ||
self.get_logger().info('Moving...') | ||
target_location = goal_handle.request.location | ||
|
||
# Validar si la locación existe en el JSON | ||
if not self.is_valid_location(target_location): | ||
self.get_logger().info(f"Invalid location: {target_location}") | ||
goal_handle.abort() | ||
result = Move.Result() | ||
result.success = False | ||
result.message = f"Location '{target_location}' not available." | ||
return result | ||
|
||
goal_handle.succeed() | ||
|
||
result = Move.Result() | ||
result.success = True | ||
result.message = f"Successfully moved to {target_location}" | ||
return result | ||
|
||
|
||
# Verificar si la locación solicitada existe en el JSON. | ||
def is_valid_location(self, location): | ||
for area, objects in self.locations.items(): | ||
if location in objects: | ||
return True | ||
return False | ||
|
||
|
||
def main(args=None): | ||
rclpy.init(args=args) | ||
|
||
fibonacci_action_server = MoveActionServer() | ||
|
||
rclpy.spin(fibonacci_action_server) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |