David's line following code from the LVBots competition, 2015.

Dependencies:   GeneralDebouncer Pacer PololuEncoder mbed

Fork of DeadReckoning by David Grayson

Committer:
DavidEGrayson
Date:
Thu Apr 16 22:00:15 2015 +0000
Revision:
57:99bec7fab454
Parent:
55:05c8f439497d
Doubled the encoder counts for indicating the end of the course because we might have to start a little bit back from the finish line.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DavidEGrayson 44:edcacba44760 1 #include "turn_sensor.h"
DavidEGrayson 44:edcacba44760 2 #include "l3g.h"
DavidEGrayson 44:edcacba44760 3
DavidEGrayson 50:517c0f0e621f 4 void TurnSensor::reset()
DavidEGrayson 50:517c0f0e621f 5 {
DavidEGrayson 50:517c0f0e621f 6 angleUnsigned = 0;
DavidEGrayson 50:517c0f0e621f 7 }
DavidEGrayson 50:517c0f0e621f 8
DavidEGrayson 44:edcacba44760 9 void TurnSensor::start()
DavidEGrayson 44:edcacba44760 10 {
DavidEGrayson 44:edcacba44760 11 timer.start();
DavidEGrayson 44:edcacba44760 12 rate = 0;
DavidEGrayson 44:edcacba44760 13 angleUnsigned = 0;
DavidEGrayson 44:edcacba44760 14 gyroLastUpdate = timer.read_us();
DavidEGrayson 44:edcacba44760 15 }
DavidEGrayson 44:edcacba44760 16
DavidEGrayson 44:edcacba44760 17 void TurnSensor::update()
DavidEGrayson 44:edcacba44760 18 {
DavidEGrayson 44:edcacba44760 19 if (l3gZAvailable() == 1)
DavidEGrayson 44:edcacba44760 20 {
DavidEGrayson 44:edcacba44760 21 int32_t gz = l3gZRead();
DavidEGrayson 44:edcacba44760 22 if (gz < -500000)
DavidEGrayson 44:edcacba44760 23 {
DavidEGrayson 44:edcacba44760 24 // error
DavidEGrayson 44:edcacba44760 25 return;
DavidEGrayson 44:edcacba44760 26 }
DavidEGrayson 46:f11cb4f93aac 27
DavidEGrayson 46:f11cb4f93aac 28 // The gyro on this robot is mounted upside down; account for that here so that
DavidEGrayson 46:f11cb4f93aac 29 // we can have counter-clockwise be a positive rotation.
DavidEGrayson 46:f11cb4f93aac 30 gz = -gz;
DavidEGrayson 46:f11cb4f93aac 31
DavidEGrayson 45:e16e74bbbf8c 32 rate = gz;
DavidEGrayson 44:edcacba44760 33
DavidEGrayson 44:edcacba44760 34 // First figure out how much time has passed since the last update (dt)
DavidEGrayson 44:edcacba44760 35 uint16_t m = timer.read_us();
DavidEGrayson 44:edcacba44760 36 uint16_t dt = m - gyroLastUpdate;
DavidEGrayson 44:edcacba44760 37 gyroLastUpdate = m;
DavidEGrayson 44:edcacba44760 38
DavidEGrayson 44:edcacba44760 39 // Multiply dt by turnRate in order to get an estimation of how
DavidEGrayson 44:edcacba44760 40 // much the robot has turned since the last update.
DavidEGrayson 44:edcacba44760 41 // (angular change = angular velocity * time)
DavidEGrayson 44:edcacba44760 42 int32_t d = (int32_t)rate * dt;
DavidEGrayson 44:edcacba44760 43
DavidEGrayson 44:edcacba44760 44 // The units of d are gyro digits times microseconds. We need
DavidEGrayson 44:edcacba44760 45 // to convert those to the units of turnAngle, where 2^29 units
DavidEGrayson 44:edcacba44760 46 // represents 45 degrees. The conversion from gyro digits to
DavidEGrayson 44:edcacba44760 47 // degrees per second (dps) is determined by the sensitivity of
DavidEGrayson 44:edcacba44760 48 // the gyro: 0.07 degrees per second per digit.
DavidEGrayson 44:edcacba44760 49 //
DavidEGrayson 44:edcacba44760 50 // (0.07 dps/digit) * (1/1000000 s/us) * (2^29/45 unit/degree)
DavidEGrayson 44:edcacba44760 51 // = 14680064/17578125 unit/(digit*us)
DavidEGrayson 55:05c8f439497d 52 //const float factor = (float)14680064 / 17578125;
DavidEGrayson 55:05c8f439497d 53
DavidEGrayson 55:05c8f439497d 54 // Fudge factor to account for the fact that the gyro might be mounted
DavidEGrayson 55:05c8f439497d 55 // at a bad angle or it might be more or less sensitive than expected.
DavidEGrayson 55:05c8f439497d 56 //const float fudge = 0.98809906722;
DavidEGrayson 55:05c8f439497d 57
DavidEGrayson 44:edcacba44760 58 angleUnsigned += (int64_t)d * 14680064 / 17578125;
DavidEGrayson 44:edcacba44760 59 }
DavidEGrayson 44:edcacba44760 60 }