Revision 1b3adcdd devices/DiWheelDrive/linefollow2.cpp
| devices/DiWheelDrive/linefollow2.cpp | ||
|---|---|---|
| 2 | 2 |
#include "linefollow2.hpp" |
| 3 | 3 |
#include <cmath> |
| 4 | 4 |
|
| 5 |
// Trash |
|
| 6 |
void LineFollow::printSensorData(){
|
|
| 7 |
chprintf((BaseSequentialStream*) &SD1, "Test!"); |
|
| 8 |
} |
|
| 9 | 5 |
|
| 10 | 6 |
|
| 11 | 7 |
LineFollow::LineFollow(Global *global){
|
| 12 | 8 |
this->global = global; |
| 13 | 9 |
} |
| 14 |
|
|
| 15 |
// trash |
|
| 16 |
int LineFollow::delta(){
|
|
| 17 |
int delta = 0; |
|
| 18 |
int FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset(); |
|
| 19 |
int FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset(); |
|
| 20 |
|
|
| 21 |
delta = abs(abs(global->threshProxyL-global->threshProxyR) - abs(FL-FR)); |
|
| 22 |
|
|
| 23 |
if (FR > global->threshProxyR && FL > global->threshProxyL ){
|
|
| 24 |
return delta ; |
|
| 25 |
}else {
|
|
| 26 |
return delta* -1; |
|
| 27 |
} |
|
| 28 |
return delta; |
|
| 29 |
} |
|
| 30 |
|
|
| 31 |
// old and trash |
|
| 32 |
void LineFollow::stableFollow(int vcnl4020Proximity[4], int (&rpmFuzzyCtrl)[2], Global *global){
|
|
| 33 |
int targetSensor = 0x38; |
|
| 34 |
int actualSensorL = vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT] ; |
|
| 35 |
int actualSensorR = vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT] ; |
|
| 36 |
int targetSpeedL = global->rpmForward[constants::DiWheelDrive::LEFT_WHEEL]; |
|
| 37 |
int targetSpeedR = global->rpmForward[constants::DiWheelDrive::RIGHT_WHEEL]; |
|
| 38 |
|
|
| 39 |
|
|
| 40 |
int diff = actualSensorR - actualSensorL; |
|
| 41 |
int error = targetSensor - (actualSensorL + actualSensorR); |
|
| 42 |
|
|
| 43 |
accSum += error; |
|
| 44 |
int dTerm = error - oldError; |
|
| 45 |
|
|
| 46 |
if (diff > biggestDiff){
|
|
| 47 |
biggestDiff = diff; |
|
| 48 |
} |
|
| 49 |
int correctionSpeed = (int) (Kp * error + Ki * accSum + Kd * dTerm); |
|
| 50 |
chprintf((BaseSequentialStream*) &SD1, "Correction Speed: %d\n", correctionSpeed); |
|
| 51 |
rpmFuzzyCtrl[constants::DiWheelDrive::LEFT_WHEEL] = targetSpeedL + correctionSpeed; |
|
| 52 |
rpmFuzzyCtrl[constants::DiWheelDrive::RIGHT_WHEEL] = targetSpeedR - correctionSpeed; |
|
| 53 |
|
|
| 54 |
chprintf((BaseSequentialStream*) &SD1, "Diff: %d, Biggest: %d\n", correctionSpeed, biggestDiff); |
|
| 55 |
|
|
| 10 |
LineFollow::LineFollow(Global *global, LineFollowStrategy strategy){
|
|
| 11 |
this->global = global; |
|
| 12 |
this-> strategy = strategy; |
|
| 56 | 13 |
} |
| 57 | 14 |
|
| 58 | 15 |
/** |
| 59 | 16 |
* Calculate the error from front proxi sensors and fixed threshold values for those sensors. |
| 60 | 17 |
*/ |
| 61 | 18 |
int LineFollow::getError(){
|
| 62 |
|
|
| 19 |
// Get actual sensor data of both front sensors |
|
| 63 | 20 |
int FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset(); |
| 64 | 21 |
int FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset(); |
| 65 | 22 |
int targetL = global->threshProxyL; |
| 66 | 23 |
int targetR = global->threshProxyR; |
| 67 |
int error = FL -targetL + FR - targetR; |
|
| 68 |
|
|
| 24 |
int error = 0; |
|
| 25 |
switch (this->strategy) |
|
| 26 |
{
|
|
| 27 |
case LineFollowStrategy::EDGE_RIGHT: |
|
| 28 |
error = -(FL -targetL + FR - targetR); |
|
| 29 |
break; |
|
| 30 |
case LineFollowStrategy::EDGE_LEFT: |
|
| 31 |
error = (FL -targetL + FR - targetR); |
|
| 32 |
break; |
|
| 33 |
case LineFollowStrategy::MIDDLE: |
|
| 34 |
// Assume that the smallest value means driving in the middle |
|
| 35 |
targetL = targetR = !(targetL<targetR)?targetR:targetL; |
|
| 36 |
error = (FL -targetL + FR - targetR); |
|
| 37 |
break; |
|
| 38 |
|
|
| 39 |
default: |
|
| 40 |
break; |
|
| 41 |
} |
|
| 42 |
// Debugging stuff ------ |
|
| 43 |
if (global->enableRecord){
|
|
| 44 |
global->senseRec[global->sensSamples].error = error; |
|
| 45 |
global->senseRec[global->sensSamples].FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset(); |
|
| 46 |
global->senseRec[global->sensSamples].FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset(); |
|
| 47 |
global->sensSamples++; |
|
| 48 |
} |
|
| 49 |
// ---------------------- |
|
| 50 |
// Register white values |
|
| 69 | 51 |
if (FL+FR > global->threshWhite){
|
| 70 | 52 |
whiteFlag = 1; |
| 71 | 53 |
}else{
|
| ... | ... | |
| 74 | 56 |
return error; |
| 75 | 57 |
} |
| 76 | 58 |
|
| 77 |
/** |
|
| 78 |
* Follow strategy for left edge. |
|
| 79 |
*/ |
|
| 80 |
int LineFollow::followLeftEdge(int rpmSpeed[2]){
|
|
| 81 |
|
|
| 82 |
int correctionSpeed = getPidCorrectionSpeed(); |
|
| 83 |
chprintf((BaseSequentialStream*) &SD1, "Correction: %d, thresh: %d\n",correctionSpeed, global->threshWhite); |
|
| 59 |
int LineFollow::followLine(int (&rpmSpeed)[2]){
|
|
| 84 | 60 |
|
| 85 |
rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] = global->forwardSpeed + correctionSpeed; |
|
| 86 |
|
|
| 87 |
rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed - correctionSpeed; |
|
| 88 |
return whiteFlag; |
|
| 89 |
} |
|
| 90 |
|
|
| 91 |
/** |
|
| 92 |
* Follow strategy for right edge. |
|
| 93 |
*/ |
|
| 94 |
int LineFollow::followRightEdge(int rpmSpeed[2]){
|
|
| 61 |
switch (this->strategy) |
|
| 62 |
{
|
|
| 63 |
case LineFollowStrategy::FUZZY: |
|
| 64 |
for (int i = 0; i < 4; i++) {
|
|
| 65 |
vcnl4020AmbientLight[i] = global->vcnl4020[i].getAmbientLight(); |
|
| 66 |
vcnl4020Proximity[i] = global->vcnl4020[i].getProximityScaledWoOffset(); |
|
| 67 |
} |
|
| 95 | 68 |
|
| 96 |
int correctionSpeed = getPidCorrectionSpeed(); |
|
| 97 |
chprintf((BaseSequentialStream*) &SD1, "Correction: %d, thresh: %d\n",correctionSpeed, global->threshWhite); |
|
| 69 |
lineFollowing(vcnl4020Proximity, rpmSpeed); |
|
| 70 |
break; |
|
| 71 |
|
|
| 72 |
default: |
|
| 73 |
int correctionSpeed = getPidCorrectionSpeed(); |
|
| 74 |
// chprintf((BaseSequentialStream*) &SD1, "Correction: %d, thresh: %d\n",correctionSpeed, global->threshWhite); |
|
| 98 | 75 |
|
| 99 |
rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] = global->forwardSpeed - correctionSpeed;
|
|
| 76 |
rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] = global->forwardSpeed + correctionSpeed;
|
|
| 100 | 77 |
|
| 101 |
rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed + correctionSpeed; |
|
| 102 |
return whiteFlag; |
|
| 78 |
rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed - correctionSpeed; |
|
| 79 |
return whiteFlag; |
|
| 80 |
break; |
|
| 81 |
} |
|
| 103 | 82 |
} |
| 104 | 83 |
|
| 84 |
|
|
| 105 | 85 |
/** |
| 106 | 86 |
* Pid controller which returns a corrections speed. |
| 107 | 87 |
*/ |
| 108 | 88 |
int LineFollow::getPidCorrectionSpeed(){
|
| 109 | 89 |
int error = getError(); |
| 110 |
int sloap = error - global->oldError;
|
|
| 111 |
int correctionSpeed = (int) (global->K_p*error + global->K_i*global->accumHist + global->K_d*sloap);
|
|
| 112 |
global->oldError = error;
|
|
| 113 |
global->accumHist += error;
|
|
| 90 |
int sloap = error - oldError; |
|
| 91 |
int correctionSpeed = (int) (Kp*error + Ki*accumHist + Kd*sloap);
|
|
| 92 |
oldError = error; |
|
| 93 |
// accumHist += (int) (0.01 * error);
|
|
| 114 | 94 |
if (abs(error) > global->maxDist.error){
|
| 115 | 95 |
global->maxDist.error = error; |
| 116 | 96 |
} |
| 117 | 97 |
return correctionSpeed; |
| 118 | 98 |
} |
| 119 | 99 |
|
| 120 |
// trash |
|
| 121 |
// void LineFollow::calibrateZiegler(float KCrit, int rpmSpeed[2]){
|
|
| 122 |
// int targetSpeedL = 5; |
|
| 123 |
// int targetSpeedR = 5; |
|
| 124 |
// int delta_ = error(); |
|
| 125 |
// int correctionSpeed = (int) (KCrit * delta_); |
|
| 126 |
// if (global->enableRecord){
|
|
| 127 |
// global->senseRec[global->sensSamples].error = delta_; |
|
| 128 |
// global->senseRec[global->sensSamples].FL = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset(); |
|
| 129 |
// global->senseRec[global->sensSamples].FR = global->vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset(); |
|
| 130 |
// global->sensSamples++; |
|
| 131 |
// } |
|
| 132 |
// if (abs(delta_) > global->maxDist.error){
|
|
| 133 |
// global->maxDist.error = delta_; |
|
| 134 |
// } |
|
| 135 |
|
|
| 136 |
// rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] = global->forwardSpeed + -1*correctionSpeed; |
|
| 137 |
// rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] = global->forwardSpeed + correctionSpeed; |
|
| 138 |
// chprintf((BaseSequentialStream*) &SD1, "CS:%d,LW:%d,RW:%d\n", correctionSpeed, rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL], rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL]); |
|
| 139 |
// } |
|
| 100 |
|
|
| 101 |
void LineFollow::setStrategy(LineFollowStrategy strategy){
|
|
| 102 |
this->strategy = strategy; |
|
| 103 |
} |
|
| 104 |
|
|
| 105 |
LineFollowStrategy LineFollow::getStrategy(){
|
|
| 106 |
return this->strategy; |
|
| 107 |
} |
|
| 108 |
void LineFollow::setGains(float Kp, float Ki, float Kd){
|
|
| 109 |
this->Kp = Kp; |
|
| 110 |
this->Ki = Ki; |
|
| 111 |
this->Kd = Kd; |
|
| 112 |
} |
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
// Lagacy code, fuzzy following----------------------------------------- |
|
| 119 |
// Line following by a fuzzy controler |
|
| 120 |
void LineFollow::lineFollowing(int (&proximity)[4], int (&rpmFuzzyCtrl)[2]) {
|
|
| 121 |
// FUZZYFICATION |
|
| 122 |
// First we need to get the fuzzy value for our 3 values {BLACK, GREY, WHITE}
|
|
| 123 |
float leftWheelFuzzyMemberValues[3], leftFrontFuzzyMemberValues[3], rightFrontFuzzyMemberValues[3], rightWheelFuzzyMemberValues[3]; |
|
| 124 |
fuzzyfication(proximity[constants::DiWheelDrive::PROX_WHEEL_LEFT], leftWheelFuzzyMemberValues); |
|
| 125 |
fuzzyfication(proximity[constants::DiWheelDrive::PROX_FRONT_LEFT], leftFrontFuzzyMemberValues); |
|
| 126 |
fuzzyfication(proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT], rightFrontFuzzyMemberValues); |
|
| 127 |
fuzzyfication(proximity[constants::DiWheelDrive::PROX_WHEEL_RIGHT], rightWheelFuzzyMemberValues); |
|
| 128 |
|
|
| 129 |
// INFERENCE RULE DEFINITION |
|
| 130 |
// Get the member for each sensor |
|
| 131 |
colorMember member[4]; |
|
| 132 |
member[constants::DiWheelDrive::PROX_WHEEL_LEFT] = getMember(leftWheelFuzzyMemberValues); |
|
| 133 |
member[constants::DiWheelDrive::PROX_FRONT_LEFT] = getMember(leftFrontFuzzyMemberValues); |
|
| 134 |
member[constants::DiWheelDrive::PROX_FRONT_RIGHT] = getMember(rightFrontFuzzyMemberValues); |
|
| 135 |
member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] = getMember(rightWheelFuzzyMemberValues); |
|
| 136 |
|
|
| 137 |
// visualize sensors via LEDs |
|
| 138 |
global->robot.setLightColor(constants::LightRing::LED_WNW, memberToLed(member[constants::DiWheelDrive::PROX_WHEEL_LEFT])); |
|
| 139 |
global->robot.setLightColor(constants::LightRing::LED_NNW, memberToLed(member[constants::DiWheelDrive::PROX_FRONT_LEFT])); |
|
| 140 |
global->robot.setLightColor(constants::LightRing::LED_NNE, memberToLed(member[constants::DiWheelDrive::PROX_FRONT_RIGHT])); |
|
| 141 |
global->robot.setLightColor(constants::LightRing::LED_ENE, memberToLed(member[constants::DiWheelDrive::PROX_WHEEL_RIGHT])); |
|
| 142 |
|
|
| 143 |
// chprintf((BaseSequentialStream*) &SD1, "Left: BLACK: %f, GREY: %f, WHITE: %f\r\n", leftWheelFuzzyMemberValues[BLACK], leftWheelFuzzyMemberValues[GREY], leftWheelFuzzyMemberValues[WHITE]); |
|
| 144 |
// chprintf((BaseSequentialStream*) &SD1, "Right: BLACK: %f, GREY: %f, WHITE: %f\r\n", rightFuzzyMemberValues[BLACK], rightFuzzyMemberValues[GREY], rightFuzzyMemberValues[WHITE]); |
|
| 145 |
|
|
| 146 |
// DEFUZZYFICATION |
|
| 147 |
defuzzyfication(member, rpmFuzzyCtrl); |
|
| 148 |
// defuzz(member, rpmFuzzyCtrl); |
|
| 149 |
} |
|
| 150 |
|
|
| 151 |
|
|
| 152 |
Color LineFollow::memberToLed(colorMember member) {
|
|
| 153 |
switch (member) {
|
|
| 154 |
case BLACK: |
|
| 155 |
return Color(Color::GREEN); |
|
| 156 |
case GREY: |
|
| 157 |
return Color(Color::YELLOW); |
|
| 158 |
case WHITE: |
|
| 159 |
return Color(Color::RED); |
|
| 160 |
default: |
|
| 161 |
return Color(Color::WHITE); |
|
| 162 |
} |
|
| 163 |
} |
|
| 164 |
|
|
| 165 |
void LineFollow::defuzzyfication(colorMember (&member)[4], int (&rpmFuzzyCtrl)[2]) {
|
|
| 166 |
whiteFlag = 0; |
|
| 167 |
// all sensors are equal |
|
| 168 |
if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == member[constants::DiWheelDrive::PROX_FRONT_LEFT] && |
|
| 169 |
member[constants::DiWheelDrive::PROX_FRONT_LEFT] == member[constants::DiWheelDrive::PROX_FRONT_RIGHT] && |
|
| 170 |
member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == member[constants::DiWheelDrive::PROX_WHEEL_RIGHT]) {
|
|
| 171 |
// something is wrong -> stop |
|
| 172 |
copyRpmSpeed(rpmHalt, rpmFuzzyCtrl); |
|
| 173 |
// both front sensor detect a line |
|
| 174 |
} else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == BLACK && |
|
| 175 |
member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == BLACK) {
|
|
| 176 |
// straight |
|
| 177 |
copyRpmSpeed(global->rpmForward, rpmFuzzyCtrl); |
|
| 178 |
// exact one front sensor detects a line |
|
| 179 |
} else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == BLACK || |
|
| 180 |
member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == BLACK) {
|
|
| 181 |
// soft correction |
|
| 182 |
if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY) {
|
|
| 183 |
// soft right |
|
| 184 |
copyRpmSpeed(global->rpmSoftRight, rpmFuzzyCtrl); |
|
| 185 |
} else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == WHITE) {
|
|
| 186 |
// hard right |
|
| 187 |
copyRpmSpeed(global->rpmHardRight, rpmFuzzyCtrl); |
|
| 188 |
} else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
|
|
| 189 |
// soft left |
|
| 190 |
copyRpmSpeed(global->rpmSoftLeft, rpmFuzzyCtrl); |
|
| 191 |
} else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == WHITE) {
|
|
| 192 |
// hard left |
|
| 193 |
copyRpmSpeed(global->rpmHardLeft, rpmFuzzyCtrl); |
|
| 194 |
} |
|
| 195 |
// both wheel sensors detect a line |
|
| 196 |
} else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK && |
|
| 197 |
member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
|
|
| 198 |
// something is wrong -> stop |
|
| 199 |
copyRpmSpeed(rpmHalt, rpmFuzzyCtrl); |
|
| 200 |
// exactly one wheel sensor detects a line |
|
| 201 |
} else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK || |
|
| 202 |
member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
|
|
| 203 |
if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == BLACK) {
|
|
| 204 |
// turn left |
|
| 205 |
copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl); |
|
| 206 |
} else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == BLACK) {
|
|
| 207 |
// turn right |
|
| 208 |
copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl); |
|
| 209 |
} |
|
| 210 |
// both front sensors may detect a line |
|
| 211 |
} else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY && |
|
| 212 |
member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
|
|
| 213 |
if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY) {
|
|
| 214 |
// turn left |
|
| 215 |
copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl); |
|
| 216 |
} else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
|
|
| 217 |
// turn right |
|
| 218 |
copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl); |
|
| 219 |
} |
|
| 220 |
// exactly one front sensor may detect a line |
|
| 221 |
} else if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY || |
|
| 222 |
member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
|
|
| 223 |
if (member[constants::DiWheelDrive::PROX_FRONT_LEFT] == GREY) {
|
|
| 224 |
// turn left |
|
| 225 |
copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl); |
|
| 226 |
} else if (member[constants::DiWheelDrive::PROX_FRONT_RIGHT] == GREY) {
|
|
| 227 |
// turn right |
|
| 228 |
copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl); |
|
| 229 |
} |
|
| 230 |
// both wheel sensors may detect a line |
|
| 231 |
} else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY && |
|
| 232 |
member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
|
|
| 233 |
// something is wrong -> stop |
|
| 234 |
copyRpmSpeed(rpmHalt, rpmFuzzyCtrl); |
|
| 235 |
// exactly one wheel sensor may detect a line |
|
| 236 |
} else if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY || |
|
| 237 |
member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
|
|
| 238 |
if (member[constants::DiWheelDrive::PROX_WHEEL_LEFT] == GREY) {
|
|
| 239 |
// turn left |
|
| 240 |
copyRpmSpeed(rpmTurnLeft, rpmFuzzyCtrl); |
|
| 241 |
} else if (member[constants::DiWheelDrive::PROX_WHEEL_RIGHT] == GREY) {
|
|
| 242 |
// turn right |
|
| 243 |
copyRpmSpeed(rpmTurnRight, rpmFuzzyCtrl); |
|
| 244 |
} |
|
| 245 |
// no sensor detects anything |
|
| 246 |
} else {
|
|
| 247 |
// line is lost -> stop |
|
| 248 |
whiteFlag = 1; |
|
| 249 |
copyRpmSpeed(rpmHalt, rpmFuzzyCtrl); |
|
| 250 |
} |
|
| 251 |
chprintf((BaseSequentialStream*) &SD1, "Fuzzy Speed: Left: %d, Right: %d\n", rpmFuzzyCtrl[0], rpmFuzzyCtrl[1]); |
|
| 252 |
return; |
|
| 253 |
} |
|
| 254 |
|
|
| 255 |
colorMember LineFollow::getMember(float (&fuzzyValue)[3]) {
|
|
| 256 |
colorMember member; |
|
| 257 |
|
|
| 258 |
if (fuzzyValue[BLACK] > fuzzyValue[GREY]) |
|
| 259 |
if (fuzzyValue[BLACK] > fuzzyValue[WHITE]) |
|
| 260 |
member = BLACK; |
|
| 261 |
else |
|
| 262 |
member = WHITE; |
|
| 263 |
else |
|
| 264 |
if (fuzzyValue[GREY] > fuzzyValue[WHITE]) |
|
| 265 |
member = GREY; |
|
| 266 |
else |
|
| 267 |
member = WHITE; |
|
| 268 |
|
|
| 269 |
return member; |
|
| 270 |
} |
|
| 271 |
|
|
| 272 |
// Fuzzyfication of the sensor values |
|
| 273 |
void LineFollow::fuzzyfication(int sensorValue, float (&fuzziedValue)[3]) {
|
|
| 274 |
if (sensorValue < blackStartFalling ) {
|
|
| 275 |
// Only black value |
|
| 276 |
fuzziedValue[BLACK] = 1.0f; |
|
| 277 |
fuzziedValue[GREY] = 0.0f; |
|
| 278 |
fuzziedValue[WHITE] = 0.0f; |
|
| 279 |
} else if (sensorValue > whiteOn ) {
|
|
| 280 |
// Only white value |
|
| 281 |
fuzziedValue[BLACK] = 0.0f; |
|
| 282 |
fuzziedValue[GREY] = 0.0f; |
|
| 283 |
fuzziedValue[WHITE] = 1.0f; |
|
| 284 |
} else if ( sensorValue < greyMax) {
|
|
| 285 |
// Some greyisch value between black and grey |
|
| 286 |
|
|
| 287 |
// Black is going down |
|
| 288 |
if ( sensorValue > blackOff) {
|
|
| 289 |
fuzziedValue[BLACK] = 0.0f; |
|
| 290 |
} else {
|
|
| 291 |
fuzziedValue[BLACK] = static_cast<float>(sensorValue-blackOff) / (blackStartFalling-blackOff); |
|
| 292 |
} |
|
| 293 |
|
|
| 294 |
// Grey is going up |
|
| 295 |
if ( sensorValue < greyStartRising) {
|
|
| 296 |
fuzziedValue[GREY] = 0.0f; |
|
| 297 |
} else {
|
|
| 298 |
fuzziedValue[GREY] = static_cast<float>(sensorValue-greyStartRising) / (greyMax-greyStartRising); |
|
| 299 |
} |
|
| 300 |
|
|
| 301 |
// White is absent |
|
| 302 |
fuzziedValue[WHITE] = 0.0f; |
|
| 303 |
|
|
| 304 |
} else if ( sensorValue >= greyMax) {
|
|
| 305 |
// Some greyisch value between grey white |
|
| 306 |
|
|
| 307 |
// Black is absent |
|
| 308 |
fuzziedValue[BLACK] = 0.0f; |
|
| 309 |
|
|
| 310 |
// Grey is going down |
|
| 311 |
if ( sensorValue < greyOff) {
|
|
| 312 |
fuzziedValue[GREY] = static_cast<float>(sensorValue-greyOff) / (greyMax-greyOff); |
|
| 313 |
} else {
|
|
| 314 |
fuzziedValue[GREY] = 0.0f; |
|
| 315 |
} |
|
| 316 |
|
|
| 317 |
// White is going up |
|
| 318 |
if ( sensorValue < whiteStartRising) {
|
|
| 319 |
fuzziedValue[WHITE] = 0.0f; |
|
| 320 |
} else {
|
|
| 321 |
fuzziedValue[WHITE] = static_cast<float>(sensorValue-whiteStartRising) / (whiteOn-whiteStartRising); |
|
| 322 |
} |
|
| 323 |
} |
|
| 324 |
} |
|
| 325 |
|
|
| 326 |
void LineFollow::copyRpmSpeed(const int (&source)[2], int (&target)[2]) {
|
|
| 327 |
target[constants::DiWheelDrive::LEFT_WHEEL] = source[constants::DiWheelDrive::LEFT_WHEEL]; |
|
| 328 |
target[constants::DiWheelDrive::RIGHT_WHEEL] = source[constants::DiWheelDrive::RIGHT_WHEEL]; |
|
| 329 |
// chprintf((BaseSequentialStream*) &SD1, "Speed left: %d, Speed right: %d\r\n", target[0], target[1]); |
|
| 330 |
} |
|
Also available in: Unified diff