1+ #ifndef FRANKA_SERVER_HPP
2+ #define FRANKA_SERVER_HPP
3+
4+ #include < zmq.hpp>
5+ #include < thread>
6+ #include < atomic>
7+ #include < mutex>
8+ #include < string>
9+ #include < memory>
10+
11+ #include < franka/robot.h>
12+ #include < franka/model.h>
13+ #include < franka/robot_state.h>
14+ #include < yaml-cpp/yaml.h>
15+ #include " protocol/franka_arm_state.hpp"
16+ #include " control_mode/abstract_control_mode.h"
17+
18+ class FrankaProxy {
19+
20+ public:
21+ // Constructor & Destructor
22+ explicit FrankaProxy (const std::string& config_path);// Constructor that initializes the proxy with a configuration file
23+ ~FrankaProxy ();// Destructor to clean up resources
24+
25+ // Core server operations
26+ bool start ();// Starts the Franka server, initializing the robot and communication sockets
27+ void stop ();// Stops the server, cleaning up resources and shutting down communication
28+ void spin ();// Main loop for processing requests
29+
30+ // State management
31+ franka::RobotState getCurrentState ();// Return the current state of the robot
32+
33+ // Mode management
34+ void registerControlMode (const std::string& mode, std::unique_ptr<AbstractControlMode> control_mode);// register the map
35+ void setControlMode (const std::string& mode);
36+
37+
38+
39+ // Configuration
40+ void displayConfig () const ;
41+
42+ private:
43+ // Initialization
44+ void initialize (const std::string &filename);// Initializes the FrankaProxy with the given configuration file
45+ void setupCommunication ();// Sets up ZMQ communication sockets and threads?
46+
47+ // Thread functions
48+ void statePublishThread ();// ZMQ PUB, Publishes the current state of the robot at a fixed rate
49+ void responseSocketThread ();// ZMQ REP,responds to incoming requests from clients
50+ void controlLoopThread ();// Main control loop for processing commands and updating the robot state
51+
52+
53+ // Message handling
54+ void handleServiceRequest (const std::string& request, std::string& response);
55+
56+
57+ private:
58+ // Configuration
59+ YAML::Node proxy_config_;
60+ std::string robot_ip_;
61+ std::string state_pub_addr_;
62+ std::string service_addr_;
63+
64+ // Franka robot
65+ std::shared_ptr<franka::Robot> robot_;
66+ std::shared_ptr<franka::Model> model_;
67+
68+ // ZMQ communication
69+ zmq::context_t context_;
70+ zmq::socket_t pub_socket_;
71+ zmq::socket_t sub_socket_;
72+ zmq::socket_t rep_socket_;
73+
74+ // Threading
75+ std::thread state_pub_thread_;// statePublishThread()
76+ std::thread service_thread_;// responseSocketThread()
77+ std::thread control_thread_;// controlLoopThread()
78+
79+
80+ // Synchronization
81+ std::atomic<bool > in_control_;// for threads
82+
83+ // State data
84+ franka::RobotState current_state_;
85+
86+ // Control mode
87+ std::string mode_name;
88+ AbstractControlMode* current_mode_ = nullptr ;
89+ std::mutex control_mutex_;
90+ std::map<std::string, std::shared_ptr<AbstractControlMode>> control_modes_map_;
91+
92+ // TODO: put all the Constants to a config file
93+ static constexpr int STATE_PUB_RATE_HZ = 100 ;
94+ static constexpr int SOCKET_TIMEOUT_MS = 100 ;
95+ static constexpr int MAX_MESSAGE_SIZE = 4096 ;
96+ };
97+
98+ #endif // FRANKA_SERVER_HPP
0 commit comments