Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Nathan Verdonk
- 2/23/2019
- This Code is an interactive sweep setter.
- */
- #include <NewPing.h>
- #include <Stepper.h>
- #include <SPI.h>
- #include <SD.h>
- // ========================== Define Constants ==========================
- const int stepsPerRevolution = 2048; // Steps per revolution
- const double DEGREE2STEP = stepsPerRevolution/360; // Contant to make conversion between num degrees to num steps
- const int DEGREE_REM = stepsPerRevolution % 360; // Remainder after integer division of steps and degrees
- const int triggerPin = 7; // Trigger pin on sonar sensor
- const int echoPin = 6; // Echo pin on sonar sensor
- const int numPings = 10; // Number of pings from sonar sensor that will be averaged
- const int stepResolution = 1; // The number of degrees to be moved between each distance
- // ========================== Define Variables ==========================
- int lowVal = -60; // Starting angle for the sweep (degrees)
- int highVal = 60; // Ending angle for sweep (degrees)
- int setInitialSteps = 100; // Set the step value for the setStep() command
- int numVals = highVal - lowVal; // Num vals to be recorded
- unsigned int val;
- File dataFile;
- // ========================== Create Class Members ==========================
- Stepper stepper1(stepsPerRevolution, 9, 5, 4, 8); // initialize the stepper library on pins 8 through 11:
- NewPing sonar1(triggerPin, echoPin, 400); // initialize the new ping library with predefined values
- void setup() {
- stepper1.setSpeed(10); // Set rotation speed in RPM
- Serial.begin(9600); // Begin serial port
- if(!SD.begin(10)){
- Serial.println(F("Card failed, or not present"));
- while(1);
- }
- // ============ Create File ============
- dataFile = SD.open("Data.txt", FILE_WRITE);
- Serial.println(F("Card Initialized"));
- if(dataFile){
- Serial.println(F("File created"));
- }
- Serial.println("");
- Serial.println("Created By: Nathan V");
- Serial.println("");
- Serial.println(F("======================================="));
- Serial.println(F("===Sonar Set-Up Complete; Begin Loop==="));
- Serial.println(F("======================================="));
- }
- void loop() {
- setStart(); // Set the origin position of the sweep
- demoSweep(); // Display the sweep to be used in the recordings
- delay(1000);
- Serial.println(F("======================================="));
- Serial.println(F("========Begin Range Collection========="));
- Serial.println(F("======================================="));
- fullScan(lowVal, highVal, numVals, dataFile);
- dataFile.close();
- }
- // ========================== Create Functions ==========================
- // Function to adjust the origin of the stepper postion
- void setStart(void){
- // Define local variables
- char check2;
- char check = 'n';
- int steps;
- // While loop for continous checking of position
- while(check != 'y'){
- // Get user input
- Serial.println(F("Is the motor facing the correct direction (y / n)?: "));
- while(Serial.available() == 0){};
- check = Serial.read();
- if(check == 'n'){
- while(Serial.available()){
- Serial.read();
- }
- // Clear Buffer
- Serial.flush();
- Serial.println(F("Select one of the following:"));
- Serial.print(F("A: Move "));
- Serial.print(setInitialSteps);
- Serial.println(F(" Steps Clockwise"));
- Serial.print(F("B: Move "));
- Serial.print(setInitialSteps);
- Serial.println(F(" Steps Counter-Clockwise"));
- // Wait for user input before reading value
- Serial.flush();
- while(Serial.available() == 0){};
- check2 = Serial.read();
- if(check2 == 'A'){
- steps = setInitialSteps;
- }
- else if(check2 == 'B'){
- steps = -1 * setInitialSteps;
- }
- stepper1.step(steps);
- }
- }
- return;
- }
- // Function to show the user the field of view that will be recorded with the currently set values
- void demoSweep(void){
- // Move to lowest point in sweep
- Serial.println(F("Moving to lowest point in sweep"));
- stepper1.step(Deg2Step(lowVal));
- delay(2000);
- // Return to zero
- Serial.println(F("Returning to Origin"));
- stepper1.step(-1 * Deg2Step(lowVal));
- delay(1000);
- // Move to highest point in sweep
- Serial.println(F("Moving to the highest point in sweep"));
- stepper1.step(Deg2Step(highVal));
- delay(2000);
- // Return to zero
- Serial.println(F("Returning to Origin"));
- stepper1.step(-1 * Deg2Step(highVal));
- delay(1000);
- return;
- }
- // Function to convert desiered number of degrees of movement to correct number of steps
- int Deg2Step(int deg){
- int total;
- total = deg * DEGREE2STEP;
- total += (deg * (DEGREE_REM / 360.0));
- return total;
- }
- // Function to run a ping and return the distance in cm of n number of pings averaged
- int getDistance(void){
- delay(50);
- int val1 = sonar1.ping_median(numPings);
- int val2 = sonar1.convert_cm(val1);
- return val2;
- }
- // Function that will collect a full run
- void fullScan(int low, int high, int num, File imp){
- //int steps = Deg2Step(num);
- // Move sensor to lowest position
- stepper1.step(Deg2Step(low));
- // Clear Serial comm before collecting data
- Serial.flush();
- // Begin collecting range values loop
- for(int i = 0; i < num; i += stepResolution){
- val = getDistance();
- Serial.println(val);
- imp.print(val);
- imp.print(", ");
- imp.println(low + i);
- stepper1.step(Deg2Step(stepResolution));
- //stepper1.step(stepResolution);
- }
- Serial.println(F("Collection Complete"));
- Serial.println(F("Returning to origin"));
- stepper1.step(Deg2Step(-1 * high));
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement