Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #include "mbed.h"
  2. #include "HCSR04.h"
  3.  
  4. #define DIST_OBSTACULO 10.0
  5.  
  6.  
  7. //Serial pc(USBTX,USBRX);
  8. InterruptIn encoderDireita(PTA13);
  9. // Ticker periodic;
  10. Ticker flip;
  11.  
  12. PwmOut IN1(PTA12); //amarelo PTA12
  13. PwmOut IN2(PTA5); //verde PTA5
  14. PwmOut IN3(PTC8); //azul PTC8
  15. PwmOut IN4(PTD4); //roxo PTA1
  16.  
  17. Serial pc(USBTX, USBRX); // tx, rx
  18. HCSR04 sonar(PTA13, PTD5);
  19.  
  20. int xAlvo = 500;
  21. int yAlvo = 500;
  22. int xRestante = xAlvo;
  23. int yRestante = yAlvo;
  24. bool completo = false;
  25. bool obstaculo = false;
  26. int pwm = 5;
  27. int pulsos = 0;
  28.  
  29. //////////////////////////////////////////////////////////////
  30. /////////// encoder //////////////////////////////////////////
  31. //////////////////////////////////////////////////////////////
  32.  
  33. void increment() {
  34. pulsos++;
  35. }
  36.  
  37. //////////////////////////////////////////////////////////////
  38. /////////// ultrasom /////////////////////////////////////////
  39. //////////////////////////////////////////////////////////////
  40.  
  41. void chamaSonar(){
  42. //pc.printf("Leitura sonar: %.2f \n\r", sonar.getCm());
  43. }
  44.  
  45. bool existeObstaculo() {
  46. pc.printf("tentativa 1...\r\n");
  47. bool tentativa1 = (sonar.getCm() < DIST_OBSTACULO);
  48. wait_ms(100);
  49. pc.printf("tentativa 2...\r\n");
  50. bool tentativa2 = (sonar.getCm() < DIST_OBSTACULO);
  51. wait_ms(100);
  52. pc.printf("tentativa 3...\r\n");
  53. bool tentativa3 = (sonar.getCm() < DIST_OBSTACULO);
  54. wait_ms(100);
  55. return tentativa1 || tentativa2 || tentativa3;
  56. }
  57.  
  58. //////////////////////////////////////////////////////////////
  59. /////////// motor ////////////////////////////////////////////
  60. //////////////////////////////////////////////////////////////
  61.  
  62.  
  63. void ptras(){
  64. IN1=0;
  65. IN2.pulsewidth_ms(pwm);
  66. IN3.pulsewidth_ms(pwm);
  67. IN4=0;
  68. }
  69.  
  70. void pfrente(){
  71. IN1.pulsewidth_ms(pwm);
  72. IN2=0;
  73. IN3=0;
  74. IN4.pulsewidth_ms(pwm);
  75. }
  76.  
  77. void stop(){
  78. //ptras();
  79. IN1=0;
  80. IN2=0;
  81. IN3=0;
  82. IN4=0;
  83. }
  84.  
  85. void seguranca(){
  86. IN1.period_ms(10);
  87. IN2.period_ms(10);
  88. IN3.period_ms(10);
  89. IN4.period_ms(10);
  90. stop();
  91. }
  92. void g90esquerda(){
  93. IN1=0;
  94. IN2.pulsewidth_ms(pwm);
  95. IN3=0;
  96. IN4.pulsewidth_ms(pwm);
  97. wait(2);
  98. }
  99.  
  100. void g90direita(){
  101. IN1.pulsewidth_ms(pwm);
  102. IN2=0;
  103. IN3.pulsewidth_ms(pwm);
  104. IN4=0;
  105. wait(2);
  106. }
  107.  
  108. int sobramPulsosX() {
  109. return (pulsosEsq < xRestanteEsq) * 1 + (pulsosDir < xRestanteDir) * 2;
  110. }
  111.  
  112. int sobramPulsosY() {
  113. return (pulsosEsq < yRestanteEsq) * 1 + (pulsosDir < yRestanteDir) * 2;
  114. }
  115.  
  116. int motorFrente(int comando /* 1 apenas esq, 2 apenas dir, 3 ambos*/) {
  117. // Motor esquerdo
  118. if((comando == 1) || (comando == 3)) {
  119. IN1.pulsewidth_ms(pwm);
  120. } else {
  121. IN1 = 0;
  122. }
  123. IN2=0;
  124. // Motor direito
  125. IN3=0;
  126. if((comando == 2) || (comando == 3)) {
  127. IN4.pulsewidth_ms(pwm);
  128. } else {
  129. IN4 = 0;
  130. }
  131. }
  132.  
  133.  
  134. int main() {
  135. int pulsosRestantes;
  136. encoderDireita.rise(&increment);
  137. //flip.attach(&chamaSonar, 2.0);
  138.  
  139. seguranca();
  140. while(!completo) {
  141. // Ir na direcao x ate acabar ou encontrar obstaculo
  142. pulsosRestantes = sobramPulsosX();
  143. while (pulsosRestantes && !obstaculo) {
  144. motorFrente(pulsosRestantes);
  145. pc.printf("obstaculo detectado: %s\r\n", obstaculo ? "sim" : "nao");
  146. obstaculo = existeObstaculo();
  147. wait_ms(500);
  148. }
  149.  
  150. // while (pulsos < xRestante && !obstaculo) {
  151. // pc.printf("pfrente();\r\n");
  152. // obstaculo = existeObstaculo();
  153. // pc.printf("obstaculo detectado: %s\r\n", obstaculo ? "sim" : "nao");
  154. // wait_ms(500);
  155. // }
  156. // Atualizar condicoes
  157. xRestante = xRestante - pulsos;
  158.  
  159. // Fazer curva
  160. wait(0.5);
  161. pc.printf("stop();\r\n");
  162. wait(3);
  163. pc.printf("g90esquerda();\r\n");
  164. pc.printf("stop();\r\n");
  165. wait(3);
  166.  
  167. // Resetar variaveis
  168. pulsos = 0;
  169. obstaculo = false;
  170.  
  171. // Ir na direcao y ate acabar ou encontrar obstaculo
  172. while (pulsos < yRestante && !obstaculo) {
  173. pc.printf("pfrente();\r\n");
  174. obstaculo = existeObstaculo();
  175. pc.printf("obstaculo detectado: %s\r\n", obstaculo ? "sim" : "nao");
  176. wait_ms(500);
  177. }
  178. // Atualizar condicoes
  179. yRestante = yRestante - pulsos;
  180.  
  181. if (yRestante <= 0 && xRestante <= 0) {
  182. completo = true;
  183. } else {
  184. wait(0.5);
  185. pc.printf("stop();\r\n");
  186. wait(3);
  187. pc.printf("g90direita();\r\n");
  188. pc.printf("stop();\r\n");
  189. wait(3);
  190. pulsos = 0;
  191. obstaculo = false;
  192. }
  193. }
  194. pc.printf("stop();\r\n");
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement