Boni.
Ho copiato lo sketch sbagliato..
Codice:
/*
* Created by Stefano Prevedini
* Date 05/05/2020 V0.1
* Project: Arduino Four Steering Wheels Control
*/
#define Tolerance 5 //-- if poor quality servo vibrates try 5
#define Max_gain 500 //-- gain steering reduction by throttle if reverse add -
//#define Slowlimit 1600 //-- slow forward without endpoint correction
#include <Servo.h>
Servo myservo;
unsigned int Rxpulse;
unsigned int Gaspulse ;
unsigned int Gascorse ;
unsigned int Gain;
unsigned int Neutral;// -- minimum forward signal
signed int NewPos, OldPos;
int Stato;
int Stato_Led = 0;
int Ledpin = 13;
int Led = 0;
boolean Cmd_Led = false;
unsigned int Maxspeed; // -- maximum forward signal
unsigned int Antsx ; // -- in front servo signal sx
unsigned int Antdx; // -- in front servo signal dx
unsigned int Postsx; // out rear servo sx endpoint if inverted with postdx it reverse
unsigned int Postdx; //-- out rear servo dx endpoint if inverted with postsx it reverse
unsigned int Center = 0; //-- add or subtract xx value to center steering
unsigned int Slowlimit = 200;
unsigned int Sterzo = 300;
unsigned int Scala_GAS = 1;
unsigned int Scala_RX = 1;
double Percentuale;
void setup() {
pinMode(7, INPUT); //-- throttle signal in pin
pinMode(8, INPUT); //-- front servo signal in pin
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
pinMode(Cmd_Led, OUTPUT);
myservo.attach(10); //-- rear servo signal out pin
Stato = 0;
}
void loop() {
if (millis() < 50) {
Stato == 10;
Led = 10;
noInterrupts();
Neutral = pulseIn(7, HIGH);
Center = pulseIn(8, HIGH);
interrupts();
Postsx = Center - Sterzo;
Postdx = Center + Sterzo;
Antsx = Center - Sterzo;
Antdx = Center + Sterzo;
Maxspeed = Neutral + Gascorse;
}
if ((millis() > 2000) && (millis() < 3100)) {
delay(50);
Stato == 10;
Maxspeed=Neutral+100;
Slowlimit=Neutral+50;
}
if (Stato == 0) {
noInterrupts();
Rxpulse = pulseIn(8, HIGH);
Gaspulse = pulseIn(7, HIGH);
interrupts();
if (Rxpulse > (Sterzo + Center)) {
Sterzo = Rxpulse - Center;
Gain = Sterzo;
}
if (Rxpulse < (Center - Sterzo)) {
Sterzo = Center - Rxpulse;
Gain = Sterzo;
}
Rxpulse = Rxpulse - Antsx;
Rxpulse = Rxpulse / Scala_RX;
Rxpulse = Rxpulse * Scala_RX ;
Rxpulse = Rxpulse + Antsx;
if (Gaspulse > Maxspeed) {
Maxspeed = Gaspulse;
Slowlimit = ((Maxspeed - Neutral) / 3) + Neutral;
}
Gaspulse = Gaspulse - Neutral;
Gaspulse = Gaspulse / Scala_GAS;
Gaspulse = Gaspulse * Scala_GAS;
Gaspulse = Gaspulse + Neutral;
}
if (Stato == 1) {
if (Gaspulse > Slowlimit) {
if (Rxpulse > Center)
Percentuale = 0;
Percentuale = ((Gaspulse - Slowlimit) * 100) / (Maxspeed - Slowlimit);
if (Rxpulse > Center) {
NewPos = Center + (((Rxpulse - Center) * (100 - Percentuale) / 100));
Led = 3;
}
if (Rxpulse < Center) {
NewPos = Center - (((Center - Rxpulse) * (100 - Percentuale) / 100));
Led = 4;
}
Led = 1;
}
else {
NewPos = Rxpulse;
Led = 2;
}
}
if (Stato == 2) {
if (abs(NewPos - OldPos) > Tolerance) {
myservo.write(NewPos );
OldPos = NewPos;
}
}
++Stato;
++Stato_Led;
if (Stato > 2) {
Stato = 0;
}
delay(1);
if (Led == 1) {
if (Stato_Led > 60) {
Cmd_Led = !Cmd_Led;
Stato_Led = 0;
}
digitalWrite(Ledpin, Cmd_Led);
}
if (Led == 2) {
if (Stato_Led > 30) {
Cmd_Led = !Cmd_Led;
Stato_Led = 0;
}
}
digitalWrite(Ledpin, Cmd_Led);
}
Pace e Bene.. E RC
Ps. slow limit e' 1/3 della corsa tra neutro e max. Centratura servo in base al segnale a riposo della radio e sterzo in base al max valore letto dalla radio.