eka sinambela / Mbed 2 deprecated mbed_stepper_motors

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DRV8825.h"
00003 #define MAX_SPEED 8000
00004 #define MICROSTEPS_PER_STEP 32
00005 #include "Ping.h"
00006 #include <stdio.h> 
00007 #include "TextLCD.h"
00008 #include <cmath>
00009 // add boundaries
00010 
00011 Ping Pinger(p30);
00012 Serial pc(USBTX, USBRX);
00013 
00014     int PULL = 1;
00015     int PUSH = 0;
00016     int SALT = 1;
00017     int PURE = 0;
00018     int PREVIOUS_DIRECTION_SALT = -1;
00019     int PREVIOUS_DIRECTION_PURE = -1;
00020     
00021     DRV8825 stpr_mtr(p21, p27, p28, p29, p22, p23);
00022     DRV8825 stpr_mtr2(p24, p27, p28, p29, p25, p26);
00023 
00024 bool proximity_ok(){
00025     int range;
00026  
00027         Pinger.Send();   
00028         wait(0.1); 
00029         range = Pinger.Read_cm();
00030         
00031         
00032         range = range/2;
00033        // lcd.printf("Range in cm: %d \n\r", range);
00034         
00035     if (range >= 12){
00036         //lcd.printf("Proximity ok");
00037         return true;
00038     }    
00039     return false;        
00040 }  
00041 
00042 void add_water(int type, double mililiters, int DIRECTION){
00043         
00044     int MOVEMENT = 4000; //constant for 1 mL
00045     double STEP_EXTRA = 4700; //for the other mL's
00046             
00047         //accelerate
00048         for (int i = 25; i < MAX_SPEED; i+=20) {    
00049             if (type == SALT)
00050                 stpr_mtr.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i);
00051            else 
00052                stpr_mtr2.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i);  
00053         } 
00054     
00055         //fix the changing of directions for pure
00056         if (type == PURE && PREVIOUS_DIRECTION_PURE != DIRECTION){          
00057             mililiters = mililiters + 1.0;
00058         }    
00059     
00060         //fix the changing of directions for salt
00061         if (type == SALT && PREVIOUS_DIRECTION_SALT != DIRECTION){
00062              mililiters = mililiters + 1.0;  
00063         }    
00064 
00065         //move with constant speed
00066         for (int i = 1; i < (MOVEMENT + (int)floor(((mililiters - 1.0)*STEP_EXTRA))); i+=1) {      
00067             if (type == SALT)
00068                 stpr_mtr.settings(1/MICROSTEPS_PER_STEP, DIRECTION, MAX_SPEED);
00069             else
00070                 stpr_mtr2.settings(1/MICROSTEPS_PER_STEP, DIRECTION, MAX_SPEED);   
00071         }
00072    
00073         //de-accelerate
00074         for (int i = MAX_SPEED; i > 0; i-=20) {    
00075             if (type == SALT)
00076                 stpr_mtr.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i);
00077             else
00078                 stpr_mtr2.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i);    
00079         }
00080     
00081         //remember previous direction
00082         if (type == SALT){    
00083             PREVIOUS_DIRECTION_SALT = DIRECTION;
00084         }else{       
00085             PREVIOUS_DIRECTION_PURE = DIRECTION;  
00086         }           
00087 }
00088 
00089 int main() {
00090         //initialize
00091         wait(3); //turn the thing to pulling from reservoir
00092         add_water(PURE, 20.0, PULL); //20
00093         wait(3); //turn the thing to pushing to glass
00094         add_water(PURE, 20.0, PUSH); //19.5 
00095         wait(3); //turn the thing to pulling from reservoir
00096         add_water(PURE, 20.0, PULL); //20
00097         wait(3); //turn the thing to pushing to glass
00098         add_water(PURE, 20.0, PUSH); //19
00099         wait(3); //turn the thing to pulling from reservoir
00100         add_water(PURE, 25.0, PULL); //20
00101         //done
00102         
00103         //initialize
00104         wait(3); //turn the thing to pulling from reservoir
00105         add_water(SALT, 20.0, PULL); //20
00106         wait(3); //turn the thing to pushing to glass
00107         add_water(SALT, 20.0, PUSH); //19.5 
00108         wait(3); //turn the thing to pulling from reservoir
00109         add_water(SALT, 20.0, PULL); //20
00110         wait(3); //turn the thing to pushing to glass
00111         add_water(SALT, 20.0, PUSH); //19
00112         wait(3); //turn the thing to pulling from reservoir
00113         add_water(SALT, 25.0, PULL); //20
00114         //done
00115 }