Netherlands
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« on: February 08, 2012, 02:56:52 pm » |
Hi there everybody i am trying to make a wall avoiding robot using a HC-SR04 ultrasonic sensor, a dagu channel4, a dagu rover 5 and a servo. but is isn't going that well. i want to let the ultrasonic sensor to continuously (or take very fast samples)meassure the distance in front of it while it is driving forward. and when it get's below a set distance to stop the sensor reading and move the servo to the right and meassure the distance, then move to the left and meassure the distance. and then decide which way to go. so far i did manage to make it stop when there is a wall in front of it. but when i try to move the servo, the servo goes all crazy, because it keeps meassuring and then it thinks the distance is more then the set distance and so moves the servo back. this is my code so far #include "Ultrasonic.h" #define TRIG_PIN 6 #define ECHO_PIN 7 Ultrasonic OurModule(TRIG_PIN, ECHO_PIN); #define PwmPinMotorA 53 #define PwmPinMotorB 51 #define DirectionPinMotorA 52 #define DirectionPinMotorB 50 int pos = 0; #include <Servo.h> Servo myservo;
void setup() { Serial.begin(9600); pinMode(PwmPinMotorA, OUTPUT); pinMode(PwmPinMotorB, OUTPUT); pinMode(DirectionPinMotorA, OUTPUT); pinMode(DirectionPinMotorB, OUTPUT); myservo.attach(9); }
void forward(){ digitalWrite(DirectionPinMotorA, HIGH); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 150); }
void backward(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, HIGH); analogWrite(PwmPinMotorB, 150); }
void left(){ digitalWrite(DirectionPinMotorA, HIGH); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, HIGH); analogWrite(PwmPinMotorB, 150); }
void right(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 255); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 255); }
void pause(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 0); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 0); }
void pos1() { OurModule.Ranging(CM); } void pos2() { OurModule.Ranging(CM); }
void loop() { pos = OurModule.Ranging(CM); myservo.write(90); if (pos <30) { forward(); } else{ pause(); myservo.write(10); } }
|
|
|
|
« Last Edit: February 08, 2012, 02:58:29 pm by cas2406 »
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 86
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #1 on: February 08, 2012, 04:00:42 pm » |
you must make a statemachine, here the concept, not complete but you should get the idea int state = MOVING;
void loop() { switch (state) { case MOVING: doMove(); break;
case DETERMINE: doDetermine(); break;
default: stop(); // for safety break; } }
doMove() { distance = Measure(); if (distance < 30) { state = DETERMINE; stop(); } else { forward(); } }
doDetermine() { // rotate servo left distanceLeft = Measure(); // rotate servo right distanceRight = Measure();
if (distanceLeft <= distanceRight) { right(); // delay(100); ??? } else { left(); // delay(100); ??? } state = MOVING; }
YOu can make the doDetermine() more intelligent, not just looking left and right but at every angle and choose the one with the most space. And of course it should move backwards if there is no left or right space.... (homework)
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #2 on: February 08, 2012, 04:27:09 pm » |
thank you very much i will go work on that, and probably upload it by suaterday
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #3 on: February 09, 2012, 12:56:44 pm » |
I have tried to fit a switch case machine in my code and i came up with this so far. but it doesn't work yet the error i am getting is this one: In function 'void doMove()': error: void value not ignored as it ought to be In function 'void doDetermine()': At global scope: In function 'void loop()': and an other error i am getting is that it can't find MOVING in: int state = MOVING ; #include "Ultrasonic.h" #define TRIG_PIN 6 #define ECHO_PIN 7 Ultrasonic OurModule(TRIG_PIN, ECHO_PIN); #define PwmPinMotorA 53 #define PwmPinMotorB 51 #define DirectionPinMotorA 52 #define DirectionPinMotorB 50 int pos = 0; #include <Servo.h> Servo myservo; int distance = 0; int distanceLeft = 0; int distanceRight = 0;
void setup() { Serial.begin(9600); pinMode(PwmPinMotorA, OUTPUT); pinMode(PwmPinMotorB, OUTPUT); pinMode(DirectionPinMotorA, OUTPUT); pinMode(DirectionPinMotorB, OUTPUT); myservo.attach(9); }
void forward(){ digitalWrite(DirectionPinMotorA, HIGH); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 150); }
void backward(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, HIGH); analogWrite(PwmPinMotorB, 150); }
void left(){ digitalWrite(DirectionPinMotorA, HIGH); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, HIGH); analogWrite(PwmPinMotorB, 150); delay(300); digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 0); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 0); }
void right(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 255); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 255); delay(300); digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 0); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 0); }
void pause(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 0); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 0); }
void Measure() { OurModule.Ranging(CM); }
void doMove() { distance = Measure(); if (distance < 30) { state = doDetermine; stop(); } else { forward(); } }
void doDetermine() // optie 2 het bepalen van de afstand links en rechts. { myservo.write(10); delay(400); distanceLeft = Measure(); delay(20); myservo.write(170); delay(400); distanceRight = Measure(); if (distanceLeft <= distanceRight) { right(); } else { left(); } state = MOVING; }
int state = MOVING ;
void loop() { switch (state) { case MOVING: // optie 1 rijden doMove(); break;
case doDetermine: // optie 2 afstanden bepalen doDetermine(); break;
default: // standaard: sta stil stop(); // for safety break; } }
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2345
|
 |
« Reply #4 on: February 09, 2012, 05:24:29 pm » |
You need to define MOVING and DETERMINE (and use it) e.g. #define MOVING 1 #define DETERMINE 2
You have tried to use doDetermine instead of DETERMINE as Robtillart had it.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #5 on: February 10, 2012, 10:27:37 am » |
void Measure() { OurModule.Ranging(CM); } Measure the distance, but don't return the distance. Now, there's a useful function!
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #6 on: February 10, 2012, 12:18:36 pm » |
i noticed too that i forgot the void's before Determine etc i changed the code again and made this of it but now getting this error: error: declaration does not declare anything In function 'void Move()': At global scope: and the this line is then highlighted: void Measure() { #include "Ultrasonic.h" #define TRIG_PIN 6 #define ECHO_PIN 7 Ultrasonic OurModule(TRIG_PIN, ECHO_PIN); #define PwmPinMotorA 53 #define PwmPinMotorB 51 #define DirectionPinMotorA 52 #define DirectionPinMotorB 50 int pos = 0; #include <Servo.h> Servo myservo; int distance = 0; int distanceLeft = 0; int distanceRight = 0; #define MOVING; #define Measure; #define Determine; #define state;
void setup() { Serial.begin(9600); pinMode(PwmPinMotorA, OUTPUT); pinMode(PwmPinMotorB, OUTPUT); pinMode(DirectionPinMotorA, OUTPUT); pinMode(DirectionPinMotorB, OUTPUT); myservo.attach(9); }
void forward(){ digitalWrite(DirectionPinMotorA, HIGH); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 150); }
void backward(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, HIGH); analogWrite(PwmPinMotorB, 150); }
void left(){ digitalWrite(DirectionPinMotorA, HIGH); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, HIGH); analogWrite(PwmPinMotorB, 150); delay(300); digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 0); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 0); }
void right(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 255); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 255); delay(300); digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 0); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 0); }
void pause(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 0); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 0); }
void Measure() { OurModule.Ranging(CM); }
void Move() { distance = Measure(); if (distance < 30) { state = Determine; stop(); } else { forward(); } }
void Determine() { myservo.write(10); delay(400); distanceLeft = Measure(); delay(20); myservo.write(170); delay(400); distanceRight = Measure();
if (distanceLeft <= distanceRight) { right(); } else { left(); } state = MOVING; }
void loop() { int state = MOVING;
switch (state) { case MOVING: Move(); break;
case Determine: Determine(); break;
default: stop(); break; }
}
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: February 10, 2012, 12:34:15 pm » |
Unless you really know what you're doing, don't put semicolons on #defines
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Netherlands
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #8 on: February 10, 2012, 12:42:12 pm » |
deleted the semicolens. but still getting the same error. could my changing of the cases be wrong ?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: February 10, 2012, 12:44:37 pm » |
What is "Measure"? A function, or an empty macro?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Netherlands
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #10 on: February 10, 2012, 12:50:29 pm » |
with measure, it should meassure the distance with an ultrasonic sensor. when i replace all the Measure(); for just OurModule.Ranging(CM); (i think this might just be easier) when i compile i get: In function 'void Move()': error: expected primary-expression before '=' token At global scope:
with state = Determine; highlighted. removing the semicolon doesn't do anything
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #11 on: February 10, 2012, 12:54:30 pm » |
state is a macro, you can't assign a value to it. Post your code.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #12 on: February 10, 2012, 12:55:12 pm » |
#define state; even with the semicolon defines a name, not a variable where you can store a value. when i compile i get: In function 'void Move()': error: expected primary-expression before '=' token At global scope:
with state = Determine; highlighted. removing the semicolon doesn't do anything Well, of course you do. Removing the ; did something (useful), just not what you wanted. Why are you using #define at all?
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Newbie
Karma: 0
Posts: 47
|
 |
