Advertisement
nverdo1

Stepper Sweep

Mar 25th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Nathan Verdonk
  3.   2/23/2019
  4.  
  5.   This Code is an interactive sweep setter.
  6. */
  7.  
  8. #include <NewPing.h>
  9. #include <Stepper.h>
  10. #include <SPI.h>
  11. #include <SD.h>
  12.  
  13.  
  14. // ========================== Define Constants ==========================
  15. const int stepsPerRevolution = 2048;                      // Steps per revolution
  16. const double DEGREE2STEP = stepsPerRevolution/360;        // Contant to make conversion between num degrees to num steps
  17. const int DEGREE_REM = stepsPerRevolution % 360;          // Remainder after integer division of steps and degrees
  18. const int triggerPin = 7;                                 // Trigger pin on sonar sensor
  19. const int echoPin = 6;                                    // Echo pin on sonar sensor
  20. const int numPings = 10;                                  // Number of pings from sonar sensor that will be averaged
  21. const int stepResolution = 1;                             // The number of degrees to be moved between each distance
  22.  
  23.  
  24.  
  25.  
  26. // ========================== Define Variables ==========================
  27. int lowVal = -60;                                        // Starting angle for the sweep (degrees)
  28. int highVal = 60;                                        // Ending angle for sweep (degrees)
  29. int setInitialSteps = 100;                               // Set the step value for the setStep() command
  30. int numVals = highVal - lowVal;                           // Num vals to be recorded
  31. unsigned int val;              
  32. File dataFile;
  33.  
  34.  
  35. // ========================== Create Class Members ==========================
  36. Stepper stepper1(stepsPerRevolution, 9, 5, 4, 8);           // initialize the stepper library on pins 8 through 11:
  37. NewPing sonar1(triggerPin, echoPin, 400);           // initialize the new ping library with predefined values
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. void setup() {
  56.  
  57.  
  58.   stepper1.setSpeed(10);                   // Set rotation speed in RPM
  59.  
  60.   Serial.begin(9600);                         // Begin serial port
  61.  
  62.  
  63.   if(!SD.begin(10)){
  64.     Serial.println(F("Card failed, or not present"));
  65.     while(1);
  66.   }
  67.   // ============ Create File ============
  68.   dataFile = SD.open("Data.txt", FILE_WRITE);
  69.  
  70.   Serial.println(F("Card Initialized"));
  71.  
  72.   if(dataFile){
  73.     Serial.println(F("File created"));
  74.   }
  75.  
  76.   Serial.println("");
  77.   Serial.println("Created By: Nathan V");
  78.   Serial.println("");
  79.  
  80.   Serial.println(F("======================================="));
  81.   Serial.println(F("===Sonar Set-Up Complete; Begin Loop==="));
  82.   Serial.println(F("======================================="));
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. void loop() {
  93.  
  94.   setStart();                                     // Set the origin position of the sweep
  95.  
  96.   demoSweep();                                    // Display the sweep to be used in the recordings
  97.  
  98.  
  99.   delay(1000);
  100.  
  101.  
  102.   Serial.println(F("======================================="));
  103.   Serial.println(F("========Begin Range Collection========="));
  104.   Serial.println(F("======================================="));
  105.  
  106.   fullScan(lowVal, highVal, numVals, dataFile);
  107.  
  108.  
  109.   dataFile.close();
  110.  
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. // ========================== Create Functions ==========================
  124.  
  125. // Function to adjust the origin of the stepper postion
  126. void setStart(void){
  127.  
  128.   // Define local variables
  129.   char check2;
  130.   char check = 'n';
  131.   int steps;
  132.  
  133.   // While loop for continous checking of position
  134.   while(check != 'y'){
  135.    
  136.     // Get user input
  137.     Serial.println(F("Is the motor facing the correct direction (y / n)?: "));
  138.  
  139.     while(Serial.available() == 0){};
  140.    
  141.     check = Serial.read();
  142.  
  143.     if(check == 'n'){
  144.  
  145.       while(Serial.available()){
  146.         Serial.read();
  147.       }
  148.  
  149.       // Clear Buffer
  150.       Serial.flush();
  151.       Serial.println(F("Select one of the following:"));
  152.      
  153.       Serial.print(F("A: Move "));
  154.       Serial.print(setInitialSteps);
  155.       Serial.println(F(" Steps Clockwise"));
  156.  
  157.       Serial.print(F("B: Move "));
  158.       Serial.print(setInitialSteps);
  159.       Serial.println(F(" Steps Counter-Clockwise"));
  160.  
  161.  
  162.       // Wait for user input before reading value
  163.       Serial.flush();
  164.       while(Serial.available() == 0){};
  165.      
  166.       check2 = Serial.read();
  167.  
  168.       if(check2 == 'A'){
  169.         steps = setInitialSteps;
  170.       }
  171.       else if(check2 == 'B'){
  172.         steps = -1 * setInitialSteps;
  173.       }
  174.  
  175.      
  176.       stepper1.step(steps);
  177.  
  178.     }
  179.  
  180.   }
  181.  
  182.  
  183.   return;
  184.  
  185. }
  186.  
  187.  
  188. // Function to show the user the field of view that will be recorded with the currently set values
  189. void demoSweep(void){
  190.  
  191.   // Move to lowest point in sweep
  192.   Serial.println(F("Moving to lowest point in sweep"));
  193.   stepper1.step(Deg2Step(lowVal));
  194.   delay(2000);
  195.  
  196.   // Return to zero
  197.   Serial.println(F("Returning to Origin"));
  198.   stepper1.step(-1 * Deg2Step(lowVal));
  199.   delay(1000);
  200.  
  201.   // Move to highest point in sweep
  202.   Serial.println(F("Moving to the highest point in sweep"));
  203.   stepper1.step(Deg2Step(highVal));
  204.   delay(2000);
  205.  
  206.   // Return to zero
  207.   Serial.println(F("Returning to Origin"));
  208.   stepper1.step(-1 * Deg2Step(highVal));
  209.   delay(1000);
  210.  
  211.   return;
  212.  
  213. }
  214.  
  215.  
  216. // Function to convert desiered number of degrees of movement to correct number of steps
  217. int Deg2Step(int deg){
  218.   int total;
  219.   total = deg * DEGREE2STEP;
  220.   total += (deg * (DEGREE_REM / 360.0));
  221.  
  222.   return total;
  223. }
  224.  
  225.  
  226. // Function to run a ping and return the distance in cm of n number of pings averaged
  227. int getDistance(void){
  228.   delay(50);
  229.   int val1 = sonar1.ping_median(numPings);
  230.  
  231.   int val2 = sonar1.convert_cm(val1);
  232.  
  233.  
  234.   return val2;
  235. }
  236.  
  237.  
  238. // Function that will collect a full run
  239. void fullScan(int low, int high, int num, File imp){
  240.  
  241.   //int steps = Deg2Step(num);
  242.   // Move sensor to lowest position
  243.   stepper1.step(Deg2Step(low));
  244.  
  245.   // Clear Serial comm before collecting data
  246.   Serial.flush();
  247.  
  248.   // Begin collecting range values loop
  249.   for(int i = 0; i < num; i += stepResolution){
  250.     val = getDistance();
  251.  
  252.     Serial.println(val);
  253.    
  254.     imp.print(val);
  255.     imp.print(", ");
  256.     imp.println(low + i);
  257.    
  258.  
  259.     stepper1.step(Deg2Step(stepResolution));
  260.     //stepper1.step(stepResolution);
  261.  
  262.   }
  263.  
  264.  
  265.   Serial.println(F("Collection Complete"));
  266.   Serial.println(F("Returning to origin"));
  267.  
  268.   stepper1.step(Deg2Step(-1 * high));
  269.  
  270.   return;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement