-
Notifications
You must be signed in to change notification settings - Fork 1
/
diyLoadCellSimRacingPedals.ino
147 lines (120 loc) · 3.57 KB
/
diyLoadCellSimRacingPedals.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#if 1
__asm volatile ("nop");
#endif
#include "HX711.h"
#include "Joystick.h"
#define DOUT 3
#define CLK 2
#define DOUT2 6
#define CLK2 7
// Setup HX711 boards
HX711 throttleScale;
HX711 brakeScale;
// Create raw variables
float rawThrottle = 0;
float rawBrake = 0;
// Create conversion variables
// Line joining (a,0) to (b, 1023) with gradient m=1023/(b-a) and c=-ma
const float minRawThrottle = 30000;
const float maxRawThrottle = 0.94*1000000;
const float mThrottle = 1023/(maxRawThrottle - minRawThrottle);
const float cThrottle = - mThrottle * minRawThrottle;
const float minRawBrake = 15000;
const float maxRawBrake = 800000;
const float mBrake = 1023/(maxRawBrake - minRawBrake);
const float cBrake = - mBrake * minRawBrake;
// Create output variables
float adjThrottle = 0;
float adjBrake = 0;
// Create Joystick
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID + 1, // Don't use first joystick ID, as this is taken by the steering wheel device.
JOYSTICK_TYPE_MULTI_AXIS, 0, 0, // Button Counts
false, false, false, false, false, false, // Axes Includes
false, true, false, true, false); // Rudder, throttle, accelerator, brake, steering
// Set auto send mode on joystick
const bool testAutoSendMode = true;
// Plotting - Comment line(s) to disable plotting and/or timer counter
//#define PLOT_RAW;
//#define PLOT_ADJ;
//#define PLOT_PER;
//#define TIMER_COUNTER;
// Loop Timer
long lastMillis = 0;
long loops = 0;
void setup() {
// Set Inbuilt LED High During Calibration
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// Begin connection
#ifdef PLOT_RAW || PLOT_ADJ || PLOT_PER || TIMER_COUNTER
Serial.begin(9600);
#endif
// Setup first HX711
throttleScale.begin(DOUT, CLK);
throttleScale.set_scale();
throttleScale.tare(); // Set scale to 0
long zero_factor = throttleScale.read_average();
// Setup second HX711
brakeScale.begin(DOUT2, CLK2);
brakeScale.set_scale();
brakeScale.tare(); // Set scale to 0
long zero_factor2 = brakeScale.read_average();
// Set Range Values for Joystick
Joystick.setThrottleRange(0, 1023);
Joystick.setBrakeRange(0, 1023);
// Start joystick connection
Joystick.begin(testAutoSendMode);
// Calibration finished, turn off LED
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
/* ------------ Read and set Values ------------ */
// Read load cell values
rawThrottle = throttleScale.get_units();
rawBrake = brakeScale.get_units();
// Convert values
adjThrottle = (mThrottle * rawThrottle) + cThrottle;
adjBrake = (mBrake * rawBrake) + cBrake;
// Write values to joystick
Joystick.setThrottle(adjThrottle);
Joystick.setBrake(adjBrake);
/* ------------------ Debugging ----------------- */
// Print to plotter
// Raw Load Cell Values
#ifdef PLOT_RAW
Serial.print(rawThrottle, 1);
Serial.print(", ");
Serial.println(rawBrake, 1);
#endif
// Adjusted values
#ifdef PLOT_ADJ
Serial.print(adjThrottle, 1);
Serial.print(", ");
Serial.println(adjBrake, 1);
#endif
// Percentage Values
#ifdef PLOT_PER
if (adjThrottle > 0) {
Serial.print(100.0*adjThrottle/1023.);
} else {
Serial.print(0.0);
}
Serial.print(", ");
if (adjBrake > 0) {
Serial.println(100.0*adjBrake/1023.);
} else {
Serial.println(0.0);
}
#endif
// Timer counter
#ifdef TIMER_COUNTER
long currMillis = millis();
loops++;
if(currMillis - lastMillis > 1000){
Serial.print("Loops last second:");
Serial.println(loops);
lastMillis = currMillis;
loops = 0;
}
#endif
}