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.
Dependents: mbed_ES410_simpleSpeedMeasurement mbed_ES20X_V21_Tester
MotCon.cpp
- Committer:
- jebradshaw
- Date:
- 2016-11-01
- Revision:
- 9:98c700c7dc30
- Parent:
- 8:67f2711fdeed
File content as of revision 9:98c700c7dc30:
#include "mbed.h" #include "MotCon.h" //Constructor MotCon::MotCon(PinName pwm_pin, PinName dir1_pin) : _pwm_pin(pwm_pin), _dir1_pin(dir1_pin), _dir2_pin(NC) { _dir2 = false; _pwm_pin.period_us(50); _pwm_pin = 0.0; _dir1_pin = 0; this->duty_cycle=0.0; } MotCon::MotCon(PinName pwm_pin, PinName dir1_pin, PinName dir2_pin) : _pwm_pin(pwm_pin), _dir1_pin(dir1_pin), _dir2_pin(dir2_pin) { _dir2 = true; _pwm_pin.period_us(50); _pwm_pin = 0.0; _dir1_pin = 0; _dir2_pin = 0; this->duty_cycle=0.0; this->mc_mode = 0; //mode pin determines braking (1 = dynamic braking, 0 = free-wheeling) } // dc is signed duty cycle (+/-1.0) void MotCon::write(float dc){ if(dc>1.0) dc=1.0; if(dc<-1.0) dc=-1.0; this->duty_cycle = dc; if(_dir2){ if(this->duty_cycle > 0.001){ _dir1_pin = 0; _dir2_pin = 1; _pwm_pin = this->duty_cycle; } else if(this->duty_cycle < -0.001){ _dir2_pin = 0; _dir1_pin = 1; _pwm_pin = abs(this->duty_cycle); } else{ if(mc_mode){ _dir1_pin = 0; _dir2_pin = 0; _pwm_pin = 1.0; } else{ _dir1_pin = 0; _dir2_pin = 0; _pwm_pin = 0.0; } } } else{ if(this->duty_cycle > 0.001){ _dir1_pin = 0; _pwm_pin = this->duty_cycle; } else if(this->duty_cycle < -0.001){ _dir1_pin = 1; _pwm_pin = abs(this->duty_cycle); } else{ _dir1_pin = 0; _pwm_pin = 0.0; } } } // dc is signed duty cycle (+/-1.0) void MotCon::write(float dc, int invert){ if(dc>1.0) dc=1.0; if(dc<-1.0) dc=-1.0; duty_cycle = dc; if(_dir2){ if(invert==0){ if(this->duty_cycle > 0.001){ _dir1_pin = 0; _dir2_pin = 1; _pwm_pin = this->duty_cycle; } else if(duty_cycle < -0.001){ _dir2_pin = 0; _dir1_pin = 1; _pwm_pin = abs(this->duty_cycle); } else{ if(mc_mode){ _dir1_pin = 0; _dir2_pin = 0; _pwm_pin = 1.0; this->duty_cycle=1.0; } else{ _dir1_pin = 0; _dir2_pin = 0; _pwm_pin = 0.0; } } } else{ if(this->duty_cycle > 0.001){ _dir2_pin = 0; _dir1_pin = 1; _pwm_pin = this->duty_cycle; } else if(this->duty_cycle < -0.001){ _dir1_pin = 0; _dir2_pin = 1; _pwm_pin = abs(this->duty_cycle); } else{ if(mc_mode){ _dir1_pin = 0; _dir2_pin = 0; _pwm_pin = 1.0; this->duty_cycle=1.0; } else{ _dir1_pin = 0; _dir2_pin = 0; _pwm_pin = 0.0; } } } } else{ if(invert==0){ if(this->duty_cycle > 0.001){ _dir1_pin = 0; _pwm_pin = this->duty_cycle; } else if(this->duty_cycle < -0.001){ _dir1_pin = 1; _pwm_pin = abs(this->duty_cycle); } else{ _dir1_pin = 0; _pwm_pin = 0.0; } } else{ if(this->duty_cycle > 0.001){ _dir1_pin = 1; _pwm_pin = this->duty_cycle; } else if(this->duty_cycle < -0.001){ _dir1_pin = 0; _pwm_pin = abs(this->duty_cycle); } else{ _dir1_pin = 0; _pwm_pin = 0.0; } } } } void MotCon::setMode(bool mode){ mc_mode = mode; } bool MotCon::getMode(void){ return mc_mode; } float MotCon::read(){ float ret = this->duty_cycle; return ret; } MotCon& MotCon::operator= (float value){ write(value); return *this; } MotCon& MotCon::operator= (MotCon& rhs) { // Underlying call is thread safe write(rhs.read()); return *this; } MotCon::operator float(){ // Underlying call is thread safe return this->read(); }