collapse

* Posts Recentes

Amplificador - Rockboard HA 1 In-Ear por almamater
[Ontem às 19:13]


O que é isto ? por KammutierSpule
[26 de Março de 2024, 19:35]


Bateria - Portátil por almamater
[25 de Março de 2024, 22:14]


Emulador NES em ESP32 por dropes
[13 de Março de 2024, 21:19]


Escolher Osciloscópio por jm_araujo
[06 de Fevereiro de 2024, 23:07]


TP4056 - Dúvida por dropes
[31 de Janeiro de 2024, 14:13]


Leitura de dados por Porta Serie por jm_araujo
[22 de Janeiro de 2024, 14:00]


Distancia Cabo por jm_araujo
[08 de Janeiro de 2024, 16:30]


Meu novo robô por josecarlos
[06 de Janeiro de 2024, 16:46]


Laser Engraver - Alguém tem? por almamater
[16 de Dezembro de 2023, 14:23]

Autor Tópico: comado RC  (Lida 8232 vezes)

0 Membros e 1 Visitante estão a ver este tópico.

Offline senso

  • Global Moderator
  • Mini Robot
  • *****
  • Mensagens: 9.733
  • Helpdesk do sitio
Re: comado RC
« Responder #30 em: 07 de Junho de 2011, 19:52 »
Se estás a usar pwm e se o pwm no máximo é 255, para quê isto:
Código: [Seleccione]
      adj_val1 = map(constrain(servo1_val, 600, 2400), low1, high1, 0, 511);
      constrain(adj_val1, 0, 511);

Estás a fazer map ao valor entre 0 e 511 e depois estás a fazer 255 ou 256 conforme calha menos esse valor, isso vai dar numeros negativos, aliás o 256 dependendo se o analogWrite usa signed ou unsigned vai ser interpretado como 0 ou -1....
Muda lá o map para 0,255 e usa os valores directos da variavel adj_val1 e faz o mesmo para o adj_val2 e tira essas subtrações todas que estás a fazer, possivelmente o problema são overflows e passares basicamente lixo para a função analogWrite.
Avr fanboy

Offline dvdt

  • Mini Robot
  • *
  • Mensagens: 1.248
  • David Teles
    • Site Pessoal
Re: comado RC
« Responder #31 em: 07 de Junho de 2011, 20:15 »
como?
opah nao estou a perceber sera que podias exemplificar?
por favor
Engenharia Electrotécnica e Computadores (IST)
Analyst in a Big 4

Offline senso

  • Global Moderator
  • Mini Robot
  • *****
  • Mensagens: 9.733
  • Helpdesk do sitio
Re: comado RC
« Responder #32 em: 07 de Junho de 2011, 20:25 »
Isto:
Código: [Seleccione]
      adj_val1 = map(constrain(servo1_val, 600, 2400), low1, high1, 0, 511);
      constrain(adj_val1, 0, 511);

Passa a isto:
Código: [Seleccione]
      adj_val1 = map(constrain(servo1_val, 600, 2400), low1, high1, 0, 255);
      constrain(adj_val1, 0, 255);

Isto é para saltar fora que está a fazer precisamente o mesmo que a função constrain.....
Código: [Seleccione]
      if (adj_val2 < 0) {
        adj_val2 = 0;
      }     
      if (adj_val2 > 511) {
        adj_val2 = 511;
      }

E depois nestas linhas todas que tens:
Código: [Seleccione]
        digitalWrite(motor1Pin2, LOW);
        digitalWrite(motor2Pin1, LOW);
        analogWrite(motor1Pin1, adj_val2 - pwm_ceiling);
        analogWrite(motor2Pin2, adj_val2 - pwm_ceiling);
        digitalWrite(ledPin2, LOW);

fica só:
Código: [Seleccione]
        digitalWrite(motor1Pin2, LOW);
        digitalWrite(motor2Pin1, LOW);
        analogWrite(motor1Pin1, adj_val2);
        analogWrite(motor2Pin2, adj_val2);
        digitalWrite(ledPin2, LOW);

Repetes para o adj_val2 e o adj_val1 e alteras tudo e testas.
Avr fanboy

Offline dvdt

  • Mini Robot
  • *
  • Mensagens: 1.248
  • David Teles
    • Site Pessoal
Re: comado RC
« Responder #33 em: 07 de Junho de 2011, 20:40 »
ok obrigado vou alterar agora e carregar para experimentar
Engenharia Electrotécnica e Computadores (IST)
Analyst in a Big 4

