Cristian Castro
/
D04-F407_Encoder1
Uso de la clase Motor para el control de velocidad de un motor continuo con encoder.
Revision 0:75421f27540e, committed 2021-09-03
- Comitter:
- CCastrop1012
- Date:
- Fri Sep 03 05:04:12 2021 +0000
- Commit message:
- Uso de la clase Motor para control de velocidad de motor continuo con encoder.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.cpp Fri Sep 03 05:04:12 2021 +0000 @@ -0,0 +1,113 @@ +#include "mbed.h" +#include "Motor.h" + + +MotorContinuo::MotorContinuo(PinName _L1, PinName _L2, PinName _speedPin, PinName _encodin, PinName _PosInicial, int _EncodPulses) : +L1(_L1), L2(_L2), speedPin(_speedPin), encodin(_encodin), PosInicial(_PosInicial), EncodPulses(_EncodPulses) +{ + speedPin.period_ms(2); + speedPin.write(0); + +}; + +void MotorContinuo::Forward() { L1=1; L2=0;} +void MotorContinuo::Back() { L1=0; L2=1;} +void MotorContinuo::Stop() { L1=0; L2=0;} +void MotorContinuo::StopT() { L1=1; L2=1;} +void MotorContinuo::SpeedDuty(int v) { speedPin.write(float(v/100.0));} +void MotorContinuo::SpinLength_ms(float t) { t++;}// Duración del giro en ms +void MotorContinuo::SpinLength(float t) { t++;}; + + + +MotorDiscreto::MotorDiscreto(PinName _Dir, PinName _Step, int _NPasos, PinName _encodin, PinName _PosInicial, int _EncodPulses) : +Dir(_Dir), Step(_Step), NPasos(_NPasos), encodin(_encodin), PosInicial(_PosInicial), EncodPulses(_EncodPulses) +{ + + + +}; + +void MotorDiscreto::moveMotor(void) + { + if (StepOnHold != 0) + { + Step = !Step; // Se hace elcambio de estado en el pin Step + entradas++; // se registra cada paso efectivo el cual es valido cada 2 entradas + // 1ra entrada: tiempo en Bajo + // 2da entrada: tiempo en Alto + // Es decir cuando Step cumple un periodo. + if (entradas >= 2) // cuando se registran 2 entradas se disminuye un paso + { + StepOnHold--; // Se elimina 1 paso de los pendientes + entradas = 0; + } + + Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); // Reiniciamos el tiempo de imterrupcion + + } + else if (StepOnHold == 0) + { + Move.detach(); + } + } + + +void MotorDiscreto::Forward() { Dir=1;} +void MotorDiscreto::Back() { Dir=0;} +void MotorDiscreto::Stop() { EnablePin = 0; } +void MotorDiscreto::StopT() { Move.detach();} +void MotorDiscreto::StepFreq(int n) { StepsBySecond = n; TStep = (1/n);} +void MotorDiscreto::RunStep(long n){ StepOnHold = n; Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); }// Duración del giro en ms +void MotorDiscreto::Run(float t) { StepOnHold = long(t/TStep); Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); } +void MotorDiscreto::RunRound(int n) { StepOnHold = (NPasos * n); Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); } + +void Ustep(int resolucion, DigitalOut* _M0, DigitalOut* _M1, DigitalOut* _M2) + { + + switch(resolucion) + { + case 1: *_M0 = 0; *_M1 = 0; *_M2 = 0; + break; + case 2: *_M0 = 1; *_M1 = 0; *_M2 = 0; + break; + case 4: *_M0 = 0; *_M1 = 1; *_M2 = 0; + break; + case 8: *_M0 = 1; *_M1 = 1; *_M2 = 1; + break; + case 16: *_M0 = 0; *_M1 = 0; *_M2 = 1; + break; + case 32: *_M0 = 1; *_M1 = 0; *_M2 = 1; + break; + + default: *_M0 = 0; *_M1 = 0; *_M2 = 0; + + + } + + + } + + + /* +void scolor_TCS3200::SetMode(uint8_t mode) { + switch (mode){ + case SCALE_100: _s0= 1; _s1=1; break; + case SCALE_20: _s0=1 ; _s1=0; break; + case SCALE_2: _s0=0 ; _s1=1; break; + case POWER_DOWN: _s0=0 ; _s1=0; break; + } +}; + + +long scolor_TCS3200::pulsewidth() { + while(!_s_in); + timer.start(); + while(_s_in); + timer.stop(); + float pulsewidth_v = timer.read_us(); + timer.reset(); + return pulsewidth_v; +}; +*/ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.h Fri Sep 03 05:04:12 2021 +0000 @@ -0,0 +1,102 @@ +#ifndef Motor_H +#define Motor_H +#include "mbed.h" + /* ************************************************************************** + +@CCastrop +cristiank.castrop@ecci.edu.co + + L1 Terminal A del motor + L2 Terminal B del motor + SpeedPin Pin de salida PWM + + +******************************************************************************/ + + +class MotorContinuo { + public: + /// Constructores para Motores Continuos + MotorContinuo(PinName _L1, PinName _L2, PinName _speedPin, PinName _encodin, PinName _PosInicial, int _EncodPulses); + + void Forward(); // Da un sentido de giro al motor + void Back(); // Genera un sentido de giro contrario al de Forward + void Stop(); // Detiene el motor dejando el movimiento libre + void StopT(); // Detiene el motor truncando o enclavando el movimiento(Lo mantiene quieto). + void SpeedDuty(int v); // Varia la velocidad de giro del motor de 0 a 100% + void SpinLength_ms(float t); // Duración del giro en ms + void SpinLength(float t); // Duración del giro + + private: + + DigitalOut L1; + DigitalOut L2; + PwmOut speedPin; + DigitalIn encodin; + DigitalIn PosInicial; + int EncodPulses; + + + + +}; + + +class MotorDiscreto { + public: + /// Constructores para Motores Continuos + MotorDiscreto(PinName _Dir, PinName _Step, int _NPasos, PinName _encodin, PinName _PosInicial, int _EncodPulses); + MotorDiscreto(void); + void Forward(); // Da un sentido de giro al motor + void Back(); // Genera un sentido de giro contrario al de Forward + void Stop(); // Detiene el motor dejando el movimiento libre. Pin Enable = 0 + void StopT(); // Detiene el motor truncando o enclavando el movimiento(Lo mantiene quieto). + void StepFreq(int n); // Configura la Velocidad en Número(n) de pasos por segundos. Es decir la Frecuencia de paso. + void RunStep(long n); // Se mueve el motor la cantidad de pasos ingresada + void Run(float t); // Gira el motor durante el tiempo ingresado, si es 0 indefinidamente hasta llamada a Stop(). + void RunRound(int n); // Girar n vueltas + void Ustep(int resolucion, PinName M0, PinName M1, PinName M2); + // Configura los pasos a 1/2, 1/4, 1/8, 1/16, 1/32 + + + long StepOnHold; // Pasos faltantes por ejecutar + long StepsBySecond; // Pasos por segundo + + private: + + void moveMotor(void); + + DigitalOut Dir; + DigitalOut Step; + int NPasos; + DigitalIn encodin; + DigitalIn PosInicial; + int EncodPulses; + + DigitalOut* EnablePin; + DigitalOut* M0; + DigitalOut* M1; + DigitalOut* M2; + + Ticker Move; /// Timer de interrupcion + float TStep; /// periodo del paso TStep = 1/StepsBySecond; + + int entradas; /// registra cada 2 ingresos a la interrupcion + +}; +#endif + + + + + + + + + + + + + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Sep 03 05:04:12 2021 +0000 @@ -0,0 +1,61 @@ +#include "mbed.h" +#include "Motor.h" + +Serial Mbed(PB_6, PB_7); +//Serial Mbed(PA_2, PA_3); +InterruptIn Encoder(PC_9); +DigitalOut LED(PA_5); +DigitalIn Button(PA_0); +MotorContinuo Mizq(PD_13, PD_11, PB_15, NC,NC,0); + + +long x = 0; +long muestras[200]; +int t = 10; +int escalon_1 = 10, escalon_2 = 80, escalon_3 = 30, escalon_4 = 50, escalon_5 = 65, escalon_6 = 5, escalon_7 = 20, escalon_8 = 40; + + + + + +void interrupt() +{ + x ++; + +} + + + +int main() { + + Mbed.printf("Hello World\n!"); + Encoder.rise(&interrupt); + + long temp = 0; + int duty = 10; + + + + while(1) + { + + //////// Giro sentido 1 + + Mbed.printf("Motor Velocidad = %d%% : ",duty); + LED = 1; + + Mizq.Forward(); + Mizq.SpeedDuty(duty); + x = 0; temp = 0; + wait(1); + temp = x; + Mbed.printf("PPS = %d : \n",temp); + LED = 0; + if(Button == 0) { duty++; if(duty > 100) duty = 10; } + wait(1); + + + + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Sep 03 05:04:12 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc \ No newline at end of file