stepper mottor for pumps

Dependencies:   mbed

Committer:
ekasinambela
Date:
Mon Jun 20 13:44:12 2016 +0000
Revision:
2:5b26538ab9ab
Parent:
1:8431b042a2eb
i changed the function, now its good; in the main function the initialization of the pumps is written

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ekasinambela 0:29fc24ce612f 1 #include "mbed.h"
ekasinambela 0:29fc24ce612f 2 #include "DRV8825.h"
ekasinambela 0:29fc24ce612f 3 #define MAX_SPEED 8000
ekasinambela 0:29fc24ce612f 4 #define MICROSTEPS_PER_STEP 32
ekasinambela 1:8431b042a2eb 5 #include "Ping.h"
ekasinambela 1:8431b042a2eb 6 #include <stdio.h>
ekasinambela 1:8431b042a2eb 7 #include "TextLCD.h"
ekasinambela 1:8431b042a2eb 8 #include <cmath>
ekasinambela 0:29fc24ce612f 9 // add boundaries
ekasinambela 0:29fc24ce612f 10
ekasinambela 1:8431b042a2eb 11 Ping Pinger(p30);
ekasinambela 1:8431b042a2eb 12 Serial pc(USBTX, USBRX);
ekasinambela 1:8431b042a2eb 13
ekasinambela 0:29fc24ce612f 14 int PULL = 1;
ekasinambela 0:29fc24ce612f 15 int PUSH = 0;
ekasinambela 0:29fc24ce612f 16 int SALT = 1;
ekasinambela 0:29fc24ce612f 17 int PURE = 0;
ekasinambela 0:29fc24ce612f 18 int PREVIOUS_DIRECTION_SALT = -1;
ekasinambela 0:29fc24ce612f 19 int PREVIOUS_DIRECTION_PURE = -1;
ekasinambela 0:29fc24ce612f 20
ekasinambela 0:29fc24ce612f 21 DRV8825 stpr_mtr(p21, p27, p28, p29, p22, p23);
ekasinambela 0:29fc24ce612f 22 DRV8825 stpr_mtr2(p24, p27, p28, p29, p25, p26);
ekasinambela 0:29fc24ce612f 23
ekasinambela 1:8431b042a2eb 24 bool proximity_ok(){
ekasinambela 1:8431b042a2eb 25 int range;
ekasinambela 1:8431b042a2eb 26
ekasinambela 1:8431b042a2eb 27 Pinger.Send();
ekasinambela 1:8431b042a2eb 28 wait(0.1);
ekasinambela 1:8431b042a2eb 29 range = Pinger.Read_cm();
ekasinambela 1:8431b042a2eb 30
ekasinambela 1:8431b042a2eb 31
ekasinambela 1:8431b042a2eb 32 range = range/2;
ekasinambela 1:8431b042a2eb 33 // lcd.printf("Range in cm: %d \n\r", range);
ekasinambela 0:29fc24ce612f 34
ekasinambela 1:8431b042a2eb 35 if (range >= 12){
ekasinambela 1:8431b042a2eb 36 //lcd.printf("Proximity ok");
ekasinambela 1:8431b042a2eb 37 return true;
ekasinambela 1:8431b042a2eb 38 }
ekasinambela 1:8431b042a2eb 39 return false;
ekasinambela 1:8431b042a2eb 40 }
ekasinambela 1:8431b042a2eb 41
ekasinambela 1:8431b042a2eb 42 void add_water(int type, double mililiters, int DIRECTION){
ekasinambela 1:8431b042a2eb 43
ekasinambela 2:5b26538ab9ab 44 int MOVEMENT = 4000; //constant for 1 mL
ekasinambela 2:5b26538ab9ab 45 double STEP_EXTRA = 4700; //for the other mL's
ekasinambela 0:29fc24ce612f 46
ekasinambela 0:29fc24ce612f 47 //accelerate
ekasinambela 1:8431b042a2eb 48 for (int i = 25; i < MAX_SPEED; i+=20) {
ekasinambela 0:29fc24ce612f 49 if (type == SALT)
ekasinambela 0:29fc24ce612f 50 stpr_mtr.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i);
ekasinambela 0:29fc24ce612f 51 else
ekasinambela 1:8431b042a2eb 52 stpr_mtr2.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i);
ekasinambela 0:29fc24ce612f 53 }
ekasinambela 0:29fc24ce612f 54
ekasinambela 0:29fc24ce612f 55 //fix the changing of directions for pure
ekasinambela 2:5b26538ab9ab 56 if (type == PURE && PREVIOUS_DIRECTION_PURE != DIRECTION){
ekasinambela 2:5b26538ab9ab 57 mililiters = mililiters + 1.0;
ekasinambela 0:29fc24ce612f 58 }
ekasinambela 0:29fc24ce612f 59
ekasinambela 0:29fc24ce612f 60 //fix the changing of directions for salt
ekasinambela 2:5b26538ab9ab 61 if (type == SALT && PREVIOUS_DIRECTION_SALT != DIRECTION){
ekasinambela 2:5b26538ab9ab 62 mililiters = mililiters + 1.0;
ekasinambela 0:29fc24ce612f 63 }
ekasinambela 0:29fc24ce612f 64
ekasinambela 0:29fc24ce612f 65 //move with constant speed
ekasinambela 1:8431b042a2eb 66 for (int i = 1; i < (MOVEMENT + (int)floor(((mililiters - 1.0)*STEP_EXTRA))); i+=1) {
ekasinambela 0:29fc24ce612f 67 if (type == SALT)
ekasinambela 0:29fc24ce612f 68 stpr_mtr.settings(1/MICROSTEPS_PER_STEP, DIRECTION, MAX_SPEED);
ekasinambela 0:29fc24ce612f 69 else
ekasinambela 1:8431b042a2eb 70 stpr_mtr2.settings(1/MICROSTEPS_PER_STEP, DIRECTION, MAX_SPEED);
ekasinambela 0:29fc24ce612f 71 }
ekasinambela 2:5b26538ab9ab 72
ekasinambela 0:29fc24ce612f 73 //de-accelerate
ekasinambela 1:8431b042a2eb 74 for (int i = MAX_SPEED; i > 0; i-=20) {
ekasinambela 0:29fc24ce612f 75 if (type == SALT)
ekasinambela 0:29fc24ce612f 76 stpr_mtr.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i);
ekasinambela 0:29fc24ce612f 77 else
ekasinambela 0:29fc24ce612f 78 stpr_mtr2.settings(1/MICROSTEPS_PER_STEP, DIRECTION, i);
ekasinambela 0:29fc24ce612f 79 }
ekasinambela 0:29fc24ce612f 80
ekasinambela 0:29fc24ce612f 81 //remember previous direction
ekasinambela 1:8431b042a2eb 82 if (type == SALT){
ekasinambela 0:29fc24ce612f 83 PREVIOUS_DIRECTION_SALT = DIRECTION;
ekasinambela 1:8431b042a2eb 84 }else{
ekasinambela 0:29fc24ce612f 85 PREVIOUS_DIRECTION_PURE = DIRECTION;
ekasinambela 1:8431b042a2eb 86 }
ekasinambela 0:29fc24ce612f 87 }
ekasinambela 0:29fc24ce612f 88
ekasinambela 0:29fc24ce612f 89 int main() {
ekasinambela 2:5b26538ab9ab 90 //initialize
ekasinambela 2:5b26538ab9ab 91 wait(3); //turn the thing to pulling from reservoir
ekasinambela 2:5b26538ab9ab 92 add_water(PURE, 20.0, PULL); //20
ekasinambela 2:5b26538ab9ab 93 wait(3); //turn the thing to pushing to glass
ekasinambela 2:5b26538ab9ab 94 add_water(PURE, 20.0, PUSH); //19.5
ekasinambela 2:5b26538ab9ab 95 wait(3); //turn the thing to pulling from reservoir
ekasinambela 2:5b26538ab9ab 96 add_water(PURE, 20.0, PULL); //20
ekasinambela 2:5b26538ab9ab 97 wait(3); //turn the thing to pushing to glass
ekasinambela 2:5b26538ab9ab 98 add_water(PURE, 20.0, PUSH); //19
ekasinambela 2:5b26538ab9ab 99 wait(3); //turn the thing to pulling from reservoir
ekasinambela 2:5b26538ab9ab 100 add_water(PURE, 25.0, PULL); //20
ekasinambela 2:5b26538ab9ab 101 //done
ekasinambela 2:5b26538ab9ab 102
ekasinambela 2:5b26538ab9ab 103 //initialize
ekasinambela 2:5b26538ab9ab 104 wait(3); //turn the thing to pulling from reservoir
ekasinambela 2:5b26538ab9ab 105 add_water(SALT, 20.0, PULL); //20
ekasinambela 2:5b26538ab9ab 106 wait(3); //turn the thing to pushing to glass
ekasinambela 2:5b26538ab9ab 107 add_water(SALT, 20.0, PUSH); //19.5
ekasinambela 2:5b26538ab9ab 108 wait(3); //turn the thing to pulling from reservoir
ekasinambela 2:5b26538ab9ab 109 add_water(SALT, 20.0, PULL); //20
ekasinambela 2:5b26538ab9ab 110 wait(3); //turn the thing to pushing to glass
ekasinambela 2:5b26538ab9ab 111 add_water(SALT, 20.0, PUSH); //19
ekasinambela 2:5b26538ab9ab 112 wait(3); //turn the thing to pulling from reservoir
ekasinambela 2:5b26538ab9ab 113 add_water(SALT, 25.0, PULL); //20
ekasinambela 2:5b26538ab9ab 114 //done
ekasinambela 0:29fc24ce612f 115 }