Offline dvdt

  • Mini Robot
  • *
  • Mensagens: 1.248
  • David Teles
    • Site Pessoal
Re: comado RC
« Responder #34 em: 07 de Junho de 2011, 20:50 »
Código: [Seleccione]
int motor1Pin1 = 5;    // Ponte H
int motor1Pin2 = 6;    // Ponte H
int motor2Pin1 = 10;   // Ponte H
int motor2Pin2 = 11;   // Ponte H

int ledPin1 = 13; // led indicator lights
int ledPin2 = 12;
int ledPin3 = 14;

int servo1 = 2;  // r/c channel 1
int servo2 = 3;  // r/c channel 2
//int servo3 = 7;

int power = 4; // power for R/C receiver, stays HIGH (5v).

//int relay_Pin = 8;

unsigned int relay_val;

volatile unsigned long servo1_startPulse;  //values for channel 1 signal capture
volatile unsigned servo1_val;
volatile int adj_val1; 
volatile int servo1_Ready;

volatile unsigned long servo2_startPulse; //values for channel 2 signal capture
volatile unsigned servo2_val;
volatile int adj_val2; 
volatile int servo2_Ready;

int deadband_high = 275;
int deadband_low = 235; 

int pwm_ceiling = 256;
int pwm_floor = 255; 

int low1 = 1100;  // adjust these values if your R/C readings are above or below these for channels 1 and 2.
int high1 = 1900;
int low2 = 1100;
int high2 = 1900;

int n = 0;

void setup() {
 
  //TCCR0B = TCCR0B & 0b11111000 | 0x03;
  //TCCR1B = TCCR1B & 0b11111000 | 0x01;
 
  Serial.begin(9600);
 
  //motor1 pins
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);

  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);

  //pinMode(relay_Pin, OUTPUT);
 
  pinMode(power, OUTPUT);

  //led's
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  //pinMode(ledPin3, OUTPUT);

  //PPM inputs from RC receiver
  pinMode(servo1, INPUT); //Pin 2 as input
  pinMode(servo2, INPUT); //Pin 3 as input
  //pinMode(servo3, INPUT); //Pin 3 as input

  digitalWrite(power, HIGH);
  //digitalWrite(relay_Pin, LOW);
 
  delay(1200);

  attachInterrupt(0, rc1_begin, RISING);    // catch interrupt 0 (digital pin 2) going HIGH and send to rc1()
  attachInterrupt(1, rc2_begin, RISING);    // catch interrupt 1 (digital pin 3) going HIGH and send to rc2()

}

////////// attach servo signal interrupts to catch signals as they go HIGH then again as they go LOW, then calculate the pulse length.

void rc1_begin() {           // enter rc1_begin when interrupt pin goes HIGH.

  servo1_startPulse = micros();     // record microseconds() value as servo1_startPulse
 
  detachInterrupt(0);  // after recording the value, detach the interrupt from rc1_begin
 
  attachInterrupt(0, rc1_end, FALLING); // re-attach the interrupt as rc1_end, so we can record the value when it goes low
 
}

void rc1_end() {
 
 servo1_val = micros() - servo1_startPulse;  // when interrupt pin goes LOW, record the total pulse length by subtracting previous start value from current micros() vlaue.
 
 detachInterrupt(0);  // detach and get ready to go HIGH again
 
 attachInterrupt(0, rc1_begin, RISING);
 
}

void rc2_begin() {

  servo2_startPulse = micros();

  detachInterrupt(1);
 
  attachInterrupt(1, rc2_end, FALLING);
 


}

void rc2_end() {
 
 servo2_val = micros() - servo2_startPulse;
 
 detachInterrupt(1);
 
 attachInterrupt(1, rc2_begin, RISING);
 
}
/////// servo interrupts end

/////// MAIN LOOP

