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.
MMouse18.h
00001 00002 #include "mbed.h" 00003 00004 class Motor { 00005 public: 00006 Motor(PinName pwm, PinName fwd, PinName rev); 00007 void SetBrake(int aOnOff); 00008 void SetPow(float aPow); 00009 void SetPow2(float aPow); 00010 protected: 00011 PwmOut _pwm; 00012 DigitalOut _fwd; 00013 DigitalOut _rev; 00014 int16_t _running; 00015 }; 00016 00017 // Motor mL(P1_19,P2_15,P2_14); Motor mR(P2_19,P2_20,P2_21); 00018 00019 Motor::Motor(PinName pwm, PinName fwd, PinName rev) : 00020 _pwm(pwm), _fwd(fwd), _rev(rev) 00021 { 00022 _pwm.period(0.001); _pwm=0; 00023 _fwd=0; _rev=0; _running=0; 00024 } 00025 00026 void Motor::SetBrake(int aOnOff) 00027 { 00028 _pwm=0; 00029 if( aOnOff ) 00030 { _fwd=0; _rev=0; } 00031 else 00032 { _fwd=1; _rev=1; } 00033 } 00034 00035 void Motor::SetPow(float aPow) 00036 { 00037 if( aPow==0 ) { 00038 _pwm=0; _running=0; return; 00039 } 00040 if( aPow>=0.0 ) { 00041 _fwd=1; _rev=0; 00042 _pwm = aPow; 00043 } 00044 else { 00045 _fwd=0; _rev=1; 00046 _pwm = -aPow; 00047 } 00048 _running=1; 00049 } 00050 00051 void Motor::SetPow2(float aPow) 00052 { 00053 float pow; 00054 if( aPow==0 ) { 00055 _pwm=0; _running=0; return; 00056 } 00057 if( aPow>=0.0 ) { 00058 _fwd=1; _rev=0; 00059 pow = aPow; 00060 } 00061 else { 00062 _fwd=0; _rev=1; 00063 pow = -aPow; 00064 } 00065 if( !_running && (pow<0.3) ) { 00066 _pwm = 0.3; 00067 wait_ms(20); // 50 00068 } 00069 _pwm = pow; 00070 _running = 1; 00071 } 00072 00073 00074 class AnalogInHL2 : public AnalogIn { 00075 public: 00076 AnalogInHL2(PinName pin) : AnalogIn(pin) {} 00077 int Read() 00078 { return read_u16()>>4; } 00079 }; 00080 00081 00082 00083 const int ENC_A = 1; 00084 const int ENC_B = 2; 00085 const int ENC_RISE = 1; 00086 const int ENC_FALL = 2; 00087 00088 class Encoder { 00089 public: 00090 Encoder(PinName sensA, PinName sensB) : 00091 _sensA(sensA), _sensB(sensB) 00092 { cnt=0; } 00093 00094 void Init(int aAB, int aRiseFall) 00095 { 00096 if( aAB & ENC_A ) { 00097 if( aRiseFall & ENC_RISE ) 00098 _sensA.rise(this, &Encoder::ISR_A); 00099 if( aRiseFall & ENC_FALL ) 00100 _sensA.fall(this, &Encoder::ISR_A); 00101 } 00102 if( aAB & ENC_B ) { 00103 if( aRiseFall & ENC_RISE ) 00104 _sensB.rise(this, &Encoder::ISR_B); 00105 if( aRiseFall & ENC_FALL ) 00106 _sensB.fall(this, &Encoder::ISR_B); 00107 } 00108 } 00109 00110 void ISR_A() 00111 { 00112 if( _sensB.read() ) 00113 cnt++; 00114 else 00115 cnt--; 00116 } 00117 00118 void ISR_B() 00119 { 00120 if( _sensA.read() ) 00121 cnt++; 00122 else 00123 cnt--; 00124 } 00125 public: 00126 uint32_t cnt; 00127 public: 00128 InterruptIn _sensA; 00129 InterruptIn _sensB; 00130 }; 00131 00132 00133 00134 00135
Generated on Wed Jul 13 2022 02:59:11 by
