Advertisement
wrighizilla

stabilized rear steering control by throttle 2.0

May 5th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. /*
  2. I used an inexpensive nano board v3 ATmega 328p the pins used are VIN(+)and GND(-)
  3. you can feed the power from your bec (5-9 volt)
  4. the pins I used are: pin D7 throttle signal input. D8 front steering input signal. D10 rear steering signal output.
  5. -----------------------------------------
  6. basic setup instructions: first you get the best mechanical setup of the rear servo.
  7. the values you find by default should make the controller work quite right away.
  8. however if you want to be sure about what are your real radio signals,check them before starting this sketch.
  9. create a new tab and check your input signals with this program (7 for steering - 8 for throttle gas)
  10.  
  11. int rxpulse;
  12. void setup () {
  13. Serial.begin (9600);
  14. }
  15. void loop () {
  16. rxpulse = pulseIn (7, HIGH); // - 7 to know true front steering signal 8 throttle
  17. Serial.println (rxpulse);
  18.  
  19. you can read the values in the serial monitor by turning the steering wheel of the radio control and moving the throttle.
  20. write the values obtained in the first part of the setup, replace the default ones with yours send by transmitter.
  21. -------------------------------------------------- ------
  22. the user driving setup consists of 2 functions: you can choose how much throttle allow at low speed
  23. before the width of the rear servo starts to close (Speedlimit function).
  24.  
  25. the second function allows you to determine the rear servo width at maximum throttle (Max_gain).
  26. you can choose to close it completely or leave a width available to turn with 4 steering wheels even at maximum speed
  27.  
  28. the other functions are: tolerance that avoids the rhythmic vibrations of the loop.
  29. if the servo is good it could stay at zero, analog or low quality servos want at least a value of 4
  30. it can happen that you exceed with the values and invert the direction, be careful to find the best suitable values for your model.
  31. 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
  32. ---------
  33. there is a relationship between Slowlimit and Max_gain, it can happen that if you lower one you will have to raise the other
  34. however I am sure that it will take you little time to understand how it works
  35. */
  36.  
  37. //----------- signal setup -------------------------------------
  38. #define Neutral 1500 // -- minimum throttle forward signal
  39. #define Maxspeed 2000 // -- maximum throttle forward signal
  40. #define Antsx 1000 // -- in front servo signal sx
  41. #define Antdx 2000 // -- in front servo signal dx
  42. #define Postsx 2000 // out rear servo sx end point (if inverted with postdx it reverse)
  43. #define Postdx 1000 //-- out rear servo dx endpoint (if inverted with postsx it reverse)
  44. #define Center 30 //-- add or subtract your value to center steering (+- 50 or more)
  45.  
  46. //--------- user driving setup ------------------------------
  47.  
  48. #define Max_gain -400 //-- gain steering reduction width at max throttle (if reverse add (-) before value)
  49. #define Slowlimit 1800 //-- slow forward percentage without endpoint correction
  50.  
  51. #define Tolerance 0 //-- if poor quality servo vibrates try 5
  52.  
  53. //----------------------------
  54. #include <Servo.h>
  55. Servo myservo;
  56. #define N_STST 7
  57. #define N_STGAS 7
  58. unsigned int stSt[ N_STST ];
  59. unsigned int stGas[ N_STGAS ];
  60. long toStSt = 0;
  61. long toStGas = 0;
  62. int inSt = 0;
  63. int inGas = 0;
  64. unsigned int Rxpulse;
  65. unsigned int Gaspulse ;
  66. unsigned int Gain;
  67. unsigned int NewPos, OldPos;
  68. void setup() {
  69. for ( int i=0; i<N_STST; i++ ) stSt[ i ] = 0;
  70. for ( int i=0; i<N_STGAS; i++ ) stGas[ i ] = 0;
  71. myservo.attach(10); //-- rear servo signal out pin
  72. pinMode(8, INPUT); //-- front servo signal in pin
  73. pinMode(7, INPUT); //-- throttle signal in pin
  74. }
  75. void loop(){
  76. noInterrupts();
  77. Rxpulse = pulseIn(8, HIGH);
  78. Gaspulse = pulseIn(7, HIGH);
  79. interrupts();
  80. delay(5);
  81. if (Gaspulse > Slowlimit) {
  82. Gain = map(Gaspulse, Slowlimit, Maxspeed, 0, Max_gain );
  83. NewPos = map(Rxpulse, Antsx, Antdx, (Postsx + Gain), (Postdx - Gain));
  84. }
  85. else {
  86. NewPos = map(Rxpulse, Antsx, Antdx, Postsx, Postdx);
  87. }
  88. if (abs(NewPos - OldPos)> Tolerance) {
  89. OldPos = NewPos;
  90. myservo.write(NewPos + Center);
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement