YukMingLaw / ArduinoJoystickWithFFBLibrary

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

stearing wheel with ffb, asseto corsa CPU 99%, slow respond in game #55

Closed fikririzal closed 1 year ago

fikririzal commented 1 year ago

Hello friend,

I have a project to make a steering wheel, when I first started this project I only used the rotary encoder IP2770 and 6 buttons, at that time the game Asseto Corsa was running very smoothly and I often played SRP.

when I try to use FFB the game feels very unresponsive and can't be played, when I return to the previous code (FFB off), it runs very smoothly.

please see and review my code, does it still have a delay()? or something else.

thanks very much.

`#include

include "Joystick.h"

define encoder0PinA 2

define encoder0PinB 3

volatile int encoder0Pos = 0; volatile int copyOfEncoder = 0; volatile int latestEncoderPos = 0;

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

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

//BTS7960

define EN 12

define R_PWM 11

define L_PWM 10

void doEncoderA() { // look for a low-to-high on channel A if (digitalRead(encoder0PinA) == HIGH) {

// check channel B to see which way encoder is turning
if (digitalRead(encoder0PinB) == LOW) {
  encoder0Pos = encoder0Pos - 1;         // CW
}
else {
  encoder0Pos = encoder0Pos + 1;         // CCW
}

}

else // must be a high-to-low edge on channel A { // check channel B to see which way encoder is turning if (digitalRead(encoder0PinB) == HIGH) { encoder0Pos = encoder0Pos - 1; // CW } else { encoder0Pos = encoder0Pos + 1; // CCW } } //Serial.println (encoder0Pos, DEC); // use for debugging - remember to comment out }

void doEncoderB() { // look for a low-to-high on channel B if (digitalRead(encoder0PinB) == HIGH) {

// check channel A to see which way encoder is turning
if (digitalRead(encoder0PinA) == HIGH) {
  encoder0Pos = encoder0Pos - 1;         // CW
}
else {
  encoder0Pos = encoder0Pos + 1;         // CCW
}

}

// Look for a high-to-low on channel B

else { // check channel B to see which way encoder is turning if (digitalRead(encoder0PinA) == LOW) { encoder0Pos = encoder0Pos - 1; // CW } else { encoder0Pos = encoder0Pos + 1; // CCW } } }

void setup() { for (byte i = 4; i <= 9 ; i++) { pinMode(i, INPUT_PULLUP); }

for (byte i = 10; i <= 12 ; i++) { pinMode(i, OUTPUT); }

setPWMPrescaler(R_PWM, 8); setPWMPrescaler(L_PWM, 8);

pinMode(encoder0PinA, INPUT); pinMode(encoder0PinB, INPUT);

// encoder pin on interrupt 0 (pin 2) attachInterrupt(0, doEncoderA, RISING);

// encoder pin on interrupt 1 (pin 3) attachInterrupt(1, doEncoderB, RISING);

Joystick.setXAxisRange(-6450, 6450); Joystick.setYAxisRange(0, 0); Joystick.setZAxisRange(0, 1000); Joystick.setRxAxisRange(0, 1000);

//set X Axis gains mygains[0].totalGain = 20;//0-100 mygains[0].springGain = 30;//0-100 mygains[1].totalGain = 0;//0-100 mygains[1].springGain = 0;//0-100 //enable gains REQUIRED Joystick.setGains(mygains); Joystick.begin(); }

void loop() { //read Encoder noInterrupts(); if (!digitalRead(4) == 1){ encoder0Pos = 0; } latestEncoderPos = encoder0Pos; interrupts();

if (latestEncoderPos != copyOfEncoder) { copyOfEncoder = latestEncoderPos; }

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

//set X Axis Spring Effect Param myeffectparams[0].springMaxPosition = 6450; myeffectparams[0].springPosition = copyOfEncoder; myeffectparams[1].springMaxPosition = 6450; myeffectparams[1].springPosition = 0;

//Recv HID-PID data from PC and caculate forces Joystick.setEffectParams(myeffectparams); Joystick.getForce(forces); if (forces[0] < 0) { digitalWrite(EN, HIGH); analogWrite(R_PWM, abs(forces[0])); analogWrite(L_PWM, 0); } else { digitalWrite(EN, HIGH); analogWrite(R_PWM, 0); analogWrite(L_PWM, abs(forces[0])); }

if (!digitalRead(8) == 1 && !digitalRead(5) == 1) { if (!digitalRead(9) == 1) { Joystick.setZAxis(1000); } else { Joystick.setZAxis(0); }

if (!digitalRead(6) == 1) {
  Joystick.setRxAxis(1000);
} else {
  Joystick.setRxAxis(0);
}

} else { if (!digitalRead(9) == 1) { Joystick.setZAxis(1000); } else { Joystick.setZAxis(0); }

if (!digitalRead(6) == 1) {
  Joystick.setRxAxis(1000);
} else {
  Joystick.setRxAxis(0);
}

}

Joystick.setButton(0, !digitalRead(4)); Joystick.setButton(1, !digitalRead(5)); Joystick.setButton(3, !digitalRead(7)); Joystick.setButton(4, !digitalRead(8)); }`

YukMingLaw commented 6 months ago

Hi, the bug fixed in v1.0.0, you can try it out.