#define RAD_WHEEL 0.028 // #define DEG2RAD PI / 180 #define LEFT OUT_C #define RIGHT OUT_B #define GYRO S1 #define LIMIT 20 // #define K_I 50 // #define K_P 0.1 // #define ERROR 0.03 // task main() { int rotA, rotB, // pwmLeft, pwmRight, // distS3, distS2, distMax, kDist, // currentTime, previousTime, dt, // gyroSpeed, gyroOffset, // fileSize = 30640; float course, courseAngle, bearing, // (psi), (alpha), (theta) xCoord, yCoord, // delthaX, delthaY, // length, intLength, // , path, prevPath, delthaPath, // xRef = -1, yRef = 0, // baseSpeed = 0, control = 0, // k = 20; string s; // byte handle; // SetSensorLowspeed(S2); SetSensorHTGyro(GYRO); // , DeleteFile("data.txt"); CreateFile("data.txt", fileSize, handle); Wait(50); // , (/) gyroOffset = SensorHTGyro(GYRO); course = 0; previousTime = CurrentTick(); while (true) { // distS3 = LIMIT - SensorUS(S3); distS2 = LIMIT - SensorUS(S2); // ( , ) if (distS3 < 0 ) distS3 = 0; if (distS2 < 0 ) distS2 = 0; if (distS2 > distS3) distMax = distS2; else distMax = distS3; kDist = sign(distS2 - distS3); currentTime = CurrentTick(); dt = currentTime - previousTime; previousTime = currentTime; // (/) gyroSpeed = SensorHTGyro(GYRO) - gyroOffset; // course = course + gyroSpeed * PI * dt / 1000.0 / 180.0; // rotA = MotorRotationCount(LEFT); rotB = MotorRotationCount(RIGHT); // path = (rotA + rotB) * DEG2RAD * RAD_WHEEL / 2; // delthaPath = path - prevPath; // prevPath = path; // X xCoord = xCoord + delthaPath * cos(course); // Y yCoord = yCoord + delthaPath * sin(course); // X delthaX = xRef - xCoord; // Y delthaY = yRef - yCoord; // bearing = atan2(delthaY, delthaX); // courseAngle = bearing - course - K_P * kDist * distMax; // if (abs(courseAngle) > PI) courseAngle = courseAngle - sign(courseAngle) * 2 * PI; // length = sqrt (delthaY * delthaY + delthaX * delthaX); intLength = intLength + length * dt / 1000; if (intLength > 10) intLength = 10; // baseSpeed = 100 * tanh (length) * cos (courseAngle) + K_I * intLength; if (abs(baseSpeed) > 40) baseSpeed = sign (baseSpeed) * 40; // control = k * courseAngle + sin(courseAngle) * baseSpeed / length; // if (abs(control) > 30) control = sign(control)*30; // float integer, pwmLeft = baseSpeed + control; // float integer, pwmRight = baseSpeed - control; if (abs(pwmLeft) > 100) pwmLeft = sign (pwmLeft) * 100; if (abs(pwmRight) > 100) pwmLeft = sign (pwmRight) * 100; // OnFwd(LEFT, pwmLeft); OnFwd(RIGHT, pwmRight); ClearScreen(); // NXT NumOut(0, 8, distS3); NumOut(0, 0, distS2); // s = NumToStr(xCoord) + " " + NumToStr(yCoord) + " " + NumToStr(bearing) + " " + NumToStr(course); // WriteLnString(handle, s, fileSize); // if(abs(length) < ERROR) { Off(OUT_BC); break; } Wait(5); } }
Source: https://habr.com/ru/post/277829/
All Articles