Advertisement
foxyBB

Untitled

May 16th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. void ReadSensors(){
  2. // nolasam pogas
  3. buttonStateUp = digitalRead(buttonUp);
  4. buttonStateDown = digitalRead(buttonDown);
  5. buttonStateLeft = digitalRead(buttonLeft);
  6. buttonStateRight = digitalRead(buttonRight);
  7.  
  8. // nolasam analogās ieejas
  9.  
  10. // pievēršiet uzmanību savu sensoru slēgumiem!!!
  11. lineLeftAnalog = analogRead(sens2);
  12. lineRightAnalog = analogRead(sens4);
  13.  
  14. opponentLeftAnalog = analogRead(sens3);
  15. opponentCenterAnalog = analogRead(sens5);
  16. opponentRightAnalog = analogRead(sens6);
  17.  
  18. // no sensoru analogā signāla, izmantojot sliekšņa vērtību,
  19. // nosakam vai ir nostrādājis kāds no sensoriem
  20.  
  21. // līnijas sensori
  22. // 450 - sliekšņa vērtība (ja līnijas sensors rādā mazāk par 450,
  23. // tad pieņemam, ka robots redz līniju
  24. if(lineLeftAnalog < 550){ lineLeft = true; }else{ lineLeft = false; }
  25. if(lineRightAnalog < 350){ lineRight = true; }else{ lineRight = false; }
  26.  
  27. // pretinieka sensori
  28. // 300 - sliekšņa vērtība (ja pretinieka sensors rādā vairāk par 300, tad pieņemam, ka robots redz pretinieku
  29. if(opponentLeftAnalog > 300){ opponentLeft = true; }else{ opponentLeft = false; }
  30. if(opponentCenterAnalog > 300){ opponentCenter = true; }else{ opponentCenter = false; }
  31. if(opponentRightAnalog > 300){ opponentRight = true; }else{ opponentRight = false; }
  32.  
  33. // nolasam Roombas sensorus
  34.  
  35. // robotam pieprasam datus ne biežāk par 50 ms
  36. if(millis() - sensor_request_time >= 50){
  37.  
  38. sensor_request_time = millis(); // saglabājam pašreizējo laiku
  39. // mainīgais, kurš skaitīs saņemto baitu skaitu
  40. byte incomming_byte_counter = 0;
  41.  
  42. /*Serial1.write(149); // sensoru nolasīšanas komanda
  43. Serial1.write(6); // nolasāmo sensoru skaits
  44.  
  45. Serial1.write(7); // bamperis*/
  46. Serial1.write(149);
  47. //Serial1.write(6);
  48. Serial1.write(1);
  49. Serial1.write(18); // pogas
  50. /*Serial1.write(28); // kreisais pakāpiena sensors
  51. Serial1.write(29); // kreisais priekšējais pakāpiena sensors
  52. Serial1.write(30); // labais priekšējais pakāpiena sensors
  53. Serial1.write(31); // labais pakāpiena sensors*/
  54.  
  55. // gaidam jaunus datus, ja saņemto baitu skaits ir mazāks par 10 un laiks, kurā gaidam datus ir mazāks par 50 ms
  56. while((incomming_byte_counter < 10) && (millis() - sensor_request_time < 50)){
  57.  
  58. // ja dati ir pienākuši pa seriālo portu no robota
  59. if(Serial1.available()){
  60. // nolasam vienu saņemto baitu un saglabājam datu masīvā
  61. incomming_byte_array[incomming_byte_counter] = Serial1.read();
  62.  
  63. // palielinam baitu skaitītāju
  64. incomming_byte_counter++;
  65. }
  66. }
  67.  
  68. buttons = incomming_byte_array[0];
  69.  
  70. /*// par cik bampera baitā ir iekšā arī riteņu izkrišanas dati, tad nolasam tikai vajadzīgos bitus
  71. if((incomming_byte_array[0] & 2) == 0){ bumper_left = false; }else{ bumper_left = true; }
  72. if((incomming_byte_array[0] & 1) == 0){ bumper_right = false; }else{ bumper_right = true; }
  73.  
  74. buttons = incomming_byte_array[1];
  75.  
  76. cliff_left_value = (incomming_byte_array[2] << 8) | incomming_byte_array[3];
  77. cliff_center_left_value = (incomming_byte_array[4] << 8) | incomming_byte_array[5];
  78. cliff_center_right_value = (incomming_byte_array[6] << 8) | incomming_byte_array[7];
  79. cliff_right_value = (incomming_byte_array[8] << 8) | incomming_byte_array[9];*/
  80.  
  81. }
  82. }
  83.  
  84. void sendSensorDataToPC(){
  85. Serial.print(buttons);
  86. /*Serial.print(buttonStateUp);
  87. Serial.print(buttonStateDown);
  88. Serial.print(buttonStateLeft);
  89. Serial.print(buttonStateRight);
  90.  
  91.  
  92. Serial.print(" line ");
  93. Serial.print(lineLeft);
  94. Serial.print(" ");
  95. Serial.print(lineRight);
  96.  
  97. Serial.print(" opponent ");
  98. Serial.print(opponentLeft);
  99. Serial.print(" ");
  100. Serial.print(opponentCenter);
  101. Serial.print(" ");
  102. Serial.println(opponentRight);
  103.  
  104. Serial.print("Kreisais bamperis: ");
  105. Serial.println(bumper_left, DEC);
  106.  
  107. Serial.print("Labais bamperis: ");
  108. Serial.println(bumper_right, DEC);
  109.  
  110. Serial.print("Pogas: ");
  111. Serial.println(buttons, DEC);
  112.  
  113. Serial.print("Kreisais pakapiena sensors: ");
  114. Serial.println(cliff_left_value, DEC);
  115.  
  116. Serial.print("Kreisais prieksejais pakapiena sensors: ");
  117. Serial.println(cliff_center_left_value, DEC);
  118.  
  119. Serial.print("Labais prieksejais pakapiena sensors: ");
  120. Serial.println(cliff_center_right_value, DEC);
  121.  
  122. Serial.print("Labais pakapiena sensors: ");
  123. Serial.println(cliff_right_value, DEC);*/
  124.  
  125.  
  126.  
  127. Serial.println();
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement