FulligJimmy

VacuumCode_with_accelerometer

Jan 26th, 2018
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.44 KB | None | 0 0
  1. //Code: VaccumCode
  2. //Version: 2.0.1
  3. //Author: Cesar Nieto refer to [email protected]
  4. //Last change: 19/05/2017
  5. //Changes: Documentation added
  6. #include <math.h>
  7. #include <Wire.h>
  8. #include <I2Cdev.h>
  9. #include <MPU6050.h>
  10.  
  11. ////////////PINS////////////////
  12. //Distance Analog Sensors (Sharp)
  13. const int SD1 = 0; //left front sensor
  14. const int SD2 = 1; //right front sensor
  15. const int SD3 = 2; //left side sensor
  16. const int SD4 = 3; //right side sensor
  17.  
  18. //Battery Voltage input
  19. const int battery = 4;
  20.  
  21. //IndicatorLED
  22. const int led = 13;
  23.  
  24. //Fan output
  25. const int fanmotor =  12;      // the number of the LED pin
  26.  
  27. // Motor1 Right
  28. const int motor1Pin1 = 3;
  29. const int motor1Pin2 = 5;
  30.  
  31. // Motor2 Left
  32. const int motor2Pin1 = 6;
  33. const int motor2Pin2 = 9;
  34.  
  35. //Bumper
  36. const int bumper1 = 10;
  37. const int bumper2 = 11;
  38. const int bumper3 = 7;
  39. const int bumper4 = 8;
  40.  
  41.  
  42. ///////////////Constants////////////////
  43. const float voltageBatCharged = 12.68; // Voltage measured when battery fully charged //Change this
  44. //PWM for the micro metal motors
  45. const int pwmMax = 160;// for 12V  pwmMAx = 170, for 10V output  pwmMax = 140
  46. const int pwmMin = 70;;// for 9V  pwmMin = 128
  47. //Mínimun distance of the sensor
  48. const int minSharp = 30;
  49.  
  50. // Variables will change:
  51. int bumperState = 0;  // variable for reading the pushbutton status
  52. boolean control = true;
  53. int counter2 = 0; //   Prevents from being stuck
  54. int bumperState2 = 0; // variable for reading the pushbutton status
  55. boolean control2 = true;
  56. int counter3 = 0; // Prevents from being stuck
  57. int bumperState3 = 0; // variable for reading the pushbutton status
  58. boolean control3 = true;
  59. int counter4 = 0; // Prevents from being stuck
  60. int bumperState4 = 0; // variable for reading the pushbutton status
  61. boolean control4 = true;
  62. int counter5 = 0; // Prevents from being stuck
  63.  
  64. MPU6050 mpu;
  65. int16_t ax, ay, az, gx, gy, gz;
  66. int vx, vy, vx_prec, vy_prec;
  67. int count=0;
  68.  
  69.  
  70. //////////////CODE/////////////
  71. void setup() {
  72.   //Initialize outputs and inputs
  73.   //Fan motor as output
  74.   pinMode(fanmotor, OUTPUT);
  75.   //Motor1
  76.   pinMode(motor1Pin1, OUTPUT);
  77.   pinMode(motor1Pin2, OUTPUT);
  78.   //Motor2
  79.   pinMode(motor2Pin1, OUTPUT);
  80.   pinMode(motor2Pin2, OUTPUT);
  81.   //LED
  82.   pinMode(led, OUTPUT);
  83.   //INPUTS
  84.   // initialize the pushbutton inputs
  85.   //Bumper
  86.   pinMode(bumper1, INPUT_PULLUP);
  87.   pinMode(bumper2, INPUT_PULLUP);
  88.   pinMode(bumper3, INPUT_PULLUP);
  89.   pinMode(bumper4, INPUT_PULLUP);
  90.   //Sensor
  91.   pinMode(SD1, INPUT);
  92.   pinMode(SD2, INPUT);
  93.   pinMode(SD3, INPUT);
  94.   pinMode(SD4, INPUT);
  95.   //Batt
  96.   pinMode(battery, INPUT);
  97.   // Initialize serial
  98.   Serial.begin(9600);    
  99.   Wire.begin();
  100.   mpu.initialize();
  101.   if (!mpu.testConnection()) {
  102.     while (1);
  103.     }
  104.  
  105.   ///////////////////////////////Wait////////////////////////////////////////
  106.   //Wait about 5 s and initialize fan if voltage ok
  107.   waitBlinking(5,1); //5 seconds at 1 Hz
  108.   //Crank (initialize the fan because the voltage drops when cranking)
  109.   if(readBattery(battery)>12.1){
  110.     digitalWrite(fanmotor, HIGH); //Turn the Fan ON
  111.     delay(1000); //For 1000ms
  112.   }
  113.   else {
  114.     //do nothing Convention
  115.     }
  116. }
  117. //////////Functions To Use //////////
  118. void waitBlinking(int n, int frequency){
  119.   //blink for n seconds at frequency hz
  120.   for (int i=1; i <= n; i++){
  121.     for(int j=1; j<=frequency; j++){
  122.       digitalWrite(led, HIGH);  
  123.       delay((1000/frequency)/2);   //Half time on            
  124.       digitalWrite(led, LOW);  
  125.       delay((1000/frequency)/2);   //Half time off
  126.     }
  127.    }
  128. }
  129. double sdSHARP(int Sensor){
  130.   //Returns the distance in cm
  131.   double dist = pow(analogRead(Sensor), -0.857); // x to power of y
  132.   return (dist * 1167.9);
  133. }
  134. void forwardMotors(int moveTime){  
  135.   //Manipulate direction according the desired movement of the motors
  136.    analogWrite(motor1Pin1, pwmMin);
  137.    analogWrite(motor1Pin2, 0); //PWM value wher 0 = 0% and 255 = 100%
  138.    analogWrite(motor2Pin1, pwmMin);
  139.    analogWrite(motor2Pin2, 0);
  140.    delay(moveTime);
  141. }
  142. void rightMotors(int moveTime){
  143.    analogWrite(motor1Pin1, 0);
  144.    analogWrite(motor1Pin2, pwmMin);
  145.    analogWrite(motor2Pin1, pwmMin);
  146.    analogWrite(motor2Pin2, 0);
  147.  
  148.    delay(moveTime);
  149. }
  150. void leftMotors(int moveTime){
  151.    analogWrite(motor1Pin1, pwmMin);
  152.    analogWrite(motor1Pin2, 0);
  153.    analogWrite(motor2Pin1, 0);
  154.    analogWrite(motor2Pin2, pwmMin+20);
  155.    delay(moveTime);
  156. }
  157. void backwardMotors(int moveTime){
  158.    analogWrite(motor1Pin1, 0);
  159.    analogWrite(motor1Pin2, pwmMin+20);
  160.    analogWrite(motor2Pin1, 0);
  161.    analogWrite(motor2Pin2, pwmMin+20);
  162.    delay(moveTime);
  163. }
  164. void stopMotors(){
  165.    analogWrite(motor1Pin1, 0);
  166.    analogWrite(motor1Pin2, 0);
  167.    analogWrite(motor2Pin1, 0);
  168.    analogWrite(motor2Pin2, 0);
  169. }
  170. float  readBattery(int input){
  171.   int readInput;
  172.   float voltage;
  173.   readInput = analogRead(input);
  174.   voltage = (((readInput*4.9)/1000)*voltageBatCharged ) / 5; // resolution of analog input = 4.9mV per Voltage
  175.   Serial.print(" Battery= ");
  176.   Serial.print(voltage);
  177.   return voltage;
  178.   }
  179. void batteryControl(int input){
  180.   //Turn everything off in case the battery is low
  181.   float v_battery;
  182.   v_battery = readBattery(input);
  183.   if(v_battery<=11.6){ //battery limit of discharge, Don't put this limit lower than  11.1V or you can kill the battery
  184.     control = false;
  185.     }
  186.   else {
  187.     //Do nothing Convention
  188.     }
  189. }
  190. /////////////////////////////////////////////////MAIN CODE//////////////////////////////
  191. void loop(){
  192.   /*  
  193.   Serial.print("SD1= ");
  194.   Serial.print(sdSHARP(SD1));
  195.   Serial.println();
  196.   Serial.print("  SD2= ");
  197.   Serial.print(sdSHARP(SD2));
  198.   Serial.println();
  199.   delay(200);*/
  200.   bumperState = digitalRead(bumper1);
  201.   bumperState2 = digitalRead(bumper2);
  202.   bumperState3 = digitalRead(bumper3);
  203.   bumperState4 = digitalRead(bumper4);
  204.   //Keep the control of the battery automatically turn the fan off
  205.   //If control = true the battery level is ok, otherwise the battery is low.
  206.   batteryControl(battery); //modifies the variable control of the battery is low
  207.    
  208.   if (control){
  209.     digitalWrite(led, HIGH);
  210.     if (sdSHARP(SD1)<=4.3 ){
  211.       //If the distance between an object and the left front sensor is less than 4.3 cm or the bumper hits, it will move to the left
  212.       if (counter2 ==2){ // prevent of being stuck on corners
  213.         counter2 = 0;
  214.         }
  215.       else {
  216.         //Do nothing Convention
  217.       }
  218.       forwardMotors(100); // approach a bit
  219.       backwardMotors(500); // backward delay of 500ms
  220.       leftMotors(300);
  221.       counter2 = counter2 + 2;
  222.       Serial.print("  Turn Left s1");
  223.       }
  224.     else if (sdSHARP(SD2)<=4.3){
  225.       //If the distance between an object and the right front sensor is less than 4.3 cm, it will move to the right
  226.       if (counter2 ==1){
  227.         counter2 = 0;
  228.         }
  229.       else{
  230.         //Do nothing Convention
  231.       }
  232.       forwardMotors(100);
  233.       backwardMotors(500);
  234.       rightMotors(300);
  235.       counter2++;
  236.       Serial.print("  Turn Right s2");
  237.       }
  238.     else if (bumperState==0){//BUMPER1
  239.       counter2 = 0;
  240.       backwardMotors(1500); //backward delay of 500ms
  241.       leftMotors(400);
  242.       Serial.print("  Turn Left b1");
  243.       }
  244.     else if (bumperState2==0){//BUMPER2
  245.       counter3 = 0;
  246.       backwardMotors(1600); //backward delay of 500ms
  247.       rightMotors(450);
  248.       Serial.print("  Turn Right b2");
  249.       }
  250.      
  251.     else if (bumperState3==0){//BUMPER3
  252.       counter4 = 0;
  253.       backwardMotors(1550); //backward delay of 500ms
  254.       leftMotors(480);
  255.       Serial.print("  Turn Left b3");
  256.       }
  257.     else if (bumperState2==0){//BUMPER4
  258.       counter2 = 4;
  259.       backwardMotors(1590); //backward delay of 500ms
  260.       rightMotors(490);
  261.       Serial.print("  Turn Right b4");
  262.       }
  263.     else {
  264.       if(counter2==3){ //Corner
  265.         leftMotors(1000);
  266.         counter2 = 0;
  267.         }
  268.       else {
  269.         forwardMotors(0);
  270.       }
  271.       Serial.print("  Move Forward");
  272.       }
  273.  
  274.  
  275. mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  276.  
  277.   vx = (gx+300)/200;  // "+300" because the x axis of gyroscope give values about -350 while it's not moving. Change this value if you get something different using the TEST code, chacking if there are values far from zero.
  278.   vy = -(gz-100)/200; // same here about "-100"
  279.  
  280.     Serial.print("gx = ");
  281.   Serial.print(gx);
  282.   Serial.print(" | gz = ");
  283.   Serial.print(gz);
  284.  
  285.   Serial.print("        | X = ");
  286.   Serial.print(vx);
  287.   Serial.print(" | Y = ");
  288.   Serial.println(vy);
  289.  
  290.  
  291. if ( (vx_prec-20)<=vx && vx<=vx_prec+20 && (vy_prec-20)<=vy && vy<=vy_prec+20) { // checking the pointer doesn't move too much from its actual position: (in this case a 10 pixel square)
  292.     count++;                                                                  
  293.     if(count == 100){ // the click will happen after 2 seconds the pointer has stopped in the 10px square: 20ms of delay 100 times it's 2000ms = 2s
  294.         backwardMotors(1200);
  295.         leftMotors(500);
  296.         count = 0;
  297.         Serial.print("  Turn Left gpu");
  298.      
  299.     }
  300.   }
  301.   else {
  302.     vx_prec = vx; // updating values to check the position of the pointer and allow the click
  303.     vy_prec = vy;
  304.     count = 0;
  305.     }
  306.     delay(20);
  307.   }
  308.   else if (!control){
  309.     //If the battery is low, turn everything off
  310.     digitalWrite(fanmotor, LOW); //Turn the Fan OFF
  311.     stopMotors();
  312.     Serial.print(" Low Battery! ");
  313.     Serial.println();
  314.     waitBlinking(1,3);  //blink as warning 3hz in a loop
  315.     }
  316.   Serial.println();
  317.  
  318.  
  319. }
Advertisement
Add Comment
Please, Sign In to add comment