Kiko Ishimoto / Stepper

Dependents:   OneCircleRobot

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stepperMotor.h Source File

stepperMotor.h

00001 
00002 #ifndef Stepper_H
00003 #define Stepper_H
00004 
00005 #include "mbed.h"
00006 #define Front 1
00007 #define Back 2
00008 #define Stop 3
00009 #define Servo 4
00010 #define OFF 0
00011 #define ON 1
00012 
00013 int step[4] = {0,1,3,2};
00014 
00015 class Stepper
00016 {
00017 protected :
00018     PinName Pin[2];
00019     float bias;
00020     Stepper& operator=(const Motor &m) {
00021         return *this;
00022     }
00023 
00024 public:
00025 
00026     Stepper(PinName _pin1, PinName _pin2) : input(_pin1,_pin2) {
00027         Max = 0;
00028         state = Stop;
00029         pulse = 0;
00030         Step = 0;
00031         intervalTime = 0.1;
00032         start();
00033         time = 0;
00034     }
00035 
00036 
00037     Stepper& operator= (float duty) {
00038         if(duty<-0.01F)
00039         {
00040             duty = -0.01 / duty;
00041             run(Back,duty);
00042         }
00043         else if(float(duty)>0.01F)
00044         {
00045             duty = 0.01 / duty;
00046             run(Front,duty);
00047         }
00048         else run(Stop,0);
00049         return *this;
00050     }
00051     void start() {
00052         tick.attach(this,&Stepper::interval,0.01);
00053 
00054     }
00055     void run(int i,float duty) {
00056         intervalTime = duty;
00057         state = i;
00058     }
00059     float min,max;
00060     void setbias_motor(float b) {
00061         bias=b;
00062     }
00063     void duty_max(float Max) {
00064         max=Max;
00065     }
00066     void duty_min(float Min) {
00067         min=Min;
00068     }
00069     void setTarget(int tar)
00070     {
00071         target = tar;
00072     }
00073     int enable;
00074     int state;
00075     int target;
00076     float Max;
00077     bool finished;
00078     int Step;
00079     float angle;
00080     int pulse;
00081     int targetPulse;
00082     float intervalTime;
00083     BusOut input;
00084     Ticker tick;
00085     double time;
00086     void interval() {
00087         /*if(enable == OFF)
00088         {
00089             return;
00090         }*/
00091         time = time + 0.01;
00092         if(intervalTime < time) {
00093             time = 0;
00094             if(state == Front) {
00095                 Step++;
00096                 if(Step>=4)Step = 0;
00097                 input = step[Step];
00098                 pulse++;
00099 
00100                 //printf("%d  %d  ",step[Step],Step);
00101             }
00102             if(state == Back) {
00103                 Step--;
00104                 if(Step<=-1)Step = 3;
00105                 input = step[Step];
00106                 pulse--;
00107             }
00108             if(state == Servo) {
00109                 if(pulse < target) {
00110                     Step++;
00111                     if(Step>=4)Step = 0;
00112                     input = step[Step];
00113                     pulse++;
00114 
00115                     //printf("%d  %d  ",step[Step],Step);
00116                 }
00117                 if(pulse > target) {
00118                     Step--;
00119                     if(Step<=-1)Step = 3;
00120                     input = step[Step];
00121                     pulse--;
00122                 }
00123             }
00124         }
00125     }
00126 };
00127 
00128 #endif /* PID_H */