• Welcome to PiBoSo Official Forum. Please login or sign up.
 

The Lumberbar/MCCC/M300/M301

Started by Chris_Beeves, May 06, 2019, 09:07:40 pm

Previous topic - Next topic

Chris_Beeves

The second prototype I made was made for DS1 mode (torque). It is all explained in my first post here I think. No ffb was planned for that one, only adjustable resistance from the GPBikes plugin depending on a number of factors.

After seeing Marcel's video (and his advice) I switched to ffb and DS2.  Now I just use the load cells for brakes.

With a better implementation of DS1 in the game I think prototype 2 would have worked fine though. :)
I had to try..

Chris_Beeves

Found out that I have access to Visual Studio Enterprise, so today I started looking at writing the example .dlo for my knee rumblers.

The function I want from the .dlo is receiving the lean angle and sending it to an Arduino via serial port.
Receiving the lean angle seems simple enough (just copy the example code), but everything with the serial port handling makes it a bit complicated.
Especially since I have never seen Visual Studio until today  ;D

I have no idea what I am doing so far, but with enough trials and errors I guess I will get there in time..
This is what I have come up with so far. In my naive mind I just need to figure out the magic word that makes the .dlo spit out telemetry data through the (hopefully) opened serial port:


#include<windows.h>
#include <stdio.h>

/*
If compiled as C++, extern "C" must be added to declaration of functions to export

X+ is right, Y+ is top and Z+ is forward.
*/


extern "C" __declspec(dllexport) char* GetModID()
{
return "gpbikes";
}

extern "C" __declspec(dllexport) int GetModDataVersion()
{
return 8;
}

extern "C" __declspec(dllexport) int GetInterfaceVersion()
{
return 9;
}

/******************************************************************************
structures and functions to receive data from the simulated bike
******************************************************************************/
extern "C" typedef struct
{
int m_iRPM; /* engine rpm */
float m_fEngineTemperature; /* degrees Celsius */
float m_fWaterTemperature; /* degrees Celsius */
int m_iGear; /* 0 = Neutral */
float m_fFuel; /* liters */
float m_fSpeedometer; /* meters/second */
float m_fPosX, m_fPosY, m_fPosZ; /* world position of a reference point attached to chassis ( not CG ) */
float m_fVelocityX, m_fVelocityY, m_fVelocityZ; /* velocity of CG in world coordinates. meters/second */
float m_fAccelerationX, m_fAccelerationY, m_fAccelerationZ; /* acceleration of CG local to chassis rotation, expressed in G ( 9.81 m/s2 ) and averaged over the latest 10ms */
float m_aafRot[3][3]; /* rotation matrix of the chassis. It incorporates lean and wheeling */
float m_fYaw, m_fPitch, m_fRoll; /* degrees, -180 to 180 */
float m_fYawVelocity, m_fPitchVelocity, m_fRollVelocity; /* degress / second */
float m_afSuspLength[2]; /* shocks length. meters. 0 = front; 1 = rear. */
float m_afSuspVelocity[2]; /* shocks velocity. meters/second. 0 = front; 1 = rear */
int m_iCrashed; /* 1 = rider is detached from bike */
float m_fSteer; /* degrees. Negative = right  */
float m_fThrottle; /* 0 to 1 */
float m_fFrontBrake; /* 0 to 1 */
float m_fRearBrake; /* 0 to 1 */
float m_fClutch; /* 0 to 1. 0 = Fully engaged */
float m_afWheelSpeed[2]; /* meters/second. 0 = front; 1 = rear */
int m_aiWheelMaterial[2]; /* material index. 0 = not in contact */
float m_fSteerTorque; /* Nm */
int m_iPitLimiter; /* 1 = pit limiter is activated */
int m_iECUMode; /* 0 = engine mapping; 1 = traction control; 2 = engine braking */
char m_szEngineMapping[3];
int m_iTractionControl;
int m_iEngineBraking;
int m_iAntiWheeling;
int m_iECUState; /* bit field. Bit 1 = TC active; bit 2 = engine braking active; bit 3 = antiwheeling active */
} SPluginsBikeData_t;

/* called when software is started */

extern "C" __declspec(dllexport) int Startup(char* _szSavePath)
{
/*
return value is requested rate
0 = 100hz; 1 = 50hz; 2 = 20hz; 3 = 10hz; -1 = disable
*/
return 3;

int main()
{
HANDLE hComm;
hComm = CreateFile("\\\\.\\COM24", //port name
GENERIC_READ | GENERIC_WRITE, //Read/Write   
0, // No Sharing                               
NULL, // No Security                             
OPEN_EXISTING, // Open existing port only                     
0, // Non Overlapped I/O                           
NULL); // Null for Comm Devices

if (hComm == INVALID_HANDLE_VALUE)
printf("Error in opening Serial port");
else
printf("\nopening serial port successful\n");

DCB dcbSerialParams = { 0 }; // Initializing DCB structure
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

Status = GetCommState(hComm, &dcbSerialParams);

dcbSerialParams.BaudRate = CBR_9600; // Setting BaudRate = 9600
dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT; // Setting StopBits = 1
dcbSerialParams.Parity = NOPARITY; // Setting Parity = None

SetCommState(hComm, &dcbSerialParams);

COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 50; // in milliseconds
timeouts.ReadTotalTimeoutConstant = 50; // in milliseconds
timeouts.ReadTotalTimeoutMultiplier = 10; // in milliseconds
timeouts.WriteTotalTimeoutConstant = 50; // in milliseconds
timeouts.WriteTotalTimeoutMultiplier = 10; // in milliseconds
}
}

/* called when software is closed */
extern "C" __declspec(dllexport) void Shutdown()
{
int main()
{
HANDLE hComm;

CloseHandle(hComm); //Closing the Serial Port
return 0;
}
}


Any input on this would be very much appreciated!

Until next time..  :o
I had to try..

HornetMaX

Try to look at existing libraries for low-level stuff like handling of serial ports, it's always easier.
For a past project, I used this: https://www.teuniz.net/RS-232/

Chris_Beeves

Great tip! Thanks!

Looks a fair bit simpler than the standard windows stuff.
I had to try..

Chris_Beeves

Still waiting for the motor driver board to make the first prototype.. Somebody is probably swimming the package over to Sweden from the UK.
I have a holiday coming up in two weeks so hopefully I'll get some time to get it running.
Can't wait to have a working rig so I can play again soon!
I had to try..