Kiko Ishimoto / MotorClass

Dependencies:  

Dependents:   MotorDriveDEBUG

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MotorClass.h Source File

MotorClass.h

00001 #ifndef MotorClass_H
00002 #define MotorClass_H
00003 
00004 #include "mbed.h"
00005 
00006 #define MotorMode        0x04
00007 #define MotorState        0x0e
00008 #define Who_am_I          0x2
00009 #define MotorPWML          0x0f
00010 #define MotorPWMH          0x10
00011 #define MoterRevolutionL   0x11
00012 #define MoterRevolutionH   0x12
00013 #define MoterPulseL 0x13
00014 #define MoterPulseH 0x14
00015 #define MoterSpeedL 0x15
00016 #define MoterSpeedH 0x16
00017 #define Automatic   0x0
00018 #define MotorP 0x30
00019 #define MotorI 0x31
00020 #define MotorD 0x32
00021 #define FinishAuto 0x00
00022 #define TargetSpeedH 0x24
00023 #define TargetSpeedL 0x23
00024 #define TargetAngleH 0x22
00025 #define TargetAngleL 0x21
00026 #define PulsePerRev 0x18
00027 #define PulsePerRev2 0x19
00028 #define PulsePerRevolutionL 0x25
00029 #define PulsePerRevolutionH 0x26
00030 #define PulsePerSec 0x27
00031 #define PulsePerSec2 0x28
00032 #define PulsePerAngle 0x20
00033 #define RotateMode  0x17
00034 #define Randam      0x3b
00035 #define ADDRESS     0x3a
00036 #define Debug1H     0x70
00037 #define Debug1L     0x71
00038 #define Debug2H     0x72
00039 #define Debug2L     0x73
00040 #define Debug3H     0x74
00041 #define Debug3L     0x75
00042 
00043 #define Front 1
00044 #define Back 2
00045 #define Stop 3
00046 #define Free 4
00047 
00048 class MotorClass
00049 {
00050     I2C& i2c;
00051     QEI& wheel;
00052     Ticker QeiInt;
00053     private:
00054     
00055         char QEIMode;
00056         int AngOrSpe;
00057         char addr;
00058         char Action;
00059         void attach()
00060         {    
00061             double i = 0;
00062             if(AngOrSpe == 0)
00063                 i=wheel.getSumangle();
00064             if(AngOrSpe == 1)
00065                 i=wheel.getRPM()*1000;
00066             char reg[2] = {PulsePerRev,PulsePerSec};
00067             char data[2]={((short)i>>8),((short)i&0xff)};
00068             OutputSomeData(reg[AngOrSpe],data,2);
00069         printf("motor speed or angle is %f \r\n",i);
00070         }
00071         bool OutputOneData(char reg, char data )
00072         {
00073             char DATA[2]={reg,1};
00074             bool N  = i2c.write(addr,DATA,2); 
00075                  N |= i2c.write(addr,&data,1);
00076             return N;
00077         }
00078         bool OutputSomeData(char reg, char *data ,int size )
00079         {
00080             char DATA[2] = {reg,size};    
00081             bool N = i2c.write(addr,DATA,2);
00082                  N|= i2c.write(addr,data,size);
00083             return N;
00084         }
00085         char InputOneData(char address){   
00086            char DATA[2] = {address,1};
00087            char output; 
00088             i2c.write( addr , DATA, 2);  //tell it what you want to read
00089             i2c.read( addr , &output, 1);    //tell it where to store the data
00090             return output; 
00091         }
00092         void InputSomeData(char address, char* output, int size) {
00093             char DATA[2] = {address,size};
00094             i2c.write( addr, DATA, 2);  //tell it where to read from
00095             i2c.read( addr , output, size);      //tell it where to store the data read
00096         }
00097         float PWM;
00098         short target;
00099     public: 
00100         void UseAddress(char ad);
00101         float interval;
00102         MotorClass(I2C& _i2c,QEI& _qei);
00103         char SetAddress(char ad);
00104         char GetAddress();
00105         bool SetRate(char rate);
00106         bool Motor_PWM(float pwm);
00107         bool Setup(char action);
00108         bool Motor(char state,float pwm);
00109         bool SetP(float p);
00110         bool SetI(float i);
00111         bool SetD(float d);
00112         bool SetPID(float *pid);
00113         bool SetTarget(short tar);
00114         void StartQEIPut();
00115         void StopQEIPut();
00116         void QEIAllSetup(short tar,float *pid);
00117         void GetDebugData(float *data,int mode);
00118         float operator++(int) {        // 後置きの場合ダミーの引数 int をつける。
00119             
00120             if(Action == 1)
00121             {
00122                 PWM+=0.01;
00123                 Motor_PWM(PWM);
00124                 return PWM;
00125             }
00126             else 
00127             {
00128                 target+=10;
00129                 SetTarget(short(target)); 
00130                 return (float)target;
00131             }
00132         }
00133         short operator--(int) {        // 後置きの場合ダミーの引数 int をつける。
00134             
00135             if(Action == 1)
00136             {
00137                 PWM-=0.01;
00138                 Motor_PWM(PWM);
00139                 return PWM;
00140             }
00141             else 
00142             {
00143                 target-=10;
00144                 SetTarget(short(target)); 
00145                 return (float)target;
00146             }
00147         }
00148         void operator=(float val)
00149         {    
00150             if(Action == 1)
00151             {
00152                 if(val<0)Motor(Back ,val*-1);
00153                 else if(val>0)Motor(Front,val);
00154                 else Motor(Stop , val);
00155             }
00156             else 
00157             {
00158                 SetTarget(short(val)); 
00159             }
00160         }
00161 };
00162 #endif