void loop() {

///// channel 1 bom sinal confirmado
 if (servo1_val < 600 || servo1_val > 2400) {  // only set the servo1_Ready flag if the value is a valid Servo pulse between 600-2400 microseconds.
  servo1_Ready = false;
  servo1_val = 1500;
 }
 else {
  servo1_Ready = true; // se nao for os valor nao sao processados
 }
 
///// channel 1 bom sinal confirmado

 if (servo2_val < 600 || servo2_val > 2400) {
  servo2_Ready = false;
  servo2_val = 1500;
 }
 else {
  servo2_Ready = true;
 }

   if (servo2_Ready) {
     
      servo2_Ready = false; 
      adj_val2 = map(constrain(servo2_val, 600, 2400), low1, high1, 0, 255);
      constrain(adj_val2, 0, 255);

      if (adj_val2 > deadband_high) {
        //curva
        digitalWrite(motor1Pin2, LOW);
        digitalWrite(motor2Pin1, LOW);
        analogWrite(motor1Pin1, adj_val2);
        analogWrite(motor2Pin2, adj_val2);
        digitalWrite(ledPin2, LOW);
      }
      else if (adj_val2 < deadband_low) {
        //curva
        digitalWrite(motor1Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
        analogWrite(motor1Pin2, adj_val2);
        analogWrite(motor2Pin1, adj_val2);
        digitalWrite(ledPin2, LOW);
      }
      else {
        //parar
        digitalWrite(motor1Pin1, LOW);
        digitalWrite(motor1Pin2, LOW);
        digitalWrite(motor2Pin1,LOW);
        digitalWrite(motor2Pin2, LOW);
        digitalWrite(ledPin2, HIGH); // liga o led de neutro e para o carro
      }
     
    }

if (servo1_Ready) {
     
      servo1_Ready = false; 
      adj_val1 = map(constrain(servo1_val, 600, 2400), low1, high1, 0, 255);
      constrain(adj_val1, 0, 255);

      if (adj_val1 > deadband_high) {
        //Frente
        digitalWrite(motor1Pin2,LOW);
        digitalWrite(motor2Pin2, LOW);
        analogWrite(motor1Pin1, adj_val1);
        analogWrite(motor2Pin1, adj_val1);
        digitalWrite(ledPin1, LOW);
      }
      else if (adj_val1 < deadband_low) {
        //Marcha Atras
        digitalWrite(motor1Pin1, LOW);
        digitalWrite(motor2Pin1, LOW);
        analogWrite(motor1Pin2, adj_val1);
        analogWrite(motor2Pin2, adj_val1);
        digitalWrite(ledPin1, LOW);
      }
      else {
        //parar
        digitalWrite(motor1Pin1, LOW);
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor1Pin2, LOW);
        digitalWrite(motor2Pin2, LOW);
        digitalWrite(ledPin1, HIGH); // liga o led neutro e para o carro
      }
     
    }
   
   
    //mostras as acçoes no Serial monitor do Arduino IDE
    Serial.print("ch1:  ");
    Serial.print(adj_val1);
    Serial.print("  ");
    Serial.print("rc1:  ");
    Serial.print(servo1_val);
    Serial.print("  ");
    Serial.print("ch2:  ");
    Serial.print(adj_val2);
    Serial.print("  ");
    Serial.print("rc2:  ");
    Serial.print(servo2_val);
    Serial.print("  ");
    Serial.print("loop counter:  ");
    Serial.print(n);
    Serial.println("  ");   

    //n++;

 
}

O codigo ficou assim mas agora nao funciona nada
Engenharia Electrotécnica e Computadores (IST)
Analyst in a Big 4

Offline senso

  • Global Moderator
  • Mini Robot
  • *****
  • Mensagens: 9.733
  • Helpdesk do sitio
Re: comado RC
« Responder #35 em: 07 de Junho de 2011, 21:27 »
Muda isto:
Código: [Seleccione]
int deadband_high = 275;
int deadband_low = 235; 

Para isto:
Código: [Seleccione]

int deadband_high = 137;
int deadband_low = 117; 
Avr fanboy

Offline dvdt

  • Mini Robot
  • *
  • Mensagens: 1.248
  • David Teles
    • Site Pessoal
Re: comado RC
« Responder #36 em: 07 de Junho de 2011, 21:29 »
eu tirei o codigo do serial monitor e simplesmente começou tudo a funcionar
Engenharia Electrotécnica e Computadores (IST)
Analyst in a Big 4

Offline senso

  • Global Moderator
  • Mini Robot
  • *****
  • Mensagens: 9.733
  • Helpdesk do sitio
Re: comado RC
« Responder #37 em: 07 de Junho de 2011, 21:33 »
Mas altera o que te disse.
Avr fanboy

Offline dvdt

  • Mini Robot
  • *
  • Mensagens: 1.248
  • David Teles
    • Site Pessoal
Re: comado RC
« Responder #38 em: 07 de Junho de 2011, 21:36 »
ok vou alterar agora
Engenharia Electrotécnica e Computadores (IST)
Analyst in a Big 4