From 557269c9f5d0908bedb18cd6eebb723352bc0f1b Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Thu, 30 May 2024 20:39:47 +0000 Subject: [PATCH] Implement tyre locking. Cleanup test gameloop --- src/monocoque/devices/sounddevice.c | 44 ++++++++++++++++++++++ src/monocoque/devices/usbhapticdevice.c | 50 +++++++++++++++++++++++-- src/monocoque/gameloop/gameloop.c | 32 +++++++++------- src/monocoque/helper/confighelper.c | 2 +- src/monocoque/helper/confighelper.h | 2 +- 5 files changed, 110 insertions(+), 20 deletions(-) diff --git a/src/monocoque/devices/sounddevice.c b/src/monocoque/devices/sounddevice.c index 9a8009c..04e5dea 100644 --- a/src/monocoque/devices/sounddevice.c +++ b/src/monocoque/devices/sounddevice.c @@ -71,6 +71,44 @@ int sounddev_tyreslip_update(SimDevice* this, SimData* simdata) } } +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) + { + play += simdata->wheelspeed[0]; + } + if (this->tyre == FRONTRIGHT || this->tyre == FRONTS || this->tyre == ALLFOUR) + { + play += simdata->wheelspeed[1]; + } + if (this->tyre == REARLEFT || this->tyre == REARS || this->tyre == ALLFOUR) + { + play += simdata->wheelspeed[2]; + } + if (this->tyre == REARRIGHT || this->tyre == REARS || this->tyre == ALLFOUR) + { + play += simdata->wheelspeed[3]; + } + } + + if (play > 0) + { + sounddevice->sounddata.curr_frequency = 0; + sounddevice->sounddata.curr_duration = 0; + + } + else + { + sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency * play; + sounddevice->sounddata.curr_duration = sounddevice->sounddata.duration; + } +} + int sounddev_absbrakes_update(SimDevice* this, SimData* simdata) { SoundDevice* sounddevice = (void *) this->derived; @@ -165,6 +203,7 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int static const vtable engine_sound_simdevice_vtable = { &sounddev_engine_update, &sounddev_free }; static const vtable gear_sound_simdevice_vtable = { &sounddev_gearshift_update, &sounddev_free }; static const vtable tyreslip_sound_simdevice_vtable = { &sounddev_tyreslip_update, &sounddev_free }; +static const vtable tyrelock_sound_simdevice_vtable = { &sounddev_tyrelock_update, &sounddev_free }; static const vtable absbrakes_sound_simdevice_vtable = { &sounddev_absbrakes_update, &sounddev_free }; SoundDevice* new_sound_device(DeviceSettings* ds) { @@ -192,6 +231,11 @@ SoundDevice* new_sound_device(DeviceSettings* ds) { this->m.vtable = &tyreslip_sound_simdevice_vtable; slogi("Initializing sound device for tyre slip vibrations."); break; + case (EFFECT_TYRELOCK): + this->effecttype = EFFECT_TYRELOCK; + this->m.vtable = &tyrelock_sound_simdevice_vtable; + slogi("Initializing sound device for tyre slip vibrations."); + break; case (EFFECT_ABSBRAKES): this->effecttype = EFFECT_ABSBRAKES; this->m.vtable = &absbrakes_sound_simdevice_vtable; diff --git a/src/monocoque/devices/usbhapticdevice.c b/src/monocoque/devices/usbhapticdevice.c index c5ae3d1..62845dd 100644 --- a/src/monocoque/devices/usbhapticdevice.c +++ b/src/monocoque/devices/usbhapticdevice.c @@ -11,10 +11,11 @@ int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdata) { double play = 0; + double playthreshhold = 0; switch (usbhapticdevice->effecttype) { case (EFFECT_TYRESLIP): - + playthreshhold = .4; if (usbhapticdevice->effecttype == EFFECT_TYRESLIP) { if (usbhapticdevice->tyre == FRONTLEFT || usbhapticdevice->tyre == FRONTS || usbhapticdevice->tyre == ALLFOUR) @@ -35,6 +36,47 @@ int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdat } } break; + case (EFFECT_TYRELOCK): + + if (simdata->velocity > 0) + { + if (usbhapticdevice->tyre == FRONTLEFT || usbhapticdevice->tyre == FRONTS || usbhapticdevice->tyre == ALLFOUR) + { + play += simdata->wheelspeed[0]; + } + if (usbhapticdevice->tyre == FRONTRIGHT || usbhapticdevice->tyre == FRONTS || usbhapticdevice->tyre == ALLFOUR) + { + play += simdata->wheelspeed[1]; + } + if (usbhapticdevice->tyre == REARLEFT || usbhapticdevice->tyre == REARS || usbhapticdevice->tyre == ALLFOUR) + { + play += simdata->wheelspeed[2]; + } + if (usbhapticdevice->tyre == REARRIGHT || usbhapticdevice->tyre == REARS || usbhapticdevice->tyre == ALLFOUR) + { + play += simdata->wheelspeed[3]; + } + } + if (usbhapticdevice->effecttype == EFFECT_TYRELOCK) + { + if (usbhapticdevice->tyre == FRONTLEFT || usbhapticdevice->tyre == FRONTS || usbhapticdevice->tyre == ALLFOUR) + { + play += (1.0 - simdata->wheelslip[0]); + } + if (usbhapticdevice->tyre == FRONTRIGHT || usbhapticdevice->tyre == FRONTS || usbhapticdevice->tyre == ALLFOUR) + { + play += (1.0 - simdata->wheelslip[1]); + } + if (usbhapticdevice->tyre == REARLEFT || usbhapticdevice->tyre == REARS || usbhapticdevice->tyre == ALLFOUR) + { + play += (1.0 - simdata->wheelslip[2]); + } + if (usbhapticdevice->tyre == REARRIGHT || usbhapticdevice->tyre == REARS || usbhapticdevice->tyre == ALLFOUR) + { + play += (1.0 - simdata->wheelslip[3]); + } + } + break; case (EFFECT_ABSBRAKES): play += simdata->abs; break; @@ -42,7 +84,7 @@ int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdat if (play != usbhapticdevice->state) { - if(play > .40) + if(play > playthreshhold) { fprintf(usbhapticdevice->handle, "%i\n", usbhapticdevice->value1); fflush(usbhapticdevice->handle); @@ -90,9 +132,9 @@ int usbhapticdev_init(USBGenericHapticDevice* usbhapticdevice, DeviceSettings* d { usbhapticdevice->effecttype = EFFECT_TYRESLIP; } - if(ds->effect_type == EFFECT_WHEELLOCK) + if(ds->effect_type == EFFECT_TYRELOCK) { - usbhapticdevice->effecttype = EFFECT_WHEELLOCK; + usbhapticdevice->effecttype = EFFECT_TYRELOCK; } return error; diff --git a/src/monocoque/gameloop/gameloop.c b/src/monocoque/gameloop/gameloop.c index 2a068db..50c6d5d 100644 --- a/src/monocoque/gameloop/gameloop.c +++ b/src/monocoque/gameloop/gameloop.c @@ -328,6 +328,10 @@ int tester(SimDevice* devices, int numdevices) simdata->wheelslip[1] = 1; simdata->wheelslip[2] = 1; simdata->wheelslip[3] = 1; + simdata->wheelspeed[0] = 100; + simdata->wheelspeed[1] = 100; + simdata->wheelspeed[2] = 100; + simdata->wheelspeed[3] = 100; sleep(3); fprintf(stdout, "Setting rpms to 1000\n"); @@ -368,10 +372,6 @@ int tester(SimDevice* devices, int numdevices) { devices[x].update(&devices[x], simdata); } - for (int x = 0; x < numdevices; x++) - { - devices[x].update(&devices[x], simdata); - } sleep(3); fprintf(stdout, "Setting speed to 100\n"); @@ -388,10 +388,6 @@ int tester(SimDevice* devices, int numdevices) { devices[x].update(&devices[x], simdata); } - for (int x = 0; x < numdevices; x++) - { - devices[x].update(&devices[x], simdata); - } sleep(3); fprintf(stdout, "Setting speed to 200\n"); @@ -402,12 +398,24 @@ int tester(SimDevice* devices, int numdevices) } sleep(3); - fprintf(stdout, "Shifting into third gear\n"); - simdata->gear = 4; + fprintf(stdout, "Setting Wheelspeed to 0, wheels locked at speed.\n"); + simdata->wheelspeed[0] = 0; + simdata->wheelspeed[1] = 0; + simdata->wheelspeed[2] = 0; + simdata->wheelspeed[3] = 0; for (int x = 0; x < numdevices; x++) { devices[x].update(&devices[x], simdata); } + sleep(3); + simdata->wheelspeed[0] = 100; + simdata->wheelspeed[1] = 100; + simdata->wheelspeed[2] = 100; + simdata->wheelspeed[3] = 100; + + + fprintf(stdout, "Shifting into third gear\n"); + simdata->gear = 4; for (int x = 0; x < numdevices; x++) { devices[x].update(&devices[x], simdata); @@ -436,10 +444,6 @@ int tester(SimDevice* devices, int numdevices) { devices[x].update(&devices[x], simdata); } - for (int x = 0; x < numdevices; x++) - { - devices[x].update(&devices[x], simdata); - } sleep(3); fprintf(stdout, "Setting speed to 300\n"); diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index ef99480..1235302 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -353,7 +353,7 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co const char* effect; config_setting_lookup_string(device_settings, "effect", &effect); strtoeffecttype(effect, ds); - if (ds->effect_type == EFFECT_TYRESLIP || ds->effect_type == EFFECT_WHEELLOCK) + if (ds->effect_type == EFFECT_TYRESLIP || ds->effect_type == EFFECT_TYRELOCK) { gettyre(device_settings, ds); } diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index 2d8f5b4..0cf85da 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -46,7 +46,7 @@ typedef enum EFFECT_GEARSHIFT = 1, EFFECT_ABSBRAKES = 2, EFFECT_TYRESLIP = 3, - EFFECT_WHEELLOCK = 4 + EFFECT_TYRELOCK = 4 } VibrationEffectType;