Hi friends
I am working in a machine that was created a few years ago from a student.
Seems everything is working.
It has 1 encoder 200 PPR and car wheel that counts the pulses from a rackwheel.
The output from encoder is in RPM from both.
I want to add an output for number of pulses of each (I will convert in distance with wheels diameters) to get the distance from each wheel.
As I am a begginer in arduino I would appreciate the help
I advice the code is not from me and I seems quite confuse....
I try to do it and I add a D1 and D2 for distances, but the counter starts at 255 and gos to 0 and restarts ![]()
//cpalha versao 0 out20
#include "math.h"
#include "Filter.h"
#define PWM1 6
#define encodPinA1 3
#define encodPinB1 11
#define pump1 4
#define pumpswitch 8
#define brakeswitch 7
#define valveswitch 9
#define pump1speed 5
#define LOOPTIME 100
unsigned long oldTime= 0;
unsigned long lastMilli = 0;
unsigned long lastMilliPrint = 0;
long lastProcess=0;
float val;
float val1;
int slip = 5;
// filtro celula:
const int numReadings = 100;
int sleep2= Serial.read();
int PWM_val = 0;
volatile long count = 0;
volatile long count2 = 0;
volatile double speed_act= 0;
volatile double speed_req= 0;
volatile double speed_act2= 0;
volatile double velocidade= 0;
volatile double catrito= 0;
volatile byte pulseCount1= 0;
volatile byte pulse1= 0;//novo
volatile byte pulse2= 0;//novo
int ps1=0;
double Kp = 0.001;
double Ki = 0.02 ;
double Kd = 0.095 ;
float readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
float total = 0; // the running total
float average = 0; // the average
void setup() {
Serial.begin(115200);
pinMode(PWM1, OUTPUT);
pinMode(encodPinA1, INPUT);
pinMode(encodPinB1, INPUT);
pinMode(pumpswitch, OUTPUT);
pinMode(brakeswitch,OUTPUT);
pinMode(valveswitch,OUTPUT);
digitalWrite(encodPinA1, HIGH);
digitalWrite(encodPinB1, HIGH);
digitalWrite(pump1,HIGH);
pinMode(pump1speed,OUTPUT);
attachInterrupt(1, rencoder, RISING);
attachInterrupt(0, rencoder2,RISING);
analogWrite(PWM1,PWM_val);
analogWrite(pump1speed,ps1);
digitalWrite(pumpswitch,LOW);
digitalWrite(brakeswitch,LOW);
digitalWrite(valveswitch,LOW);
// filtro célula
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
delay (LOOPTIME);
getParam();
pulseCounter1();
pulseCounter2();
relay();
printinfo();
ps1=255;
PWM_val= updatePid(PWM_val, speed_req, speed_act);
analogWrite(PWM1, PWM_val);
if (sleep2 > 97 || sleep2 <122) sleep2 =0;
else slip = sleep2;
}
void getParam() {
//leitura da celula de carga
val=analogRead(0);
float voltage= (val) * (5.0 / 1024.0);
double forca=(0.0662*val+0.506);// calib cp 21out20 para a media (0.0598*val + 0.9045); //11.164*voltage + 0.7191;
total = total - readings[readIndex];
readings[readIndex] = forca;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings;
delay(1);
float binario=(average*0.32); //braco do travao a celula
float btraseiro= 1.8695652173913*binario ;
float fatrito=(btraseiro/0.21);
catrito=fatrito/64;
speed_req=((1.8695652173913)*(speed_act2)*(1-slip*0.01))*(0.605/0.42);
//velocidade da bomba de agua
velocidade=((speed_act2)*(1.90664*0.001*60));
analogWrite(pump1,LOW);
analogWrite(pump1speed,ps1);
double litros= (velocidade*0.6)*1.25;
double ps1 =180;//5.0009*litros - 56.972;
}
//ajuste da RPM do travao
int updatePid(int command, int targetValue, int currentValue) {
float pidTerm = 0;
int error=0;
double P=0,I=0,D=0;
static int last_error=0;
static int last_speed_act=0;
float deltaTime = (millis()-lastProcess)/1000;
lastProcess=millis();
error =(abs(speed_act)-abs(speed_req));
P=Kp * error;
I+=(Ki * error);
D=Kd * (error - last_error);
last_speed_act = speed_act;
pidTerm = P+I+D;
last_error = error;
return constrain(command + int(pidTerm), 0, 255);
}
void rencoder() {
if (PINB & 0b00000001) count++;
else count--;
}
void rencoder2() {
if (digitalRead(encodPinB1)==HIGH) count2++;
else count2--;
}
void pulseCounter1(){
static long countAnt = 0;
speed_act =((count - countAnt)*(60*(1000/LOOPTIME)))/(200);//encoder travao 200PPR
pulse1=count - countAnt; //novo
if (speed_act>0){
speed_act=speed_act;}
if (speed_act<0){
speed_act=speed_act*-1;}
countAnt = count;}
void pulseCounter2() {
static long countAnt = 0;
speed_act2 = (((count2 - countAnt)*(60*(1000/LOOPTIME)))/(52)); //encoder 2 rodas 52 PPR
pulse2=count - countAnt; //novo
countAnt = count2; }
void printinfo(){
if((millis()-lastMilliPrint) >= 500) { //delay para o print passei a 500
lastMilliPrint = millis();
double forca=(0.0662*val+0.506);//(0.0598*val + 0.9045); calib cp 21out20 valor instantaneo
//Serial.print(" RPMFW: "); Serial.print(speed_act2*1.8695652173913*(0.605/0.42)); //RPM das rodas da frente
Serial.print("RPMF"); Serial.print((speed_act));
Serial.print(" SR"); Serial.print(speed_req);
Serial.print(" PWM"); Serial.print(PWM_val);
Serial.print(" Ca"); Serial.print(catrito);
Serial.print(" AF"); Serial.print(average);
Serial.print(" F"); Serial.print(forca);
Serial.print(" D1"); Serial.print(pulse1);//novo
Serial.print(" D2"); Serial.print(pulse2);//novo
Serial.print(" SLIP"); Serial.println(slip);
//Serial.print("\n"); //fim de string novo
}
}
void relay(){
char usbdata=Serial.read();
delay(10);
usb_action(usbdata);
Serial.flush();}
void usb_action(char nova){
switch (nova)
{
case 'p':
digitalWrite(pumpswitch,HIGH);
break;
case 'o':
digitalWrite(pumpswitch,LOW);
break;
case 'b':
digitalWrite(brakeswitch,HIGH);
break;
case 'n':
digitalWrite(brakeswitch,LOW);
break;
case'v':
digitalWrite(valveswitch,HIGH);
break;
case'c':
digitalWrite(valveswitch,LOW);
break;
case'+':
slip++;
break;
case'-':
slip--;
break;
case'4':
Kp=Kp+0.0001;
break;
case'1':
Kp=Kp-0.0001;
break;
case'5':
Ki=Ki+0.01;
break;
case'2':
Ki=Ki-0.01;
break;
case'6':
Kd=Kd+0.001;
break;
case'3':
Kd=Kd-0.001;
break;
}
}
cpalha