Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
motor.h
- Committer:
- kangmingyo
- Date:
- 2019-07-10
- Revision:
- 7:42c478f9a1fe
- Parent:
- 6:366ec55e64fe
File content as of revision 7:42c478f9a1fe:
#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();
float CalculateCumDis();
float CalculateRelaDis();
float CalculateVelocity();
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, DistancePosition; // Position Value
int CurrentSpeed, Error,PreviousError; // Speed Data
float 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
float vel;
int TargetSpeed;
};
#endif