Cristian Castro
/
D04-F407_Encoder1
Uso de la clase Motor para el control de velocidad de un motor continuo con encoder.
Motor.cpp@0:75421f27540e, 2021-09-03 (annotated)
- Committer:
- CCastrop1012
- Date:
- Fri Sep 03 05:04:12 2021 +0000
- Revision:
- 0:75421f27540e
Uso de la clase Motor para control de velocidad de motor continuo con encoder.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CCastrop1012 | 0:75421f27540e | 1 | #include "mbed.h" |
CCastrop1012 | 0:75421f27540e | 2 | #include "Motor.h" |
CCastrop1012 | 0:75421f27540e | 3 | |
CCastrop1012 | 0:75421f27540e | 4 | |
CCastrop1012 | 0:75421f27540e | 5 | MotorContinuo::MotorContinuo(PinName _L1, PinName _L2, PinName _speedPin, PinName _encodin, PinName _PosInicial, int _EncodPulses) : |
CCastrop1012 | 0:75421f27540e | 6 | L1(_L1), L2(_L2), speedPin(_speedPin), encodin(_encodin), PosInicial(_PosInicial), EncodPulses(_EncodPulses) |
CCastrop1012 | 0:75421f27540e | 7 | { |
CCastrop1012 | 0:75421f27540e | 8 | speedPin.period_ms(2); |
CCastrop1012 | 0:75421f27540e | 9 | speedPin.write(0); |
CCastrop1012 | 0:75421f27540e | 10 | |
CCastrop1012 | 0:75421f27540e | 11 | }; |
CCastrop1012 | 0:75421f27540e | 12 | |
CCastrop1012 | 0:75421f27540e | 13 | void MotorContinuo::Forward() { L1=1; L2=0;} |
CCastrop1012 | 0:75421f27540e | 14 | void MotorContinuo::Back() { L1=0; L2=1;} |
CCastrop1012 | 0:75421f27540e | 15 | void MotorContinuo::Stop() { L1=0; L2=0;} |
CCastrop1012 | 0:75421f27540e | 16 | void MotorContinuo::StopT() { L1=1; L2=1;} |
CCastrop1012 | 0:75421f27540e | 17 | void MotorContinuo::SpeedDuty(int v) { speedPin.write(float(v/100.0));} |
CCastrop1012 | 0:75421f27540e | 18 | void MotorContinuo::SpinLength_ms(float t) { t++;}// Duración del giro en ms |
CCastrop1012 | 0:75421f27540e | 19 | void MotorContinuo::SpinLength(float t) { t++;}; |
CCastrop1012 | 0:75421f27540e | 20 | |
CCastrop1012 | 0:75421f27540e | 21 | |
CCastrop1012 | 0:75421f27540e | 22 | |
CCastrop1012 | 0:75421f27540e | 23 | MotorDiscreto::MotorDiscreto(PinName _Dir, PinName _Step, int _NPasos, PinName _encodin, PinName _PosInicial, int _EncodPulses) : |
CCastrop1012 | 0:75421f27540e | 24 | Dir(_Dir), Step(_Step), NPasos(_NPasos), encodin(_encodin), PosInicial(_PosInicial), EncodPulses(_EncodPulses) |
CCastrop1012 | 0:75421f27540e | 25 | { |
CCastrop1012 | 0:75421f27540e | 26 | |
CCastrop1012 | 0:75421f27540e | 27 | |
CCastrop1012 | 0:75421f27540e | 28 | |
CCastrop1012 | 0:75421f27540e | 29 | }; |
CCastrop1012 | 0:75421f27540e | 30 | |
CCastrop1012 | 0:75421f27540e | 31 | void MotorDiscreto::moveMotor(void) |
CCastrop1012 | 0:75421f27540e | 32 | { |
CCastrop1012 | 0:75421f27540e | 33 | if (StepOnHold != 0) |
CCastrop1012 | 0:75421f27540e | 34 | { |
CCastrop1012 | 0:75421f27540e | 35 | Step = !Step; // Se hace elcambio de estado en el pin Step |
CCastrop1012 | 0:75421f27540e | 36 | entradas++; // se registra cada paso efectivo el cual es valido cada 2 entradas |
CCastrop1012 | 0:75421f27540e | 37 | // 1ra entrada: tiempo en Bajo |
CCastrop1012 | 0:75421f27540e | 38 | // 2da entrada: tiempo en Alto |
CCastrop1012 | 0:75421f27540e | 39 | // Es decir cuando Step cumple un periodo. |
CCastrop1012 | 0:75421f27540e | 40 | if (entradas >= 2) // cuando se registran 2 entradas se disminuye un paso |
CCastrop1012 | 0:75421f27540e | 41 | { |
CCastrop1012 | 0:75421f27540e | 42 | StepOnHold--; // Se elimina 1 paso de los pendientes |
CCastrop1012 | 0:75421f27540e | 43 | entradas = 0; |
CCastrop1012 | 0:75421f27540e | 44 | } |
CCastrop1012 | 0:75421f27540e | 45 | |
CCastrop1012 | 0:75421f27540e | 46 | Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); // Reiniciamos el tiempo de imterrupcion |
CCastrop1012 | 0:75421f27540e | 47 | |
CCastrop1012 | 0:75421f27540e | 48 | } |
CCastrop1012 | 0:75421f27540e | 49 | else if (StepOnHold == 0) |
CCastrop1012 | 0:75421f27540e | 50 | { |
CCastrop1012 | 0:75421f27540e | 51 | Move.detach(); |
CCastrop1012 | 0:75421f27540e | 52 | } |
CCastrop1012 | 0:75421f27540e | 53 | } |
CCastrop1012 | 0:75421f27540e | 54 | |
CCastrop1012 | 0:75421f27540e | 55 | |
CCastrop1012 | 0:75421f27540e | 56 | void MotorDiscreto::Forward() { Dir=1;} |
CCastrop1012 | 0:75421f27540e | 57 | void MotorDiscreto::Back() { Dir=0;} |
CCastrop1012 | 0:75421f27540e | 58 | void MotorDiscreto::Stop() { EnablePin = 0; } |
CCastrop1012 | 0:75421f27540e | 59 | void MotorDiscreto::StopT() { Move.detach();} |
CCastrop1012 | 0:75421f27540e | 60 | void MotorDiscreto::StepFreq(int n) { StepsBySecond = n; TStep = (1/n);} |
CCastrop1012 | 0:75421f27540e | 61 | void MotorDiscreto::RunStep(long n){ StepOnHold = n; Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); }// Duración del giro en ms |
CCastrop1012 | 0:75421f27540e | 62 | void MotorDiscreto::Run(float t) { StepOnHold = long(t/TStep); Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); } |
CCastrop1012 | 0:75421f27540e | 63 | void MotorDiscreto::RunRound(int n) { StepOnHold = (NPasos * n); Move.attach(callback(this,&MotorDiscreto::moveMotor), (TStep/2) ); } |
CCastrop1012 | 0:75421f27540e | 64 | |
CCastrop1012 | 0:75421f27540e | 65 | void Ustep(int resolucion, DigitalOut* _M0, DigitalOut* _M1, DigitalOut* _M2) |
CCastrop1012 | 0:75421f27540e | 66 | { |
CCastrop1012 | 0:75421f27540e | 67 | |
CCastrop1012 | 0:75421f27540e | 68 | switch(resolucion) |
CCastrop1012 | 0:75421f27540e | 69 | { |
CCastrop1012 | 0:75421f27540e | 70 | case 1: *_M0 = 0; *_M1 = 0; *_M2 = 0; |
CCastrop1012 | 0:75421f27540e | 71 | break; |
CCastrop1012 | 0:75421f27540e | 72 | case 2: *_M0 = 1; *_M1 = 0; *_M2 = 0; |
CCastrop1012 | 0:75421f27540e | 73 | break; |
CCastrop1012 | 0:75421f27540e | 74 | case 4: *_M0 = 0; *_M1 = 1; *_M2 = 0; |
CCastrop1012 | 0:75421f27540e | 75 | break; |
CCastrop1012 | 0:75421f27540e | 76 | case 8: *_M0 = 1; *_M1 = 1; *_M2 = 1; |
CCastrop1012 | 0:75421f27540e | 77 | break; |
CCastrop1012 | 0:75421f27540e | 78 | case 16: *_M0 = 0; *_M1 = 0; *_M2 = 1; |
CCastrop1012 | 0:75421f27540e | 79 | break; |
CCastrop1012 | 0:75421f27540e | 80 | case 32: *_M0 = 1; *_M1 = 0; *_M2 = 1; |
CCastrop1012 | 0:75421f27540e | 81 | break; |
CCastrop1012 | 0:75421f27540e | 82 | |
CCastrop1012 | 0:75421f27540e | 83 | default: *_M0 = 0; *_M1 = 0; *_M2 = 0; |
CCastrop1012 | 0:75421f27540e | 84 | |
CCastrop1012 | 0:75421f27540e | 85 | |
CCastrop1012 | 0:75421f27540e | 86 | } |
CCastrop1012 | 0:75421f27540e | 87 | |
CCastrop1012 | 0:75421f27540e | 88 | |
CCastrop1012 | 0:75421f27540e | 89 | } |
CCastrop1012 | 0:75421f27540e | 90 | |
CCastrop1012 | 0:75421f27540e | 91 | |
CCastrop1012 | 0:75421f27540e | 92 | /* |
CCastrop1012 | 0:75421f27540e | 93 | void scolor_TCS3200::SetMode(uint8_t mode) { |
CCastrop1012 | 0:75421f27540e | 94 | switch (mode){ |
CCastrop1012 | 0:75421f27540e | 95 | case SCALE_100: _s0= 1; _s1=1; break; |
CCastrop1012 | 0:75421f27540e | 96 | case SCALE_20: _s0=1 ; _s1=0; break; |
CCastrop1012 | 0:75421f27540e | 97 | case SCALE_2: _s0=0 ; _s1=1; break; |
CCastrop1012 | 0:75421f27540e | 98 | case POWER_DOWN: _s0=0 ; _s1=0; break; |
CCastrop1012 | 0:75421f27540e | 99 | } |
CCastrop1012 | 0:75421f27540e | 100 | }; |
CCastrop1012 | 0:75421f27540e | 101 | |
CCastrop1012 | 0:75421f27540e | 102 | |
CCastrop1012 | 0:75421f27540e | 103 | long scolor_TCS3200::pulsewidth() { |
CCastrop1012 | 0:75421f27540e | 104 | while(!_s_in); |
CCastrop1012 | 0:75421f27540e | 105 | timer.start(); |
CCastrop1012 | 0:75421f27540e | 106 | while(_s_in); |
CCastrop1012 | 0:75421f27540e | 107 | timer.stop(); |
CCastrop1012 | 0:75421f27540e | 108 | float pulsewidth_v = timer.read_us(); |
CCastrop1012 | 0:75421f27540e | 109 | timer.reset(); |
CCastrop1012 | 0:75421f27540e | 110 | return pulsewidth_v; |
CCastrop1012 | 0:75421f27540e | 111 | }; |
CCastrop1012 | 0:75421f27540e | 112 | */ |
CCastrop1012 | 0:75421f27540e | 113 |