Advertisement
Guest User

Untitled

a guest
Apr 9th, 2012
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #include "Ultrasonic.h"
  2. #include <Servo.h>
  3. #include <Wire.h>
  4. #include <Adafruit_BMP085.h>
  5.  
  6.  
  7. Servo myThrottleChOut;
  8.  
  9. Adafruit_BMP085 bmp; //This is the barosensor. VCC to Ard3v, GND to ArdGnd,
  10.  
  11. // Connect VCC of the BMP085 sensor to 3.3V (NOT 5.0V!)
  12. // Connect GND to Ground
  13. // Connect SCL to i2c clock - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 5
  14. // Connect SDA to i2c data - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 4
  15. // EOC is not used, it signifies an end of conversion
  16. // XCLR is a reset pin, also not used here
  17.  
  18. int throttlePin1 = 11;
  19. int pinReadCh6 = 6; //Reads channel 6 from RX
  20. int pinReadCh3 = 3; //Reads channel 3 from RX
  21.  
  22. unsigned long duration1;
  23. unsigned long duration2;
  24. unsigned long duration3; //duration to use inside the altitude hold loop
  25.  
  26. Ultrasonic ultrasonic(12,13); //12 = digitalpin 12 -> trig; 13 = digitalpin 13 -> echo;
  27.  
  28.  
  29. //Future implementation:
  30. //If the ch6 is turned to max arduino enters altitude control
  31. //while in the altitude control - read ch3 (throttle).
  32. //if ch3 > 50% hold the altitude
  33. //if ch3 < 50% decrease throttle just below hoover level to make tricopter land slowly.
  34.  
  35. void setup() {
  36. Serial.begin(9600);
  37.  
  38. bmp.begin();
  39.  
  40. pinMode(pinReadCh3, INPUT);
  41. pinMode(pinReadCh6, INPUT);
  42. myThrottleChOut.attach(throttlePin1); //attach the "servo" to digital pin 7, this is the digital pin which is going to write to the KKboards throttle channel.
  43.  
  44. }
  45.  
  46. void loop()
  47. {
  48.  
  49. duration1 = pulseIn(pinReadCh3, HIGH); //Reads the throttle level, returns 1150 - 1839 microseconds. 1 millisecond = 1000 microseconds -> duration1/1000: ex 1,736ms
  50. duration2 = pulseIn(pinReadCh6, HIGH); //Reads the ch6 level
  51.  
  52. //Serial.print(bmp.readPressure());
  53.  
  54.  
  55. if (duration2 > 1700) //If the ch6 knob is turned almost to max, arduino enter altitude hold mode
  56. {
  57. int distance = (ultrasonic.Ranging(CM)); //Gets the altitude when entering altitude hold mode, this will be a reference //Serial.print(bmp.readPressure());
  58. while (duration2 > 1700) //If the ch6 knob is turned almost to max, arduino enters altitude control loop.
  59. duration3 = duration1;
  60. {
  61. int newDistance = (ultrasonic.Ranging(CM)); //Gets the current altitude, this will be compared to the reference altitude //Serial.print(bmp.readPressure());
  62. if (newDistance > distance + 15) //+ 10 will give an intervall between desired altitude +- 10 cm in the upper and lower bounds.
  63. {
  64. //TODO: implement algorithm to calculate how much to decrease on throttle depending
  65. // on how far away the newDistance is from the desired reference altitude (distance).
  66. //close to reference alt: small increments to throttle
  67. //far away from reference alt: big increments to throttle
  68. if(duration3 > 1550) //this is to prevent the tricopter to throttling down to no power and fall from the sky
  69. {
  70. duration3 -= 5;
  71. }
  72. myThrottleChOut.writeMicroseconds(duration3);
  73. }
  74. else if(newDistance < distance - 15) // - 10 will give an intervall between desired altitude +- 10 cm in the upper and lower bounds.
  75. {
  76. if(duration3 < 1680) //this is to prevent the tricopter from throttling up to max throttle
  77. {
  78. duration3 += 5;
  79. }
  80. myThrottleChOut.writeMicroseconds(duration3);
  81. }
  82. delay(20);
  83. duration2 = pulseIn(pinReadCh6, HIGH); //Reads the ch6 level, this is needed to be able to exit the while-iteration
  84. }
  85.  
  86.  
  87. }
  88. else
  89. {
  90. myThrottleChOut.writeMicroseconds(duration1);
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. delay(20);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement