Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // Simple altitude logging code for first flight test. Used to gather data on system dynamics. Written by Euan French. //
  3. // Future changes: - Include battery voltage measurement and compensation, somehow. //
  4. // - Include mass change compensation. (This and previous could be done by estimating hover throttle) //
  5. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  6.  
  7. #include <SD.h>
  8. #include <SPI.h>
  9. #include <EEPROM.h>
  10. #include <Servo.h>
  11. #include <PID_v1.h>
  12.  
  13.  
  14.  
  15. // Pins currently in use: 5 for throttle in, 6 for throttle out, 8 for echo pin, 9 for trigger pin, 3 for servo
  16.  
  17. // Global variables
  18. Servo dropServo;
  19. Servo naze32;
  20. File logfile;
  21. String logname; // This might not have to be global but it's no biggie
  22. int servoPos;
  23. unsigned long timer; // Timer variable (needs to be in startup and loop)
  24. int unThrottleIn = 0;
  25. int triggerPin = 9;
  26. int echoPin = 8;
  27. float alpha = 0.02;
  28. float lastDistance = 0;
  29. bool dropped = false;
  30. double Setpoint, Input, Output;
  31. double preKp=4, preKi=0.2, preKd=1; // Values before drop (PLACEHOLDER)
  32. double postKp=1, postKi=0.05, postKd=0.25; // Values after drop (PLACEHOLDER)
  33. PID myPID(&Input, &Output, &Setpoint, preKp, preKi, preKd, DIRECT);
  34.  
  35.  
  36. // Custom functions
  37.  
  38. float ultrasonicRead() { // Uses delays, but they're tiny, so I think we can get away with it. Ripped from https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/
  39. digitalWrite(triggerPin, LOW);
  40. delayMicroseconds(2);
  41. digitalWrite(triggerPin, HIGH);
  42. delayMicroseconds(10);
  43. digitalWrite(triggerPin, LOW);
  44. float duration = pulseIn(echoPin,HIGH);
  45. float distance_cm = ((duration+ 106.63)/57.609); // Calibrated with team test data
  46. float smoothedDistance = alpha*distance_cm + (1-alpha)*lastDistance;
  47. lastDistance = smoothedDistance;
  48. return smoothedDistance;
  49. }
  50.  
  51. // Standard functions
  52.  
  53. void setup() {
  54. Serial.begin(9600);
  55.  
  56. // Servo setup
  57. dropServo.attach(3);
  58. naze32.attach(7);
  59.  
  60. // SD card setup
  61. pinMode(10, OUTPUT); // Using pin 10 for SD card but could change it according to https://learn.adafruit.com/adafruit-micro-sd-breakout-board-card-tutorial?view=all
  62. float lognum; // Increment variable, lets us make a new log for each flight, MAKE SURE YOU HAVE SET THIS AS 0 OR SOME OTHER FLOAT FIRST, USING ANOTHER SKETCH
  63. EEPROM.get(0, lognum); // EEPROM address for storing increment variable
  64. if(isnan(lognum)){EEPROM.put(0, 0);} // If the EEPROM has nothing in then start at 0
  65. int intLognum = round(lognum)+1; // Increment it
  66. EEPROM.put(0, float(intLognum));
  67. logname = "Log_"; // Name of the log files
  68. logname.concat(intLognum); // Add on the incremented float
  69. logname.concat(".csv"); // Make it a .csv
  70. Serial.println(logname);
  71. SD.begin(10);
  72. logfile = SD.open(logname, FILE_WRITE); // Create the log file and prepare it for writing to
  73. logfile.close();
  74.  
  75. if (SD.exists(logname)) {
  76. Serial.println("log exists.");
  77. } else {
  78. Serial.println("log doesn't exist.");
  79. }
  80.  
  81. // Ultrasonic setup
  82. pinMode(triggerPin, OUTPUT); // Sets the trigger pin, output
  83. pinMode(echoPin, INPUT); // Sets the echo pin, input
  84.  
  85.  
  86. // Accelerometer setup
  87. // This needs that whole FrSky telemetry decoding shit that i really dont want to write right now, so we'll just use the ultrasonic for now
  88.  
  89. // Throttle logging setup
  90. pinMode(A0, INPUT);
  91. pinMode(5, INPUT);
  92. pinMode(4, OUTPUT);
  93. pinMode(6, OUTPUT);
  94. timer = millis(); // Start timer for logging
  95. dropServo.write(30);
  96.  
  97. // PID Setup
  98. myPID.SetOutputLimits(1098, 1900);
  99. Input = ultrasonicRead();
  100. Setpoint = 80;
  101.  
  102. }
  103.  
  104. void loop() {
  105.  
  106. if(pulseIn(A0, HIGH) < 1500){
  107. digitalWrite(4, LOW);
  108. myPID.SetMode(MANUAL);
  109. unThrottleIn = (pulseIn(5, HIGH));
  110. // Serial.println(unThrottleIn);
  111. }
  112. else{
  113. myPID.SetMode(AUTOMATIC);
  114. digitalWrite(4, HIGH);
  115. if(dropped==true)
  116. {
  117. myPID.SetTunings(postKp, postKi, postKd);
  118. myPID.Compute();
  119. naze32.write(Output);
  120. unThrottleIn = Output;
  121. }
  122. else
  123. {
  124. myPID.SetTunings(preKp, preKi, preKd);
  125. myPID.Compute();
  126. naze32.write(Output);
  127. unThrottleIn = Output;
  128. }
  129. }
  130.  
  131. float currentAlt = ultrasonicRead();
  132. if(fabs(currentAlt-80) < 5 && dropped==false){
  133. dropServo.write(0);
  134. Serial.println("Opened!");
  135. logfile = SD.open(logname, FILE_WRITE);
  136. logfile.println("Servo opened!");
  137. logfile.close();
  138. delay(50);
  139. dropServo.write(50);
  140. dropped = true;
  141. };
  142.  
  143. // Read Z accelerometer (assumption that angle is small)
  144. // Integrate twice
  145. // Sensor fuse, complimentary filter <- output estimate altitude
  146.  
  147. // Write estimate altitude, throttle signal, and millis() to SD card
  148. unsigned long timeElapsed = (millis()-timer);
  149. String writeOut = "";
  150. writeOut.concat(String(currentAlt));
  151. writeOut.concat(",");
  152. writeOut.concat(unThrottleIn);
  153. writeOut.concat(",");
  154. writeOut.concat(timeElapsed);
  155. logfile = SD.open(logname, FILE_WRITE);
  156. logfile.println(writeOut);
  157. logfile.close();
  158. //Serial.println(writeOut);
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement