Refactor effects into separate module
This commit is contained in:
parent
1bfe8c94ac
commit
980860eb60
|
|
@ -11,6 +11,8 @@ set(devices_source_files
|
|||
tachdevice.c
|
||||
usbhapticdevice.h
|
||||
usbhapticdevice.c
|
||||
hapticeffect.h
|
||||
hapticeffect.c
|
||||
sound.h
|
||||
sound.c
|
||||
usb/revburner.h
|
||||
|
|
|
|||
|
|
@ -0,0 +1,160 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "usbhapticdevice.h"
|
||||
#include "../../helper/confighelper.h"
|
||||
#include "../../simulatorapi/simapi/simapi/simdata.h"
|
||||
#include "../../slog/slog.h"
|
||||
|
||||
#define kmhtoms 0.277778
|
||||
#define minspeedinms 0.5
|
||||
#define minvelocity 50
|
||||
#define maxbrake 0
|
||||
#define maxthrottle 0
|
||||
#define maxXvelocity 0.001
|
||||
|
||||
bool hasTyreDiameter(SimData* simdata)
|
||||
{
|
||||
if (simdata->tyrediameter[0] == -1 || simdata->tyrediameter[1] == -1 || simdata->tyrediameter[2] == -1 || simdata->tyrediameter[3] == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void getTyreDiameter(SimData* simdata)
|
||||
{
|
||||
if(simdata->velocity > minvelocity && simdata->brake <= maxbrake && simdata->gas <= maxthrottle)
|
||||
{
|
||||
if (simdata->velocityX/simdata->velocity < maxXvelocity)
|
||||
{
|
||||
double Speedms = kmhtoms * simdata->velocity;
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
simdata->tyrediameter[i] = Speedms / simdata->tyreRPS[i] * 2;
|
||||
}
|
||||
slogi("Successfully set tyre diameters for wheel slip effects.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int slipeffect(SimData* simdata, int effecttype, int tyre, double threshhold)
|
||||
{
|
||||
int play = 0;
|
||||
double wheelslip[4];
|
||||
wheelslip[0] = 0;
|
||||
wheelslip[1] = 0;
|
||||
wheelslip[2] = 0;
|
||||
wheelslip[3] = 0;
|
||||
|
||||
|
||||
switch (effecttype)
|
||||
{
|
||||
case (EFFECT_TYRESLIP):
|
||||
case (EFFECT_TYRELOCK):
|
||||
case (EFFECT_ABSBRAKES):
|
||||
|
||||
if(hasTyreDiameter(simdata)==false)
|
||||
{
|
||||
getTyreDiameter(simdata);
|
||||
}
|
||||
if(hasTyreDiameter(simdata)==true)
|
||||
{
|
||||
double Speedms = kmhtoms * simdata->velocity;
|
||||
if (Speedms > minspeedinms)
|
||||
{
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
wheelslip[i] = (Speedms - simdata->tyrediameter[i] * simdata->tyreRPS[i] / 2) / Speedms;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
wheelslip[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
switch (effecttype)
|
||||
{
|
||||
case (EFFECT_TYRESLIP):
|
||||
|
||||
|
||||
if (tyre == FRONTLEFT || tyre == FRONTS || tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[0] < -.5)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (tyre == FRONTRIGHT || tyre == FRONTS || tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[1] < -.5)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (tyre == REARLEFT || tyre == REARS || tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[2] < -.5)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (tyre == REARRIGHT || tyre == REARS || tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[3] < -.5)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
case (EFFECT_TYRELOCK):
|
||||
if (tyre == FRONTLEFT || tyre == FRONTS || tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[0] > .75)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (tyre == FRONTRIGHT || tyre == FRONTS || tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[1] > .75)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (tyre == REARLEFT || tyre == REARS || tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[2] > .75)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (tyre == REARRIGHT || tyre == REARS || tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[3] > .75)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case (EFFECT_ABSBRAKES):
|
||||
break;
|
||||
}
|
||||
|
||||
return play;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef _HAPTICEFFECT_H
|
||||
#define _HAPTICEFFECT_H
|
||||
|
||||
#include "../simulatorapi/simapi/simapi/simdata.h"
|
||||
|
||||
int slipeffect(SimData* simdata, int effecttype, int tyre, double threshhold);
|
||||
|
||||
#endif
|
||||
|
|
@ -8,11 +8,13 @@
|
|||
#include "sound.h"
|
||||
#include "simdevice.h"
|
||||
#include "sounddevice.h"
|
||||
#include "hapticeffect.h"
|
||||
#include "sound/usb_generic_shaker.h"
|
||||
#include "../simulatorapi/simapi/simapi/simdata.h"
|
||||
#include "../helper/parameters.h"
|
||||
#include "../slog/slog.h"
|
||||
|
||||
#define slipthreshold 0.75
|
||||
|
||||
int gear_sound_set(SoundDevice* sounddevice, SimData* simdata)
|
||||
{
|
||||
|
|
@ -42,24 +44,9 @@ int sounddev_tyreslip_update(SimDevice* this, SimData* simdata)
|
|||
{
|
||||
SoundDevice* sounddevice = (void *) this->derived;
|
||||
|
||||
double play = 0;
|
||||
//if (this->tyre == FRONTLEFT || this->tyre == FRONTS || this->tyre == ALLFOUR)
|
||||
//{
|
||||
// play += (1.0 - simdata->wheelslip[0]);
|
||||
//}
|
||||
//if (this->tyre == FRONTRIGHT || this->tyre == FRONTS || this->tyre == ALLFOUR)
|
||||
//{
|
||||
// play += (1.0 - simdata->wheelslip[1]);
|
||||
//}
|
||||
//if (this->tyre == REARLEFT || this->tyre == REARS || this->tyre == ALLFOUR)
|
||||
//{
|
||||
// play += (1.0 - simdata->wheelslip[2]);
|
||||
//}
|
||||
//if (this->tyre == REARRIGHT || this->tyre == REARS || this->tyre == ALLFOUR)
|
||||
//{
|
||||
// play += (1.0 - simdata->wheelslip[3]);
|
||||
//}
|
||||
if (play > .40)
|
||||
int play = slipeffect(simdata, sounddevice->effecttype, this->tyre, slipthreshold);
|
||||
|
||||
if (play > 0)
|
||||
{
|
||||
sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency * play;
|
||||
sounddevice->sounddata.curr_duration = sounddevice->sounddata.duration;
|
||||
|
|
@ -75,40 +62,7 @@ int sounddev_tyrelock_update(SimDevice* this, SimData* simdata)
|
|||
{
|
||||
SoundDevice* sounddevice = (void *) this->derived;
|
||||
|
||||
double play = 0;
|
||||
//if (simdata->velocity > 0)
|
||||
//{
|
||||
// if (this->tyre == FRONTLEFT || this->tyre == FRONTS || this->tyre == ALLFOUR)
|
||||
// {
|
||||
// if(simdata->wheelspeed[0])
|
||||
// {
|
||||
// play++;
|
||||
// }
|
||||
// }
|
||||
// if (this->tyre == FRONTRIGHT || this->tyre == FRONTS || this->tyre == ALLFOUR)
|
||||
// {
|
||||
// if(simdata->wheelspeed[1])
|
||||
// {
|
||||
// play++;
|
||||
// }
|
||||
// play += simdata->wheelspeed[1];
|
||||
// }
|
||||
// if (this->tyre == REARLEFT || this->tyre == REARS || this->tyre == ALLFOUR)
|
||||
// {
|
||||
// if(simdata->wheelspeed[2])
|
||||
// {
|
||||
// play++;
|
||||
// }
|
||||
// play += simdata->wheelspeed[2];
|
||||
// }
|
||||
// if (this->tyre == REARRIGHT || this->tyre == REARS || this->tyre == ALLFOUR)
|
||||
// {
|
||||
// if(simdata->wheelspeed[3])
|
||||
// {
|
||||
// play++;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
int play = slipeffect(simdata, sounddevice->effecttype, this->tyre, slipthreshold);
|
||||
|
||||
if (play > 0)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,154 +4,22 @@
|
|||
#include <fcntl.h>
|
||||
|
||||
#include "usbhapticdevice.h"
|
||||
#include "hapticeffect.h"
|
||||
#include "../../helper/confighelper.h"
|
||||
#include "../../simulatorapi/simapi/simapi/simdata.h"
|
||||
#include "../../slog/slog.h"
|
||||
|
||||
bool hasTyreDiameter(SimData* simdata)
|
||||
{
|
||||
if (simdata->tyrediameter[0] == -1 || simdata->tyrediameter[1] == -1 || simdata->tyrediameter[2] == -1 || simdata->tyrediameter[3] == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void getTyreDiameter(SimData* simdata)
|
||||
{
|
||||
if(simdata->velocity > 50 && simdata->brake <= 0 && simdata->gas <= 0)
|
||||
{
|
||||
if (simdata->velocityX/simdata->velocity < 0.001)
|
||||
{
|
||||
double Speedms = 0.277778 * simdata->velocity;
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
simdata->tyrediameter[i] = Speedms / simdata->tyreRPS[i] * 2;
|
||||
}
|
||||
slogi("Successfully set tyre diameters for wheel slip effects.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#define slipthreshold 0.75
|
||||
|
||||
int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdata)
|
||||
{
|
||||
double play = 0;
|
||||
double playthreshhold = 0;
|
||||
double wheelslip[4];
|
||||
wheelslip[0] = 0;
|
||||
wheelslip[1] = 0;
|
||||
wheelslip[2] = 0;
|
||||
wheelslip[3] = 0;
|
||||
|
||||
int play = slipeffect(simdata, usbhapticdevice->effecttype, usbhapticdevice->tyre, slipthreshold);
|
||||
|
||||
switch (usbhapticdevice->effecttype)
|
||||
{
|
||||
case (EFFECT_TYRESLIP):
|
||||
case (EFFECT_TYRELOCK):
|
||||
case (EFFECT_ABSBRAKES):
|
||||
|
||||
if(hasTyreDiameter(simdata)==false)
|
||||
{
|
||||
getTyreDiameter(simdata);
|
||||
}
|
||||
if(hasTyreDiameter(simdata)==true)
|
||||
{
|
||||
double Speedms = 0.277778 * simdata->velocity;
|
||||
if (Speedms > 0.5)
|
||||
{
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
wheelslip[i] = (Speedms - simdata->tyrediameter[i] * simdata->tyreRPS[i] / 2) / Speedms;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
wheelslip[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
switch (usbhapticdevice->effecttype)
|
||||
{
|
||||
case (EFFECT_TYRESLIP):
|
||||
|
||||
|
||||
if (usbhapticdevice->tyre == FRONTLEFT || usbhapticdevice->tyre == FRONTS || usbhapticdevice->tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[0] < -.5)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (usbhapticdevice->tyre == FRONTRIGHT || usbhapticdevice->tyre == FRONTS || usbhapticdevice->tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[1] < -.5)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (usbhapticdevice->tyre == REARLEFT || usbhapticdevice->tyre == REARS || usbhapticdevice->tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[2] < -.5)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (usbhapticdevice->tyre == REARRIGHT || usbhapticdevice->tyre == REARS || usbhapticdevice->tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[3] < -.5)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
case (EFFECT_TYRELOCK):
|
||||
if (usbhapticdevice->tyre == FRONTLEFT || usbhapticdevice->tyre == FRONTS || usbhapticdevice->tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[0] > .75)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (usbhapticdevice->tyre == FRONTRIGHT || usbhapticdevice->tyre == FRONTS || usbhapticdevice->tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[1] > .75)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (usbhapticdevice->tyre == REARLEFT || usbhapticdevice->tyre == REARS || usbhapticdevice->tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[2] > .75)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
if (usbhapticdevice->tyre == REARRIGHT || usbhapticdevice->tyre == REARS || usbhapticdevice->tyre == ALLFOUR)
|
||||
{
|
||||
if(wheelslip[3] > .75)
|
||||
{
|
||||
play++;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case (EFFECT_ABSBRAKES):
|
||||
break;
|
||||
}
|
||||
|
||||
if (play != usbhapticdevice->state)
|
||||
{
|
||||
if(play > playthreshhold)
|
||||
if(play > 0)
|
||||
{
|
||||
fprintf(usbhapticdevice->handle, "%i\n", usbhapticdevice->value1);
|
||||
fflush(usbhapticdevice->handle);
|
||||
|
|
|
|||
Loading…
Reference in New Issue