Visualizza messaggio singolo
Vecchio 04 maggio 20, 20:51   #97 (permalink)  Top
Smsteves
User
 
Data registr.: 03-05-2020
Residenza: milano
Messaggi: 47
Ho cercato i squadrettare il segnale, dividendolo in sezioni di valore definiti.
questo e' il massimo che ottengo, ma non cambia molto provalo te.
hai due scale una per sterzo e una per gas.
piu' piccolo e' il valore maggiore e' la precisione 1-n.

Codice:
#define Neutral 1500 // -- minimum forward signal
#define Maxspeed 2000 // -- maximum forward signal
#define Antsx 1000 // -- in front servo signal sx
#define Antdx 2000 // -- in front servo signal dx
#define Postsx 1000 // out rear servo sx endpoint if inverted with postdx it reverse
#define Postdx 2000 //-- out rear servo dx endpoint if inverted with postsx it reverse
#define Center 0 //-- add or subtract xx value to center steering
#define Tolerance 3 //-- if poor quality servo vibrates try 5
#define Max_gain 950 //-- gain steering reduction by throttle if reverse add -
#define Slowlimit 1600 //-- slow forward without endpoint correction
#include <MobaTools.h>
MoToServo myservo;
unsigned int Rxpulse;
unsigned int Gaspulse ;
unsigned int Gain;
signed int NewPos, OldPos;
int  Stato;
int Stato_Led=0;
boolean Cmd_Led = false;
int Ledpin=13;
int Led=0;
unsigned int scala_RX=5;
unsigned int scala_GAS=20;

void setup() {
  pinMode(7, INPUT); //-- throttle signal in pin
  pinMode(8, INPUT); //-- front servo signal in pin
  pinMode(Cmd_Led, OUTPUT);
  myservo.attach(10); //-- rear servo signal out pin
   myservo.setSpeed(128);
  Stato = 0;
 }

void loop() {
  if (Stato == 0) {
    //lettura
    Rxpulse = pulseIn(8, HIGH);   
    //Rxpulse = (((Rxpulse-Antsx)/scala)*((Antdx-Antsx)/scala))+Antsx;
    Rxpulse = Rxpulse - Antsx;
    Rxpulse = Rxpulse / scala_RX;
    Rxpulse = Rxpulse * scala_RX ;
    Rxpulse = Rxpulse + Antsx;
    Gaspulse = pulseIn(7, HIGH);
    Gaspulse = Gaspulse - Neutral;
    Gaspulse = Gaspulse / scala_GAS;
    Gaspulse = Gaspulse * scala_GAS;
    Gaspulse = Gaspulse + Neutral; 
  }
  if (Stato == 1) {
    //Calcolo
    if (Gaspulse > Slowlimit) {
      Gain = map(Gaspulse, Slowlimit, Maxspeed, 0, Max_gain );
      NewPos = map(Rxpulse, Antsx, Antdx, (Postsx + Gain), (Postdx - Gain));
      Led=1;
    }
    else {
      NewPos = map(Rxpulse, Antsx, Antdx, Postsx, Postdx);
      Led=2;
    }
  }
  if (Stato == 2) {
    //scrittura
    if (abs(NewPos - OldPos) > Tolerance) {
      myservo.write(NewPos + Center);
         OldPos=NewPos;
    }
  }
  // Incrementa stato per il prossimo loop
  ++Stato;
  // Incrementa tempo led per il prossimo loop
  ++Stato_Led;
  if (Stato == 3) {
    // riparte da 0
    Stato = 0;
  }  
  //ritardo esecuzione cicli/stati
  delay(1);
  if (Led == 1) {
    //led veloce
    if (Stato_Led>20) {
    Cmd_Led= !Cmd_Led;
      Stato_Led=0;
    }
    digitalWrite(Ledpin, Cmd_Led);
  }
  if (Led == 2) {
    //led lento
    if (Stato_Led>60) {
    Cmd_Led= !Cmd_Led;
      Stato_Led=0;
    }
    digitalWrite(Ledpin, Cmd_Led);
  } 
}
Smsteves non รจ collegato   Rispondi citando