« Reply #13 on: February 10, 2012, 12:56:43 pm » |
#include "Ultrasonic.h" #define TRIG_PIN 6 #define ECHO_PIN 7 Ultrasonic OurModule(TRIG_PIN, ECHO_PIN); #define PwmPinMotorA 53 #define PwmPinMotorB 51 #define DirectionPinMotorA 52 #define DirectionPinMotorB 50 int pos = 0; #include <Servo.h> Servo myservo; int distance = 0; int distanceLeft = 0; int distanceRight = 0; #define MOVING #define Determine #define state
void setup() { Serial.begin(9600); pinMode(PwmPinMotorA, OUTPUT); pinMode(PwmPinMotorB, OUTPUT); pinMode(DirectionPinMotorA, OUTPUT); pinMode(DirectionPinMotorB, OUTPUT); myservo.attach(9); }
void forward(){ digitalWrite(DirectionPinMotorA, HIGH); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 150); }
void backward(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, HIGH); analogWrite(PwmPinMotorB, 150); }
void left(){ digitalWrite(DirectionPinMotorA, HIGH); analogWrite(PwmPinMotorA, 150); digitalWrite(DirectionPinMotorB, HIGH); analogWrite(PwmPinMotorB, 150); delay(300); digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 0); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 0); }
void right(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 255); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 255); delay(300); digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 0); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 0); }
void pause(){ digitalWrite(DirectionPinMotorA, LOW); analogWrite(PwmPinMotorA, 0); digitalWrite(DirectionPinMotorB, LOW); analogWrite(PwmPinMotorB, 0); }
void Move() { distance = OurModule.Ranging(CM); if (distance < 30) { state = Determine; stop(); } else { forward(); } }
void Determine() { myservo.write(10); delay(400); distanceLeft = OurModule.Ranging(CM); delay(20); myservo.write(170); delay(400); distanceRight = OurModule.Ranging(CM);
if (distanceLeft <= distanceRight) { right(); } else { left(); } state = MOVING; }
void loop() { int state = MOVING;
switch (state) { case MOVING: Move(); break;
case Determine: Determine(); break;
default: stop(); break; }
}
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #14 on: February 10, 2012, 01:04:45 pm » |
state = Determine; You can't assign a value to a name!!! Make state a variable: int state = 0; myservo.write(10); delay(400); distanceLeft = OurModule.Ranging(CM); delay(20); myservo.write(170); delay(400); distanceRight = OurModule.Ranging(CM); Have you timed how long it actually takes the servo to move? #define Determine void Determine()
Do you want Determine to be a name or a function? Don't try to use the same name for two different things.
|
|
|
|
|
Logged
|
|
|
|
|
|