rejig motor code

This commit is contained in:
shrkey
2016-10-17 16:14:56 +01:00
parent c2a0c483a1
commit a49a6c66a3
2 changed files with 31 additions and 38 deletions

View File

@@ -46,7 +46,7 @@ bool DWESCAPE::initialize() {
this->pwm = new PCA9685( this->devAddr ); this->pwm = new PCA9685( this->devAddr );
this->pwm->initialize(); this->pwm->initialize();
if (!testConnection() ) { if (!testConnection() ) {
printf("No 640 board found\n"); printf("No ESCAPE board found\n");
return 0; return 0;
} }
// set the default frequency // set the default frequency
@@ -267,15 +267,15 @@ void DW_Motor::setMotorSpeed(int16_t speed) {
// Speed deciphering for the two control modes // Speed deciphering for the two control modes
if( speed >= 1000 && speed < 1500 ) { if( speed >= 1000 && speed < 1500 ) {
run( DW_REVERSE, map(speed, 1500, 1000, 0, 255 ) ); run( DW_REVERSE, speed );
} else if( speed > 1500 && speed <= 2000 ) { } else if( speed > 1500 && speed <= 2000 ) {
run( DW_FORWARD, map(speed, 1500, 2000, 0, 255 ) ); run( DW_FORWARD, speed );
} else if( speed > 0 && speed <= 255 ) { } else if( speed > 0 && speed <= 255 ) {
run( DW_FORWARD, speed ); run( DW_FORWARD, map(speed, 0, 255, 1500, 2000 ) );
} else if( speed < 0 && speed >= -255 ) { } else if( speed < 0 && speed >= -255 ) {
run( DW_REVERSE, abs(speed) ); run( DW_REVERSE, map( abs(speed, 0, 255, 1500, 1000) );
} else if( speed == 0 || speed == 1500 ) { } else if( speed == 0 || speed == 1500 ) {
run( DW_STOP, speed ); run( DW_STOP, 1500 );
} }
@@ -287,33 +287,16 @@ void DW_Motor::off(void) {
} }
void DW_Motor::run( uint8_t control, uint16_t speed ) { void DW_Motor::run( uint8_t control, uint16_t speed ) {
// get the mode
if( DWC->getMode() == DW_PHASE ) { // The if statement isn't really needed - but it's there in case we want to add more functionality later
if( control == DW_FORWARD ) { if( control == DW_FORWARD ) {
DWC->setPin( in2, 0 ); setPWMuS( speed );
DWC->setPWM( in1, 0, speed * 16 ); } else if( control == DW_REVERSE ) {
} else if( control == DW_REVERSE ) { setPWMuS( speed );
DWC->setPin( in2, 1 ); } else if( control == DW_STOP ) {
DWC->setPWM( in1, 0, speed * 16 ); setPWMuS( speed );
} else if( control == DW_STOP ) {
DWC->setPin( in2, 0 );
DWC->setPin( in1, 0 );
} }
} else { // DW_ININ
if( control == DW_FORWARD ) {
DWC->setPin( in2, 0 );
DWC->setPWM( in1, 0, speed * 16 );
} else if( control == DW_REVERSE ) {
DWC->setPin( in1, 0 );
DWC->setPWM( in2, 0, speed * 16 );
} else if( control == DW_STOP ) {
DWC->setPin( in1, 1 );
DWC->setPin( in2, 1 );
} else if( control == DW_COAST ) {
DWC->setPin( in1, 0 );
DWC->setPin( in2, 0 );
}
}
} }
uint16_t DW_Motor::map(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max) uint16_t DW_Motor::map(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max)
@@ -321,6 +304,19 @@ uint16_t DW_Motor::map(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t ou
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
} }
void DW_Motor::setPWMmS(float length_mS) {
DWC->setPWMmS( pin, length_mS ); // Servo 1 is on PWM 0
}
void DW_Motor::setPWMuS(float length_uS) {
DWC->setPWMuS( pin, length_uS ); // Servo 1 is on PWM 0
}
/* Servo code */ /* Servo code */
DW_Servo::DW_Servo(void) { DW_Servo::DW_Servo(void) {

View File

@@ -61,6 +61,8 @@ class DW_Motor {
void setMotorSpeed(int16_t speed); void setMotorSpeed(int16_t speed);
void off(void); void off(void);
void run(uint8_t control, uint16_t speed ); void run(uint8_t control, uint16_t speed );
void setPWMmS(float length_mS);
void setPWMuS(float length_uS);
private: private:
uint8_t pin; uint8_t pin;
@@ -115,11 +117,6 @@ class DWESCAPE {
DW_Motor *getMotor(uint8_t motor); DW_Motor *getMotor(uint8_t motor);
DW_Servo *getServo(uint8_t servo); DW_Servo *getServo(uint8_t servo);
// void setStepperOff(uint8_t stepper);
// void setStepperSpeed(uint8_t stepper, uint16_t speed);
// void oneStep(uint8_t stepper, uint8_t direction, uint8_t style);
// void step(uint8_t stepper, uint16_t steps, uint8_t direction, uint8_t style = DW_SINGLE);
private: private:
uint8_t devAddr; uint8_t devAddr;
float frequency; float frequency;