|
@@ -0,0 +1,322 @@
|
|
|
+#include <QCoreApplication>
|
|
|
+
|
|
|
+#include <QTime>
|
|
|
+
|
|
|
+#include <QMutex>
|
|
|
+
|
|
|
+#include "control/control_status.h"
|
|
|
+#include "control/controller.h"
|
|
|
+
|
|
|
+#include "xmlparam.h"
|
|
|
+#include "modulecomm.h"
|
|
|
+#include "ivbacktrace.h"
|
|
|
+#include "ivversion.h"
|
|
|
+
|
|
|
+#include "canmsg.pb.h"
|
|
|
+#include "decition.pb.h"
|
|
|
+#include "chassis.pb.h"
|
|
|
+
|
|
|
+#include "torquebrake.h"
|
|
|
+
|
|
|
+#include <thread>
|
|
|
+
|
|
|
+void * gpacansend;
|
|
|
+void * gpadecition;
|
|
|
+void * gpachassis;
|
|
|
+
|
|
|
+std::string gstrmemdecition;
|
|
|
+std::string gstrmemcansend;
|
|
|
+std::string gstrmemchassis;
|
|
|
+bool gbSendRun = true;
|
|
|
+
|
|
|
+bool gbChassisEPS = false;
|
|
|
+
|
|
|
+iv::brain::decition gdecition_def;
|
|
|
+iv::brain::decition gdecition;
|
|
|
+
|
|
|
+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<iv::control::Controller> gcontroller; //实际车辆控制器
|
|
|
+
|
|
|
+bool bHasGearCmd = false;
|
|
|
+bool bHasSteerCmd = false;
|
|
|
+bool bHasDriveCmd = false;
|
|
|
+bool bHasBrakeCmd = false;
|
|
|
+bool bHasParkCmd = false;
|
|
|
+
|
|
|
+static QMutex gMutex;
|
|
|
+
|
|
|
+void executeDecition(const iv::brain::decition decition)
|
|
|
+{
|
|
|
+ if(decition.has_gear())
|
|
|
+ {
|
|
|
+ gcontroller->control_gear_en(true);
|
|
|
+ gcontroller->control_gear(decition.gear());
|
|
|
+ bHasGearCmd = true;
|
|
|
+ }
|
|
|
+ if(decition.has_wheelangle())
|
|
|
+ {
|
|
|
+ gcontroller->control_angle_enable(true);
|
|
|
+ gcontroller->control_wheel(decition.wheelangle());
|
|
|
+ gcontroller->control_angle_speed(decition.wheelspeed());
|
|
|
+ bHasSteerCmd = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(decition.has_accelerator())
|
|
|
+ {
|
|
|
+ gcontroller->control_acc_en(true);
|
|
|
+ gcontroller->control_torque(decition.torque());
|
|
|
+ bHasDriveCmd = true;
|
|
|
+ }
|
|
|
+ if(decition.has_brake())
|
|
|
+ {
|
|
|
+ gcontroller->control_brake_en(true);
|
|
|
+ gcontroller->control_brake(decition.brake());
|
|
|
+ bHasBrakeCmd = true;
|
|
|
+ }
|
|
|
+ if(decition.has_handbrake())
|
|
|
+ {
|
|
|
+ gcontroller->control_handBrake_en(true);
|
|
|
+ gcontroller->control_handBrake(decition.handbrake());
|
|
|
+ bHasParkCmd = true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+#ifdef TORQUEBRAKETEST
|
|
|
+ if(gnothavetb < 10)
|
|
|
+ {
|
|
|
+ iv::controller::torquebrake xtb;
|
|
|
+ gMutextb.lock();
|
|
|
+ xtb.CopyFrom(gtb);
|
|
|
+ gMutextb.unlock();
|
|
|
+ if(xtb.enable())
|
|
|
+ {
|
|
|
+ gcontroller->control_torque(xtb.torque());
|
|
|
+ gcontroller->control_brake(xtb.brake());
|
|
|
+ }
|
|
|
+ gnothavetb++;
|
|
|
+ }
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void UpdateChassis(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
|
|
|
+{
|
|
|
+ iv::chassis xchassis;
|
|
|
+ static int ncount = 0;
|
|
|
+ if(!xchassis.ParseFromArray(strdata,nSize))
|
|
|
+ {
|
|
|
+ std::cout<<"iv::decition::BrainDecition::UpdateChassis ParseFrom Array Error."<<std::endl;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(xchassis.epsmode() == 0)
|
|
|
+ {
|
|
|
+ gbChassisEPS = true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void ListenDeciton(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
|
|
|
+{
|
|
|
+ qint64 oldtime;
|
|
|
+ iv::brain::decition xdecition;
|
|
|
+
|
|
|
+ if(!xdecition.ParseFromArray(strdata,nSize))
|
|
|
+ {
|
|
|
+ std::cout<<"ListenDecition parse error."<<std::endl;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(xdecition.gear() != 4)
|
|
|
+ {
|
|
|
+ qDebug("not D");
|
|
|
+ }
|
|
|
+ if((oldtime - QDateTime::currentMSecsSinceEpoch())<-100)qDebug("dection time is %ld diff is %ld ",QDateTime::currentMSecsSinceEpoch(),oldtime - QDateTime::currentMSecsSinceEpoch());
|
|
|
+ oldtime = QDateTime::currentMSecsSinceEpoch();
|
|
|
+ gMutex.lock();
|
|
|
+ gdecition.CopyFrom(xdecition);
|
|
|
+ gMutex.unlock();
|
|
|
+ gnDecitionNum = gnDecitionNumMax;
|
|
|
+ gbChassisEPS = false;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void ExecSend()
|
|
|
+{
|
|
|
+ iv::can::canmsg xmsg;
|
|
|
+ iv::can::canraw xraw;
|
|
|
+ if(bHasGearCmd)
|
|
|
+ {
|
|
|
+ xraw.set_id(ServiceControlStatus.cmd_id_gear);
|
|
|
+ xraw.set_data(ServiceControlStatus.cmd_gear.byte,8);
|
|
|
+ xraw.set_bext(false);
|
|
|
+ xraw.set_bremote(false);
|
|
|
+ xraw.set_len(8);
|
|
|
+ iv::can::canraw * pxraw = xmsg.add_rawmsg();
|
|
|
+ pxraw->CopyFrom(xraw);
|
|
|
+ xmsg.set_channel(0);
|
|
|
+ xmsg.set_index(gnIndex);
|
|
|
+ bHasGearCmd = false;
|
|
|
+ }
|
|
|
+ if(bHasSteerCmd)
|
|
|
+ {
|
|
|
+ xraw.set_id(ServiceControlStatus.cmd_id_steer);
|
|
|
+ xraw.set_data(ServiceControlStatus.cmd_steer.byte,8);
|
|
|
+ xraw.set_bext(false);
|
|
|
+ xraw.set_bremote(false);
|
|
|
+ xraw.set_len(8);
|
|
|
+ iv::can::canraw * pxraw1 = xmsg.add_rawmsg();
|
|
|
+ pxraw1->CopyFrom(xraw);
|
|
|
+ xmsg.set_channel(0);
|
|
|
+ xmsg.set_index(gnIndex);
|
|
|
+ bHasSteerCmd = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(bHasDriveCmd)
|
|
|
+ {
|
|
|
+ xraw.set_id(ServiceControlStatus.cmd_id_drive);
|
|
|
+ xraw.set_data(ServiceControlStatus.cmd_drive.byte,8);
|
|
|
+ xraw.set_bext(false);
|
|
|
+ xraw.set_bremote(false);
|
|
|
+ xraw.set_len(8);
|
|
|
+ iv::can::canraw * pxraw2 = xmsg.add_rawmsg();
|
|
|
+ pxraw2->CopyFrom(xraw);
|
|
|
+ xmsg.set_channel(0);
|
|
|
+ xmsg.set_index(gnIndex);
|
|
|
+ bHasDriveCmd = false;
|
|
|
+ }
|
|
|
+ if(bHasBrakeCmd)
|
|
|
+ {
|
|
|
+ xraw.set_id(ServiceControlStatus.cmd_id_brake);
|
|
|
+ xraw.set_data(ServiceControlStatus.cmd_brake.byte,8);
|
|
|
+ xraw.set_bext(false);
|
|
|
+ xraw.set_bremote(false);
|
|
|
+ xraw.set_len(8);
|
|
|
+ iv::can::canraw * pxraw2 = xmsg.add_rawmsg();
|
|
|
+ pxraw2->CopyFrom(xraw);
|
|
|
+ xmsg.set_channel(0);
|
|
|
+ xmsg.set_index(gnIndex);
|
|
|
+ bHasBrakeCmd = false;
|
|
|
+ }
|
|
|
+ if(bHasParkCmd)
|
|
|
+ {
|
|
|
+ xraw.set_id(ServiceControlStatus.cmd_id_park);
|
|
|
+ xraw.set_data(ServiceControlStatus.cmd_park.byte,8);
|
|
|
+ xraw.set_bext(false);
|
|
|
+ xraw.set_bremote(false);
|
|
|
+ xraw.set_len(8);
|
|
|
+ iv::can::canraw * pxraw2 = xmsg.add_rawmsg();
|
|
|
+ pxraw2->CopyFrom(xraw);
|
|
|
+ xmsg.set_channel(0);
|
|
|
+ xmsg.set_index(gnIndex);
|
|
|
+ bHasParkCmd = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ gnIndex++;
|
|
|
+ xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
|
|
|
+ int ndatasize = xmsg.ByteSize();
|
|
|
+ char * strser = new char[ndatasize];
|
|
|
+ std::shared_ptr<char> pstrser;
|
|
|
+ pstrser.reset(strser);
|
|
|
+ if(xmsg.SerializeToArray(strser,ndatasize))
|
|
|
+ {
|
|
|
+ iv::modulecomm::ModuleSendMsg(gpacansend,strser,ndatasize);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ std::cout<<"MainWindow::onTimer serialize error."<<std::endl;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//void initial()
|
|
|
+//{
|
|
|
+// // ServiceControlStatus.command.byte[0] = 0;
|
|
|
+// // ServiceControlStatus.command.byte[1] = 0;
|
|
|
+// // ServiceControlStatus.command.byte[2] = 0;
|
|
|
+// // ServiceControlStatus.command.byte[3] = 0;
|
|
|
+// // ServiceControlStatus.command.byte[4] = 0;
|
|
|
+// // ServiceControlStatus.command.byte[5] = 0;
|
|
|
+// // ServiceControlStatus.command.byte[6] = 0;
|
|
|
+// // ServiceControlStatus.command.byte[7] = 0;
|
|
|
+//}
|
|
|
+
|
|
|
+void sendthread()
|
|
|
+{
|
|
|
+// initial();
|
|
|
+ iv::brain::decition xdecition;
|
|
|
+ while(gbSendRun)
|
|
|
+ {
|
|
|
+ if(gnDecitionNum <= 0)
|
|
|
+ {
|
|
|
+ xdecition.CopyFrom(gdecition_def);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ gMutex.lock();
|
|
|
+ xdecition.CopyFrom(gdecition);
|
|
|
+ gMutex.unlock();
|
|
|
+ gnDecitionNum--;
|
|
|
+ }
|
|
|
+ executeDecition(xdecition);
|
|
|
+ if(gbChassisEPS == false) ExecSend();
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+int main(int argc, char *argv[])
|
|
|
+{
|
|
|
+ RegisterIVBackTrace();
|
|
|
+ showversion("controller_hapo");
|
|
|
+ QCoreApplication a(argc, argv);
|
|
|
+
|
|
|
+ QString strpath = QCoreApplication::applicationDirPath();
|
|
|
+
|
|
|
+ if(argc < 2)
|
|
|
+ strpath = strpath + "/controller_vv7.xml";
|
|
|
+ else
|
|
|
+ strpath = argv[1];
|
|
|
+ std::cout<<strpath.toStdString()<<std::endl;
|
|
|
+
|
|
|
+ gdecition_def.set_accelerator(-0.5);
|
|
|
+ // gdecition_def.set_brake(1.0);
|
|
|
+ gdecition_def.set_brake(20); //apollo_fu 20200417
|
|
|
+ gdecition_def.set_torque(0);
|
|
|
+ // gdecition_def.set_rightlamp(true);
|
|
|
+ // gdecition_def.set_doublespark(true);
|
|
|
+ gdecition_def.set_wheelangle(0);
|
|
|
+ gdecition_def.set_mode(1);
|
|
|
+ gdecition_def.set_grade(1);
|
|
|
+ gdecition_def.set_gear(1);
|
|
|
+ gdecition_def.set_engine(0);
|
|
|
+ // gdecition_def.set_gear(0);
|
|
|
+ gTime.start();
|
|
|
+
|
|
|
+ gcontroller = boost::shared_ptr<iv::control::Controller>(new iv::control::Controller());
|
|
|
+
|
|
|
+ iv::xmlparam::Xmlparam xp(strpath.toStdString());
|
|
|
+
|
|
|
+ gstrmemcansend = xp.GetParam("cansend","cansend0");
|
|
|
+ gstrmemdecition = xp.GetParam("dection","deciton");
|
|
|
+ gstrmemchassis = xp.GetParam("chassismsgname","chassis");
|
|
|
+
|
|
|
+ gpacansend = iv::modulecomm::RegisterSend(gstrmemcansend.data(),10000,1);
|
|
|
+ gpadecition = iv::modulecomm::RegisterRecv(gstrmemdecition.data(),ListenDeciton);
|
|
|
+ gpachassis = iv::modulecomm::RegisterRecv(gstrmemchassis.data(),UpdateChassis);
|
|
|
+
|
|
|
+#ifdef TORQUEBRAKETEST
|
|
|
+ EnableTorqueBrakeTest();
|
|
|
+#endif
|
|
|
+
|
|
|
+ std::thread xthread(sendthread);
|
|
|
+
|
|
|
+ return a.exec();
|
|
|
+}
|