Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void ReadSensors(){
- // nolasam pogas
- buttonStateUp = digitalRead(buttonUp);
- buttonStateDown = digitalRead(buttonDown);
- buttonStateLeft = digitalRead(buttonLeft);
- buttonStateRight = digitalRead(buttonRight);
- // nolasam analogās ieejas
- // pievēršiet uzmanību savu sensoru slēgumiem!!!
- lineLeftAnalog = analogRead(sens2);
- lineRightAnalog = analogRead(sens4);
- opponentLeftAnalog = analogRead(sens3);
- opponentCenterAnalog = analogRead(sens5);
- opponentRightAnalog = analogRead(sens6);
- // no sensoru analogā signāla, izmantojot sliekšņa vērtību,
- // nosakam vai ir nostrādājis kāds no sensoriem
- // līnijas sensori
- // 450 - sliekšņa vērtība (ja līnijas sensors rādā mazāk par 450,
- // tad pieņemam, ka robots redz līniju
- if(lineLeftAnalog < 550){ lineLeft = true; }else{ lineLeft = false; }
- if(lineRightAnalog < 350){ lineRight = true; }else{ lineRight = false; }
- // pretinieka sensori
- // 300 - sliekšņa vērtība (ja pretinieka sensors rādā vairāk par 300, tad pieņemam, ka robots redz pretinieku
- if(opponentLeftAnalog > 300){ opponentLeft = true; }else{ opponentLeft = false; }
- if(opponentCenterAnalog > 300){ opponentCenter = true; }else{ opponentCenter = false; }
- if(opponentRightAnalog > 300){ opponentRight = true; }else{ opponentRight = false; }
- // nolasam Roombas sensorus
- // robotam pieprasam datus ne biežāk par 50 ms
- if(millis() - sensor_request_time >= 50){
- sensor_request_time = millis(); // saglabājam pašreizējo laiku
- // mainīgais, kurš skaitīs saņemto baitu skaitu
- byte incomming_byte_counter = 0;
- /*Serial1.write(149); // sensoru nolasīšanas komanda
- Serial1.write(6); // nolasāmo sensoru skaits
- Serial1.write(7); // bamperis*/
- Serial1.write(149);
- //Serial1.write(6);
- Serial1.write(1);
- Serial1.write(18); // pogas
- /*Serial1.write(28); // kreisais pakāpiena sensors
- Serial1.write(29); // kreisais priekšējais pakāpiena sensors
- Serial1.write(30); // labais priekšējais pakāpiena sensors
- Serial1.write(31); // labais pakāpiena sensors*/
- // gaidam jaunus datus, ja saņemto baitu skaits ir mazāks par 10 un laiks, kurā gaidam datus ir mazāks par 50 ms
- while((incomming_byte_counter < 10) && (millis() - sensor_request_time < 50)){
- // ja dati ir pienākuši pa seriālo portu no robota
- if(Serial1.available()){
- // nolasam vienu saņemto baitu un saglabājam datu masīvā
- incomming_byte_array[incomming_byte_counter] = Serial1.read();
- // palielinam baitu skaitītāju
- incomming_byte_counter++;
- }
- }
- buttons = incomming_byte_array[0];
- /*// par cik bampera baitā ir iekšā arī riteņu izkrišanas dati, tad nolasam tikai vajadzīgos bitus
- if((incomming_byte_array[0] & 2) == 0){ bumper_left = false; }else{ bumper_left = true; }
- if((incomming_byte_array[0] & 1) == 0){ bumper_right = false; }else{ bumper_right = true; }
- buttons = incomming_byte_array[1];
- cliff_left_value = (incomming_byte_array[2] << 8) | incomming_byte_array[3];
- cliff_center_left_value = (incomming_byte_array[4] << 8) | incomming_byte_array[5];
- cliff_center_right_value = (incomming_byte_array[6] << 8) | incomming_byte_array[7];
- cliff_right_value = (incomming_byte_array[8] << 8) | incomming_byte_array[9];*/
- }
- }
- void sendSensorDataToPC(){
- Serial.print(buttons);
- /*Serial.print(buttonStateUp);
- Serial.print(buttonStateDown);
- Serial.print(buttonStateLeft);
- Serial.print(buttonStateRight);
- Serial.print(" line ");
- Serial.print(lineLeft);
- Serial.print(" ");
- Serial.print(lineRight);
- Serial.print(" opponent ");
- Serial.print(opponentLeft);
- Serial.print(" ");
- Serial.print(opponentCenter);
- Serial.print(" ");
- Serial.println(opponentRight);
- Serial.print("Kreisais bamperis: ");
- Serial.println(bumper_left, DEC);
- Serial.print("Labais bamperis: ");
- Serial.println(bumper_right, DEC);
- Serial.print("Pogas: ");
- Serial.println(buttons, DEC);
- Serial.print("Kreisais pakapiena sensors: ");
- Serial.println(cliff_left_value, DEC);
- Serial.print("Kreisais prieksejais pakapiena sensors: ");
- Serial.println(cliff_center_left_value, DEC);
- Serial.print("Labais prieksejais pakapiena sensors: ");
- Serial.println(cliff_center_right_value, DEC);
- Serial.print("Labais pakapiena sensors: ");
- Serial.println(cliff_right_value, DEC);*/
- Serial.println();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement