Thank you PJ_Glasso
Motor control with the encoder is not the problem. My problem is slow motor response to command because:
- I2C library is too slow (solved with library modification)
- PWM frequency is too slow: 40ms period (not solved)
Here is the context:
We try to find an improve solution for EMG30 motor+encoder+arduino+Arduino motor shield L298.
Here is one of the box which we use with our 800 students:
The improved solution (in test) is made with an I2C Motor Driver V1.3
Why migrate: I2C free a lot of port for more complex projets and we can use interrupt pin 2 & 3(pin 3 is used by arduino motor shield).
The old design motor control can be done with this Arduino program (proportional control):
#include <Encoder.h>
Encoder myEnc(2, 5);
long commande = 0;
long Position;
int erreur;
int consigne = 180;
float kp = 20;
void setup() {
pinMode(12, OUTPUT); //pin Direction
pinMode(3, OUTPUT); //pin PWM
}
void loop() {
Position = myEnc.read();
erreur = consigne - Position;
commande = kp * erreur;
moteur(commande);
}
//Commande du moteur
void moteur(int vit) {
if (vit > 255) vit = 255;
if (vit < -255) vit = -255;
analogWrite(3, abs(vit));
digitalWrite(12, (vit > 0));
}
The new design can be control with this program with a library modification:
#include "Grove_I2C_Motor_Driver.h"
#include <Encoder.h>
#define I2C_ADDRESS 0x0f
long consigne = 100;
float kp = 0.8;
Encoder myEnc(2, 3);
void setup() {
Motor.begin(I2C_ADDRESS);
Motor.stop(MOTOR1);
}
void loop() {
long position = -myEnc.read();
long erreur = consigne - position;
float commande = kp * (float)(consigne - position);
float commande2 =constrain(commande,-100,100);
Motor.speed(MOTOR1, commande2);
}
This motor control can only be performed with library modification: remove the delay(4) in the library (point one solved at the beginning of this message). Without this, motor control is impossible
Note that Kp=40 in the old design and Kp= 0.8 in the new design!
The Kp value are obtained with Ziegler–Nichols method
Why such a difference? Because PWM is too slow. The result is very poor performance compared to old design: high error and slow speed: this motor control is not usable in real projects.
Next pictures/text in next message: I cannot post more as new user!