#include "vehicle_control.h" #include #include "modulecomm.h" #include "remotectrl.pb.h" extern std::string gstrserverip; extern std::string gstrcontrolPort; extern std::string gstrcontrolInterval; extern std::string gstruploadMapInterval; extern std::string gstrid; extern std::string gstrplateNumber; extern char stryamlpath[256]; extern uint8_t gCtrlMode_Status; //0 auto 1 remote 2 stop 3 platform using org::jeecg::defsControl::grpc::Empty; ///< other message using org::jeecg::defsControl::grpc::GPSPoint; using org::jeecg::defsControl::grpc::MapPoint; using org::jeecg::defsControl::grpc::CtrlMode; ///< other enum using org::jeecg::defsControl::grpc::ShiftStatus; VehicleControlClient::VehicleControlClient(std::shared_ptr channel) { stub_ = VehicleControl::NewStub(channel); dec_yaml(stryamlpath); shmRemoteCtrl.mpa = iv::modulecomm::RegisterSend(shmRemoteCtrl.mstrmsgname,shmRemoteCtrl.mnBufferSize,shmRemoteCtrl.mnBufferCount); } VehicleControlClient::~VehicleControlClient(void) { if(shmRemoteCtrl.mpa != nullptr)iv::modulecomm::Unregister(shmRemoteCtrl.mpa); requestInterruption(); while(this->isFinished() == false); } void VehicleControlClient::dec_yaml(const char *stryamlpath) { YAML::Node config; try { config = YAML::LoadFile(stryamlpath); } catch(YAML::BadFile &e) { std::cout<(); strncpy(shmRemoteCtrl.mstrmsgname,strmsgname.data(),255); shmRemoteCtrl.mnBufferSize = config["remote_ctrl"]["buffersize"].as(); shmRemoteCtrl.mnBufferCount = config["remote_ctrl"]["buffercount"].as(); std::cout << "RemoteCtrl:" << shmRemoteCtrl.mstrmsgname << "," << shmRemoteCtrl.mnBufferSize << "," << shmRemoteCtrl.mnBufferCount << std::endl; } } else { strmsgname = "remotectrl"; strncpy(shmRemoteCtrl.mstrmsgname,strmsgname.data(),255); shmRemoteCtrl.mnBufferSize = 10000; shmRemoteCtrl.mnBufferCount = 1; } return; } std::string VehicleControlClient::vehicleControl(void) { // Data we are sending to the server. Empty request; request.set_id(gstrid); // Container for the data we expect from the server. ControlReply reply; // Context for the client. It could be used to convey extra information to // the server and/or tweak certain RPC behaviors. ClientContext context; gpr_timespec timespec; timespec.tv_sec = 1; timespec.tv_nsec = 0; timespec.clock_type = GPR_TIMESPAN; context.set_deadline(timespec); // The actual RPC. Status status = stub_ -> vehicleControl(&context,request,&reply); // Act upon its status. if (status.ok()) { if(reply.id() == gstrid) { shiftCMD = reply.shiftcmd(); steeringWheelAngleCMD = reply.steeringwheelanglecmd(); throttleCMD = reply.throttlecmd(); brakeCMD = reply.brakecmd(); } else { // std::cout<<"id:"< 0)throttleCMD -= 1.5; // if(throttleCMD <= 0)throttleCMD = 0; // if(brakeCMD > 0)brakeCMD -= 1.5; // if(brakeCMD <= 0)brakeCMD = 0; } return "vehicleControl RPC successed"; } else { std::cout << status.error_code() << ": " << status.error_message() << std::endl; if(status.error_code() == 4) { std::cout << "vehicleControl RPC connect timeout" << std::endl; } shiftCMD = ShiftStatus::SHIFT_DRIVE; steeringWheelAngleCMD = 0.0; throttleCMD = 0.0; brakeCMD = 100.0; return "vehicleControl RPC failed"; } } void VehicleControlClient::updateControlData(void) { #if 0 std::cout<<"shift:"< pstr;pstr.reset(str); if(!xmsg.SerializeToArray(str,ndatasize)) { std::cout<<"updateControlData serialize error."<=interval) { std::string reply = vehicleControl(); // std::cout<< reply < channel) { stub_ = VehicleControl::NewStub(channel); } VehicleChangeCtrlModeClient::~VehicleChangeCtrlModeClient(void) { requestInterruption(); while(this->isFinished() == false); } std::string VehicleChangeCtrlModeClient::changeCtrlMode(void) { // Data we are sending to the server. Empty request; request.set_id(gstrid); // Container for the data we expect from the server. CtrlModeReply reply; // Context for the client. It could be used to convey extra information to // the server and/or tweastd::cout<<"shift:"< changeCtrlMode(&context,request,&reply); // Act upon its status. if (status.ok()) { if(reply.id() == gstrid) { modeCMD = reply.modecmd(); } return "changeCtrlMode RPC successed"; } else { std::cout << status.error_code() << ": " << status.error_message() << std::endl; if(status.error_code() == 4) { std::cout << "vehicleControl RPC connect timeout" << std::endl; } modeCMD = CtrlMode::CMD_EMERGENCY_STOP; return "changeCtrlMode RPC failed"; } } void VehicleChangeCtrlModeClient::updateCtrolMode(void) { // std::cout<<"modeCMD:"<=interval) { std::string reply = changeCtrlMode(); // std::cout<< reply < channel) { stub_ = VehicleControl::NewStub(channel); } VehicleUploadMapClient::~VehicleUploadMapClient(void) { requestInterruption(); while(this->isFinished() == false); } std::string VehicleUploadMapClient::uploadMap(void) { // Data we are sending to the server. Empty request; request.set_id(gstrid); // Container for the data we expect from the server. UploadMapReply reply; // Context for the client. It could be used to convey extra information to // the server and/or tweak certain RPC behaviors. ClientContext context; gpr_timespec timespec; timespec.tv_sec = 2; timespec.tv_nsec = 0; timespec.clock_type = GPR_TIMESPAN; context.set_deadline(timespec); // The actual RPC. Status status = stub_ -> UploadMap(&context,request,&reply); // Act upon its status. if (status.ok()) { if(reply.id() == gstrid) { // std::cout<=interval) { if(isNeedMap == false) { std::string reply = uploadMap(); // std::cout<< reply <