Fix dumb issues with haptic arduino code. Add tyre slip vibrations to test script.

This commit is contained in:
Paul Dino Jones 2024-07-09 17:14:40 -04:00
parent f413e749b9
commit 03b96e84b0
7 changed files with 87 additions and 39 deletions

View File

@ -7,8 +7,7 @@
typedef struct typedef struct
{ {
int motor; int motor;
float effect; int effect;
float power;
} }
SimHapticData; SimHapticData;

View File

@ -12,7 +12,8 @@ Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3);
//Adafruit_DCMotor *myMotor4 = AFMS.getMotor(4); //Adafruit_DCMotor *myMotor4 = AFMS.getMotor(4);
SimHapticData sd; SimHapticData sd;
int velocity = 0; int effect = 0;
int motor = 15;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
@ -20,7 +21,8 @@ void setup() {
Serial.println("Could not find Motor Shield. Check wiring."); Serial.println("Could not find Motor Shield. Check wiring.");
while (1); while (1);
} }
sd.velocity = 10; sd.effect = 0;
sd.motor = 15;
myMotor1->setSpeed(0); myMotor1->setSpeed(0);
myMotor1->run(FORWARD); myMotor1->run(FORWARD);
@ -42,43 +44,23 @@ void loop() {
{ {
Serial.readBytes(buff, BYTE_SIZE); Serial.readBytes(buff, BYTE_SIZE);
memcpy(&sd, &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) 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); // 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); // myMotor4->setSpeed(v*POWER);
//} //}

View File

@ -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) 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; return false;
} }
slogt("tyre diameter data found");
return true; return true;
} }
@ -292,12 +294,13 @@ double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold,
wheelslip[2] = 0; wheelslip[2] = 0;
wheelslip[3] = 0; wheelslip[3] = 0;
slogt("wheel vibration calculation");
switch (effecttype) switch (effecttype)
{ {
case (EFFECT_TYRESLIP): case (EFFECT_TYRESLIP):
case (EFFECT_TYRELOCK): case (EFFECT_TYRELOCK):
case (EFFECT_ABSBRAKES): case (EFFECT_ABSBRAKES):
if(hasTyreDiameter(simdata)==false) if(hasTyreDiameter(simdata)==false)
{ {
// check for saved tyre diameter in config file // 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) if(hasTyreDiameter(simdata)==true)
{ {
double Speedms = kmhtoms * simdata->velocity; double Speedms = kmhtoms * simdata->velocity;
slogt("attempting wheel slip calculation");
if (Speedms > minspeedinms) if (Speedms > minspeedinms)
{ {
for(int i = 0; i < 4; i++) for(int i = 0; i < 4; i++)
@ -339,6 +343,8 @@ double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold,
} }
break; break;
default:
slogt("Unknown effect type");
} }
switch (effecttype) switch (effecttype)
@ -444,7 +450,7 @@ double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold,
} }
break; break;
} }
return play; return play;
} }

View File

@ -16,6 +16,6 @@ typedef struct
} }
HapticEffect; 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 #endif

View File

@ -58,15 +58,25 @@ int arduino_simwind_update(SimDevice* this, SimData* simdata)
int arduino_simhaptic_update(SimDevice* this, SimData* simdata) int arduino_simhaptic_update(SimDevice* this, SimData* simdata)
{ {
SerialDevice* serialdevice = (void *) this->derived; SerialDevice* serialdevice = (void *) this->derived;
int result = 1; 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); 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"); if(play > 1.0)
serialdevice->u.simhapticdata.power = 0.6; {
serialdevice->u.simhapticdata.motor = serialdevice->motorsposition; 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; return result;
} }
@ -133,6 +143,7 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) {
{ {
this->m.hapticeffect.threshold = ds->threshold; this->m.hapticeffect.threshold = ds->threshold;
this->m.hapticeffect.effecttype = ds->effect_type; 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.tyre = ds->tyre;
this->m.hapticeffect.useconfig = ms->useconfig; this->m.hapticeffect.useconfig = ms->useconfig;
this->m.hapticeffect.configcheck = &ms->configcheck; this->m.hapticeffect.configcheck = &ms->configcheck;

View File

@ -339,6 +339,10 @@ int tester(SimDevice* devices, int numdevices)
simdata->tyrediameter[1] = -1; simdata->tyrediameter[1] = -1;
simdata->tyrediameter[2] = -1; simdata->tyrediameter[2] = -1;
simdata->tyrediameter[3] = -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); sleep(3);
fprintf(stdout, "Setting rpms to 1000\n"); fprintf(stdout, "Setting rpms to 1000\n");
@ -363,6 +367,22 @@ int tester(SimDevice* devices, int numdevices)
} }
sleep(3); 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"); fprintf(stdout, "Setting speed to 100\n");
simdata->velocity = 100; simdata->velocity = 100;
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
@ -374,6 +394,21 @@ int tester(SimDevice* devices, int numdevices)
} }
sleep(3); 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"); fprintf(stdout, "Shifting into second gear\n");
simdata->gear = 3; simdata->gear = 3;
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)

View File

@ -59,25 +59,38 @@ int strtoeffecttype(const char* effect, DeviceSettings* ds)
if (strcicmp(effect, "Engine") == 0) if (strcicmp(effect, "Engine") == 0)
{ {
ds->is_valid = true;
ds->effect_type = EFFECT_ENGINERPM; ds->effect_type = EFFECT_ENGINERPM;
} }
if (strcicmp(effect, "Gear") == 0) if (strcicmp(effect, "Gear") == 0)
{ {
ds->is_valid = true;
ds->effect_type = EFFECT_GEARSHIFT; ds->effect_type = EFFECT_GEARSHIFT;
} }
if (strcicmp(effect, "ABS") == 0) if (strcicmp(effect, "ABS") == 0)
{ {
ds->is_valid = true;
slogt("found abas effect set");
ds->effect_type = EFFECT_ABSBRAKES; ds->effect_type = EFFECT_ABSBRAKES;
} }
if ((strcicmp(effect, "SLIP") == 0) || (strcicmp(effect, "TYRESLIP") == 0) || (strcicmp(effect, "TIRESLIP") == 0)) 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; ds->effect_type = EFFECT_TYRESLIP;
} }
if ((strcicmp(effect, "LOCK") == 0) || (strcicmp(effect, "TYRELOCK") == 0) || (strcicmp(effect, "TIRELOCK") == 0)) 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; ds->effect_type = EFFECT_TYRELOCK;
} }
if (ds->is_valid == false)
{
slogw("effect %s is not a valid effect", effect);
}
ds->is_valid = true; ds->is_valid = true;
return MONOCOQUE_ERROR_NONE; 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) if (strcicmp(device_subtype, "SerialHaptic") == 0 || strcicmp(device_subtype, "Haptic") == 0)
{ {
slogt("found serial haptic device settings");
ds->dev_subtype = SIMDEVTYPE_SERIALHAPTIC; ds->dev_subtype = SIMDEVTYPE_SERIALHAPTIC;
break; break;
} }
@ -341,8 +355,9 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
// logic for different devices // 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; const char* effect;
config_setting_lookup_string(device_settings, "effect", &effect); config_setting_lookup_string(device_settings, "effect", &effect);
strtoeffecttype(effect, ds); strtoeffecttype(effect, ds);