v1
Diff: motor.h
- Revision:
- 0:20e026e254d6
- Child:
- 1:911f5a86c105
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/motor.h Thu May 09 22:20:02 2019 +0000 @@ -0,0 +1,62 @@ +#ifndef MBED_MOTOR_h +#define MBED_MOTOR_h + +#include "mbed.h" + +#define PWMOFFSET 512 // pwm @ Duty 50% +#define MaxBuf 255 // buffer size to store the speed data + +class MotorCtl +{ + public: + + MotorCtl(PinName Pwm, PinName Dir, PinName tachoA, PinName tachoB); + + ~MotorCtl(); + + int getRPM(); + int getTarget(); + int getError(); + float getKP(); + float getKI(); + float getKD(); + int *getHistory(); + int getCurrentPosition(); + + void SetPeriod(long pwmPeriod); // set pwm period + void setTarget(int spd); + void setPID(float p, float i, float d); + void setDirection(); // set the direction + + //Control Methods + void UpdateCurrentPosition(); + void PIDControl(); + void Reset(void); + + + private: + PwmOut _pwm; + DigitalOut _Dir; + DigitalInOut _tachoA; + DigitalInOut _tachoB; + Ticker _tick; + + long CurrentPosition, PreviousPosition; // Position Value + int TargetSpeed, CurrentSpeed, Error,PreviousError; // Speed Data + unsigned int duty; // Duty level ( 0-1024) + unsigned int pwmPeriod; // default 50ms + unsigned char pwmpin; // PB2 ( 10 ) + unsigned char ppin, npin,dr; // dr =10 means ccwise, dr=0 means clockwise + unsigned int CntPerRev; + unsigned int DeltaT; + unsigned char PreviousEncode; + float kp, ki, kd; + int integ, derv,control,cw0[MaxBuf]; + unsigned char cpidx; + float CalculateRPM(int DeltaCnt); // Calculate RPM value + +}; +#endif + + + \ No newline at end of file