Skip to content

Commit

Permalink
Worked on receiving the proto file
Browse files Browse the repository at this point in the history
  • Loading branch information
HamzaKady committed Oct 29, 2024
1 parent f2e5b5a commit b9b7d7c
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 11 deletions.
3 changes: 2 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# RTT Build Environment
# The purpose of this container is to have a stable, quick and reliable environment
# to build RTT software.
Expand Down Expand Up @@ -104,4 +105,4 @@ ENV LD_LIBRARY_PATH=$HOME/lib/

# Copy just build binaries (roboteam software) from volume to container home
# Note: cannot COPY from outside context, build from parent folder
COPY --chown=$USER:$USER ../build/release/ $HOME/
COPY --chown=$USER:$USER ../build/release/ $HOME/
2 changes: 1 addition & 1 deletion docker/builder/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
container_name: RTT_roboteam_builder
restart: "no"
working_dir: "/home/roboteamtwente/"
command: sh -c "./build.sh"
command: sh -c "git config --global --add safe.directory /home/roboteamtwente && git config --global --add safe.directory /home/roboteamtwente/build/_deps/tracy-src && ./build.sh"
volumes:
- ../../:/home/roboteamtwente/

Expand Down
33 changes: 33 additions & 0 deletions roboteam_ai/src/RL/receive_action_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import zmq
import sys
import os

# Make sure to go back to the main roboteam directory
current_dir = os.path.dirname(os.path.abspath(__file__))
roboteam_path = os.path.abspath(os.path.join(current_dir, "..", "..", ".."))

# Add to sys.path
sys.path.append(roboteam_path)

# Import the generated protobuf classes
from roboteam_networking.proto import ActionCommand_pb2

def receive_action_command():
context = zmq.Context()
socket = context.socket(zmq.PULL) # Use PULL to receive messages
socket.connect("tcp://localhost:5555") # Connect to the sender's port.

while True:
message = socket.recv() # Receive the message.

action_command = ActionCommand_pb2.ActionCommand()
action_command.ParseFromString(message) # Deserialize the message.

# Print the received data
print(f"Received: numDefender={action_command.numDefender}, "
f"numAttacker={action_command.numAttacker}, "
f"numWaller={action_command.numWaller}")

if __name__ == "__main__":
print("Receiver is running...")
receive_action_command()
15 changes: 9 additions & 6 deletions roboteam_ai/src/RL/sentActionCommand.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import zmq
import sys
import os
import time # Importing time for sleep

# Make sure to go back to the main roboteam directory
current_dir = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -9,23 +10,25 @@
# Add to sys.path
sys.path.append(roboteam_path)

# Now import the generated protobuf classes
# Import the generated protobuf classes
from roboteam_networking.proto import ActionCommand_pb2

def send_action_command(num_attacker, num_defender, num_waller):
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5555") # Send data over this port.
socket = context.socket(zmq.PUSH) # Use PUSH socket type
socket.bind("tcp://*:5555") # Send data over this port.


action_command = ActionCommand_pb2.ActionCommand()
action_command.numDefender = num_defender
action_command.numAttacker = num_attacker
action_command.numWaller = num_waller

message = action_command.SerializeToString()
socket.send(message)
#print(f"Sent: numDefender={num_defender}, numAttacker={num_attacker}, numWaller={num_waller}")
socket.send(message)
print(f"Sent: numDefender={num_defender}, numAttacker={num_attacker}, numWaller={num_waller}")


if __name__ == "__main__":
# Example usage
send_action_command(2, 3, 1)
send_action_command(2, 3, 1)
81 changes: 78 additions & 3 deletions roboteam_ai/src/stp/plays/offensive/Attack.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "stp/plays/offensive/Attack.h"

#include <iostream>
#include <thread>
#include "stp/plays/offensive/Attack.h"
#include <roboteam_utils/Hungarian.h>

#include "stp/computations/GoalComputations.h"
#include "stp/computations/PositionScoring.h"
#include "stp/roles/Keeper.h"
Expand All @@ -11,6 +11,58 @@

namespace rtt::ai::stp::play {

void Attack::receiveActionCommand() {
zmq::context_t context(1); // Create a context
zmq::socket_t socket(context, ZMQ_PULL); // Create a PULL socket
socket.bind("tcp://*:5555"); // Bind to the same port as the sender

while (true) {
zmq::message_t message; // Create a message to receive
socket.recv(&message); // Receive the message

// Parse the protobuf message
ActionCommand action_command;
action_command.ParseFromArray(message.data(), message.size()); // Deserialize

int num_attacker = action_command.numAttacker();
int num_defender = action_command.numDefender();
int num_waller = action_command.numWaller();

// Clear existing roles (if any)
roles.clear();

// Create Striker roles
for (int i = 0; i < num_attacker; ++i) {
std::string name = "striker_" + std::to_string(i);
roles.push_back(std::make_unique<role::Striker>(name));
}

// Create Defender roles
for (int i = 0; i < num_defender; ++i) {
std::string name = "defender_" + std::to_string(i);
roles.push_back(std::make_unique<role::Defender>(name));
}

// Create Waller roles
for (int i = 0; i < num_waller; ++i) {
std::string name = "waller_" + std::to_string(i);
roles.push_back(std::make_unique<role::Defender>(name)); // Assuming Waller is also a Defender
}

// Print the received values for debugging
std::cout << "Received: numDefender=" << num_defender
<< ", numAttacker=" << num_attacker
<< ", numWaller=" << num_waller << std::endl;

// Optionally, print created roles
for (const auto& role : roles) {
std::cout << "Created role: " << role->getName() << std::endl; // Assuming getName() method exists
}
}
}



Attack::Attack() : Play() {
// Evaluations that have to be true in order for this play to be considered valid.
startPlayEvaluation.clear();
Expand Down Expand Up @@ -102,3 +154,26 @@ bool Attack::shouldEndPlay() noexcept {
const char* Attack::getName() const { return "Attack"; }

} // namespace rtt::ai::stp::play






int main() {
// Create an instance of the Attack class
rtt::ai::stp::play::Attack attack;

// Start receiving action commands in a separate thread
std::thread receiver_thread(&rtt::ai::stp::play::Attack::receiveActionCommand, &attack);
receiver_thread.detach(); // Detach the thread to allow it to run independently

// Allow some time for receiving messages
std::cout << "Receiving messages. Press Enter to exit..." << std::endl;

// Wait for user input to exit
std::cin.get(); // This will block until you press Enter

return 0;
}

4 changes: 4 additions & 0 deletions roboteam_networking/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ target_link_libraries(roboteam_networking_syscheck

gtest_discover_tests(roboteam_networking_syscheck)

add_executable(test_attack test_attack.cpp)
target_link_libraries(test_attack PRIVATE zmq protobuf::libprotobuf)


0 comments on commit b9b7d7c

Please sign in to comment.