
autocar with cliff and obstacle avoidance feature
Revision 1:c7d5b9662b4f, committed 2018-07-19
- Comitter:
- cudaChen
- Date:
- Thu Jul 19 05:41:17 2018 +0000
- Parent:
- 0:b9d5ff825f0f
- Commit message:
- [milestone] first commit
Changed in this revision
--- a/autocar/autocar.cpp Thu Jul 19 05:17:01 2018 +0000 +++ b/autocar/autocar.cpp Thu Jul 19 05:41:17 2018 +0000 @@ -236,6 +236,22 @@ } } +void obstacle(bool hasCliff, bool hasObstacle) +{ + if(hasCliff || hasObstacle) + { + backward(); + wait_ms(300); + stop(); + wait_ms(200); + turnRight(); + wait_ms(200); + } + + forward(); + wait_ms(200); +} + void init() { DriveSingleMotor(MOTOR_M1, PWR_STOP, DIR_FORWARD);
--- a/autocar/autocar.h Thu Jul 19 05:17:01 2018 +0000 +++ b/autocar/autocar.h Thu Jul 19 05:41:17 2018 +0000 @@ -33,6 +33,7 @@ void driveMotor(bool left, bool middle, bool right); void driveMotor(bool left, bool middle, bool right, bool hasObstacle); void driveMotorPID(int values, float Kp, float Ki, float Kd); +void obstacle(bool hasCliff, bool hasObstacle); void init(); void stop();
--- a/main.cpp Thu Jul 19 05:17:01 2018 +0000 +++ b/main.cpp Thu Jul 19 05:41:17 2018 +0000 @@ -34,10 +34,12 @@ bool left = false; bool middle = false; bool right = false; + + bool hasCliff = false; bool hasObstacle = false; int threshold = 500; // here I use 500 as threshold of IR sensor - int range = 30; // range of obstacle (cm) + int range = 15; // range of obstacle (cm) // set two motors to stop init(); @@ -68,9 +70,12 @@ readSensor(&left, &middle, &right, &hasObstacle, threshold, range); //values = readIRValues(); // read IR values + hasCliff = left || middle || right; + //driveMotor(left, middle, right); - driveMotor(left, middle, right, hasObstacle); + //driveMotor(left, middle, right, hasObstacle); //driveMotorPID(values, Kp, Ki, Kd); + obstacle(hasCliff, hasObstacle); } } }