SmartVehicle-Basis
SmartFactory
Sonar.cpp
Go to the documentation of this file.
1
23
// #include "Sonar.h"
24
// #include "Arduino.h"
25
26
// Sonar::Sonar(int servoPin, int triggerPin, int echoPin, int maxDistance, int min_Error, int max_Error, int min_TurnAngle, int max_TurnAngle) {
27
// DBFUNCCALLln("Sonar::Sonar(servoPin,triggerPin,echoPin,maxDistance,min_Error,max_Error,min_TurnAngle,max_TurnAngle)");
28
// DBINFO1("Initializing sonar...");
29
// sonar = new NewPing(triggerPin, echoPin, maxDistance);
30
// minError = min_Error;
31
// maxError = max_Error;
32
// minTurnAngle = min_TurnAngle;
33
// maxTurnAngle = max_TurnAngle;
34
// _servoPin = servoPin;
35
// DBINFO1ln(" complete!");
36
// }
37
38
// void Sonar::loop(SonarState *state, int directionError, bool servoActive) {
39
// DBFUNCCALLln("Sonar::loop(*state,directionError,servoActive)");
40
// if ((servoActive == true) && (state->isAttached == false)) {
41
// sonarServo.attach(_servoPin);
42
// state->isAttached = true;
43
// }
44
45
// state->obstacleDistance = getDistanceToObstacle();
46
// if (servoActive == true) {
47
// DBSTATUS("Turn sonar with directionError: ");
48
// DBSTATUSln(directionError);
49
// turnSonar(directionError);
50
// }
51
// calculateSonarFactor(state);
52
// if (state->detachServo == true) {
53
// sonarServo.detach();
54
// state->detachServo = false;
55
// state->isAttached = false;
56
// }
57
// }
58
59
// int Sonar::getDistanceToObstacle() {
60
// DBFUNCCALLln("Sonar::getDistanceToObstacle()");
61
// int uSonar = sonar->ping_cm();
62
// DBINFO1("Distance: ");
63
// DBINFO1ln(uSonar);
64
// return uSonar;
65
// }
66
67
// void Sonar::turnSonar(int directionError) {
68
// DBFUNCCALLln("Sonar::turnSonar(int directionError)");
69
// DBINFO1("directionError: ");
70
// DBINFO1ln(directionError);
71
// int turnSonar = map(directionError, minError, maxError, minTurnAngle, maxTurnAngle); // map direction error (-5 to 5) to angle (180 to 0)
72
// DBINFO1("Turn anglesonar: ");
73
// DBINFO1ln(turnSonar);
74
// sonarServo.write(turnSonar);
75
// }
76
77
// void Sonar::calculateSonarFactor(SonarState *state) {
78
// DBFUNCCALLln("Sonar::calculateSonarFactor(SonarState *state)");
79
// if ((state->obstacleDistance > 10) || (state->obstacleDistance == 0)) {
80
// DBINFO1ln("Factor: 1");
81
// state->sonarFactor = 1;
82
// } else if ((state->obstacleDistance <= 10) && (state->obstacleDistance >= 5)) {
83
// DBINFO1ln("Factor: 0.5");
84
// state->sonarFactor = 0.5;
85
// } else {
86
// DBINFO1ln("Factor: 0");
87
// state->sonarFactor = 0;
88
// }
89
// }
90
91
// void Sonar::Test(const int test) {
92
// DBFUNCCALLln("Sonar::Test()");
93
// int testdelay = 1000;
94
// if (test == 0 || test == 1) { //Test Servo
95
// int maxinc = 6; //Chose max increment range for left/ritgh-turn
96
// sonarServo.attach(_servoPin);
97
// for (size_t i = 0; i < maxinc; i++) {
98
// turnSonar(i);
99
// delay(testdelay);
100
// }
101
// for (int i = -maxinc; i < maxinc; i++) {
102
// turnSonar(-i);
103
// delay(testdelay);
104
// }
105
// turnSonar(0);
106
// delay(testdelay);
107
// sonarServo.detach();
108
// delay(testdelay);
109
// } else if (test == 0 || test == 2) { //Test Distance to Obstacle
110
// for (size_t i = 0; i < 50; i++) {
111
// getDistanceToObstacle();
112
// delay(testdelay / 5);
113
// }
114
// } else {
115
// DBERROR("No vailid Test selected.");
116
// DBINFO1("Your Input: ");
117
// DBINFO1(test);
118
// }
119
// }
src
EnvironmentDetection
Sonar
Sonar.cpp
Generated by
1.8.15