David's dead reckoning code for the LVBots competition on March 6th. Uses the mbed LPC1768, DRV8835, QTR-3RC, and two DC motors with encoders.
Dependencies: PololuEncoder Pacer mbed GeneralDebouncer
Revision 24:fc01d9125d3b, committed 2014-02-28
- Comitter:
- DavidEGrayson
- Date:
- Fri Feb 28 01:07:14 2014 +0000
- Parent:
- 23:aae5cbe3b924
- Child:
- 25:73c2eedb3b91
- Commit message:
- Fixed the problem with LineTracker always thinking the line was visible.
Changed in this revision
--- a/line_tracker.cpp Fri Feb 28 00:23:00 2014 +0000 +++ b/line_tracker.cpp Fri Feb 28 01:07:14 2014 +0000 @@ -59,11 +59,12 @@ uint32_t avg = 0; uint32_t sum = 0; + lineVisible = false; for(uint8_t s = 0; s < LINE_SENSOR_COUNT; s++) { // keep track of whether we see the line at all uint16_t value = calibratedValues[s]; - if (value > 200) + if (value > 500) { lineVisible = true; }
--- a/main.cpp Fri Feb 28 00:23:00 2014 +0000 +++ b/main.cpp Fri Feb 28 01:07:14 2014 +0000 @@ -145,11 +145,68 @@ reckoner.reset(); } +// Keep the robot pointing the in the right direction (1, 0). +// This should basically keep it going straight. +void updateMotorsToDriveStraight() +{ + const int32_t straightDriveStrength = 1000; + int16_t speedLeft = drivingSpeed; + int16_t speedRight = drivingSpeed; + int32_t reduction = reckoner.sin / (1<<15) * straightDriveStrength / (1 << 15); + if (reduction > 0) + { + speedRight = reduceSpeed(speedRight, reduction); + } + else + { + speedLeft = reduceSpeed(speedLeft, -reduction); + } + motorsSpeedSet(speedLeft, speedRight); +} + +void loadCalibrationAndFindLine() +{ + lineTracker.calibratedMinimum[0] = 34872; + lineTracker.calibratedMinimum[1] = 29335; + lineTracker.calibratedMinimum[2] = 23845; + lineTracker.calibratedMaximum[0] = 59726; + lineTracker.calibratedMaximum[1] = 60110; + lineTracker.calibratedMaximum[2] = 58446; + + Timer foundLineTimer; + foundLineTimer.start(); + bool foundLine = false; + + GeneralDebouncer lineStatus(10000); + while(1) + { + lineTracker.read(); + lineTracker.updateCalibration(); + updateReckonerFromEncoders(); + updateMotorsToDriveStraight(); + lineStatus.update(lineTracker.getLineVisible()); + + if (foundLine) + { + if(foundLineTimer.read_ms() >= 500) + { + // We found the line and traveled for a bit more, so now we can be done. + break; + } + } + else + { + if(lineStatus.getState() == true && lineStatus.getTimeInCurrentStateMicroseconds() > 150000) + { + foundLine = true; + foundLineTimer.start(); + } + } + } +} void findLineAndCalibrate() { - const int32_t straightDriveStrength = 1000; - Timer timer; timer.start(); @@ -160,21 +217,8 @@ { lineTracker.read(); lineTracker.updateCalibration(); - updateReckonerFromEncoders(); - - // Keep the robot pointing the in the right direction. This should basically keep it going straight. - int16_t speedLeft = drivingSpeed; - int16_t speedRight = drivingSpeed; - int32_t reduction = reckoner.sin / (1<<15) * straightDriveStrength / (1 << 15); - if (reduction > 0) - { - speedRight = reduceSpeed(speedRight, reduction); - } - else - { - speedLeft = reduceSpeed(speedLeft, -reduction); - } - motorsSpeedSet(speedLeft, speedRight); + updateReckonerFromEncoders(); + updateMotorsToDriveStraight(); if (goodCalibration) {
--- a/main.h Fri Feb 28 00:23:00 2014 +0000 +++ b/main.h Fri Feb 28 01:07:14 2014 +0000 @@ -4,7 +4,7 @@ #include "line_tracker.h" void waitForSignalToStart(); -void findLineAndCalibrate(); +void findLineAndCalibrate(); void loadCalibrationAndFindLine(); // two alternatives void turnRightToFindLine(); void followLineToEnd(); void driveHomeAlmost();
--- a/test.cpp Fri Feb 28 00:23:00 2014 +0000 +++ b/test.cpp Fri Feb 28 01:07:14 2014 +0000 @@ -107,11 +107,12 @@ } } -// If the calibration stops working, we could just use these values from David's office in the day time: +// Values from David's office Values from dev lab, +// in the day time, 2014-02-27: 2014-02-27: // # calmin calmax -// 0 34872 59726 -// 1 29335 60110 -// 2 23845 58446 +// 0 34872 59726 0 40617 60222 +// 1 29335 60110 1 36937 61198 +// 2 23845 58446 2 33848 58862 void testCalibrate() { Timer timer; @@ -119,11 +120,26 @@ Pacer reportPacer(200000); + bool doneCalibrating = false; + + led1 = 1; + while(1) { lineTracker.read(); - lineTracker.updateCalibration(); - led4 = calibrationLooksGood(); + if(!doneCalibrating) + { + lineTracker.updateCalibration(); + } + + led2 = calibrationLooksGood(); + led3 = doneCalibrating; + led4 = lineTracker.getLineVisible(); + + if (button1DefinitelyPressed()) + { + doneCalibrating = true; + } if (reportPacer.pace()) {