CG_MarioKart.ino 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. RC PulseIn Joystick Servo Control
  3. By: Nick Poole
  4. SparkFun Electronics
  5. Date: 5
  6. License: CC-BY SA 3.0 - Creative commons share-alike 3.0
  7. use this code however you'd like, just keep this license and
  8. attribute. Let me know if you make hugely, awesome, great changes.
  9. */
  10. const int ch1_pin = 10 ;
  11. const int ch2_pin = 11 ;
  12. int ch1; // Here's where we'll keep our channel values
  13. int ch2;
  14. int move; // Forward/Back speed
  15. int turn; // Turning Factor
  16. int speeds_pin[2] = {5, 6};
  17. int dirs_pin[2] = {4, 7};
  18. //int pwm_a = 5; //PWM control for motor outputs
  19. //int pwm_b = 6; //PWM control for motor outputs
  20. //int dir_a = 4; //direction control for motor outputs
  21. //int dir_b = 7; //direction control for motor outputs
  22. int threshold = 30 ;
  23. int manualMaxSpeed = 1024 ;
  24. const int maxSpeed_pin = A0;
  25. const int driftCorrection_pin = A1 ;
  26. int maxSpeed = 1024;
  27. float driftCorrection = 511 ;
  28. int middleThreshold (int RCvalue, int threshold) { //
  29. if ( (RCvalue < 1500 + threshold) && (RCvalue > 1500 - threshold) ) { RCvalue = 1500;}
  30. return RCvalue ;
  31. }
  32. void setup() {
  33. pinMode(ch1_pin, INPUT); // Set our input pins as such
  34. pinMode(ch2_pin, INPUT);
  35. Serial.begin(9600); // Pour a bowl of Serial (for debugging)
  36. for(int i; i<2; i++){
  37. pinMode(speeds_pin[i], OUTPUT);
  38. pinMode(dirs_pin[i], OUTPUT);
  39. analogWrite(speeds_pin[i], 0) ;
  40. }
  41. // pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
  42. // pinMode(pwm_b, OUTPUT);
  43. // pinMode(dir_a, OUTPUT);
  44. // pinMode(dir_b, OUTPUT);
  45. // analogWrite(pwm_a, 0);
  46. // analogWrite(pwm_b, 0);
  47. }
  48. void loop() {
  49. // Read and scale analog inputs
  50. // maxSpeed = map (analogRead(maxSpeed_pin), 0, 1024, 0, 255);
  51. maxSpeed = manualMaxSpeed ;
  52. // driftCorrection = analogRead(driftCorrection_pin) ;
  53. driftCorrection = 511 ;
  54. driftCorrection = driftCorrection / 512 - 1 ; // -1..1
  55. driftCorrection *= 0.5 ;
  56. Serial.print("maxSpeed:"); //Serial debugging stuff
  57. Serial.println(maxSpeed);
  58. Serial.print("correction:"); //Serial debugging stuff
  59. Serial.println(driftCorrection);
  60. //Read RC values and give wide center point
  61. ch1 = pulseIn(ch1_pin, HIGH, 25000); // Read the pulse width of
  62. ch2 = pulseIn(ch2_pin, HIGH, 25000); // each channel
  63. ch1 = middleThreshold(ch1, threshold);
  64. ch2 = middleThreshold(ch2, threshold);
  65. if(ch1 == 0) {ch1 = 1500;}
  66. if(ch2 == 0) {ch2 = 1500;}
  67. Serial.print("ch1:"); //Serial debugging stuff
  68. Serial.println(ch1);
  69. Serial.print("ch2:"); //Serial debugging stuff
  70. Serial.println(ch2);
  71. //Refresh and scale raw move values
  72. move = map(ch2, 1000,2000, -255, 255); //center over zero
  73. move = constrain(move, -255, 255);
  74. turn = map(ch1,1000,2000,-255,255);
  75. Serial.print("move:"); //Serial debugging stuff
  76. Serial.println(move);
  77. Serial.print("turn:"); //Serial debugging stuff
  78. Serial.println(turn);
  79. //calculate each motor speed
  80. int speeds[2] = {0, 0} ;
  81. int Dir = move>0 ? 1 : 0;
  82. // move = min( abs(move), maxSpeed) ;
  83. move = abs(move);
  84. turn = constrain(turn, -move, move);
  85. if (turn>0) { speeds[0] = move; speeds[1] = move - turn ; }
  86. if (turn<=0) { speeds[0] = move + turn ; speeds[1] = move ; }
  87. // if (driftCorrection>0) { speeds[0] *= 1-driftCorrection ; }
  88. // if (driftCorrection<=0) { speeds[1] *= 1+driftCorrection ; }
  89. //
  90. Serial.print("speeds: "); //Serial debugging stuff
  91. Serial.print(speeds[0]);
  92. Serial.print(" - ");
  93. Serial.println(speeds[1]);
  94. //apply to driver
  95. for(int i =0; i<2; i++) {
  96. speeds[i] = abs( speeds[i]);
  97. digitalWrite(dirs_pin[i], Dir) ;
  98. analogWrite(speeds_pin[i], speeds[i]) ;
  99. }
  100. Serial.println();
  101. }