v1
motor.h@1:911f5a86c105, 2019-05-09 (annotated)
- Committer:
- kangmingyo
- Date:
- Thu May 09 23:51:13 2019 +0000
- Revision:
- 1:911f5a86c105
- Parent:
- 0:20e026e254d6
- Child:
- 4:a629f46d6594
i
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kangmingyo | 0:20e026e254d6 | 1 | #ifndef MBED_MOTOR_h |
kangmingyo | 0:20e026e254d6 | 2 | #define MBED_MOTOR_h |
kangmingyo | 0:20e026e254d6 | 3 | |
kangmingyo | 0:20e026e254d6 | 4 | #include "mbed.h" |
kangmingyo | 0:20e026e254d6 | 5 | |
kangmingyo | 0:20e026e254d6 | 6 | #define PWMOFFSET 512 // pwm @ Duty 50% |
kangmingyo | 0:20e026e254d6 | 7 | #define MaxBuf 255 // buffer size to store the speed data |
kangmingyo | 0:20e026e254d6 | 8 | |
kangmingyo | 0:20e026e254d6 | 9 | class MotorCtl |
kangmingyo | 0:20e026e254d6 | 10 | { |
kangmingyo | 0:20e026e254d6 | 11 | public: |
kangmingyo | 0:20e026e254d6 | 12 | |
kangmingyo | 0:20e026e254d6 | 13 | MotorCtl(PinName Pwm, PinName Dir, PinName tachoA, PinName tachoB); |
kangmingyo | 0:20e026e254d6 | 14 | |
kangmingyo | 0:20e026e254d6 | 15 | ~MotorCtl(); |
kangmingyo | 0:20e026e254d6 | 16 | |
kangmingyo | 0:20e026e254d6 | 17 | int getRPM(); |
kangmingyo | 0:20e026e254d6 | 18 | int getTarget(); |
kangmingyo | 0:20e026e254d6 | 19 | int getError(); |
kangmingyo | 0:20e026e254d6 | 20 | float getKP(); |
kangmingyo | 0:20e026e254d6 | 21 | float getKI(); |
kangmingyo | 0:20e026e254d6 | 22 | float getKD(); |
kangmingyo | 0:20e026e254d6 | 23 | int *getHistory(); |
kangmingyo | 0:20e026e254d6 | 24 | int getCurrentPosition(); |
kangmingyo | 0:20e026e254d6 | 25 | |
kangmingyo | 0:20e026e254d6 | 26 | void SetPeriod(long pwmPeriod); // set pwm period |
kangmingyo | 0:20e026e254d6 | 27 | void setTarget(int spd); |
kangmingyo | 0:20e026e254d6 | 28 | void setPID(float p, float i, float d); |
kangmingyo | 0:20e026e254d6 | 29 | void setDirection(); // set the direction |
kangmingyo | 0:20e026e254d6 | 30 | |
kangmingyo | 0:20e026e254d6 | 31 | //Control Methods |
kangmingyo | 0:20e026e254d6 | 32 | void UpdateCurrentPosition(); |
kangmingyo | 0:20e026e254d6 | 33 | void PIDControl(); |
kangmingyo | 0:20e026e254d6 | 34 | void Reset(void); |
kangmingyo | 0:20e026e254d6 | 35 | |
kangmingyo | 0:20e026e254d6 | 36 | |
kangmingyo | 0:20e026e254d6 | 37 | private: |
kangmingyo | 0:20e026e254d6 | 38 | PwmOut _pwm; |
kangmingyo | 0:20e026e254d6 | 39 | DigitalOut _Dir; |
kangmingyo | 0:20e026e254d6 | 40 | DigitalInOut _tachoA; |
kangmingyo | 0:20e026e254d6 | 41 | DigitalInOut _tachoB; |
kangmingyo | 0:20e026e254d6 | 42 | Ticker _tick; |
kangmingyo | 0:20e026e254d6 | 43 | |
kangmingyo | 0:20e026e254d6 | 44 | long CurrentPosition, PreviousPosition; // Position Value |
kangmingyo | 0:20e026e254d6 | 45 | int TargetSpeed, CurrentSpeed, Error,PreviousError; // Speed Data |
kangmingyo | 0:20e026e254d6 | 46 | unsigned int duty; // Duty level ( 0-1024) |
kangmingyo | 0:20e026e254d6 | 47 | unsigned int pwmPeriod; // default 50ms |
kangmingyo | 0:20e026e254d6 | 48 | unsigned char pwmpin; // PB2 ( 10 ) |
kangmingyo | 0:20e026e254d6 | 49 | unsigned char ppin, npin,dr; // dr =10 means ccwise, dr=0 means clockwise |
kangmingyo | 0:20e026e254d6 | 50 | unsigned int CntPerRev; |
kangmingyo | 0:20e026e254d6 | 51 | unsigned int DeltaT; |
kangmingyo | 0:20e026e254d6 | 52 | unsigned char PreviousEncode; |
kangmingyo | 0:20e026e254d6 | 53 | float kp, ki, kd; |
kangmingyo | 0:20e026e254d6 | 54 | int integ, derv,control,cw0[MaxBuf]; |
kangmingyo | 0:20e026e254d6 | 55 | unsigned char cpidx; |
kangmingyo | 0:20e026e254d6 | 56 | float CalculateRPM(int DeltaCnt); // Calculate RPM value |
kangmingyo | 1:911f5a86c105 | 57 | float vel; |
kangmingyo | 0:20e026e254d6 | 58 | |
kangmingyo | 0:20e026e254d6 | 59 | }; |
kangmingyo | 0:20e026e254d6 | 60 | #endif |
kangmingyo | 0:20e026e254d6 | 61 | |
kangmingyo | 0:20e026e254d6 | 62 | |
kangmingyo | 0:20e026e254d6 | 63 |