-
Notifications
You must be signed in to change notification settings - Fork 0
/
g29-pedal-usb-controller.ino
200 lines (180 loc) · 5.03 KB
/
g29-pedal-usb-controller.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
*
*/
#include "controller.h"
#include "pedal.h"
// set the pins here
Pedal brake(A0, 0);
Pedal accelerator(A1, 1);
Pedal clutch(A2, 2);
enum State {
STATE_REPORT,
STATE_CALIBRATE,
STATE_MONITOR,
STATE_PLOT
};
Controller controller;
int cmd;
enum State state = STATE_REPORT;
int yawMode = 0; // all inputs reported
void dump_cal() {
Serial.print("a=");
Serial.print(accelerator.value(), DEC);
Serial.print(" amin=");
Serial.print(accelerator.vmin(), DEC);
Serial.print(" amax=");
Serial.print(accelerator.vmax(), DEC);
Serial.print(" b=");
Serial.print(brake.value(), DEC);
Serial.print(" bmin=");
Serial.print(brake.vmin(), DEC);
Serial.print(" bmax=");
Serial.print(brake.vmax(), DEC);
Serial.print(" c=");
Serial.print(clutch.value(), DEC);
Serial.print(" cmin=");
Serial.print(clutch.vmin(), DEC);
Serial.print(" cmax=");
Serial.print(clutch.vmax(), DEC);
Serial.println();
}
void setup() {
Serial.begin(9600);
accelerator.load();
brake.load();
//while(!Serial);
}
void loop() {
cmd = 0; // reset the command
if(Serial.available()) {
cmd = Serial.read();
}
switch(cmd) {
case 'd':
case 'D':
dump_cal();
break;
case 'r':
case 'R':
state = STATE_REPORT;
// reload the calibration in case we didn't finish it
accelerator.load();
brake.load();
clutch.load();
Serial.println("USB HID Reporting");
break;
case 'm':
case 'M':
// monitor
state = STATE_MONITOR;
// reload the calibration in case we didn't finish it
accelerator.load();
brake.load();
clutch.load();
Serial.println("Monitoring");
break;
case 'p':
case 'P':
// plot
state = STATE_PLOT;
// reload the calibration in case we didn't finish it
accelerator.load();
brake.load();
Serial.println("Plotting");
break;
case 'c':
case 'C':
state = STATE_CALIBRATE;
Serial.println("Starting Calibration");
accelerator.reset();
brake.reset();
clutch.reset();
break;
case 's':
case 'S':
if(state == STATE_CALIBRATE) {
Serial.println("Saving Calibration");
accelerator.save();
brake.save();
clutch.save();
state = STATE_REPORT;
}
break;
case 'y':
case 'Y':
yawMode = (yawMode + 1) % 3;
Serial.print("Yaw mode: ");
switch(yawMode){
case 0:
Serial.println("Rudder and Pedals");
break;
case 1:
Serial.println("Rudder only");
break;
case 2:
Serial.println("Pedals only");
break;
default:
Serial.println(yawMode);
break;
}
break;
case 'h':
case 'H':
case '?':
Serial.println("(C)alibrate");
Serial.println("(D)ump calibration");
Serial.println("(M)onitor");
Serial.println("(P)lot accelerator/brake/clutch");
Serial.println("(R)eport HID");
Serial.println("(S)ave calibration");
Serial.println("(Y)aw mode toggle");
case 0x0a:
case 0x00:
// ignore the CR and 0
break;
default:
Serial.print("Unknown cmd: ");
Serial.println(cmd);
}
switch(state) {
case STATE_REPORT:
controller.begin();
if(yawMode == 0 || yawMode == 1) {
controller.setRudder( 512 + ( clutch.value() / 2) - (accelerator.value() /2) );
}
if(yawMode == 0 || yawMode == 2) {
controller.setAccelerator(accelerator.value());
controller.setBrake(brake.value());
controller.setClutch(clutch.value());
}
controller.end();
break;
case STATE_MONITOR:
Serial.print("a=");
Serial.print(accelerator.value());
Serial.print(" b=");
Serial.print(brake.value());
Serial.print(" c=");
Serial.print(clutch.value());
Serial.println();
delay(500);
break;
case STATE_PLOT:
Serial.print(accelerator.raw());
Serial.print("\t");
Serial.print(brake.raw());
Serial.println();
delay(50);
break;
case STATE_CALIBRATE:
accelerator.calibrate();
brake.calibrate();
clutch.calibrate();
dump_cal();
delay(500);
break;
default:
break;
}
}