client.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <condition_variable>
  2. #include <iostream>
  3. #include <memory>
  4. #include <mutex>
  5. #include <string>
  6. #include <unistd.h>
  7. #include <grpcpp/grpcpp.h>
  8. #include "grpc_ex.grpc.pb.h"
  9. using grpc::Channel;
  10. using grpc::ClientContext;
  11. using grpc::Status;
  12. using grpc_ex::MainGateway;
  13. using grpc_ex::Main_Ctrl_Takeoff_Reply;
  14. using grpc_ex::Main_Ctrl_Takeoff_Request;
  15. using grpc_ex::Main_Ctrl_Land_Reply;
  16. using grpc_ex::Main_Ctrl_Land_Request;
  17. using grpc_ex::Main_Ctrl_RTL_Reply;
  18. using grpc_ex::Main_Ctrl_RTL_Request;
  19. using grpc_ex::Main_Ctrl_PositionGlobal_Reply;
  20. using grpc_ex::Main_Ctrl_PositionGlobal_Request;
  21. using grpc_ex::Main_Ctrl_PositionNED_Reply;
  22. using grpc_ex::Main_Ctrl_PositionNED_Request;
  23. class MainGatewayClient {
  24. public:
  25. MainGatewayClient(std::shared_ptr<Channel> channel)
  26. : stub_(MainGateway::NewStub(channel)) {}
  27. std::string Ctrl_Takeoff(double alt_m) {
  28. Main_Ctrl_Takeoff_Request request;
  29. request.set_alt_m(alt_m);
  30. Main_Ctrl_Takeoff_Reply reply;
  31. ClientContext context;
  32. std::mutex mu;
  33. std::condition_variable cv;
  34. bool done = false;
  35. Status status;
  36. stub_->async()->Ctrl_Takeoff(&context, &request, &reply,
  37. [&mu, &cv, &done, &status](Status s) {
  38. status = std::move(s);
  39. std::lock_guard<std::mutex> lock(mu);
  40. done = true;
  41. cv.notify_one();
  42. });
  43. std::unique_lock<std::mutex> lock(mu);
  44. while (!done) {
  45. cv.wait(lock);
  46. }
  47. if (status.ok()) {
  48. return reply.message();
  49. } else {
  50. std::cout << status.error_code() << ": " << status.error_message()
  51. << std::endl;
  52. return "RPC failed";
  53. }
  54. }
  55. std::string Ctrl_Land(const std::string& message) {
  56. Main_Ctrl_Land_Request request;
  57. request.set_message(message);
  58. Main_Ctrl_Land_Reply reply;
  59. ClientContext context;
  60. std::mutex mu;
  61. std::condition_variable cv;
  62. bool done = false;
  63. Status status;
  64. stub_->async()->Ctrl_Land(&context, &request, &reply,
  65. [&mu, &cv, &done, &status](Status s) {
  66. status = std::move(s);
  67. std::lock_guard<std::mutex> lock(mu);
  68. done = true;
  69. cv.notify_one();
  70. });
  71. std::unique_lock<std::mutex> lock(mu);
  72. while (!done) {
  73. cv.wait(lock);
  74. }
  75. if (status.ok()) {
  76. return reply.message();
  77. } else {
  78. std::cout << status.error_code() << ": " << status.error_message()
  79. << std::endl;
  80. return "RPC failed";
  81. }
  82. }
  83. std::string Ctrl_RTL(const std::string& message) {
  84. Main_Ctrl_RTL_Request request;
  85. request.set_message(message);
  86. Main_Ctrl_RTL_Reply reply;
  87. ClientContext context;
  88. std::mutex mu;
  89. std::condition_variable cv;
  90. bool done = false;
  91. Status status;
  92. stub_->async()->Ctrl_RTL(&context, &request, &reply,
  93. [&mu, &cv, &done, &status](Status s) {
  94. status = std::move(s);
  95. std::lock_guard<std::mutex> lock(mu);
  96. done = true;
  97. cv.notify_one();
  98. });
  99. std::unique_lock<std::mutex> lock(mu);
  100. while (!done) {
  101. cv.wait(lock);
  102. }
  103. if (status.ok()) {
  104. return reply.message();
  105. } else {
  106. std::cout << status.error_code() << ": " << status.error_message()
  107. << std::endl;
  108. return "RPC failed";
  109. }
  110. }
  111. std::string Ctrl_PositionGlobal(double lat_deg, double lon_deg, double alt_m, double yaw_deg) {
  112. Main_Ctrl_PositionGlobal_Request request;
  113. request.set_lat_deg(lat_deg);
  114. request.set_lon_deg(lon_deg);
  115. request.set_alt_m(alt_m);
  116. request.set_yaw_deg(yaw_deg);
  117. Main_Ctrl_PositionGlobal_Reply reply;
  118. ClientContext context;
  119. std::mutex mu;
  120. std::condition_variable cv;
  121. bool done = false;
  122. Status status;
  123. stub_->async()->Ctrl_PositionGlobal(&context, &request, &reply,
  124. [&mu, &cv, &done, &status](Status s) {
  125. status = std::move(s);
  126. std::lock_guard<std::mutex> lock(mu);
  127. done = true;
  128. cv.notify_one();
  129. });
  130. std::unique_lock<std::mutex> lock(mu);
  131. while (!done) {
  132. cv.wait(lock);
  133. }
  134. if (status.ok()) {
  135. return reply.message();
  136. } else {
  137. std::cout << status.error_code() << ": " << status.error_message()
  138. << std::endl;
  139. return "RPC failed";
  140. }
  141. }
  142. std::string Ctrl_PositionNED(double north_m, double east_m, double down_m, double yaw_deg) {
  143. Main_Ctrl_PositionNED_Request request;
  144. request.set_north_m(north_m);
  145. request.set_east_m(east_m);
  146. request.set_down_m(down_m);
  147. request.set_yaw_deg(yaw_deg);
  148. Main_Ctrl_PositionNED_Reply reply;
  149. ClientContext context;
  150. std::mutex mu;
  151. std::condition_variable cv;
  152. bool done = false;
  153. Status status;
  154. stub_->async()->Ctrl_PositionNED(&context, &request, &reply,
  155. [&mu, &cv, &done, &status](Status s) {
  156. status = std::move(s);
  157. std::lock_guard<std::mutex> lock(mu);
  158. done = true;
  159. cv.notify_one();
  160. });
  161. std::unique_lock<std::mutex> lock(mu);
  162. while (!done) {
  163. cv.wait(lock);
  164. }
  165. if (status.ok()) {
  166. return reply.message();
  167. } else {
  168. std::cout << status.error_code() << ": " << status.error_message()
  169. << std::endl;
  170. return "RPC failed";
  171. }
  172. }
  173. private:
  174. std::unique_ptr<MainGateway::Stub> stub_;
  175. };
  176. int main(int argc, char** argv) {
  177. std::string target_str = "127.0.0.1:8081";
  178. MainGatewayClient client(
  179. grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
  180. std::string reply{};
  181. for(int i = 0; i < 1000; ++i){
  182. reply = client.Ctrl_Takeoff(i);
  183. std::cout << reply << std::endl;
  184. reply = client.Ctrl_Land("Ctrl_Land " + std::to_string(i));
  185. std::cout << reply << std::endl;
  186. reply = client.Ctrl_RTL("Ctrl_RTL " + std::to_string(i));
  187. std::cout << reply << std::endl;
  188. reply = client.Ctrl_PositionGlobal(i, i, i, i);
  189. std::cout << reply << std::endl;
  190. reply = client.Ctrl_PositionNED(i, i, i, i);
  191. std::cout << reply << std::endl;
  192. std::cout << std::endl;
  193. sleep(1);
  194. }
  195. return 0;
  196. }