Hi !, as I said in the title I have a big problem with attach and all my code.
Im doing a follower line robot and I added a turbine to make it strong when it is go faster.
The problem is focused when I add the "attach" instruction which make the behavior of my robot change, I don't know why. When my robot doesn't have that line the car works great but the turbine doesnt turn on. And when I put that line my car doesnt move propperly
I am using Arduino NANO
The code:
// Source: raptor/raptor_nl_pid.ino at master · NicolasU-N/raptor · GitHub
// Arduino Pro Micro, TB6612 driver, Pololu QTR-8 sensor
// Adaptado por: @NicolasU-N Y @OrlandoMurcia1
// TB6612 driver pinout
//const int STBY = 15; // standby
const int PWMA = 3; // speed and direction control motor A (left)
const int AIN1 = 5;
const int AIN2 = 4;
const int PWMB = 9; // speed and direction control motor B (right)
const int BIN1 = 10;
const int BIN2 = 11;
// Pololu QTR-8A analog array readout
#include <ServoTimer2.h>
#include <stdio.h>
#include <string.h>
#include <QTRSensors.h>
#define PIN_ESC 6
ServoTimer2 servo;
QTRSensorsAnalog qtra((unsigned char[]) {7, 6, 5, 4, 3, 2,1,0}, 8,1);
unsigned int IR[8];
// parameters and variables for non linear PID
const int vmin=50; //Estaba 80
const int vmax=80; // 150
const float kp=0.2;
const float ki=0.0001;
const float kd=1;
const float kv=0.07;
int p,d,u,vbase;
long i=0;
int p_old=0;
int V_TURBINA = 100;
int x=vbase+u;
int vel = 1000; //amplitud del pulso
void setup() {
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(PWMA,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
pinMode(PIN_ESC,OUTPUT);
servo.attach(6);
delay(5000);
//
//// ESC.attach(6);
//////
////// //Activar el ESC
////myservo.writeMicroseconds(1000); //1000 = 1ms
////// //Cambia el 1000 anterior por 2000 si
////// //tu ESC se activa con un pulso de 2ms
//// delay(5000); //Esperar 5 segundos para hacer la activacion
//////
////// //Iniciar puerto serial
////Serial.begin(9600);
//// Serial.setTimeout(10);}
//myservo.writeMicroseconds(1000); //1000 = 1ms
//Cambia el 1000 anterior por 2000 si
//tu ESC se activa con un pulso de 2ms
//Esperar 5 segundos para hacer la activacion
servo.write(90);
}
void loop()
{
// myservo.write(100);
//digitalWrite(STBY, HIGH);
// ESC.writeMicroseconds(1500);
// //EDF27.setSpeed(1550);
qtra.read(IR); // read raw sensor values
p = -7IR[0]-5IR[1]-3IR[2]-IR[3]+IR[4]+3IR[5]+5IR[6]+7IR[7];
// Serial.println(p);
i=i+p;
d=p-p_old;
p_old=p;
if ((p*i)<0) i=0; // integral windup
u=kpp+kii+kdd;
vbase=vmin+(vmax-vmin)exp(-kvabs(kpp));
drive(vbase+u,vbase-u);
//imprimir();
}
void drive(int L, int R) // speed for wheels Left and Right, positive is forward
{
L=constrain(L,-255,255); // avoid PWM overflow
R=constrain(R,-255,255);
digitalWrite(AIN1, L<0); // Estaba < switch < and >= if left wheel doesnt spin as expected
digitalWrite(AIN2, L>=0);// >=
analogWrite(PWMA, abs(L));
digitalWrite(BIN1, R<0); // Estaba < switch < and >= if left wheel doesnt spin as expected
digitalWrite(BIN2, R>=0);// >=
analogWrite(PWMB, abs(R));
}
//void imprimir(){
// Serial.println(p);
// delay(250);
//}
PID-Veloras.ino (2.77 KB)