Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #define X_ZERO 332
  2. #define Y_ZERO 324
  3. #define Z_ZERO 396
  4. #define PITCH_ZERO 249
  5. #define ROLL_ZERO 249
  6. #define YAW_ZERO 248
  7.  
  8. #define GYRO_CON 1.47
  9. #define ACCEL_CON 0.93
  10.  
  11. #define TIME_CON 0.02
  12. #define SEN_CON 0.95
  13.  
  14. //motor speed vars
  15. int speeds[4];
  16.  
  17. //gyro inputs - current tilt vars
  18. float pitch, roll, yaw;
  19. int pitchzero, rollzero;
  20. //accelerometer inputs - current acceleration vars
  21. float xin, yin, zin;
  22.  
  23. //human inputs - control info vars
  24. float pitchin, rollin, yawin, zhuman;
  25.  
  26. //random other vars
  27. float xaverage=0, yaverage=0;
  28. int y=0;
  29. int blah;
  30.  
  31. //proportionality constants
  32. float p=2.5; // P proportionality constant
  33. float d=0.5; // D proportionality constant
  34.  
  35. void setup() {
  36. zhuman=0;
  37. rollin=0;
  38. Serial.begin(9600);
  39. for(int x=6; x<10; x++) {
  40. pinMode(x, OUTPUT);
  41. }
  42.  
  43. //send upper bound for human inputs to the motor speed controllers
  44. for(int x=6; x<10; x++) {
  45. pulsout(x,2000);
  46. }
  47. delay(5000);
  48.  
  49. //get zeros for pitch and roll human inputs
  50. for(int x=0; x<10; x++) {
  51. y=y+analogRead(3);
  52. }
  53. pitchzero=y/10;
  54. y=0;
  55. for(int x=0; x<10; x++) {
  56. y=y+analogRead(4);
  57. }
  58. rollzero=y/10;
  59. }
  60.  
  61. void loop () {
  62. //accelerometer and gyro inputs ranged -232 to 232?
  63. xin=(analogRead(0)-X_ZERO)*ACCEL_CON;
  64. yin=(analogRead(1)-Y_ZERO)*ACCEL_CON;
  65. zin=(analogRead(2)-Z_ZERO)*ACCEL_CON;
  66. pitch=(pitchzero-analogRead(3))*GYRO_CON;
  67. roll=(rollzero-analogRead(4))*GYRO_CON;
  68. yaw=(analogRead(5)-YAW_ZERO)*GYRO_CON;
  69.  
  70. //get human inputs through radio here range of -30 to 30 except for zhuman which has an ideal range of 1000-2000, only 2 pulses per loop
  71. if(blah==0) {
  72. yawin=0.06*((signed int) pulseIn(2,HIGH)-1500);
  73. pitchin=0.06*((signed int) pulseIn(3,HIGH)-1500);
  74. blah=1;
  75. }
  76. else {
  77. zhuman=(signed int) pulseIn(4,HIGH);
  78. rollin=0.06*((signed int) pulseIn(5,HIGH)-1400); //1400 instead of 1500 is to correct for the underpowered motor #4 by trimming it in code
  79. blah=0;
  80. }
  81.  
  82. //averaging, etc.
  83. xaverage= SEN_CON *( xaverage + TIME_CON * pitch) + ( 1 - SEN_CON ) * xin;
  84. yaverage= SEN_CON *( yaverage + TIME_CON * roll) + ( 1 - SEN_CON ) * yin;
  85.  
  86. //calculate the motor speeds
  87. if(zhuman<1150) {
  88. for(int x=0; x<4; x++) {
  89. speeds[x]=zhuman;
  90. }
  91. }
  92. else {
  93. if(zhuman > 1450) {
  94. zhuman = 1450;
  95. }
  96. speeds[0] = zhuman - p*(xaverage - pitchin) - p*(yawin) - d*pitch;
  97. speeds[1] = zhuman - p*(pitchin - xaverage) - p*(yawin) + d*pitch;
  98. speeds[2] = zhuman - p*(yaverage - rollin) + p*(yawin) - d*roll;
  99. speeds[3] = zhuman - p*(rollin - yaverage) + p*(yawin) + d*roll;
  100. }
  101. //set the upper and lower bounds for motor speeds (1000=no speed, 1600=upper speed limit, 2000=maximum possible speed)
  102. for(int x=0; x<4; x++) {
  103. //speed limit between 1000 and 1600
  104. if(speeds[x]<1000) {
  105. speeds[x]=1000;
  106. }
  107. if(speeds[x]>1600) {
  108. speeds[x]=1600;
  109. }
  110. }
  111.  
  112. //pulsouts to motor speed controllers
  113. for(int x=0; x<4; x++) {
  114. pulsout(x+6,speeds[x]);
  115. }
  116. }
  117. void pulsout (int pin, int duration) {
  118. digitalWrite(pin, HIGH);
  119. delayMicroseconds(duration);
  120. digitalWrite(pin, LOW);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement