Explorar el Código

git g29zlgchassistest for g29 and zlg test chassis, not complete.

yuchuli hace 1 semana
padre
commit
e2a1baa714

+ 73 - 0
src/tool/g29zlgchassistest/.gitignore

@@ -0,0 +1,73 @@
+# This file is used to ignore files which are generated
+# ----------------------------------------------------------------------------
+
+*~
+*.autosave
+*.a
+*.core
+*.moc
+*.o
+*.obj
+*.orig
+*.rej
+*.so
+*.so.*
+*_pch.h.cpp
+*_resource.rc
+*.qm
+.#*
+*.*#
+core
+!core/
+tags
+.DS_Store
+.directory
+*.debug
+Makefile*
+*.prl
+*.app
+moc_*.cpp
+ui_*.h
+qrc_*.cpp
+Thumbs.db
+*.res
+*.rc
+/.qmake.cache
+/.qmake.stash
+
+# qtcreator generated files
+*.pro.user*
+
+# xemacs temporary files
+*.flc
+
+# Vim temporary files
+.*.swp
+
+# Visual Studio generated files
+*.ib_pdb_index
+*.idb
+*.ilk
+*.pdb
+*.sln
+*.suo
+*.vcproj
+*vcproj.*.*.user
+*.ncb
+*.sdf
+*.opensdf
+*.vcxproj
+*vcxproj.*
+
+# MinGW generated files
+*.Debug
+*.Release
+
+# Python byte code
+*.pyc
+
+# Binaries
+# --------
+*.dll
+*.exe
+

+ 33 - 0
src/tool/g29zlgchassistest/g29zlgchassistest.pro

@@ -0,0 +1,33 @@
+QT       += core gui
+
+greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+
+CONFIG += c++11
+
+# The following define makes your compiler emit warnings if you use
+# any Qt feature that has been marked deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if it uses deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+SOURCES += \
+    joyreadthread.cpp \
+    main.cpp \
+    mainwindow.cpp
+
+HEADERS += \
+    joyreadthread.h \
+    mainwindow.h
+
+FORMS += \
+    mainwindow.ui
+
+# Default rules for deployment.
+qnx: target.path = /tmp/$${TARGET}/bin
+else: unix:!android: target.path = /opt/$${TARGET}/bin
+!isEmpty(target.path): INSTALLS += target

+ 353 - 0
src/tool/g29zlgchassistest/joyreadthread.cpp

@@ -0,0 +1,353 @@
+
+
+#include <QThread>
+
+#include <QFile>
+
+
+#ifndef Q_OS_WIN
+
+#include "joyreadthread.h"
+#include <assert.h>
+#include <math.h>
+#include <algorithm>
+#include <iostream>
+#include <stdexcept>
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sstream>
+#include <unistd.h>
+#include <linux/joystick.h>
+
+#include <QFile>
+
+extern std::string gstrjoy_path;
+
+JoyReadThread::JoyReadThread()
+{
+
+    if ((fd = open(gstrjoy_path.data(), O_RDONLY)) < 0)
+    {
+        mbJoyOK = false;
+      std::ostringstream str;
+//      str << filename << ": " << strerror(errno);
+//      throw std::runtime_error(str.str());
+
+    }
+    else
+    {
+      // ok
+      uint8_t num_axis   = 0;
+      uint8_t num_button = 0;
+      ioctl(fd, JSIOCGAXES,    &num_axis);
+      ioctl(fd, JSIOCGBUTTONS, &num_button);
+      axis_count   = num_axis;
+      button_count = num_button;
+
+      // Get Name
+      char name_c_str[1024];
+      if (ioctl(fd, JSIOCGNAME(sizeof(name_c_str)), name_c_str) < 0)
+      {
+          mbJoyOK = false;
+          std::ostringstream str;
+    //      str << filename << ": " << strerror(errno);
+          str << "joy:" << ": " << strerror(errno);
+  //        throw std::runtime_error(str.str());
+      }
+      else
+      {
+          orig_name = name_c_str;
+//          try {
+//              name = Glib::convert_with_fallback(name_c_str, "UTF-8", "ISO-8859-1");
+//          } catch(Glib::ConvertError& err) {
+//              std::cout << err.what() << std::endl;
+//          }
+      }
+
+//      axis_state.resize(axis_count);
+    }
+}
+
+void JoyReadThread::run()
+{
+    if(mbJoyOK == false)return;
+    while(!QThread::isInterruptionRequested())
+    {
+        struct js_event event;
+
+        ssize_t len = read(fd, &event, sizeof(event));
+
+        if(len < 0)
+        {
+//            qDebug("fail.");
+            mbJoyOK = false;
+            break;
+        }
+
+ //       if(len >0)qDebug("len = %d",len);
+
+        if(len == sizeof(js_event))
+        {
+            switch (event.number) {
+            case 0:
+                mfWheel = event.value;
+                break;
+            case 2:
+                mfAcc = event.value;
+                break;
+            case 3:
+                mfBrake = event.value;
+                break;
+            case 12:
+                if(event.value ==1 ) mnShift = 1;
+                else mnShift = 0;
+                break;
+            case 13:
+                if(event.value ==1 ) mnShift = 2;
+                else mnShift = 0;
+                break;
+            case 14:
+                if(event.value ==1 ) mnShift = 3;
+                else mnShift = 0;
+                break;
+            case 15:
+                if(event.value ==1 ) mnShift = 4;
+                else mnShift = 0;
+                break;
+            case 16:
+                if(event.value ==1 ) mnShift = 5;
+                else mnShift = 0;
+                break;
+            case 17:
+                if(event.value ==1 ) mnShift = 6;
+                else mnShift = 0;
+                break;
+            case 18:
+                if(event.value ==1 ) mnShift = -1;
+                else mnShift = 0;
+                break;
+            default:
+                break;
+            }
+            qDebug("shift = %d",mnShift);
+//            qDebug("type = %d number = %d value = %d ",event.type,event.number,event.value);
+//            qDebug("wheel:%g acc:%g brake:%g",mfWheel,mfAcc,mfBrake);
+        }
+    }
+    close(fd);
+    mbJoyOK = false;
+}
+
+bool JoyReadThread::isOK()
+{
+    return mbJoyOK;
+}
+
+double JoyReadThread::GetAcc()
+{
+    return mfAcc;
+}
+
+double JoyReadThread::GetBrake()
+{
+    return mfBrake;
+}
+
+double JoyReadThread::GetWheel()
+{
+    return mfWheel;
+}
+
+int JoyReadThread::GetShift()
+{
+    return mnShift;
+}
+
+
+#else
+
+#include <QFile>
+#include <QLibrary>
+#include <iostream>
+#include "dinput.h"
+#include "joyreadthread.h"
+
+#ifdef Q_OS_WIN
+extern HWND gwnd;
+#endif
+
+//void JoyReadThread::LoadAPI()
+//{
+//    mbJoyOK = false;
+//    std::cout<<"Load Logi g29 API"<<std::endl;
+//    QLibrary xlib("./LogitechSteeringWheelEnginesWrapper.dll");
+//    if(!xlib.load())
+//    {
+//        std::cout<<" load LogitechSteeringWheelEnginesWrapper fail."<<xlib.errorString().toStdString()<< std::endl;
+//        return ;
+//    }
+
+//    LogiUpdate =(LogiUpdateFunction)xlib.resolve("LogiUpdate");
+
+//    if(LogiUpdate == NULL)
+//    {
+//        std::cout<<" no this api."<<std::endl;
+//        return;
+//    }
+//    else
+//    {
+//        std::cout<<" Load API Successfully. "<<std::endl;
+//    }
+
+//    LogiSteeringInitialize =(LogiSteeringInitializeFunction)xlib.resolve("LogiSteeringInitialize");
+//    LogiSteeringInitializeWithWindow =(LogiSteeringInitializeWithWindowFunction)xlib.resolve("LogiSteeringInitializeWithWindow");
+//    LogiIsConnected =(LogiIsConnectedFunction)xlib.resolve("LogiIsConnected");
+//    LogiGetState =(LogiGetStateFunction)xlib.resolve("LogiGetState");
+//    LogiSteeringShutdown =(LogiSteeringShutdownFunction)xlib.resolve("LogiSteeringShutdown");
+//    LogiPlayConstantForce =(LogiPlayConstantForceFunction)xlib.resolve("LogiPlayConstantForce");
+//    LogiStopConstantForce =(LogiStopConstantForceFunction)xlib.resolve("LogiStopConstantForce");
+
+//    if((LogiSteeringInitialize == NULL)||(LogiIsConnected == NULL)||(LogiGetState == NULL)||(LogiSteeringShutdown == NULL)||(LogiSteeringInitializeWithWindow == NULL))
+//    {
+//        std::cout<<" no this apiS."<<std::endl;
+//        return;
+//    }
+//    else
+//    {
+//        std::cout<<" Load All API Successfully. "<<std::endl;
+//    }
+
+//    mbJoyOK = true;
+//}
+
+JoyReadThread::JoyReadThread()
+{
+
+//    LoadAPI();
+}
+
+void JoyReadThread::run()
+{
+    if(mbJoyOK == false)return;
+
+    return;
+
+     msleep(3000);
+//    bool bStart = LogiSteeringInitialize(false);
+
+
+
+    bool bStart = LogiSteeringInitializeWithWindow(false,gwnd);
+    std::cout<<" init. "<<bStart<<std::endl;
+
+    int nFail = 0;
+    while(!QThread::isInterruptionRequested())
+    {
+
+        double angle,acc,brake;
+        if((LogiUpdate())&&(LogiIsConnected(0)))
+        {
+            std::cout<<"get state."<<std::endl;
+            DIJOYSTATE2* wheel = LogiGetState(0);
+            angle = wheel->lX;
+            acc = wheel->lY;
+            brake = wheel->lRz;
+            if(angle>100)
+            {
+                int force;
+                if(angle>3000)force = 30;
+                else
+                {
+                    force = angle*30/3000;
+                }
+                LogiPlayConstantForce(0,force);
+            }
+            else
+            {
+                if(angle<-100)
+                {
+                    int force;
+                    if(angle<-3000)force = -30;
+                    else
+                    {
+                        force = angle*30/3000;
+                    }
+                   LogiPlayConstantForce(0,force);
+                }
+                else
+                {
+                    LogiStopConstantForce(0);
+                }
+            }
+
+            mfWheel = angle;
+            mfAcc = acc;
+            mfBrake = brake;
+            nFail = 0;
+            mbJoyOK = true;
+        }
+        else
+        {
+            nFail++;
+        }
+        if(nFail>10)
+        {
+            mbJoyOK = false;
+        }
+        msleep(20);
+
+    }
+
+    LogiSteeringShutdown();
+    mbJoyOK = false;
+}
+
+bool JoyReadThread::isOK()
+{
+    return mbJoyOK;
+}
+
+double JoyReadThread::GetAcc()
+{
+    return mfAcc;
+}
+
+double JoyReadThread::GetBrake()
+{
+    return mfBrake;
+}
+
+double JoyReadThread::GetWheel()
+{
+    return mfWheel;
+}
+
+int JoyReadThread::GetShift()
+{
+    return mnShift;
+}
+
+void JoyReadThread::SetWheel(double fWheel)
+{
+    mfWheel = fWheel;
+}
+void JoyReadThread::SetAcc(double fAcc)
+{
+    mfAcc = fAcc;
+}
+
+void JoyReadThread::SetBrake(double fBrake)
+{
+    mfBrake = fBrake;
+}
+void JoyReadThread::SetJoyOK(bool bOk)
+{
+    mbJoyOK = bOk;
+}
+
+#endif

+ 89 - 0
src/tool/g29zlgchassistest/joyreadthread.h

@@ -0,0 +1,89 @@
+#ifndef JOYREADTHREAD_H
+#define JOYREADTHREAD_H
+
+
+
+#include <QThread>
+
+#include <QFile>
+
+#ifdef Q_OS_WIN
+ #include "dinput.h"
+
+typedef bool (*LogiSteeringInitializeFunction)(bool ignoreXInputControllers);
+
+typedef bool (*LogiSteeringInitializeWithWindowFunction)(bool ignoreXInputControllers,HWND hwnd);
+
+
+typedef bool  (*LogiUpdateFunction)();
+//Check if a generic device at index is connected
+typedef  bool (*LogiIsConnectedFunction)(const int index);
+
+
+//Get the state of the controller in the standard way.
+typedef   DIJOYSTATE2* (*LogiGetStateFunction)(const int index);
+//Call this function to shutdown the SDK and destroy the controller and wheel objects
+typedef void (*LogiSteeringShutdownFunction)();
+
+//Play the constant force on the controller at index with the specified parameter
+typedef bool (*LogiPlayConstantForceFunction)(const int index, const int magnitudePercentage);
+
+//Stop the constant force on the controller at index
+typedef bool (*LogiStopConstantForceFunction)(const int index);
+#endif
+
+class JoyReadThread : public QThread
+{
+public:
+    JoyReadThread();
+
+public:
+    bool isOK();
+    double GetWheel();
+    double GetAcc();
+    double GetBrake();
+    int GetShift();
+private:
+    void run();
+
+private:
+    int fd;
+    int axis_count;
+    int button_count;
+    std::string orig_name;
+
+    QFile mxFile;
+    QFile * mpFile;
+    bool mbJoyOK = true;
+
+    double mfWheel = 0;
+    double mfAcc = 32767;
+    double mfBrake = 32767;
+
+    int mnShift = 0; //0 N >0 D -1 R
+
+private:
+#ifdef Q_OS_WIN
+
+
+
+
+    LogiUpdateFunction LogiUpdate;
+    LogiSteeringInitializeFunction LogiSteeringInitialize;
+    LogiIsConnectedFunction LogiIsConnected;
+    LogiGetStateFunction LogiGetState;
+    LogiSteeringShutdownFunction LogiSteeringShutdown;
+    LogiPlayConstantForceFunction LogiPlayConstantForce;
+    LogiStopConstantForceFunction LogiStopConstantForce;
+    LogiSteeringInitializeWithWindowFunction LogiSteeringInitializeWithWindow;
+//    void LoadAPI();
+public:
+    void SetWheel(double fWheel);
+    void SetAcc(double fAcc);
+    void SetBrake(double fBrake);
+    void SetJoyOK(bool bOk);
+#endif
+};
+
+
+#endif // JOYREADTHREAD_H

+ 11 - 0
src/tool/g29zlgchassistest/main.cpp

@@ -0,0 +1,11 @@
+#include "mainwindow.h"
+
+#include <QApplication>
+
+int main(int argc, char *argv[])
+{
+    QApplication a(argc, argv);
+    MainWindow w;
+    w.show();
+    return a.exec();
+}

+ 71 - 0
src/tool/g29zlgchassistest/mainwindow.cpp

@@ -0,0 +1,71 @@
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+
+#include <iostream>
+
+MainWindow::MainWindow(QWidget *parent)
+    : QMainWindow(parent)
+    , ui(new Ui::MainWindow)
+{
+    ui->setupUi(this);
+
+    LoadAPI();
+
+    HWND hwnd = (HWND)this->winId();
+    bool bStart = LogiSteeringInitializeWithWindow(false,hwnd);
+    std::cout<<" init. "<<bStart<<std::endl;
+
+    setWindowTitle("Chassis Test Use G29 & ZLG");
+}
+
+MainWindow::~MainWindow()
+{
+    LogiSteeringShutdown();
+
+    delete ui;
+}
+
+void MainWindow::LoadAPI()
+{
+
+    std::cout<<"Load Logi g29 API"<<std::endl;
+    QLibrary xlib("./LogitechSteeringWheelEnginesWrapper.dll");
+    if(!xlib.load())
+    {
+        std::cout<<" load LogitechSteeringWheelEnginesWrapper fail."<<xlib.errorString().toStdString()<< std::endl;
+        return ;
+    }
+
+    LogiUpdate =(LogiUpdateFunction)xlib.resolve("LogiUpdate");
+
+    if(LogiUpdate == NULL)
+    {
+        std::cout<<" no this api."<<std::endl;
+        return;
+    }
+    else
+    {
+        std::cout<<" Load API Successfully. "<<std::endl;
+    }
+
+    LogiSteeringInitialize =(LogiSteeringInitializeFunction)xlib.resolve("LogiSteeringInitialize");
+    LogiSteeringInitializeWithWindow =(LogiSteeringInitializeWithWindowFunction)xlib.resolve("LogiSteeringInitializeWithWindow");
+    LogiIsConnected =(LogiIsConnectedFunction)xlib.resolve("LogiIsConnected");
+    LogiGetState =(LogiGetStateFunction)xlib.resolve("LogiGetState");
+    LogiSteeringShutdown =(LogiSteeringShutdownFunction)xlib.resolve("LogiSteeringShutdown");
+    LogiPlayConstantForce =(LogiPlayConstantForceFunction)xlib.resolve("LogiPlayConstantForce");
+    LogiStopConstantForce =(LogiStopConstantForceFunction)xlib.resolve("LogiStopConstantForce");
+
+    if((LogiSteeringInitialize == NULL)||(LogiIsConnected == NULL)||(LogiGetState == NULL)||(LogiSteeringShutdown == NULL)||(LogiSteeringInitializeWithWindow == NULL))
+    {
+        std::cout<<" no this apiS."<<std::endl;
+        return;
+    }
+    else
+    {
+        std::cout<<" Load All API Successfully. "<<std::endl;
+    }
+
+}
+
+

+ 36 - 0
src/tool/g29zlgchassistest/mainwindow.h

@@ -0,0 +1,36 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+#include <QLibrary>
+
+#include "joyreadthread.h"
+
+QT_BEGIN_NAMESPACE
+namespace Ui { class MainWindow; }
+QT_END_NAMESPACE
+
+class MainWindow : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    MainWindow(QWidget *parent = nullptr);
+    ~MainWindow();
+
+private:
+    Ui::MainWindow *ui;
+
+
+private:
+    LogiUpdateFunction LogiUpdate;
+    LogiSteeringInitializeFunction LogiSteeringInitialize;
+    LogiIsConnectedFunction LogiIsConnected;
+    LogiGetStateFunction LogiGetState;
+    LogiSteeringShutdownFunction LogiSteeringShutdown;
+    LogiPlayConstantForceFunction LogiPlayConstantForce;
+    LogiStopConstantForceFunction LogiStopConstantForce;
+    LogiSteeringInitializeWithWindowFunction LogiSteeringInitializeWithWindow;
+    void LoadAPI();
+};
+#endif // MAINWINDOW_H

+ 22 - 0
src/tool/g29zlgchassistest/mainwindow.ui

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>800</width>
+    <height>600</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>MainWindow</string>
+  </property>
+  <widget class="QWidget" name="centralwidget"/>
+  <widget class="QMenuBar" name="menubar"/>
+  <widget class="QStatusBar" name="statusbar"/>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>