Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 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. bool autoOn;
  24. unsigned long timer; // Timer variable (needs to be in startup and loop)
  25. int unThrottleIn = 0;
  26. int triggerPin = 9;
  27. int echoPin = 8;
  28. float alpha = 0.1;
  29. float lastDistance = 0;
  30. bool dropped = false;
  31. double Setpoint, Input, Output;
  32. double preKp=7.5, preKi=1, preKd=0.5; // Values before drop (PLACEHOLDER)
  33. double postKp=7.5, postKi=1, postKd=0.5; // Values after drop (PLACEHOLDER)
  34. PID myPID(&Input, &Output, &Setpoint, preKp, preKi, preKd, DIRECT);
  35.  
  36.  
  37. // Custom functions
  38.  
  39. 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/
  40. digitalWrite(triggerPin, LOW);
  41. delayMicroseconds(2);
  42. digitalWrite(triggerPin, HIGH);
  43. delayMicroseconds(10);
  44. digitalWrite(triggerPin, LOW);
  45. float duration = pulseIn(echoPin,HIGH);
  46. float distance_cm = ((duration+ 106.63)/57.609); // Calibrated with team test data
  47. float smoothedDistance = alpha*distance_cm + (1-alpha)*lastDistance;
  48. lastDistance = smoothedDistance;
  49. // if(fabs(smoothedDistance-distance_cm)>10){ // Code to make it more responsive to fast changes in altitude
  50. // return distance_cm;
  51. // }
  52. // else{
  53. return smoothedDistance;
  54. // }
  55. }
  56.  
  57. // Standard functions
  58.  
  59. void setup() {
  60. Serial.begin(9600);
  61.  
  62. // Servo setup
  63. dropServo.attach(3);
  64. naze32.attach(6);
  65.  
  66. // SD card setup
  67. 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
  68. 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
  69. EEPROM.get(0, lognum); // EEPROM address for storing increment variable
  70. if(isnan(lognum)){EEPROM.put(0, 0);} // If the EEPROM has nothing in then start at 0
  71. int intLognum = round(lognum)+1; // Increment it
  72. EEPROM.put(0, float(intLognum));
  73. logname = "Log_"; // Name of the log files
  74. logname.concat(intLognum); // Add on the incremented float
  75. logname.concat(".csv"); // Make it a .csv
  76. Serial.println(logname);
  77. SD.begin(10);
  78. logfile = SD.open(logname, FILE_WRITE); // Create the log file and prepare it for writing to
  79. logfile.close();
  80.  
  81. if (SD.exists(logname)) {
  82. Serial.println("log exists.");
  83. } else {
  84. Serial.println("log doesn't exist.");
  85. }
  86.  
  87. // Ultrasonic setup
  88. pinMode(triggerPin, OUTPUT); // Sets the trigger pin, output
  89. pinMode(echoPin, INPUT); // Sets the echo pin, input
  90.  
  91.  
  92. // Accelerometer setup
  93. // 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
  94.  
  95. // Throttle logging setup
  96. pinMode(A0, INPUT);
  97. pinMode(5, INPUT);
  98. pinMode(4, OUTPUT);
  99. pinMode(6, OUTPUT);
  100. timer = millis(); // Start timer for logging
  101. dropServo.write(35);
  102.  
  103. // PID Setup
  104. myPID.SetOutputLimits(1098, 1900);
  105. Setpoint = 100;
  106.  
  107. }
  108.  
  109. void loop() {
  110. Input = ultrasonicRead();
  111.  
  112. if(pulseIn(A0, HIGH) < 1500){
  113. digitalWrite(4, LOW);
  114. myPID.SetMode(MANUAL);
  115. autoOn = false;
  116. unThrottleIn = (pulseIn(5, HIGH));
  117. // Serial.println(unThrottleIn);
  118. }
  119. else if(pulseIn(A0,HIGH) > 1500 && autoOn==false){
  120. myPID.SetMode(AUTOMATIC);
  121. autoOn = true;
  122. digitalWrite(4, HIGH);
  123. }
  124. else{
  125. if(dropped==true)
  126. {
  127. myPID.SetTunings(postKp, postKi, postKd);
  128. myPID.Compute();
  129. naze32.write(Output);
  130. unThrottleIn = Output;
  131. }
  132. else
  133. {
  134. myPID.SetTunings(preKp, preKi, preKd);
  135. myPID.Compute();
  136. naze32.write(Output);
  137. unThrottleIn = Output;
  138. }
  139. }
  140.  
  141. float currentAlt = ultrasonicRead();
  142. if(fabs(currentAlt-100) < 2 && dropped==false){
  143. dropServo.write(0);
  144. Serial.println("Opened!");
  145. logfile = SD.open(logname, FILE_WRITE);
  146. logfile.println("Servo opened!");
  147. logfile.close();
  148. delay(50);
  149. dropServo.write(50);
  150. dropped = true;
  151. };
  152.  
  153. // Read Z accelerometer (assumption that angle is small)
  154. // Integrate twice
  155. // Sensor fuse, complimentary filter <- output estimate altitude
  156.  
  157. // Write estimate altitude, throttle signal, and millis() to SD card
  158. unsigned long timeElapsed = (millis()-timer);
  159. String writeOut = "";
  160. writeOut.concat(timeElapsed);
  161. writeOut.concat(",");
  162. writeOut.concat(String(currentAlt));
  163. writeOut.concat(",");
  164. writeOut.concat(unThrottleIn);
  165.  
  166.  
  167. logfile = SD.open(logname, FILE_WRITE);
  168. logfile.println(writeOut);
  169. logfile.close();
  170. //Serial.println(writeOut);
  171. //Serial.println(currentAlt);
  172. Serial.println(unThrottleIn);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement