From 03b96e84b0a6c24ebe130de037afdc9fc7de5fbd Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Tue, 9 Jul 2024 17:14:40 -0400 Subject: [PATCH] Fix dumb issues with haptic arduino code. Add tyre slip vibrations to test script. --- src/arduino/simhaptic/simhaptic.h | 3 +-- src/arduino/simhaptic/simhaptic.ino | 40 ++++++++-------------------- src/monocoque/devices/hapticeffect.c | 10 +++++-- src/monocoque/devices/hapticeffect.h | 2 +- src/monocoque/devices/serialdevice.c | 19 ++++++++++--- src/monocoque/gameloop/gameloop.c | 35 ++++++++++++++++++++++++ src/monocoque/helper/confighelper.c | 17 +++++++++++- 7 files changed, 87 insertions(+), 39 deletions(-) diff --git a/src/arduino/simhaptic/simhaptic.h b/src/arduino/simhaptic/simhaptic.h index ebb9cfc..d5a393f 100755 --- a/src/arduino/simhaptic/simhaptic.h +++ b/src/arduino/simhaptic/simhaptic.h @@ -7,8 +7,7 @@ typedef struct { int motor; - float effect; - float power; + int effect; } SimHapticData; diff --git a/src/arduino/simhaptic/simhaptic.ino b/src/arduino/simhaptic/simhaptic.ino index f85824f..89ec2ea 100644 --- a/src/arduino/simhaptic/simhaptic.ino +++ b/src/arduino/simhaptic/simhaptic.ino @@ -12,7 +12,8 @@ Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3); //Adafruit_DCMotor *myMotor4 = AFMS.getMotor(4); SimHapticData sd; -int velocity = 0; +int effect = 0; +int motor = 15; void setup() { Serial.begin(9600); @@ -20,7 +21,8 @@ void setup() { Serial.println("Could not find Motor Shield. Check wiring."); while (1); } - sd.velocity = 10; + sd.effect = 0; + sd.motor = 15; myMotor1->setSpeed(0); myMotor1->run(FORWARD); @@ -42,43 +44,23 @@ void loop() { { Serial.readBytes(buff, BYTE_SIZE); memcpy(&sd, &buff, BYTE_SIZE); - velocity = sd.velocity; + effect = sd.effect; + motor = sd.motor; } - int v = ceil(effect * 255); - if (v >= 255) - { - v = 255; - } - - MOTOR_1 = 0, - MOTOR_2 = 1, - MOTOR_3 = 2, - MOTOR_4 = 3, - MOTOR_1_4 = 4, - MOTOR_2_4 = 5, - MOTOR_3_4 = 6, - MOTOR_1_2 = 7, - MOTOR_1_3 = 8, - MOTOR_2_3 = 9, - MOTOR_1_2_3_4 = 10, - MOTOR_1_2_3 = 11, - MOTOR_2_3_4 = 12, - MOTOR_1_2_4 = 13, - MOTOR_1_3_4 = 14 if (motor == 0 || motor == 4 || motor == 7 || motor == 8 || motor == 10 || motor == 11 || motor == 13 || motor == 14) { - myMotor1->setSpeed(v*POWER); + myMotor1->setSpeed(effect); } - //if (motor == 1 || motor == 5 || motor = 7 || motor == 9 || motor == 10 || motor == 11 || motor == 12 || motor = 13) + //if (motor == 1 || motor == 5 || motor == 7 || motor == 9 || motor == 10 || motor == 11 || motor == 12 || motor == 13) //{ // myMotor2->setSpeed(v*POWER); //} - if (motor == 2 || motor == 6 || motor = 8 || motor == 9 || motor == 10 || motor == 11 || motor == 12 || motor = 14) + if (motor == 2 || motor == 6 || motor == 8 || motor == 9 || motor == 10 || motor == 11 || motor == 12 || motor == 14) { - myMotor3->setSpeed(v*POWER); + myMotor3->setSpeed(effect); } - //if (motor == 3 || motor == 4 || motor = 5 || motor == 6 || motor == 10 || motor == 12 || motor == 13 || motor = 14) + //if (motor == 3 || motor == 4 || motor == 5 || motor == 6 || motor == 10 || motor == 12 || motor == 13 || motor == 14) //{ // myMotor4->setSpeed(v*POWER); //} diff --git a/src/monocoque/devices/hapticeffect.c b/src/monocoque/devices/hapticeffect.c index 810f87e..506fcf1 100644 --- a/src/monocoque/devices/hapticeffect.c +++ b/src/monocoque/devices/hapticeffect.c @@ -23,8 +23,10 @@ bool hasTyreDiameter(SimData* simdata) { if (simdata->tyrediameter[0] == -1 || simdata->tyrediameter[1] == -1 || simdata->tyrediameter[2] == -1 || simdata->tyrediameter[3] == -1) { + slogt("failed to find tyre diameter data"); return false; } + slogt("tyre diameter data found"); return true; } @@ -292,12 +294,13 @@ double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, wheelslip[2] = 0; wheelslip[3] = 0; + slogt("wheel vibration calculation"); + switch (effecttype) { case (EFFECT_TYRESLIP): case (EFFECT_TYRELOCK): case (EFFECT_ABSBRAKES): - if(hasTyreDiameter(simdata)==false) { // check for saved tyre diameter in config file @@ -322,6 +325,7 @@ double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, if(hasTyreDiameter(simdata)==true) { double Speedms = kmhtoms * simdata->velocity; + slogt("attempting wheel slip calculation"); if (Speedms > minspeedinms) { for(int i = 0; i < 4; i++) @@ -339,6 +343,8 @@ double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, } break; + default: + slogt("Unknown effect type"); } switch (effecttype) @@ -444,7 +450,7 @@ double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, } break; } - + return play; } diff --git a/src/monocoque/devices/hapticeffect.h b/src/monocoque/devices/hapticeffect.h index 5321502..f69e197 100644 --- a/src/monocoque/devices/hapticeffect.h +++ b/src/monocoque/devices/hapticeffect.h @@ -16,6 +16,6 @@ typedef struct } HapticEffect; -int slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, int useconfig, int* configcheck, char* configfile); +double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, int useconfig, int* configcheck, char* configfile); #endif diff --git a/src/monocoque/devices/serialdevice.c b/src/monocoque/devices/serialdevice.c index 5cd983e..7ecb0d3 100644 --- a/src/monocoque/devices/serialdevice.c +++ b/src/monocoque/devices/serialdevice.c @@ -58,15 +58,25 @@ int arduino_simwind_update(SimDevice* this, SimData* simdata) int arduino_simhaptic_update(SimDevice* this, SimData* simdata) { SerialDevice* serialdevice = (void *) this->derived; + int result = 1; + slogt("arduino haptic device updating"); + double play = slipeffect(simdata, this->hapticeffect.effecttype, this->hapticeffect.tyre, this->hapticeffect.threshold, this->hapticeffect.useconfig, this->hapticeffect.configcheck, this->hapticeffect.tyrediameterconfig); - slogt("Updating arduino haptic device"); - serialdevice->u.simhapticdata.power = 0.6; - serialdevice->u.simhapticdata.motor = serialdevice->motorsposition; + if(play > 1.0) + { + play = 1.0; + } - arduino_update(serialdevice, simdata, sizeof(SimData)); + int effectspeed = ceil( 255 * play ); + serialdevice->u.simhapticdata.motor = serialdevice->motorsposition; + serialdevice->u.simhapticdata.effect = effectspeed; + slogt("Updating arduino haptic device speed %i motor code %i", serialdevice->u.simhapticdata.effect, serialdevice->u.simhapticdata.motor); + size_t size = sizeof(SimHapticData); + + arduino_update(serialdevice, &serialdevice->u.simhapticdata, size); return result; } @@ -133,6 +143,7 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) { { this->m.hapticeffect.threshold = ds->threshold; this->m.hapticeffect.effecttype = ds->effect_type; + slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->effect_type); this->m.hapticeffect.tyre = ds->tyre; this->m.hapticeffect.useconfig = ms->useconfig; this->m.hapticeffect.configcheck = &ms->configcheck; diff --git a/src/monocoque/gameloop/gameloop.c b/src/monocoque/gameloop/gameloop.c index 2c841bb..21729db 100644 --- a/src/monocoque/gameloop/gameloop.c +++ b/src/monocoque/gameloop/gameloop.c @@ -339,6 +339,10 @@ int tester(SimDevice* devices, int numdevices) simdata->tyrediameter[1] = -1; simdata->tyrediameter[2] = -1; simdata->tyrediameter[3] = -1; + simdata->tyrediameter[0] = 0.638636385206394; + simdata->tyrediameter[1] = 0.633384434597093; + simdata->tyrediameter[2] = 0.710475735564615; + simdata->tyrediameter[3] = 0.710475735564615; sleep(3); fprintf(stdout, "Setting rpms to 1000\n"); @@ -363,6 +367,22 @@ int tester(SimDevice* devices, int numdevices) } sleep(3); + fprintf(stdout, "Testing wheel spin\n"); + simdata->velocity = 20; + simdata->tyreRPS[0] = 133; + simdata->tyreRPS[1] = 133; + simdata->tyreRPS[2] = 133; + simdata->tyreRPS[3] = 133; + simdata->velocity = 100; + for (int x = 0; x < numdevices; x++) + { + if (devices[x].initialized == true) + { + devices[x].update(&devices[x], simdata); + } + } + sleep(3); + fprintf(stdout, "Setting speed to 100\n"); simdata->velocity = 100; for (int x = 0; x < numdevices; x++) @@ -374,6 +394,21 @@ int tester(SimDevice* devices, int numdevices) } sleep(3); + //fprintf(stdout, "Testing wheel spin\n"); + //simdata->tyreRPS[0] = 8000; + //simdata->tyreRPS[1] = 8000; + //simdata->tyreRPS[2] = 8000; + //simdata->tyreRPS[3] = 8000; + //simdata->velocity = 100; + //for (int x = 0; x < numdevices; x++) + //{ + // if (devices[x].initialized == true) + // { + // devices[x].update(&devices[x], simdata); + // } + //} + //sleep(3); + fprintf(stdout, "Shifting into second gear\n"); simdata->gear = 3; for (int x = 0; x < numdevices; x++) diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index 241bb1f..b62bedd 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -59,25 +59,38 @@ int strtoeffecttype(const char* effect, DeviceSettings* ds) if (strcicmp(effect, "Engine") == 0) { + ds->is_valid = true; ds->effect_type = EFFECT_ENGINERPM; } if (strcicmp(effect, "Gear") == 0) { + ds->is_valid = true; ds->effect_type = EFFECT_GEARSHIFT; } if (strcicmp(effect, "ABS") == 0) { + ds->is_valid = true; + slogt("found abas effect set"); ds->effect_type = EFFECT_ABSBRAKES; } if ((strcicmp(effect, "SLIP") == 0) || (strcicmp(effect, "TYRESLIP") == 0) || (strcicmp(effect, "TIRESLIP") == 0)) { + ds->is_valid = true; + slogt("found tyreslip effect set"); ds->effect_type = EFFECT_TYRESLIP; } if ((strcicmp(effect, "LOCK") == 0) || (strcicmp(effect, "TYRELOCK") == 0) || (strcicmp(effect, "TIRELOCK") == 0)) { + ds->is_valid = true; + slogt("found tyreslock effect set"); ds->effect_type = EFFECT_TYRELOCK; } + if (ds->is_valid == false) + { + slogw("effect %s is not a valid effect", effect); + } + ds->is_valid = true; return MONOCOQUE_ERROR_NONE; } @@ -112,6 +125,7 @@ int strtodevsubtype(const char* device_subtype, DeviceSettings* ds, int simdev) } if (strcicmp(device_subtype, "SerialHaptic") == 0 || strcicmp(device_subtype, "Haptic") == 0) { + slogt("found serial haptic device settings"); ds->dev_subtype = SIMDEVTYPE_SERIALHAPTIC; break; } @@ -341,8 +355,9 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co // logic for different devices } - if (ds->dev_subtype == SIMDEVTYPE_USBHAPTIC || ds->dev_type == SIMDEV_SOUND || ds->dev_type == SIMDEVTYPE_SERIALHAPTIC) + if (ds->dev_subtype == SIMDEVTYPE_USBHAPTIC || ds->dev_type == SIMDEV_SOUND || ds->dev_subtype == SIMDEVTYPE_SERIALHAPTIC) { + slogt("analysing haptic effect settings"); const char* effect; config_setting_lookup_string(device_settings, "effect", &effect); strtoeffecttype(effect, ds);