Προσπαθω να τροποποιησω τον κωδικα που καναμε μαζι με τον orfeus για την ανιχνευση εμποδιων μπροστα απο το "ΘΕΡΙΟ".
Το σκεπτικο ειναι αυτο που φαινεται στην φωτο. 2 σενσορες πανω σε 2 σερβο.Ο πανω να κανει οριζοντια σαρωση και να βλεπει εμποδια
και ο κατω να κανει καθετη σαρωση 2 σημειων , 90 και 180 μοιρων, για να βλεπει αν υπαρχει κενο μπροστα ή εμποδιο αντιστοιχα.
Εχω προσθεσει αρκετα στοιχεια στον ηδη υπαρχοντα κωδικα οπως τα πιν του καθετου σερβο και σενσορα , τις μεταβλητες για ν αποθηκευονται οι μετρησεις αναλλογα με την γωνια σαρωσης κλπ...
.................... ΤΟ ΠΡΟΒΛΗΜΑ ....................
Καπου εδω χρειαζομαι την βοηθεια σας.
Αν παρατηρησετε τον κωδικα στην void scanObstacle () και την void avoidObstacle (int obstaclePos) δεν εχω τροποποιησει σχεδον τιποτα
Οι σκεψεις μου ειναι.....
1ον. Να μειωσω την γωνια της οριζοντιας σαρωσης απο τις 180 μοιρες στις ας πουμε 120 μοιρες και εδω....
if( obstaclePos < 60 ) // obstacle in the left <<<<<--- να γινει if( obstaclePos < 85 )
{
turnR(); <<<<<<<---- εδω να δημιουργησω flag οτι το εμποδιο ειναι αριστερα π.χ obstacle_in_left=1;
}
else
if( obstaclePos > 120 ) // obstacle in the right<<<<<<<<<<--- να γινει if( obstaclePos > 95 )
{
turnL(); <<<<<<<---- εδω να δημιουργησω flag οτι το εμποδιο ειναι δεξια π.χ obstacle_in_right=1;
}
else //όλα τα άλλα είναι : obstacle in the front center <<<<<---- ετσι ωστε αυτο να γινει 90 μοιρες μιας και το βημα ειναι 10 μοιρες
{
moveBack(); <<<<--- αρα εδω μενει να δημιουργησω flag οτι το εμποδιο ειναι στο κεντρο π.χ obstacle_in_center=1;
}
το σκεπτικο ειναι να κανω το ιδιο και για το vertical scan δλδ
αν υπαρχει κενο μπροστα απο το "ΘΕΡΙΟ" Vertical_scann_90=1; και
αν υπαρχει εμποδιο μπροστα απο τον καθετο σενσορα Vertical_scann_180=1;
και μετα να παιξω με if για ν αποφασιζω τι να κανει το "ΘΗΡΙΟ"......πχ
if (obstacle_in_left=1;) τοτε στριψε δεξια
if (obstacle_in_right=1;) τοτε στριψε αριστερα .....
με τελικο σκοπο να αποφασιζει στο
if(obstacle_in_center=1; && Vertical_scann_180=1;) τοτε υπαρχει μεγαλο εμποδιο μπροστα και πρεπει να το αποφυγω
ενω....
if(obstacle_in_center!=1; && Vertical_scann_180=1;)
δλδ οτι ο πανω αισθητηρας δεν βλεπει εμποδιο αλλα ο κατω βλεπει .... αρα το εμποδιο ειναι χαμηλο... και μπορω να το ανεβω.
Ελπιζω να καταφερα να εγινα κατανοητος για το τι ζηταω
....................ΟΙ ΕΡΩΤΗΣΕΙΣ....................
1.) Υπαρχει καποιο κενο στο σκεπτικο μου ??? Καποια παραμετρο του προβληματος που δεν υπολογισα ???
2.) Δεν μπορω να καταλαβω πως θα πρσθεσω τις "απαιτησεις μου" στον ηδη υπαρχοντα κωδικα
Αααα.... και ο ολοκληρος ο κωδικας με τις τροποποιησεις που εχω κανει....
- Κώδικας: Επιλογή όλων
// Sketch for Obstacle Avoiding Rover using Servo moved IR Sensor
// by Orfeus 9/10 and ntgr 1/10 :))
// for GRobot.gr
// HER 200911211748
#include <Servo.h>
Servo myservo_pan; // create servo object to control pan servo
Servo myservo_vertical; // create servo object to control vertical servo
int pos = 0; // variable for servo position
int sStep = 10 ; // Real Step to turn servo
int servoStep ; // foo Step to turn servo
int aDelay = 30; // Delay for the servo and let the IR to return results
int readDistance ; // Variable to keep pan IR readouts
int readDistance180 ; // Variable to keep Vertical IR readouts @ 180 degree(horizontal detection)
int readDistance90 ; // Variable to keep Vertical IR readouts @ 90 degree(gap detection)
int minDistanceLimit = 300 ; // Distance to set as Obstacle in pan mode
int minDistanceLimit180 = 300 ; // Distance to set as Obstacle @ 180 degree (horizontal position)
int minDistanceLimit90 = 90 ; // Distance to set as GAP @ 90 degree (Vertical position)
int sensorPin = 3; // Paning IR Sensor on Analog PIN 3
int Vertical_sensorPin = 2; // Vertical IR Sensore on Analog PIN 2
int motor_2_direc = 8; //D3 GREY PINS to motors module
int motor_2_speed = 9; //d1 MAUVE
int motor_1_direc = 10; //D2 WHITE
int motor_1_speed = 11; //D0 BLUE
void setup()
{
myservo_pan.attach(7); // attaches the pan servo on pin 7 to the servo object for the IR sensor movement
myservo_vertical.attach(6); // attaches the vertical servo on pin 9 to the servo object
pinMode(motor_1_direc, OUTPUT);
pinMode(motor_1_speed, OUTPUT);
pinMode(motor_2_direc, OUTPUT);
pinMode(motor_2_speed, OUTPUT);
}
void loop()
{
moveFront(); // Start moving front
scanObstacle(); // Scan for obstacle
}
void scanObstacle ()
{
myservo_pan.write(pos); // sets the servo position
myservo_vertical.write(90); // sets the servo position @ 90 degree
delay(aDelay); // waits for the servo to get there
readDistance = analogRead(sensorPin); // Take a reading from sensor
readDistance90 = analogRead(Vertical_sensorPin); // Take a reading from Vertical sensor @90 degree and store it
delay(aDelay); // Wait to get the reading SOSOSOSOS !!!! We need this
if (readDistance > minDistanceLimit) // If we can scan an obstacle
{
avoidObstacle(pos); // Then do avoiding actions
}
if (pos == 180) // If we reach end of servo
{
servoStep= -sStep; // Move it back
}
if (pos == 0) // If we are at the start
{
servoStep = sStep; // Move it right
}
pos += servoStep;
}
void avoidObstacle (int obstaclePos) // Obsacle avoiding function
{
if( obstaclePos < 60 ) // obstacle in the left
{
turnR();
}
else
if( obstaclePos > 120 ) // obstacle in the right
{
turnL();
}
else //όλα τα άλλα είναι : obstacle in the front center
{
moveBack();
}
}
// All the functions below are for motors, we have some LEDs for reference.
// On the real Rover just put your motors/servos code here.
void moveFront() // Function to Move Forward
{
digitalWrite(motor_1_direc, LOW);
analogWrite(motor_1_speed, 0);
digitalWrite(motor_2_direc, LOW);
analogWrite(motor_2_speed, 0);
}
void moveBack() // Function to Move Back
{
digitalWrite(motor_1_direc, LOW); // First STOP
analogWrite(motor_1_speed, 255); // First STOP
digitalWrite(motor_2_direc, LOW); // First STOP
analogWrite(motor_2_speed, 255); // First STOP
delay(1000); // Wait some time to STOP then
digitalWrite(motor_1_direc, HIGH); // Turn Clockwise
analogWrite(motor_1_speed, 0);
digitalWrite(motor_2_direc, LOW);
analogWrite(motor_2_speed, 0);
delay(2000);
}
void turnR() // Function to Turn Right
{
digitalWrite(motor_1_direc, HIGH); // I must ADD a stop function here ALSO
analogWrite(motor_1_speed, 125);
digitalWrite(motor_2_direc, LOW);
analogWrite(motor_2_speed, 0);
delay(2000);
}
void turnL() // Function to Turn Left
{
digitalWrite(motor_1_direc, LOW); // I must ADD a stop function here ALSO
analogWrite(motor_1_speed, 0);
digitalWrite(motor_2_direc, HIGH);
analogWrite(motor_2_speed, 125);
delay(2000);
}
Ευχαριστω για την υπομονη σας

