YukMingLaw / ArduinoJoystickWithFFBLibrary

An Arduino Joystick Library With Force Feedback Feature
GNU Lesser General Public License v3.0
208 stars 55 forks source link

library dont work? #39

Open DimaMosur opened 2 years ago

DimaMosur commented 2 years ago

Hello everyone, I'm new to arduino and I'm trying to write my own steering wheel with FFB, so far it's not very good. I can write code without FFB but I want to have it I have everything I need to create it motor and h-bridges the code I try to write gives me a lot of errors that I don't know how to fix. I would be very grateful if there is somewhere a code with ffb on this library, because while I understand a little. Also my experience in programming is low, all I did was read the book "a byte of python" I also know layout, but this has nothing to do with my question

DimaMosur commented 2 years ago

addition

when I make my steering wheel on the original library, the code works for me and when I calibrate the device everything is correct, but when I do it on this library, the code is also compiled but does not react to HID-device at the same time, if I make a welcome through the "println" com port, you can see that the signals there are also they are working if you do it without ffb on the library with ffb the story is the same when using the original library everything works correctly I don’t know if it’s my problem or it’s the library just like I already mentioned I’m a beginner

code with ffb on this library but of course it doesn’t work, I don’t know why the device also doesn’t react on the PC

17.02 I made a working code with ffb on a multi-turn potentiometer, initially there was an idea to do it on an encoder, but I have a bad usual one for 20 cycles, which, as I understand it, is not very suitable for this task because it does not read data very correctly. The only thing to do is to put the right values in myeffectparams. Now my question is what are the effects with what values ​​you need to set and what specific gains that would be the maximum effects from the steering wheel with a minimum loss of fps. I used the Gyver Weel project as a code source: https://alexgyver.ru/gyverwheel/ . In the code, you can have a debug mode to see if the knobs and buttons work for you, as well as the calibration mode that you need to do it:

1.Set the potentiometers to the minimum position you need 2.Hold down the calibration button 3.Connect usb cable to arduino 4.Wait 5 seconds 5.Release the calibration button 6.Set the potentiometer to the maximum position you need 7.Click on the calibration button

`

include "Joystick.h"

include

//X-axis & Y-axis REQUIRED Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_JOYSTICK, 4, 0, true, true, true, //X,Y,Z true, true, true,//Rx,Ry,Rz false, false, false, false, false);

Gains mygains[2]; EffectParams myeffectparams[2]; int32_t forces[2] = {0};

define DEBUG 0 // режим отладки

define POT_THR A0 // педаль газа

define POT_BR A1 // педаль тормоза

define POT_CTH A2 // педаль сцепления

define POT_JOY_X A3 // джойстик Y

define POT_JOY_Y A4 // джойстик Y

define POT_WHEEL A5

define BUTT_BR 4 // кнопка ручника

define BUTT_CAL 3 // кнопка калибровки

int throttleMin, throttleMax, brakeMin, brakeMax, clutchMin, clutchMax, joystickXMin, joystickXMax, joystickYMin, joystickYMax, wheelMin, wheelMax;

void setup(){ Serial.begin(9600); pinMode(BUTT_BR, INPUT_PULLUP); pinMode(BUTT_CAL, INPUT_PULLUP);

debug();
calibration();

pinMode(9,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);

mygains[0].totalGain = 100;//0-100
mygains[0].springGain = 100;//0-100
mygains[0].constantGain      = 100;
mygains[0].rampGain          = 100;
mygains[0].squareGain        = 100;
mygains[0].sineGain          = 100;
mygains[0].triangleGain      = 100;
mygains[0].sawtoothdownGain  = 100;
mygains[0].sawtoothupGain    = 100;
mygains[0].damperGain        = 100;
mygains[0].inertiaGain       = 100;
mygains[0].frictionGain      = 100;
mygains[0].customGain        = 100;

EEPROM.get(0, throttleMin);
EEPROM.get(2, brakeMin);
EEPROM.get(4, clutchMin);
EEPROM.get(6, throttleMax);
EEPROM.get(8, brakeMax);
EEPROM.get(10, clutchMax);
EEPROM.get(14, joystickXMin);
EEPROM.get(16, joystickXMax);
EEPROM.get(18, joystickYMin);
EEPROM.get(20, joystickYMax);
EEPROM.get(12, wheelMin);
EEPROM.get(22, wheelMax);

Joystick.setGains(mygains);
Joystick.begin();

}

void loop(){ int wheel; wheel = map(analogRead(POT_WHEEL), wheelMin, wheelMax, 0, 1023); wheel = constrain(wheel, 0, 1023);

//set X Axis Spring Effect Param myeffectparams[0].springMaxPosition = 1023; myeffectparams[0].springPosition = wheel;//0-1023 myeffectparams[0].damperMaxVelocity = 1023; myeffectparams[0].damperVelocity = wheel; myeffectparams[0].inertiaMaxAcceleration = 1023; myeffectparams[0].inertiaAcceleration = wheel; myeffectparams[0].frictionMaxPositionChange = 1023; myeffectparams[0].frictionPositionChange = wheel; //Steering wheel //myeffectparams[0].springMaxPosition = 512; //myeffectparams[0].springPosition = wheel - 512; //-512-512

//Send HID data to PC Joystick.setXAxis(wheel);

int thr, br, cth, joyx, joyy; thr = map(analogRead(POT_THR), throttleMin, throttleMax, 0, 1023); thr = constrain(thr, 0, 1023); Joystick.setYAxis(thr);

br = map(analogRead(POT_BR), brakeMin, brakeMax, 0, 1023); br = constrain(br, 0, 1023); Joystick.setZAxis(br);

cth = map(analogRead(POT_CTH), clutchMin, clutchMax, 0, 1023); cth = constrain(cth, 0, 1023); Joystick.setRxAxis(cth);

joyx = map(analogRead(POT_JOY_X), joystickXMin, joystickXMax, 0, 1023); joyx = constrain(joyx, 0, 1023); Joystick.setRyAxis(joyx);

joyy = map(analogRead(POT_JOY_Y), joystickYMin, joystickYMax, 0, 1023); joyy = constrain(joyy, 0, 1023); Joystick.setRzAxis(joyy);

Joystick.setEffectParams(myeffectparams); Joystick.getForce(forces); Joystick.sendState();

if(forces[0] > 0){ digitalWrite(6,LOW); digitalWrite(7,HIGH); analogWrite(9,abs(forces[0])); }else{ digitalWrite(6,HIGH); digitalWrite(7,LOW); analogWrite(9,abs(forces[0])); } delay(1);

}

void calibration() { if (!digitalRead(BUTT_CAL)) { // нажата кнопка while (!digitalRead(BUTT_CAL)); // пока кнопка удерживается Serial.begin(9600); delay(100); Serial.print(F("Calibration start")); int zeroTHR = analogRead(POT_THR); int zeroBR = analogRead(POT_BR); int zeroCTH = analogRead(POT_CTH); int zeroWHEEL = analogRead(POT_WHEEL); int zeroJOY_X = analogRead(POT_JOY_X); int zeroJOY_Y = analogRead(POT_JOY_Y); int maxTHR, maxBR, maxCTH, maxWHEEL, maxJOY_X, maxJOY_Y;

EEPROM.put(0, zeroTHR);
EEPROM.put(2, zeroBR);
EEPROM.put(4, zeroCTH);
EEPROM.put(12, zeroWHEEL);
EEPROM.put(14, zeroJOY_X);
EEPROM.put(18, zeroJOY_Y);
delay(100);                     // дебаунс
while (true) {                  // крутимся
  if (!digitalRead(BUTT_CAL)) break;
  maxTHR = analogRead(POT_THR);
  maxBR = analogRead(POT_BR);
  maxCTH = analogRead(POT_CTH);
  maxWHEEL = analogRead(POT_WHEEL);
  maxJOY_X = analogRead(POT_JOY_X);
  maxJOY_Y = analogRead(POT_JOY_Y);
}
EEPROM.put(6, maxTHR);
EEPROM.put(8, maxBR);
EEPROM.put(10, maxCTH);
EEPROM.put(16, maxJOY_X);
EEPROM.put(20, maxJOY_Y);

EEPROM.put(22, maxWHEEL);

Serial.println(F("Calibration end"));
Serial.print(F("Wheek: "));
Serial.print(zeroWHEEL);
Serial.print(" - ");
Serial.println(maxWHEEL);
Serial.print(F("Throat: "));
Serial.print(zeroTHR);
Serial.print(" - ");
Serial.println(maxTHR);
Serial.print(F("Brake: "));
Serial.print(zeroBR);
Serial.print(" - ");
Serial.println(maxBR);
Serial.print(F("Clutch: "));
Serial.print(zeroCTH);
Serial.print(" - ");
Serial.println(maxCTH);
Serial.print(F("JoystickX: "));
Serial.print(zeroJOY_X);
Serial.print(" - ");
Serial.println(maxJOY_X);
Serial.print(F("JoystickY: "));
Serial.print(zeroJOY_Y);
Serial.print(" - ");
Serial.println(maxJOY_Y);
Serial.println();

} Serial.end(); delay(3000); // задержка чтобы кнопку отпустить }

void debug() {

if (DEBUG == 1)

Serial.begin(9600); uint32_t timer; while (true) { if (millis() - timer > 100) { timer = millis(); Serial.print(analogRead(POT_WHEEL)); Serial.print("\t"); Serial.print(analogRead(POT_THR)); Serial.print("\t"); Serial.print(analogRead(POT_BR)); Serial.print("\t"); Serial.print(analogRead(POT_CTH)); Serial.print("\t"); Serial.print(analogRead(POT_JOY_X)); Serial.print("\t"); Serial.print(analogRead(POT_JOY_Y)); Serial.print("\t"); Serial.print(!digitalRead(BUTT_BR)); Serial.print("\t"); Serial.println(!digitalRead(BUTT_CAL)); } } Serial.end();

endif

} `

YukMingLaw commented 2 years ago

which arduino hardware did you use for the project?

DimaMosur commented 2 years ago

arduino leonardo

YukMingLaw commented 2 years ago
- Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_JOYSTICK, 4, 0,
+Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_MULTI_AXIS, 4, 0,
true, true, true, //X,Y,Z
true, true, true,//Rx,Ry,Rz
false, false, false, false, false);

try this code?

DimaMosur commented 2 years ago

maybe you didn’t really understand what I need, the steering wheel is already working for me, I highlighted the text that now I don’t understand what values to be set in effects, I only understand what needs to be set for the spring. And it seems that as far as I know, when using a multi-axis, the device is not displayed in the control panel

1781741527 commented 1 year ago

Hello, I would like to ask, have you ever encountered a situation where the force feedback done by this library causes the game to drop frames in the game? I'm also having a similar issue, I seem to have fixed the issue with force feedback acquisition, but it's causing my game to drop frames very badly! I feel like the device is constantly fetching game data from the game, causing the game to drop frames, but I don't know how to fix it yet, so I'm wondering if your project is complete? Can you give me some help?

DimaMosur commented 1 year ago

I also had slow fps in most games, unfortunately I abandoned this project at the moment