#include #include #include #include "control/control_status.h" #include "control/controller.h" #include "xmlparam.h" #include "modulecomm.h" #include "ivversion.h" #include "ivbacktrace.h" #include "canmsg.pb.h" #include "decition.pb.h" #include #include "remotectrl.pb.h" #include "platform_feedback.pb.h" QMutex gMutex; void * gpacansend; void * gpadecition; void * gparemote; void * gpaPaltformFeedback; std::string gstrmemdecition; std::string gstrmemcansend; std::string gstrmemremote; //Remote Ctrl std::string gstrmemPaltformFeedback; bool gbSendRun = true; iv::brain::decition gdecition_def; iv::brain::decition gdecition; bool gbAllowRemote = false; //Default, Not Allow Remote bool gbAutoDriving = true; //if true, Auto Driving, false, remote controll bool gbChassisEPS = true; iv::brain::decition gdecition_remote; iv::platformFeedback gPlatformFeedback; qint64 gLastRemoteTime = 0; QTime gTime; int gnLastSendTime = 0; int gnLastRecvDecTime = -1000; int gnDecitionNum = 0; //when is zero,send default; const int gnDecitionNumMax = 100; int gnIndex = 0; boost::shared_ptr gcontroller; //实际车辆控制器 void executeDecition(const iv::brain::decition decition) { std::cout<<"acc is "<inialize(); gcontroller->control_torque(decition.torque()); gcontroller->control_wheel(decition.wheelangle()); gcontroller->control_brake(decition.brake()); gcontroller->control_limit_speed(decition.speed()); gcontroller->control_drive_mode(decition.mode()); gcontroller->control_elec_brake(decition.handbrake()); gcontroller->control_brake_light(decition.brakelamp()); gcontroller->control_dangwei(decition.gear()); //0P 1D 2R 3N switch (decition.gear()) { case 0: gPlatformFeedback.set_shift(3); //0N 1D 2R 3P break; case 1: gPlatformFeedback.set_shift(1); //0N 1D 2R 3P break; case 2: gPlatformFeedback.set_shift(2); //0N 1D 2R 3P break; case 3: gPlatformFeedback.set_shift(0); //0N 1D 2R 3P break; default: break; } gPlatformFeedback.set_throttle(decition.torque()); gPlatformFeedback.set_brake(decition.brake()); gPlatformFeedback.set_steeringwheelangle(decition.wheelangle()); } void ListenRemotectrl(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) { // qint64 oldtime; iv::brain::decition xdecition; iv::remotectrl xrc; if(!xrc.ParseFromArray(strdata,nSize)) { std::cout<<"ListenRemotectrl parse error."< 0) //D shift xdecition.set_gear(1); //0P 1D 2R 3N else if(xrc.shift() < 0) xdecition.set_gear(2); else xdecition.set_gear(3); xdecition.set_handbrake(0); xdecition.set_grade(1); xdecition.set_mode(1); xdecition.set_speak(0); xdecition.set_headlight(false); xdecition.set_engine(0); xdecition.set_taillight(false); gMutex.lock(); gdecition_remote.CopyFrom(xdecition); gMutex.unlock(); gLastRemoteTime = QDateTime::currentMSecsSinceEpoch(); } } void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) { iv::brain::decition xdecition; if(!xdecition.ParseFromArray(strdata,nSize)) { std::cout<<"ListenDecition parse error."<CopyFrom(xraw); xmsg.set_channel(0); xmsg.set_index(gnIndex); gnIndex++; xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch()); int ndatasize = xmsg.ByteSize(); char * strser = new char[ndatasize]; std::shared_ptr pstrser; pstrser.reset(strser); if(xmsg.SerializePartialToArray(strser,ndatasize)) { iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize); } else { std::cout<<"MainWindow::onTimer serialize error."< pstr;pstr.reset(str_pf); if(!gPlatformFeedback.SerializeToArray(str_pf,ndatasize_pf)) { std::cout<<"MainWindow::on_horizontalSlider_valueChanged serialize error."< 1000) { xdecition.CopyFrom(gdecition_def); } else { gMutex.lock(); xdecition.CopyFrom(gdecition_remote); gMutex.unlock(); } } executeDecition(xdecition); ExecSend(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } int main(int argc, char *argv[]) { RegisterIVBackTrace(); showversion("controller_midcar"); QCoreApplication a(argc, argv); QString strpath = QCoreApplication::applicationDirPath(); if(argc < 2) strpath = strpath + "/controller_midcar.xml"; else strpath = argv[1]; std::cout<(new iv::control::Controller()); iv::xmlparam::Xmlparam xp(strpath.toStdString()); gstrmemcansend = xp.GetParam("cansend","cansend0"); gstrmemdecition = xp.GetParam("dection","deciton"); gstrmemremote = xp.GetParam("remotectrl","remotectrl"); gstrmemPaltformFeedback = xp.GetParam("paltformFeedback","platformFeedback"); std::string strremote = xp.GetParam("allowremote","true"); if(strremote == "true") { gbAllowRemote = true; } gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,3); gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton); if(gbAllowRemote) { gpaPaltformFeedback = iv::modulecomm::RegisterSend(gstrmemPaltformFeedback.data(),10000,1); gparemote = iv::modulecomm::RegisterRecv(gstrmemremote.data(),ListenRemotectrl); } std::thread xthread(sendthread); return a.exec(); }