Uso de la clase Motor para el control de velocidad de un motor continuo con encoder.
Embed:
(wiki syntax)
Show/hide line numbers
Motor.cpp
00001 #include "mbed.h" 00002 #include "Motor.h" 00003 00004 00005 MotorContinuo::MotorContinuo(PinName _L1, PinName _L2, PinName _speedPin, PinName _encodin, PinName _PosInicial, int _EncodPulses) : 00006 L1(_L1), L2(_L2), speedPin(_speedPin), encodin(_encodin), PosInicial(_PosInicial), EncodPulses(_EncodPulses) 00007 { 00008 speedPin.period_ms(2); 00009 speedPin.write(0); 00010 00011 }; 00012 00013 void MotorContinuo::Forward() { L1=1; L2=0;} 00014 void MotorContinuo::Back() { L1=0; L2=1;} 00015 void MotorContinuo::Stop() { L1=0; L2=0;} 00016 void MotorContinuo::StopT() { L1=1; L2=1;} 00017 void MotorContinuo::SpeedDuty(int v) { speedPin.write(float(v/100.0));} 00018 void MotorContinuo::SpinLength_ms(float t) { t++;}// Duración del giro en ms 00019 void MotorContinuo::SpinLength(float t) { t++;}; 00020 00021 00022 00023 MotorDiscreto::MotorDiscreto(PinName _Dir, PinName _Step, int _NPasos, PinName _encodin, PinName _PosInicial, int _EncodPulses) : 00024 Dir(_Dir), Step(_Step), NPasos(_NPasos), encodin(_encodin), PosInicial(_PosInicial), EncodPulses(_EncodPulses) 00025 { 00026 00027 00028 00029 }; 00030 00031 void MotorDiscreto::moveMotor(void) 00032 { 00033 if (StepOnHold != 0) 00034 { 00035 Step = !Step; // Se hace elcambio de estado en el pin Step 00036 entradas++; // se registra cada paso efectivo el cual es valido cada 2 entradas 00037 // 1ra entrada: tiempo en Bajo 00038 // 2da entrada: tiempo en Alto 00039 // Es decir cuando Step cumple un periodo. 00040 if (entradas >= 2) // cuando se registran 2 entradas se disminuye un paso 00041 { 00042 StepOnHold--; // Se elimina 1 paso de los pendientes 00043 entradas = 0; 00044 } 00045 00046 Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); // Reiniciamos el tiempo de imterrupcion 00047 00048 } 00049 else if (StepOnHold == 0) 00050 { 00051 Move.detach(); 00052 } 00053 } 00054 00055 00056 void MotorDiscreto::Forward() { Dir=1;} 00057 void MotorDiscreto::Back() { Dir=0;} 00058 void MotorDiscreto::Stop() { EnablePin = 0; } 00059 void MotorDiscreto::StopT() { Move.detach();} 00060 void MotorDiscreto::StepFreq(int n) { StepsBySecond = n; TStep = (1/n);} 00061 void MotorDiscreto::RunStep(long n){ StepOnHold = n; Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); }// Duración del giro en ms 00062 void MotorDiscreto::Run(float t) { StepOnHold = long(t/TStep); Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); } 00063 void MotorDiscreto::RunRound(int n) { StepOnHold = (NPasos * n); Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); } 00064 00065 void Ustep(int resolucion, DigitalOut* _M0, DigitalOut* _M1, DigitalOut* _M2) 00066 { 00067 00068 switch(resolucion) 00069 { 00070 case 1: *_M0 = 0; *_M1 = 0; *_M2 = 0; 00071 break; 00072 case 2: *_M0 = 1; *_M1 = 0; *_M2 = 0; 00073 break; 00074 case 4: *_M0 = 0; *_M1 = 1; *_M2 = 0; 00075 break; 00076 case 8: *_M0 = 1; *_M1 = 1; *_M2 = 1; 00077 break; 00078 case 16: *_M0 = 0; *_M1 = 0; *_M2 = 1; 00079 break; 00080 case 32: *_M0 = 1; *_M1 = 0; *_M2 = 1; 00081 break; 00082 00083 default: *_M0 = 0; *_M1 = 0; *_M2 = 0; 00084 00085 00086 } 00087 00088 00089 } 00090 00091 00092 /* 00093 void scolor_TCS3200::SetMode(uint8_t mode) { 00094 switch (mode){ 00095 case SCALE_100: _s0= 1; _s1=1; break; 00096 case SCALE_20: _s0=1 ; _s1=0; break; 00097 case SCALE_2: _s0=0 ; _s1=1; break; 00098 case POWER_DOWN: _s0=0 ; _s1=0; break; 00099 } 00100 }; 00101 00102 00103 long scolor_TCS3200::pulsewidth() { 00104 while(!_s_in); 00105 timer.start(); 00106 while(_s_in); 00107 timer.stop(); 00108 float pulsewidth_v = timer.read_us(); 00109 timer.reset(); 00110 return pulsewidth_v; 00111 }; 00112 */ 00113
Generated on Wed Sep 7 2022 01:54:55 by
1.7.2