Guest User

Untitled

a guest
Oct 20th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.15 KB | None | 0 0
  1. // G27_Pedals_and_Shifter.ino
  2. // by Jason Duncan
  3.  
  4. // Partially adapted from the work done by isrtv.com forums members pascalh and xxValiumxx:
  5. // http://www.isrtv.com/forums/topic/13189-diy-g25-shifter-interface-with-h-pattern-sequential-and-handbrake-modes/
  6.  
  7. #include <HID.h>
  8. #include "./lib/G27PedalsShifter.h"
  9.  
  10. // comment either out to disable
  11. #define USE_PEDALS
  12. #define USE_SHIFTER
  13.  
  14. // for debugging, gives serial output rather than working as a joystick
  15. //#define DEBUG true
  16.  
  17. #if defined(DEBUG)
  18. #define DEBUG_PEDALS true
  19. #define DEBUG_SHIFTER true
  20. #endif
  21.  
  22. //#define DEBUG_PEDALS true
  23. //#define DEBUG_SHIFTER true
  24.  
  25. // red for brake, green for gas, blue for clutch
  26. //#define PEDAL_COLORS true
  27.  
  28. // for load-cell users and Australians
  29. //#define INVERT_BRAKE true
  30.  
  31. // use static thresholds rather than on-the-fly calibration
  32. //#define STATIC_THRESHOLDS true
  33.  
  34. // LED PINS
  35. #define RED_PIN 3
  36. #define GREEN_PIN 5
  37. #define BLUE_PIN 6
  38.  
  39. // PEDAL PINS
  40. //| DB9 | Original | Harness | Description | Pro Micro |
  41. //| 1 | Black | Red | +5v | +5v |
  42. //| 2 | Orange | Yellow | Throttle | pin 18 (A0) |
  43. //| 3 | White | White | Brake | pin 19 (A1) |
  44. //| 4 | Green | Green | Clutch | pin 20 (A2) |
  45. //| 5 | | | | |
  46. //| 6 | Red | Black | GND | GND |
  47. //| 7 | | | | |
  48. //| 8 | | | | |
  49. //| 9 | Red | Black | GND | GND |
  50. #define GAS_PIN 18
  51. #define BRAKE_PIN 19
  52. #define CLUTCH_PIN 20
  53.  
  54. // SHIFTER PINS
  55. //| DB9 | Original | Harness | Shifter | Description | Pro Micro |
  56. //| 1 | Purple | Purple | 1 | Button Clock | pin 0 |
  57. //| 2 | Grey | Blue | 7 | Button Data | pin 1 |
  58. //| 3 | Yellow | Yellow | 5 | Button !CS & !PL (Mode) | pin 4 |
  59. //| 4 | Orange | Orange | 3 | Shifter X axis | pin 8 (A8) |
  60. //| 5 | White | White | 2 | SPI input | |
  61. //| 6 | Black | Black | 8 | GND | GND |
  62. //| 7 | Red | Red | 6 | +5V | VCC |
  63. //| 8 | Green | Green | 4 | Shifter Y axis | pin 9 (A9) |
  64. //| 9 | Red | Red | 1 | +5V | VCC |
  65. #define SHIFTER_CLOCK_PIN 0
  66. #define SHIFTER_DATA_PIN 1
  67. #define SHIFTER_MODE_PIN 4
  68. #define SHIFTER_X_PIN 8
  69. #define SHIFTER_Y_PIN 9
  70.  
  71. // BUTTON DEFINITIONS
  72. #define BUTTON_REVERSE 1
  73.  
  74. #define BUTTON_RED_CENTERRIGHT 4
  75. #define BUTTON_RED_CENTERLEFT 5
  76. #define BUTTON_RED_RIGHT 6
  77. #define BUTTON_RED_LEFT 7
  78. #define BUTTON_BLACK_TOP 8
  79. #define BUTTON_BLACK_RIGHT 9
  80. #define BUTTON_BLACK_LEFT 10
  81. #define BUTTON_BLACK_BOTTOM 11
  82. #define BUTTON_DPAD_RIGHT 12
  83. #define BUTTON_DPAD_LEFT 13
  84. #define BUTTON_DPAD_BOTTOM 14
  85. #define BUTTON_DPAD_TOP 15
  86.  
  87. #define OUTPUT_BLACK_TOP 7
  88. #define OUTPUT_BLACK_LEFT 8
  89. #define OUTPUT_BLACK_RIGHT 9
  90. #define OUTPUT_BLACK_BOTTOM 10
  91. #define OUTPUT_DPAD_TOP 11
  92. #define OUTPUT_DPAD_LEFT 12
  93. #define OUTPUT_DPAD_RIGHT 13
  94. #define OUTPUT_DPAD_BOTTOM 14
  95. #define OUTPUT_RED_LEFT 15
  96. #define OUTPUT_RED_CENTERLEFT 16
  97. #define OUTPUT_RED_CENTERRIGHT 17
  98. #define OUTPUT_RED_RIGHT 18
  99.  
  100. // SHIFTER AXIS THRESHOLDS
  101. #define SHIFTER_XAXIS_12 290 //Gears 1,2
  102. #define SHIFTER_XAXIS_56 600 //Gears 5,6, R
  103. #define SHIFTER_YAXIS_135 750 //Gears 1,3,5
  104. #define SHIFTER_YAXIS_246 290 //Gears 2,4,6, R
  105.  
  106. // PEDAL AXIS THRESHOLDS
  107. #define MIN_GAS 27
  108. #define MAX_GAS 925
  109. #define MIN_BRAKE 26
  110. #define MAX_BRAKE 845
  111. #define MIN_CLUTCH 45
  112. #define MAX_CLUTCH 932
  113.  
  114. // MISC.
  115. #define MAX_AXIS 1023
  116. #define SIGNAL_SETTLE_DELAY 10
  117.  
  118. // PEDAL CODE
  119. typedef struct pedal {
  120. byte pin;
  121. int min, max, cur, axis;
  122. };
  123.  
  124. typedef struct pedal Pedal;
  125.  
  126. void* gasPedal;
  127. void* brakePedal;
  128. void* clutchPedal;
  129.  
  130. int axisValue(void* in) {
  131. Pedal* input = (Pedal*)in;
  132.  
  133. int physicalRange = input->max - input->min;
  134. if (physicalRange == 0) {
  135. return 0;
  136. }
  137.  
  138. int result = map(input->cur, input->min, input->max, 0, MAX_AXIS);
  139.  
  140. if (result < 0) {
  141. return 0;
  142. }
  143. if (result > MAX_AXIS) {
  144. return MAX_AXIS;
  145. }
  146. return result;
  147. }
  148.  
  149. void processPedal(void* in) {
  150. Pedal* input = (Pedal*)in;
  151.  
  152. input->cur = analogRead(input->pin);
  153.  
  154. #if !defined(STATIC_THRESHOLDS)
  155. // calibrate, we want the highest this pedal has been
  156. input->max = input->cur > input->max ? input->cur : input->max;
  157. // same for lowest, but bottom out at current value rather than 0
  158. input->min = input->min == 0 || input->cur < input->min ? input->cur : input->min;
  159. #endif
  160.  
  161. input->axis = axisValue(input);
  162. }
  163.  
  164. void describePedal(char* name, char* axisName, void* in) {
  165. Pedal* input = (Pedal*)in;
  166. Serial.print("\nPIN: ");
  167. Serial.print(input->pin);
  168. Serial.print(" ");
  169. Serial.print(name);
  170. Serial.print(": ");
  171. Serial.print(input->cur);
  172. Serial.print(" MIN: ");
  173. Serial.print(input->min);
  174. Serial.print(" MAX: ");
  175. Serial.print(input->max);
  176. Serial.print(" ");
  177. Serial.print(axisName);
  178. Serial.print(" VALUE: ");
  179. Serial.print(input->axis);
  180. }
  181.  
  182. void setXAxis(void* in) {
  183. Pedal* input = (Pedal*)in;
  184. G27.setXAxis(input->axis);
  185. }
  186.  
  187. void setYAxis(void* in) {
  188. Pedal* input = (Pedal*)in;
  189. G27.setYAxis(input->axis);
  190. }
  191.  
  192. void setZAxis(void* in) {
  193. Pedal* input = (Pedal*)in;
  194. G27.setZAxis(input->axis);
  195. }
  196.  
  197. void pedalColor(void* inGas, void* inBrake, void* inClutch){
  198. Pedal* gas = (Pedal*)inGas;
  199. Pedal* brake = (Pedal*)inBrake;
  200. Pedal* clutch = (Pedal*)inClutch;
  201.  
  202. setColor(brake->axis + MAX_AXIS, gas->axis + MAX_AXIS, clutch->axis + MAX_AXIS);
  203. }
  204.  
  205. // SHIFTER CODE
  206. int buttonTable[] = {
  207. // first four are unused
  208. 0, 0, 0, 0,
  209. OUTPUT_RED_CENTERRIGHT,
  210. OUTPUT_RED_CENTERLEFT,
  211. OUTPUT_RED_RIGHT,
  212. OUTPUT_RED_LEFT,
  213. OUTPUT_BLACK_TOP,
  214. OUTPUT_BLACK_RIGHT,
  215. OUTPUT_BLACK_LEFT,
  216. OUTPUT_BLACK_BOTTOM,
  217. OUTPUT_DPAD_RIGHT,
  218. OUTPUT_DPAD_LEFT,
  219. OUTPUT_DPAD_BOTTOM,
  220. OUTPUT_DPAD_TOP
  221. };
  222.  
  223. void waitForSignalToSettle() {
  224. delayMicroseconds(SIGNAL_SETTLE_DELAY);
  225. }
  226.  
  227. void getButtonStates(int *ret) {
  228. digitalWrite(SHIFTER_MODE_PIN, LOW); // Switch to parallel mode: digital inputs are read into shift register
  229. waitForSignalToSettle();
  230. digitalWrite(SHIFTER_MODE_PIN, HIGH); // Switch to serial mode: one data bit is output on each clock falling edge
  231.  
  232. #if defined(DEBUG_SHIFTER)
  233. Serial.print("\nBUTTON STATES:");
  234. #endif
  235.  
  236. for(int i = 0; i < 16; ++i) { // Iteration over both 8 bit registers
  237. digitalWrite(SHIFTER_CLOCK_PIN, LOW); // Generate clock falling edge
  238. waitForSignalToSettle();
  239.  
  240. ret[i] = digitalRead(SHIFTER_DATA_PIN);
  241.  
  242. #if defined(DEBUG_SHIFTER)
  243. if (!(i % 4)) Serial.print("\n");
  244. Serial.print(" button");
  245. if (i < 10) Serial.print(0);
  246. Serial.print(i);
  247. Serial.print(" = ");
  248. Serial.print(ret[i]);
  249. #endif
  250.  
  251. digitalWrite(SHIFTER_CLOCK_PIN, HIGH); // Generate clock rising edge
  252. waitForSignalToSettle();
  253. }
  254. }
  255.  
  256. void getShifterPosition(int *ret) {
  257. ret[0] = analogRead(SHIFTER_X_PIN);
  258. ret[1] = analogRead(SHIFTER_Y_PIN);
  259. }
  260.  
  261. int getCurrentGear(int shifterPosition[], int btns[]) {
  262. int gear = 0; // default to neutral
  263. int x = shifterPosition[0], y = shifterPosition[1];
  264.  
  265. if (x < SHIFTER_XAXIS_12) // Shifter on the left?
  266. {
  267. if (y > SHIFTER_YAXIS_135) gear = 1; // 1st gear
  268. if (y < SHIFTER_YAXIS_246) gear = 2; // 2nd gear
  269. }
  270. else if (x > SHIFTER_XAXIS_56) // Shifter on the right?
  271. {
  272. if (y > SHIFTER_YAXIS_135) gear = 5; // 5th gear
  273. if (y < SHIFTER_YAXIS_246) gear = 6; // 6th gear
  274. }
  275. else // Shifter is in the middle
  276. {
  277. if (y > SHIFTER_YAXIS_135) gear = 3; // 3rd gear
  278. if (y < SHIFTER_YAXIS_246) gear = 4; // 4th gear
  279. }
  280.  
  281. if (gear != 6) btns[BUTTON_REVERSE] = 0; // Reverse gear is allowed only on 6th gear position
  282. if (btns[BUTTON_REVERSE] == 1) gear = 7; // Reverse is 7th gear (for the sake of argument)
  283.  
  284. return gear;
  285. }
  286.  
  287. void setButtonStates(int buttons[], int gear) {
  288. // release virtual buttons for all gears
  289. for (byte i = 0; i < 7; ++i) {
  290. G27.setButton(i, LOW);
  291. }
  292.  
  293. if (gear > 0) {
  294. G27.setButton(gear - 1, HIGH);
  295. }
  296.  
  297. for (byte i = BUTTON_RED_CENTERRIGHT; i <= BUTTON_DPAD_TOP; ++i) {
  298. G27.setButton(buttonTable[i], buttons[i]);
  299. }
  300. }
  301.  
  302. void describeButtonStates(int buttons[], int shifterPosition[], int gear) {
  303. Serial.print("\nSHIFTER X: ");
  304. Serial.print(shifterPosition[0]);
  305. Serial.print(" Y: ");
  306. Serial.print(shifterPosition[1]);
  307.  
  308. Serial.print(" GEAR: ");
  309. Serial.print(gear);
  310. Serial.print(" REVERSE: ");
  311. Serial.print(buttons[BUTTON_REVERSE]);
  312.  
  313. Serial.print(" RED BUTTONS:");
  314. if (buttons[BUTTON_RED_LEFT]) {
  315. Serial.print(" 1");
  316. }
  317. if (buttons[BUTTON_RED_CENTERLEFT]) {
  318. Serial.print(" 2");
  319. }
  320. if (buttons[BUTTON_RED_CENTERLEFT]) {
  321. Serial.print(" 3");
  322. }
  323. if (buttons[BUTTON_RED_RIGHT]) {
  324. Serial.print(" 4");
  325. }
  326.  
  327. Serial.print(" BLACK BUTTONS:");
  328. if (buttons[BUTTON_BLACK_LEFT]) {
  329. Serial.print(" LEFT");
  330. }
  331. if (buttons[BUTTON_BLACK_TOP]) {
  332. Serial.print(" TOP");
  333. }
  334. if (buttons[BUTTON_BLACK_BOTTOM]) {
  335. Serial.print(" BOTTOM");
  336. }
  337. if (buttons[BUTTON_BLACK_RIGHT]) {
  338. Serial.print(" RIGHT");
  339. }
  340.  
  341. Serial.print(" D-PAD:");
  342. if (buttons[BUTTON_DPAD_LEFT]) {
  343. Serial.print(" LEFT");
  344. }
  345. if (buttons[BUTTON_DPAD_TOP]) {
  346. Serial.print(" UP");
  347. }
  348. if (buttons[BUTTON_DPAD_BOTTOM]) {
  349. Serial.print(" DOWN");
  350. }
  351. if (buttons[BUTTON_DPAD_RIGHT]) {
  352. Serial.print(" RIGHT");
  353. }
  354. }
  355.  
  356. void setup() {
  357. Serial.begin(38400);
  358. #if !defined(DEBUG_PEDALS) && !defined(DEBUG_SHIFTER)
  359. G27.begin(false);
  360. #endif
  361.  
  362. // lights
  363. pinMode(RED_PIN, OUTPUT);
  364. pinMode(GREEN_PIN, OUTPUT);
  365. pinMode(BLUE_PIN, OUTPUT);
  366.  
  367. // shifter
  368. pinMode(SHIFTER_MODE_PIN, OUTPUT);
  369. pinMode(SHIFTER_CLOCK_PIN, OUTPUT);
  370.  
  371. digitalWrite(SHIFTER_MODE_PIN, HIGH);
  372. digitalWrite(SHIFTER_CLOCK_PIN, HIGH);
  373.  
  374. // pedals
  375. Pedal* gas = new Pedal();
  376. Pedal* brake = new Pedal();
  377. Pedal* clutch = new Pedal();
  378.  
  379. gas->pin = GAS_PIN;
  380. brake->pin = BRAKE_PIN;
  381. clutch->pin = CLUTCH_PIN;
  382.  
  383. #if defined(STATIC_THRESHOLDS)
  384. gas->min = MIN_GAS;
  385. gas->max = MAX_GAS;
  386.  
  387. brake->min = MIN_BRAKE;
  388. brake->max = MAX_BRAKE;
  389.  
  390. clutch->min = MIN_CLUTCH;
  391. clutch->max = MAX_CLUTCH;
  392. #endif
  393.  
  394. gasPedal = gas;
  395. brakePedal = brake;
  396. clutchPedal = clutch;
  397. }
  398.  
  399. void loop() {
  400. // pedals
  401. processPedal(gasPedal);
  402. processPedal(brakePedal);
  403. processPedal(clutchPedal);
  404.  
  405. #if defined(INVERT_BRAKE)
  406. Pedal* brake = (Pedal*)brakePedal;
  407. brake->axis = map(brake->axis, 0, MAX_AXIS, MAX_AXIS, 0);
  408. #endif
  409.  
  410. #if defined(DEBUG_PEDALS)
  411. describePedal("GAS", "X", gasPedal);
  412. describePedal("BRAKE", "Y", brakePedal);
  413. describePedal("CLUTCH", "Z", clutchPedal);
  414. #elif defined(USE_PEDALS)
  415. setXAxis(gasPedal);
  416. setYAxis(brakePedal);
  417. setZAxis(clutchPedal);
  418. #endif
  419.  
  420. #if defined(PEDAL_COLORS)
  421. pedalColor(gasPedal, brakePedal, clutchPedal);
  422. #endif
  423.  
  424. // shifter
  425. int buttonStates[16];
  426. getButtonStates(buttonStates);
  427. int shifterPosition[2];
  428. getShifterPosition(shifterPosition);
  429. int gear = getCurrentGear(shifterPosition, buttonStates);
  430.  
  431. #if defined(DEBUG_SHIFTER)
  432. describeButtonStates(buttonStates, shifterPosition, gear);
  433. #elif defined(USE_SHIFTER)
  434. setButtonStates(buttonStates, gear);
  435. #endif
  436.  
  437. #if !defined(DEBUG_SHIFTER) || !defined(DEBUG_PEDALS)
  438. G27.sendState();
  439. #endif
  440.  
  441. #if defined(DEBUG_PEDALS) || defined(DEBUG_SHIFTER)
  442. Serial.print("\n----------------------------------------------------------------------------");
  443. // slow the output down a bit
  444. delay(500);
  445. #endif
  446. }
  447.  
  448. void setColor(int red, int green, int blue) {
  449. analogWrite(RED_PIN, red);
  450. analogWrite(GREEN_PIN, green);
  451. analogWrite(BLUE_PIN, blue);
  452. }
Advertisement
Add Comment
Please, Sign In to add comment