00001 #include "motor.h" 00002 #include "scorbot.h" 00003 00004 Serial g_external_pwm(p28,p27); 00005 00006 Motor::Motor(bool internal_pwm, uint8_t external_speed_pin, 00007 PinName internal_speed_pin, PinName microswitch_pin, 00008 PinMode microswitch_mode) : 00009 speed_(internal_speed_pin), 00010 microswitch_(microswitch_pin) 00011 { 00012 speed_.period_us(PWM_PERIOD); 00013 00014 if (microswitch_mode != PullNone) 00015 microswitch_.mode(microswitch_mode); 00016 00017 g_external_pwm.baud(115200); 00018 00019 internal_pwm_ = internal_pwm; 00020 external_speed_pin_ = external_speed_pin; 00021 external_speed_ = 0; 00022 update_in_progress_ = false; 00023 encoder_ = 0; 00024 desired_move_duration_ = 0; 00025 desired_distance_ = 0; 00026 desired_encoder_ = 0; 00027 } 00028 00029 Motor::~Motor() 00030 { 00031 } 00032 00033 uint8_t Motor::readMicroswitch() 00034 { 00035 return !microswitch_; 00036 } 00037 00038 long Motor::readEncoder() 00039 { 00040 return encoder_; 00041 } 00042 00043 float Motor::readPWMDuty() 00044 { 00045 if (internal_pwm_) 00046 return 2.0*speed_ - 1.0; 00047 else 00048 return ((float)external_speed_)/127.0 - 1.0; 00049 } 00050 00051 void Motor::setPWMDuty(float duty) 00052 { 00053 if(duty > 1.0) duty = 1.0; 00054 else if(duty < -1.0) duty = -1.0; 00055 00056 if (internal_pwm_) 00057 speed_ = duty/2.0 + 0.5; 00058 else 00059 { 00060 external_speed_ = (uint8_t) ((duty + 1.0)*127.0); 00061 //Scorbot::setExternalPWMDuty(external_speed_pin_, external_speed_); 00062 g_external_pwm.printf("A%c%cZ", external_speed_pin_, external_speed_); 00063 //wait_us(200); 00064 } 00065 } 00066 00067 void Motor::setEncoder(long position) 00068 { 00069 encoder_ = position; 00070 } 00071 00072 void Motor::setDestination(long encoder_position, long duration_ms) 00073 { 00074 if (duration_ms > 0) 00075 { 00076 desired_encoder_ = encoder_position; 00077 desired_distance_ = encoder_position - encoder_; 00078 desired_move_duration_ = ((float) duration_ms) / (1000.0); 00079 move_duration_.start(); 00080 } 00081 }