Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values all over the place / Gear Detection Issue #6

Open
RetiredHippie opened this issue Apr 13, 2024 · 1 comment
Open

Values all over the place / Gear Detection Issue #6

RetiredHippie opened this issue Apr 13, 2024 · 1 comment

Comments

@RetiredHippie
Copy link

I just want to start by saying i apologize in advance.. This is gonna be a fun one.

Okay, so ive spent the last few hours trying to get things situated but i keep getting backed into a corner. A few things to know beforehand:
1- My potentiometers arent in the best shape and have been removed, cleaned, replaced, reinstalled, opened, and anything else you can conjure up. They DO however have a stable reading and are reliably replicating values.
2- My polarity may be backwards in the wiring to the pots, (I.E. Positive and Negative wires on either end of connection) however they are both the same so they should still work as long as values are correct.

I've gone through and recorded all values both on a graph and raw data. My issue lies where Sometimes the gearing will indicate I'm say stuck in 5th in N, 3d, 4th, and lighter ends of the side gearing, and will SOMETIMES tickle a trigger for 2nd or 6th/7th, never 1st it seems.
Other Times it will record 2nd, 4th, and 6th, but none of the others (Ive gotten it to do this once..).
I know its more than likely an issue in the values for the upper and lower axis (probably both x & y) but i cannot seem to get it set right for the life of me.
Also, i do not have the Reverse button hooked up currently, as the connector broke off the little pcb, but it still registers Reverse as always on. Once in a while the value goes to 0 but i have no control over it, as bridging the two wires together give no change in value.

Here's the values ive recorded from the arduino software:
Reverse: 1
1st: 923, 898
2nd: 700, 625
3rd: 898, 900
4th: 755, 600
5th: 946, 898
6th: 875, 624
Neutral: ~875, 740

@JRomainG
Copy link
Owner

JRomainG commented Apr 14, 2024

Hi, thanks for the interest in the project!

Indeed, the thresholds need to be changed to match your values, but there are 2 difficulties that I see:

  • The thresholds for even gears and odd gears don't seem to be the same, which is surprising. I can patch it in the code if needed though;
  • The x value for gear 1 is greater than the x value for gear 3, but less that the x value for gear 5, as if gears 1 and 3 were swapped. I could also patch the code so you can have arbitrary values for each gear, but it does look like a wiring issue.

Playing around a bit with your values (using the script here), here are the best thresholds I have found:

XAXIS_LEFT_THRESH = 725
XAXIS_RIGHT_THRESH = 800
YAXIS_UP_THRESH = 800
YAXIS_DOWN_THRESH = 700

It gives the following output:

[OK] Guessed gear Gear.Neutral for (875, 740) matched expected Gear.Neutral
[FAIL] Guessed gear Gear.Fifth for (923, 898) did NOT match expected Gear.First
[OK] Guessed gear Gear.Second for (700, 625) matched expected Gear.Second
[FAIL] Guessed gear Gear.Fifth for (898, 900) did NOT match expected Gear.Third
[OK] Guessed gear Gear.Fourth for (755, 600) matched expected Gear.Fourth
[OK] Guessed gear Gear.Fifth for (946, 898) matched expected Gear.Fifth
[OK] Guessed gear Gear.Sixth for (875, 624) matched expected Gear.Sixth

You can also try this script (not tested) which should be able to handle a threshold specific to each gear:

// Logitech Driving Force Shifter USB Adapter
// Inspired by projects from Armandoiglesias and Jason Duncan
// Copyright (C) 2020-2024  Jean-Romain Garnier, Constant Zion
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

#include "UnoJoy.h"

#define GEAR_1                   1
#define GEAR_2                   2
#define GEAR_3                   3
#define GEAR_4                   4
#define GEAR_5                   5
#define GEAR_6                   6
#define GEAR_REVERSE             7
#define GEAR_NEUTRAL             0

// Shifter analog axis thresholds
// Change these values if the gears aren't detected correctly
const unsigned int thresholds[][][] = {
  //min_x,   max_x,       min_y, max_y
  {{910,     930},       {800,   1000}},  // GEAR_1
  {{0,       725},       {0,     700}},   // GEAR_2
  {{0,       910},       {800,   1000}},  // GEAR_3
  {{725,     800},       {0,     700}},   // GEAR_4
  {{930,     1000},      {800,   1000}},  // GEAR_5
  {{800,     1000},      {0,     700}},   // GEAR_6
};
const int gears[] = {
  GEAR_1, GEAR_2, GEAR_3, GEAR_4, GEAR_5, GEAR_6,
};

// Whether switching to neutral should register as a button (0 or 1)
#define REGISTER_NEUTRAL         0

// Delay after each loop reading the input (in ms)
const int ControllerUpdateRate = 50;

void setup() {
  // Shifter analog input pins (make sure they are correctly plugged in Arduino)
  pinMode(A0, INPUT_PULLUP);   // X axis
  pinMode(A2, INPUT_PULLUP);   // Y axis
  pinMode(2, INPUT);           // Reverse button

  // Initialize Joystick Library
  setupUnoJoy();
}

void loop() {
  setControllerData(readController());
  delay(ControllerUpdateRate);
}

void matches_threshold(int x, int y, unsigned int threshold[][]) {
  return (
    x >= threshold[0][0]
    && x < threshold[0][1]
    && y >= threshold[1][0]
    && y < threshold[1][1]
  )
}

dataForController_t readController() {
  dataForController_t controllerData = getBlankDataForController();
  int x = analogRead(0);                // X axis
  int y = analogRead(2);                // Y axis
  int is_reverse = digitalRead(2);      // Reverse button

  // Find out which gear the user is now in
  int gear = GEAR_NEUTRAL;
  unsigned int nb_thresholds = sizeof(thresholds) / sizeof(thresholds[0]);
  for (int i = 0; i < nb_thresholds; i++) {
    if (matches_threshold(x, y, thresholds[i])) {
      gear = gears[i];
      break;
    }
  }

  // Handle reverse
  if (GEAR_6 == gear && is_reverse) {
    gear = GEAR_REVERSE;
  }

  // List of buttons used by the shifter
  // controllerData.squareOn      Button 1
  // controllerData.crossOn       Button 2
  // controllerData.circleOn      Button 3
  // controllerData.triangleOn    Button 4
  // controllerData.l1On          Button 5
  // controllerData.r1On          Button 6
  // controllerData.l2On          Button 7 (reverse)
  // controllerData.r2On          Button 8 (neutral)

  // Enable the right button based on gear
  switch (gear) {
    case GEAR_1: {
        controllerData.squareOn = 1;
        break;
      }
    case GEAR_2: {
        controllerData.crossOn = 1;
        break;
      }
    case GEAR_3: {
        controllerData.circleOn = 1;
        break;
      }
    case GEAR_4: {
        controllerData.triangleOn = 1;
        break;
      }
    case GEAR_5: {
        controllerData.l1On = 1;
        break;
      }
    case GEAR_6: {
        controllerData.r1On = 1;
        break;
      }
    case GEAR_REVERSE: {
        controllerData.l2On = 1;
        break;
      }
    case GEAR_NEUTRAL: {
        controllerData.r2On = REGISTER_NEUTRAL; 
        break;
      }
  }
  return controllerData;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants