I encounter issues about pwm motor control using wiringPi softPwm.
both motors are partially running smooth from 0-255, and partially are jerking when the direction is changed from forward to backwards.
this is my pin setup:
Code: Select all
// Raspi motor pins, wiringPi numbering (in parenthesis: BCM numbering)
motor[0].pinQa = 5; // (BCM 24) encpin
motor[0].pinQb = 4; // (BCM 23) encpin
motor[0].pind1 =24; // (BCM 19) dirpin
motor[0].pind2 =25; // (BCM 26) dirpin
motor[0].pinpwm=26; // (BCM 12) pwmpin
motor[1].pinQa = 0; // (BCM 17) encpin
motor[1].pinQb = 2; // (BCM 27) encpin
motor[1].pind1 =21; // (BCM 5) dirpin
motor[1].pind2 =22; // (BCM 6) dirpin
motor[1].pinpwm=23; // (BCM 13) pwmpin
Code: Select all
#define MAXPWMRANGE 255 // maximum software-pwm range (0-255)
void setupDpins() {
int i, err;
// motor pin settings
// encoder pin settings
// setup for L293D motor driver
// motor pins, wiringPi numbering (in parenthesis: BCM numbering)
motor[0].pinQa = 5; // (BCM 24) encpin
motor[0].pinQb = 4; // (BCM 23) encpin
motor[0].pind1 =24; // (BCM 19) dirpin
motor[0].pind2 =25; // (BCM 26) dirpin
motor[0].pinpwm=26; // (BCM 12) pwmpin
motor[1].pinQa = 0; // (BCM 17) encpin
motor[1].pinQb = 2; // (BCM 27) encpin
motor[1].pind1 =21; // (BCM 5) dirpin
motor[1].pind2 =22; // (BCM 6) dirpin
motor[1].pinpwm=23; // (BCM 13) pwmpin
for( i=0; i< MAXMOTORSLOCAL; ++i) {
pinMode( motor[i].pinQa, INPUT); // encA
pinMode( motor[i].pinQb, INPUT); // encB
pinMode( motor[i].pind1, OUTPUT); // dir-1
pinMode( motor[i].pind2, OUTPUT); // dir-2
pinMode( motor[i].pinpwm, PWM_OUTPUT); // pwm
err= softPwmCreate( motor[i].pinpwm, 0, MAXPWMRANGE);
printf("err %-4d qa %-4d qb %-4d d1 %-4d d2 %-4d pwm %-4d \n",
err, motor[i].pinQa, motor[i].pinQb, motor[i].pind1, motor[i].pind2, motor[i].pinpwm);
motor[i].motenc = 0;
motor[i].oldenc = 0;
ISRab[i] = 0;
}
//printf("press ENTER");
//getchar();
}
Code: Select all
// motor control structure
typedef struct {
// electrical motor pins
uint8_t pind1, pind2, pinpwm; // dir + pwm L293 H-Bridge type
uint8_t pinQa, pinQb; // rotary enc pins Qa,Qb
// pwm and encoder values
int16_t dirpwm;
int32_t motenc, oldenc; // rotary encoder values
} tEncMotor;
tEncMotor motor[MAXMOTORS];
#define motorLeft motor[0]
#define motorRight motor[1]
#define motorCoast(nr) motorOn(nr, 0) // alias for motor coast
//*************************************************************
void motorBrake(int nr, int dirpwm) { // brake by pwm power
int pwm;
pwm = abs(dirpwm);
digitalWrite(motor[nr].pind1, HIGH);
digitalWrite(motor[nr].pind2, HIGH);
motor[nr].dirpwm = pwm;
softPwmWrite(motor[nr].pinpwm, pwm); // brake power always > 0
}
//*************************************************************
void motorOn(int nr, int dirpwm) { // motor On (nr, dir_pwm)
int dir, pwm; // signed pwm:
if(dirpwm > 0) dir = +1; // pwm > 0: forward
else if(dirpwm < 0) dir = -1; // pwm < 0: reverse
else dir = 0; // pwm = 0: coast
if(! _REMOTE_OK_) dirpwm=0;
pwm = abs(dirpwm);
if(dir> 0) {
digitalWrite( motor[nr].pind1, HIGH);
digitalWrite( motor[nr].pind2, LOW);
}
else
if(dir==0) {
digitalWrite( motor[nr].pind1, LOW);
digitalWrite( motor[nr].pind2, LOW);
}
else {
digitalWrite( motor[nr].pind1, LOW);
digitalWrite( motor[nr].pind2, HIGH);
}
motor[nr].dirpwm = dirpwm;
softPwmWrite( motor[nr].pinpwm, pwm);
}Code: Select all
void* thread2Go (void* ) { // high priority: motor remote control
static int pwm0, pwm1, i;
int deadzone = 10;
while(_TASKS_ACTIVE_) {
if( ! _REMOTE_OK_ ) {
for (i=0; i< MAXMOTORS; ++i ) {
motorCoast(i);
}
}
else
if( _REMOTE_OK_ ) {
pwm0 = recvanaval[0];
pwm1 = recvanaval[1];
if(abs(pwm0) < deadzone) pwm0=0;
if(abs(pwm1) < deadzone) pwm1=0;
motorOn(0, pwm0 );
motorOn(1, pwm1 );
// debug
// printf("pwm0=%5d pwm1=%5d \n", pwm0, pwm1);
}
if (!_TASKS_ACTIVE_) return NULL;
delay(10);
}
return NULL;
}is anything faulty with the pin setup?
2nd,
is anything wrong for wiringPi softPwm (range 0-255) ?