Skip to content

Commit 078c5e0

Browse files
authored
Merge pull request #3 from dmadison/pedal-deadzone
Send pedal position on change
2 parents 0ecb9d6 + 098111c commit 078c5e0

3 files changed

Lines changed: 102 additions & 5 deletions

File tree

examples/Pedals/PedalsJoystick/PedalsJoystick.ino

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Joystick_ Joystick(
4949
false, false, false, false, false, false); // no other axes
5050

5151
const int ADC_Max = 1023; // max value of the analog inputs, 10-bit on AVR boards
52+
const bool AlwaysSend = false; // override the position checks, *always* send data constantly
5253

5354

5455
void setup() {
@@ -61,11 +62,19 @@ void setup() {
6162
Joystick.setZAxisRange(0, ADC_Max);
6263
Joystick.setRxAxisRange(0, ADC_Max);
6364
Joystick.setRyAxisRange(0, ADC_Max);
65+
66+
updateJoystick(); // send initial state
6467
}
6568

6669
void loop() {
6770
pedals.update();
6871

72+
if (pedals.positionChanged() || AlwaysSend) {
73+
updateJoystick();
74+
}
75+
}
76+
77+
void updateJoystick() {
6978
if (pedals.hasPedal(SimRacing::Gas)) {
7079
int gasPedal = pedals.getPosition(SimRacing::Gas, 0, ADC_Max);
7180
Joystick.setRyAxis(gasPedal);

src/SimRacing.cpp

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,33 @@ AnalogInput::AnalogInput(uint8_t p)
268268

269269
bool AnalogInput::read() {
270270
bool changed = false;
271+
271272
if (Pin != NOT_A_PIN) {
272-
const int current = this->position;
273+
const int previous = this->position;
273274
this->position = analogRead(Pin);
274-
if (current != this->position) changed = true;
275+
276+
// check if value is different for 'changed' flag
277+
if (previous != this->position) {
278+
279+
const int rMin = isInverted() ? getMax() : getMin();
280+
const int rMax = isInverted() ? getMin() : getMax();
281+
282+
if (
283+
// if the previous value was under the minimum range
284+
// and the current value is as well, no change
285+
!(previous < rMin && this->position < rMin) &&
286+
287+
// if the previous value was over the maximum range
288+
// and the current value is as well, no change
289+
!(previous > rMax && this->position > rMax)
290+
)
291+
{
292+
// otherwise, the current value is either within the
293+
// range limits *or* it has changed from one extreme
294+
// to the other. Either way, mark it changed!
295+
changed = true;
296+
}
297+
}
275298
}
276299
return changed;
277300
}
@@ -311,15 +334,19 @@ void AnalogInput::setCalibration(AnalogInput::Calibration newCal) {
311334
//#########################################################
312335

313336
Pedals::Pedals(AnalogInput* dataPtr, uint8_t nPedals, uint8_t detectPin)
314-
: pedalData(dataPtr), NumPedals(nPedals), detector(detectPin)
337+
:
338+
pedalData(dataPtr),
339+
NumPedals(nPedals),
340+
detector(detectPin),
341+
changed(false)
315342
{}
316343

317344
void Pedals::begin() {
318345
update(); // set initial pedal position
319346
}
320347

321348
bool Pedals::update() {
322-
bool changed = false;
349+
changed = false;
323350

324351
detector.poll();
325352
if (detector.getState() == DeviceConnection::Connected) {
@@ -420,8 +447,61 @@ void Pedals::serialCalibration(Stream& iface) {
420447
pedalData[i].read(); // read position
421448
pedalCal[i].max = pedalData[i].getPositionRaw(); // set max to the recorded position
422449
}
450+
451+
// deadzone options
452+
iface.println(separator);
423453
iface.println();
424454

455+
float DeadzoneMin = 0.01; // by default, 1% (trying to keep things responsive)
456+
float DeadzoneMax = 0.025; // by default, 2.5%
457+
458+
iface.println(F("These settings are optional. Send 'y' to customize. Send any other character to continue with the default values."));
459+
460+
iface.print(F(" * Pedal Travel Deadzone, Start: \t"));
461+
iface.print(DeadzoneMin);
462+
iface.println(F(" (Used to avoid the pedal always being slightly pressed)"));
463+
464+
iface.print(F(" * Pedal Travel Deadzone, End: \t"));
465+
iface.print(DeadzoneMax);
466+
iface.println(F(" (Used to guarantee that the pedal can be fully pressed)"));
467+
468+
iface.println();
469+
470+
waitClient(iface);
471+
472+
if (iface.read() == 'y') {
473+
iface.println(F("Set the pedal travel starting deadzone as a floating point percentage."));
474+
readFloat(DeadzoneMin, iface);
475+
iface.println();
476+
477+
iface.println(F("Set the pedal travel ending deadzone as a floating point percentage."));
478+
readFloat(DeadzoneMax, iface);
479+
iface.println();
480+
}
481+
482+
flushClient(iface);
483+
484+
// calculate deadzone offsets
485+
for (int i = 0; (i < getNumPedals()) && (i < MaxPedals); i++) {
486+
auto &cMin = pedalCal[i].min;
487+
auto &cMax = pedalCal[i].max;
488+
489+
const int range = abs(cMax - cMin);
490+
const int dzMin = DeadzoneMin * (float)range;
491+
const int dzMax = DeadzoneMax * (float)range;
492+
493+
// non-inverted
494+
if (cMax >= cMin) {
495+
cMax -= dzMax; // 'cut' into the range so it limits sooner
496+
cMin += dzMin;
497+
}
498+
// inverted
499+
else {
500+
cMax += dzMax;
501+
cMin -= dzMin;
502+
}
503+
}
504+
425505
// print finished calibration
426506
iface.println(F("Here is your calibration:"));
427507
iface.println(separator);
@@ -487,7 +567,7 @@ LogitechPedals::LogitechPedals(uint8_t gasPin, uint8_t brakePin, uint8_t clutchP
487567
// taken from calibrating my own pedals. the springs are pretty stiff so while
488568
// this covers the whole travel range, users may want to back it down for casual
489569
// use (esp. for the brake travel)
490-
this->setCalibration({ 904, 48 }, { 949, 286 }, { 881, 59 });
570+
this->setCalibration({ 904, 48 }, { 944, 286 }, { 881, 59 });
491571
}
492572

493573
LogitechDrivingForceGT_Pedals::LogitechDrivingForceGT_Pedals(uint8_t gasPin, uint8_t brakePin, uint8_t detectPin)

src/SimRacing.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,13 @@ namespace SimRacing {
331331
*/
332332
int getNumPedals() const { return this->NumPedals; }
333333

334+
/**
335+
* Checks whether the current pedal positions have changed since the last update.
336+
*
337+
* @return 'true' if position has changed, 'false' otherwise
338+
*/
339+
bool positionChanged() const { return changed; }
340+
334341
/**
335342
* Calibrate a pedal's min/max values for rescaling.
336343
*
@@ -362,6 +369,7 @@ namespace SimRacing {
362369
AnalogInput* pedalData; ///< pointer to the pedal data
363370
const int NumPedals; ///< number of pedals managed by this class
364371
DeviceConnection detector; ///< detector instance for checking if the pedals are connected
372+
bool changed; ///< whether the pedal position has changed since the previous update
365373
};
366374

367375

0 commit comments

Comments
 (0)