Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- I used an inexpensive nano board v3 ATmega 328p the pins used are VIN(+)and GND(-)
- you can feed the power from your bec (5-9 volt)
- the pins I used are: pin D7 throttle signal input. D8 front steering input signal. D10 rear steering signal output.
- -----------------------------------------
- basic setup instructions: first you get the best mechanical setup of the rear servo.
- the values you find by default should make the controller work quite right away.
- however if you want to be sure about what are your real radio signals,check them before starting this sketch.
- create a new tab and check your input signals with this program (7 for steering - 8 for throttle gas)
- int rxpulse;
- void setup () {
- Serial.begin (9600);
- }
- void loop () {
- rxpulse = pulseIn (7, HIGH); // - 7 to know true front steering signal 8 throttle
- Serial.println (rxpulse);
- you can read the values in the serial monitor by turning the steering wheel of the radio control and moving the throttle.
- write the values obtained in the first part of the setup, replace the default ones with yours send by transmitter.
- -------------------------------------------------- ------
- the user driving setup consists of 2 functions: you can choose how much throttle allow at low speed
- before the width of the rear servo starts to close (Speedlimit function).
- the second function allows you to determine the rear servo width at maximum throttle (Max_gain).
- you can choose to close it completely or leave a width available to turn with 4 steering wheels even at maximum speed
- the other functions are: tolerance that avoids the rhythmic vibrations of the loop.
- if the servo is good it could stay at zero, analog or low quality servos want at least a value of 4
- it can happen that you exceed with the values and invert the direction, be careful to find the best suitable values for your model.
- to get the servo reverse you just have to exchange the Postsx Postdx rear steering values and put a sign (-) in front of the Max_gain value
- ---------
- there is a relationship between Slowlimit and Max_gain, it can happen that if you lower one you will have to raise the other
- however I am sure that it will take you little time to understand how it works
- */
- //----------- signal setup -------------------------------------
- #define Neutral 1500 // -- minimum throttle forward signal
- #define Maxspeed 2000 // -- maximum throttle forward signal
- #define Antsx 1000 // -- in front servo signal sx
- #define Antdx 2000 // -- in front servo signal dx
- #define Postsx 2000 // out rear servo sx end point (if inverted with postdx it reverse)
- #define Postdx 1000 //-- out rear servo dx endpoint (if inverted with postsx it reverse)
- #define Center 30 //-- add or subtract your value to center steering (+- 50 or more)
- //--------- user driving setup ------------------------------
- #define Max_gain -400 //-- gain steering reduction width at max throttle (if reverse add (-) before value)
- #define Slowlimit 1800 //-- slow forward percentage without endpoint correction
- #define Tolerance 0 //-- if poor quality servo vibrates try 5
- //----------------------------
- #include <Servo.h>
- Servo myservo;
- #define N_STST 7
- #define N_STGAS 7
- unsigned int stSt[ N_STST ];
- unsigned int stGas[ N_STGAS ];
- long toStSt = 0;
- long toStGas = 0;
- int inSt = 0;
- int inGas = 0;
- unsigned int Rxpulse;
- unsigned int Gaspulse ;
- unsigned int Gain;
- unsigned int NewPos, OldPos;
- void setup() {
- for ( int i=0; i<N_STST; i++ ) stSt[ i ] = 0;
- for ( int i=0; i<N_STGAS; i++ ) stGas[ i ] = 0;
- 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(){
- noInterrupts();
- Rxpulse = pulseIn(8, HIGH);
- Gaspulse = pulseIn(7, HIGH);
- interrupts();
- delay(5);
- if (Gaspulse > Slowlimit) {
- Gain = map(Gaspulse, Slowlimit, Maxspeed, 0, Max_gain );
- NewPos = map(Rxpulse, Antsx, Antdx, (Postsx + Gain), (Postdx - Gain));
- }
- else {
- NewPos = map(Rxpulse, Antsx, Antdx, Postsx, Postdx);
- }
- if (abs(NewPos - OldPos)> Tolerance) {
- OldPos = NewPos;
- myservo.write(NewPos + Center);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement