Visualizza messaggio singolo
Vecchio 03 maggio 20, 15:18   #55 (permalink)  Top
Smsteves
User
 
Data registr.: 03-05-2020
Residenza: milano
Messaggi: 47
Qualche aggiustamento, dimmi se va.. altrimenti debugga con il serial.print

-------------------------------------------------
#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 5 // out rear servo sx endpoint if inverted with postdx it reverse
#define Postdx 175 //-- 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 100 //-- gain steering reduction by throttle if reverse add -
#define Slowlimit 1700 //-- slow forward without endpoint correction
#include <Servo.h>
Servo myservo;
unsigned long Rxpulse;//pulsin restituisce un long
unsigned long Gaspulse ;
unsigned long Gain;//Giusto per.. mettiamo long
unsigned long NewPos, OldPos;
byte Stato=0;
void setup(){
myservo.attach(10); //-- rear servo signal out pin
pinMode(8, INPUT); //-- front servo signal in pin
pinMode(7, INPUT); //-- throttle signal in pin
}



void loop(){

if (Stato==0) {
//lettura
Rxpulse = pulseIn(8, HIGH);
Gaspulse = pulseIn(7, HIGH);
}


if (Stato==1) {
//Calcolo
Gaspulse = constrain (Slowlimit, Neutral, Maxspeed);
Gain = map(Gaspulse, Neutral, Maxspeed, 0, Max_gain);
NewPos = map(Rxpulse, Antsx, Antdx, (Postsx + Gain), (Postdx - Gain));
}

if (Stato==2) {
//scrittura
if (abs(NewPos - OldPos)> Tolerance) {
OldPos = NewPos;
myservo.write(Center +NewPos);//da rivedere cosi' centri solo in un senso credo..
}
}

//ritardo esecuzione cicli/stati
delay(3);

// Incrementa stato per il prossimo loop
++Stato;

if (Stato==3) {
// riparte da 1
Stato=0; //qui ci va 0 non 1
}


}

Ultima modifica di Smsteves : 03 maggio 20 alle ore 15:21
Smsteves non รจ collegato   Rispondi citando