Advertisement
6677

Rover 5 RC WIP

Jan 1st, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.86 KB | None | 0 0
  1. int throttleVal; // Here's where we'll keep our channel values
  2. int steerVal;
  3. //int ch3;
  4.  
  5. const String terminalReset = "\x1b[2J\x1b[1;1H";
  6.  
  7. const int steerChannel = PN_3;
  8. const int throttleChannel = PN_2;
  9.  
  10. int leftThrottle;
  11. int rightThrottle;
  12.  
  13. const int leftMotorDir = PL_3;
  14. const int leftMotorPWM = PM_5;
  15. const int rightMotorDir = PL_2;
  16. const int rightMotorPWM = PM_4;
  17.  
  18. const int leftEncA = PQ_1;
  19. const int leftEncB = PP_3;
  20. const int leftEncX = PQ_0;
  21. const int rightEncA = PQ_3;
  22. const int rightEncB = PQ_2;
  23. const int rightEncX = PP_4;
  24.  
  25. unsigned long leftEncTicks = 0;
  26. unsigned long rightEncTicks = 0;
  27.  
  28. void setup() {
  29.  
  30.   pinMode(steerChannel, INPUT); // Set our input pins as such
  31.   pinMode(throttleChannel, INPUT);
  32.   //pinMode(7, INPUT);
  33.  
  34.   pinMode(leftMotorDir, OUTPUT);
  35.   pinMode(leftMotorPWM, OUTPUT);
  36.   pinMode(rightMotorDir, OUTPUT);
  37.   pinMode(rightMotorPWM, OUTPUT);
  38.  
  39.   pinMode(leftEncA, INPUT);
  40.   pinMode(leftEncB, INPUT);
  41.   pinMode(leftEncX, INPUT);
  42.   pinMode(rightEncA, INPUT);
  43.   pinMode(rightEncB, INPUT);
  44.   pinMode(rightEncX, INPUT);
  45.  
  46.   Serial.begin(115200); // Pour a bowl of Serial
  47.   attachInterrupt(leftEncX, leftEncoderInterrupt, CHANGE);
  48.   attachInterrupt(rightEncX, rightEncoderInterrupt, CHANGE);
  49. }
  50.  
  51. void loop() {
  52.   Serial.print(terminalReset);
  53.   throttleVal = pulseIn(throttleChannel, HIGH, 25000); // Read the pulse width of
  54.   steerVal = pulseIn(steerChannel, HIGH, 25000); // each channel
  55.   //ch3 = pulseIn(7, HIGH, 25000);
  56.   if (steerVal < 750) {
  57.     Serial.println("***ERROR***");
  58.     Serial.println("*No Signal*");
  59.    
  60.     throttleVal = 0;
  61.     steerVal = 0;
  62.   }
  63.   else {
  64.     steerVal = convertPulseToSpeed(steerVal, 20, true);
  65.     throttleVal = convertPulseToSpeed(throttleVal, 20, false);
  66.   }
  67.   leftThrottle = throttleVal + steerVal;
  68.   rightThrottle = throttleVal - steerVal;
  69.   leftThrottle = constrain(leftThrottle, -500, 500);
  70.   rightThrottle = constrain(rightThrottle, -500, 500);
  71.   Serial.print("Throttle Channel:"); // Print the value of
  72.   Serial.println(throttleVal);        // each channel
  73.   Serial.print("Steering Channel:");
  74.   Serial.println(steerVal);
  75.   Serial.println();
  76.   Serial.print("Left Throttle: ");Serial.println(leftThrottle);
  77.   Serial.print("Right Throttle: ");Serial.println(rightThrottle);
  78.   Serial.print('\n');
  79.   Serial.print("LTicks: ");Serial.println(leftEncTicks);
  80.   Serial.print("RTicks: ");Serial.println(rightEncTicks);
  81.   //SAFETY MODE
  82.   leftThrottle = 0;
  83.   rightThrottle = 0;
  84.  
  85.   setValues(leftThrottle, rightThrottle);
  86.   delay(100); // I put this here just to make the terminal
  87.               // window happier
  88. }
  89.  
  90. int convertPulseToSpeed(int pulseWidth, int deadzone, boolean invertAxis) {
  91.   pulseWidth = constrain(pulseWidth, 1000, 2000);
  92.   pulseWidth = map(pulseWidth, 1000, 2000, -500, 500);
  93.   if (invertAxis) {
  94.     pulseWidth = -pulseWidth;
  95.   }
  96.   if ((pulseWidth > -deadzone) && (pulseWidth < deadzone)) {
  97.     pulseWidth = 0;
  98.   }
  99.   return pulseWidth;
  100. }
  101.  
  102. void setValues(int leftSpeed, int rightSpeed) {
  103.   leftSpeed = map(leftSpeed, -500, 500, -255, 255);
  104.   if (leftSpeed == 0) {
  105.     digitalWrite(leftMotorDir, HIGH);
  106.     analogWrite(leftMotorPWM, 0);
  107.   }
  108.   else if (leftSpeed > 0) {
  109.     digitalWrite(leftMotorDir, HIGH);
  110.     analogWrite(leftMotorPWM, leftSpeed);
  111.   }
  112.   else {
  113.     digitalWrite(leftMotorDir, LOW);
  114.     analogWrite(leftMotorPWM, -leftSpeed);
  115.   }
  116.  
  117.   rightSpeed = map(rightSpeed, -500, 500, -255, 255);
  118.   if (rightSpeed == 0) {
  119.     digitalWrite(rightMotorDir, HIGH);
  120.     analogWrite(rightMotorPWM, 0);
  121.   }
  122.   else if (rightSpeed > 0) {
  123.     digitalWrite(rightMotorDir, HIGH);
  124.     analogWrite(rightMotorPWM, rightSpeed);
  125.   }
  126.   else {
  127.     digitalWrite(rightMotorDir, LOW);
  128.     analogWrite(rightMotorPWM, -rightSpeed);
  129.   }
  130. }
  131.  
  132. void leftEncoderInterrupt(void) {
  133.   leftEncTicks++;
  134. }
  135. void rightEncoderInterrupt(void) {
  136.   rightEncTicks++;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement