Go Down

Topic: Conflict with the "attach" instruction and my code (Read 191 times) previous topic - next topic

Thesunnym

Sep 30, 2019, 06:57 pm Last Edit: Sep 30, 2019, 06:58 pm by Thesunnym
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: https://github.com/NicolasU-N/raptor/blob/master/raptor_nl_pid.ino
// 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 = -7*IR[0]-5*IR[1]-3*IR[2]-IR[3]+IR[4]+3*IR[5]+5*IR[6]+7*IR[7];
//  Serial.println(p);
  i=i+p;
  d=p-p_old;
  p_old=p;
  if ((p*i)<0) i=0;  // integral windup

  u=kp*p+ki*i+kd*d;
  vbase=vmin+(vmax-vmin)*exp(-kv*abs(kp*p));
  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);
//}

groundFungus

Code: [Select]
const int PWMA = 3; // speed and direction control motor A (left)

From the ServoTimer2.h file:
Quote
Note that analogWrite of PWM on pins 3 and 11 is disabled when the first servo is attached
See a problem there?  It never hurts to read the documentation for libraries that you use.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask a good question.  Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

UKHeliBob

From https://www.arduino.cc/en/reference/servo

Quote
The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

groundFungus

Code: [Select]
#include <ServoTimer2.h>
He is using the ServoTimer2 library, not Servo.  Timer 2 uses pins 11 and 3.
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

GolamMostafa

#5
Sep 30, 2019, 08:02 pm Last Edit: Sep 30, 2019, 08:03 pm by GolamMostafa

Robin2

I may be wrong but the statement in Reply #3 "Timer 2 uses pins 11 and 3" seems to me easier to assimilate than the diagram in Reply #5 :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

GolamMostafa

I may be wrong but the statement in Reply #3 "Timer 2 uses pins 11 and 3" seems to me easier to assimilate than the diagram in Reply #5 :)
There are many ways for the representation of knowledge; who knows who likes what! Figure of Post#5 aims at amplifying the information of Post#3 through visualization which "has been an effective way to communicate both abstract and concrete ideas since the dawn of humanity". 

TheMemberFormerlyKnownAsAWOL

There are many ways for the representation of knowledge; who knows who likes what! Figure of Post#5 aims at amplifying the information of Post#3 through visualization which "has been an effective way to communicate both abstract and concrete ideas since the dawn of humanity". 
...but usually only when the recipient recognises what the abstraction is.
Or a key is provided.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

Thesunnym

Code: [Select]
const int PWMA = 3; // speed and direction control motor A (left)

From the ServoTimer2.h file:See a problem there?  It never hurts to read the documentation for libraries that you use.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask a good question.  Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Hi im from Colombia, it's a little bit hard to understand all the rules that this forum has, so please excuse me, i'll try to read them.

I was reading the documentation but i don't understand, is there a way to solve my problem?

groundFungus

#10
Sep 30, 2019, 10:05 pm Last Edit: Sep 30, 2019, 10:06 pm by groundFungus
Use a different PWM pin than pin 3 or pin 11 (other PWM pins are 5,6,9,10 on an Uno) for the motor speed control.
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

Go Up