Advertisement
UnaClocker

KartBot rev3

Mar 8th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. // Una's crazed Go-Kart Project, aka, KartBot
  2. // Dual OSMC's controlling a wheelchair base
  3. // Creative Commons License. Written by Brian Gosney 2012
  4. // Revision 3, pretty much.
  5.  
  6. //#include <LiquidCrystalFast.h>
  7.  
  8. //LiquidCrystalFast lcd(7,8,9,10,11,12,13);
  9.  
  10.  
  11. int readings[100];
  12. byte readingNumber=0;
  13. long averageMe = 0;
  14. int brakeRaw;
  15. int tpsRaw;
  16. byte dutyCycle;
  17. byte brakeLevel;
  18. byte tpsLevel;
  19. boolean motorEnabled= false;
  20. boolean braking = false;
  21. boolean goForward = true;
  22.  
  23. #define disablePin 5
  24. #define leftForward 14
  25. #define leftBackward 24
  26. #define rightForward 15
  27. #define rightBackward 25
  28. #define brakeControl 16
  29. #define throttleSensor 38
  30. #define brakeSensor 39
  31. #define steeringSensor 40 // Not yet implemented - placeholder for now
  32. #define leftMotorTemp 41 // Moving to I2C temp sensors asap.
  33. #define rightMotorTemp 42
  34.  
  35.  
  36.  
  37.  
  38. void setup() {
  39. //  Serial.begin(9600);
  40.   pinMode(disablePin, OUTPUT);
  41.   digitalWrite(disablePin, HIGH); // Go high immediately to prevent twitching on startup. High = disabled.
  42.   pinMode(leftForward, OUTPUT);
  43.   pinMode(leftBackward, OUTPUT);
  44.   pinMode(rightForward, OUTPUT);
  45.   pinMode(rightBackward, OUTPUT);
  46.   pinMode(brakeControl, OUTPUT);
  47.   digitalWrite(brakeControl, HIGH); // Release the brake, PWM mode so we can ease it back on.
  48.   /*  lcd.begin(20,4);
  49.    lcd.setCursor(0,0);
  50.    lcd.print(F("Throttle:    %"));
  51.    lcd.setCursor(0,1);
  52.    lcd.print(F("Brake:    %")); */
  53.   pinMode(throttleSensor, INPUT);
  54.   pinMode(brakeSensor, INPUT);
  55. }
  56.  
  57. void applyBrakes() {
  58.   if (brakeRaw < 800) {
  59.     digitalWrite(disablePin, LOW); // engage motor electric brakes
  60.     digitalWrite(brakeControl, HIGH); // make sure mag brakes are released
  61.   }
  62.   else {
  63.     digitalWrite(brakeControl, LOW); // lock up the mag brakes!
  64.   }
  65.   braking = true;
  66. }
  67.  
  68. void stopMotors() {
  69.   // stop the motors
  70.   motorEnabled = false;
  71.   digitalWrite(disablePin, HIGH);
  72.   analogWrite(leftForward, 0);
  73.   analogWrite(rightForward, 0);
  74. }
  75.  
  76. void readInputs() {
  77.   tpsRaw = analogRead(throttleSensor);
  78.   brakeRaw = analogRead(brakeSensor);
  79. }
  80.  
  81. void driveMotors() {
  82.   dutyCycle = map(tpsRaw, 0, 1024, 0, 255);
  83.   // Serial.print("Motor Duty Cycle ");
  84.   // Serial.println(dutyCycle);
  85.   if (goForward) {
  86.     analogWrite(leftForward, dutyCycle);
  87.     analogWrite(rightForward, dutyCycle);
  88.   }
  89.   else {
  90.     analogWrite(leftBackward, dutyCycle);
  91.     analogWrite(rightBackward, dutyCycle);
  92.   }
  93.   if (!motorEnabled) {
  94.     digitalWrite(disablePin, LOW); // enable the motor controllers
  95.     motorEnabled = true;
  96.   }
  97.  
  98. }
  99.  
  100.  
  101.  
  102. void loop() {
  103.   readInputs();
  104.   //  lcd.setCursor(10,0);
  105.   //  tpsLevel = map(tpsRaw, 0, 1023, 0, 100);
  106.   //  brakeLevel = map(brakeRaw, 0, 1023, 0, 100);
  107.   /*  lcd.print(tpsLevel);
  108.    lcd.print("%   ");
  109.    lcd.setCursor(7,1);
  110.    lcd.print(brakeLevel);
  111.    lcd.print("%   "); */
  112.   //  Serial.print("TPS: ");
  113.   //  Serial.println(tpsLevel);
  114.   if (tpsRaw > 3) { // Gas pedal is pressed
  115.     if (brakeRaw < 10) { // Brake pedal isn't
  116.       if (braking) { // Brake pedal had been pressed, but isn't now
  117.         digitalWrite(brakeControl, HIGH); // Release magnetic brakes
  118.         digitalWrite(disablePin, HIGH); // Release motor electric brakes
  119.         braking = false;
  120.       }
  121.       // drive the motors
  122.       driveMotors();
  123.     }
  124.   }
  125.   else if (motorEnabled) {
  126.     stopMotors();
  127.   }
  128.   if ((!motorEnabled) and (brakeLevel > 5)) { // let's use the brakes!
  129.     applyBrakes();
  130.   }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement