I want to control a DC motor, im using the arduino and an isolation phase using an optocupler and an IGBT, the thing is, the motor doesn´t do anything :~, and i´ve printed some variables and it seems there isnt any comunication between processes, should i declare the processes as not void? so the processes can pass values through?
The code is as follows
//Variables para obtener rpm´s del motor
int rps = 0; //revoluciones por segundo
int rpm = 0; //revoluciones por minuto
int lastrpm = 0; //los rpm´s anteriores
int lastrpm2 = 0;
int lastrpm3 = 0;
int c = 0;
unsigned long past = 0; // El tiempo anterior
//Variables para el PID
double error, lerror, derror; // error acutal error anterior diferencia del error
int referencia = 1800; //referencia
int kp, ki, kd, Kp, Ki, Kd;// constantes
int output; // salida
int pwm = 9;// Salida del PWM
void setup()
{
Serial.begin(9600);
attachInterrupt(0,encoder,FALLING);
pinMode(pwm,OUTPUT);
}
void loop()
{
referencia = 1800;
if (millis() - past == 1000)// se activa cada segundo para obtener RPM´s
{
RPM();
if (lastrpm != rpm)
calcula();
if (output > 255) digitalWrite(pwm,255);
digitalWrite(pwm,output);
}
}
//Interrupcion se activa cada que el sensor detecta un pulso en bajo contando las vueltas
void RPM()
{
detachInterrupt(0); // Apagar interrupcion
//lastrpm = rpm; //Recordar el ultimo valor de rpm
rpm = rps * 60; //Calcular los RPM
if (c>3)
{
rpm = (rpm + lastrpm+ lastrpm2+lastrpm3)/4;
}
Serial.print("RPM =\t"); //Imprimir RPM
Serial.print(rpm); //Imprimir valor de rpm
Serial.print("\t out =\t"); //
Serial.println(error);
rps = 0;
lastrpm3 = lastrpm2;
lastrpm2 = lastrpm;
lastrpm = rpm;
c ++;
past = millis();
attachInterrupt(0,encoder,FALLING);
}
void calcula()
{
error = referencia - rpm;
set_kons();
output = Kp + Ki + Kd;
}
void set_kons()
{
derror = error - lerror;
Kp = 5 * error;
Ki += 5 * error;
Kd = 5 * derror;
}
void encoder()
{
rps++;
}
nefisto:
i´ve printed some variables and it seems there isnt any comunication between processes, should i declare the processes as not void? so the processes can pass values through?
What 'processes' are you referring to?
Detaching and reattaching interrupts like that seems like a very bad idea to me.
This code is quite fragile and relies on loop() executing at the precise millisecond that the condition is true:
if (millis() - past == 1000)
If you replaced the == with >= it would be more robust.
Ill try the mills change you proposed, thank you for the advice.
And by processes i mean functions.
Maybe if i do change the void so i can return values, ex.
void name (){code .....}
change it to
int name(){code .... return X}
the attaching and reataching the interrupt already worked in the code were i compute the rpm(revolutions per second).
Might i ask why is a bad idea?
You haven't said anything about how the motor is connected to the Arduino. Surely, the motor isn't attached to the pin named rpm, is it? How does that name relate to what is connected to it? What kind of shield or other hardware is between the Arduino and the motor? How is the motor powered?
The lack of answers here makes me suspect that you have either a hardware error or a PEBKAC error, not a software problem.
nefisto:
by processes i mean functions.
Maybe if i do change the void so i can return values
There's no general rule on whether it's right or wrong to return a value except that you must return one when you need it and should not when you don't. If you want to know whether a particular function should return a value, you need to explain what the function does and in particular what data needs to be passed into and out of the function.
Sorry for the lack of info, here it is.
A pwm output of the arduino digital pin 9 is connected to a FOD315 (optocoupler) then to an IGBT to trim the voltage,
digitalWrite(pwm,output);
the rpm is used to calculate the revolutions per second, so i have the interrupt detached every second so it cant continue counting to calculate rpm
rpm = rps * 60;
And i already solved this last night, thank you all for the answers, it was a line of code.