Kang mingyo / motor
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers motor.h Source File

motor.h

00001 #ifndef MBED_MOTOR_h
00002 #define MBED_MOTOR_h
00003 
00004 #include "mbed.h"
00005 
00006 #define PWMOFFSET 512 // pwm @ Duty 50% 
00007 #define MaxBuf 255  // buffer size to store the speed data
00008 
00009 class MotorCtl
00010 {
00011     public:
00012     
00013         MotorCtl(PinName Pwm, PinName Dir, PinName tachoA, PinName tachoB);
00014         
00015         ~MotorCtl();
00016 
00017         int getRPM();
00018         float CalculateCumDis();
00019         float CalculateRelaDis();
00020         float CalculateVelocity();
00021         int getTarget();
00022         int getError();
00023         float getKP();
00024         float getKI();
00025         float getKD();
00026         int *getHistory();
00027         int getCurrentPosition();
00028         
00029         void SetPeriod(long pwmPeriod); // set pwm period
00030         void setTarget(int spd);
00031         void setPID(float p, float i, float d);
00032         void setDirection(); // set the direction
00033         
00034         
00035         //Control Methods
00036         void UpdateCurrentPosition();
00037         void PIDControl();
00038         void Reset(void);
00039 
00040 
00041         
00042     private:
00043         PwmOut _pwm;
00044         DigitalOut _Dir;
00045         DigitalInOut _tachoA;
00046         DigitalInOut _tachoB;
00047         Ticker _tick;
00048         
00049         long CurrentPosition, PreviousPosition, DistancePosition; // Position Value
00050         int CurrentSpeed, Error,PreviousError; // Speed Data
00051         float duty; // Duty level ( 0-1024)
00052         unsigned int pwmPeriod; // default 50ms
00053         unsigned char pwmpin; // PB2 ( 10 )
00054         unsigned char ppin, npin,dr;  // dr =10 means ccwise, dr=0 means clockwise
00055         unsigned int CntPerRev;
00056         unsigned int DeltaT;
00057         unsigned char PreviousEncode;
00058         float kp, ki, kd;
00059         int integ, derv,control,cw0[MaxBuf];
00060         unsigned char cpidx;
00061         float CalculateRPM(int DeltaCnt); // Calculate RPM value
00062 
00063         float vel;
00064         int TargetSpeed;
00065 
00066 };
00067 #endif
00068         
00069         
00070