Fixed a few more small TODOs

Timer::Get now compensates for the FPGA time rolling over after 71 minutes

UltraSonic::Ping doesn't bother disabling automatic mode, since it asserts
that it's not in automatic mode on the line before.

Change-Id: I6b0f45327c453abd8a846ec8da0f9676e210d909
This commit is contained in:
Thomas Clark
2014-08-04 15:25:41 -04:00
parent d521eb79b9
commit e73c8d06eb
2 changed files with 21 additions and 14 deletions

View File

@@ -11,14 +11,18 @@
#include "HAL/HAL.hpp" #include "HAL/HAL.hpp"
#include "HAL/cpp/Synchronized.hpp" #include "HAL/cpp/Synchronized.hpp"
#include "Utility.h" #include "Utility.h"
#include <iostream>
// The time, in seconds, at which the 32-bit FPGA timestamp rolls over to 0
static const double kRolloverTime = (1ll << 32) / 1e6;
/** /**
* Pause the task for a specified time. * Pause the task for a specified time.
* *
* Pause the execution of the program for a specified period of time given in seconds. * Pause the execution of the program for a specified period of time given in seconds.
* Motors will continue to run at their last assigned values, and sensors will continue to * Motors will continue to run at their last assigned values, and sensors will continue to
* update. Only the task containing the wait will pause until the wait time is expired. * update. Only the task containing the wait will pause until the wait time is expired.
* *
* @param seconds Length of time to pause, in seconds. * @param seconds Length of time to pause, in seconds.
*/ */
void Wait(double seconds) void Wait(double seconds)
@@ -41,19 +45,19 @@ double GetClock()
* @brief Gives real-time clock system time with nanosecond resolution * @brief Gives real-time clock system time with nanosecond resolution
* @return The time, just in case you want the robot to start autonomous at 8pm on Saturday. * @return The time, just in case you want the robot to start autonomous at 8pm on Saturday.
*/ */
double GetTime() double GetTime()
{ {
struct timespec tp; struct timespec tp;
clock_gettime(CLOCK_REALTIME,&tp); clock_gettime(CLOCK_REALTIME,&tp);
double realTime = (double)tp.tv_sec + (double)((double)tp.tv_nsec*1e-9); double realTime = (double)tp.tv_sec + (double)((double)tp.tv_nsec*1e-9);
return (realTime); return (realTime);
} }
/** /**
* Create a new timer object. * Create a new timer object.
* *
* Create a new timer object and reset the time to zero. The timer is initially not running and * Create a new timer object and reset the time to zero. The timer is initially not running and
* must be started. * must be started.
*/ */
@@ -78,7 +82,7 @@ Timer::~Timer()
* Get the current time from the timer. If the clock is running it is derived from * Get the current time from the timer. If the clock is running it is derived from
* the current system clock the start time stored in the timer class. If the clock * the current system clock the start time stored in the timer class. If the clock
* is not running, then return the time when it was last stopped. * is not running, then return the time when it was last stopped.
* *
* @return unsigned Current time value for this timer in seconds * @return unsigned Current time value for this timer in seconds
*/ */
double Timer::Get() double Timer::Get()
@@ -89,8 +93,13 @@ double Timer::Get()
Synchronized sync(m_semaphore); Synchronized sync(m_semaphore);
if(m_running) if(m_running)
{ {
// This math won't work if the timer rolled over (71 minutes after boot). // If the current time is before the start time, then the FPGA clock
// TODO: Check for it and compensate. // rolled over. Compensate by adding the ~71 minutes that it takes
// to roll over to the current time.
if(currentTime < m_startTime) {
currentTime += kRolloverTime;
}
result = (currentTime - m_startTime) + m_accumulatedTime; result = (currentTime - m_startTime) + m_accumulatedTime;
} }
else else
@@ -103,7 +112,7 @@ double Timer::Get()
/** /**
* Reset the timer by setting the time to 0. * Reset the timer by setting the time to 0.
* *
* Make the timer startTime the current time so new requests will be relative to now * Make the timer startTime the current time so new requests will be relative to now
*/ */
void Timer::Reset() void Timer::Reset()
@@ -141,7 +150,7 @@ void Timer::Stop()
Synchronized sync(m_semaphore); Synchronized sync(m_semaphore);
if (m_running) if (m_running)
{ {
m_accumulatedTime = temp; m_accumulatedTime = temp;
m_running = false; m_running = false;
} }
} }
@@ -169,7 +178,7 @@ bool Timer::HasPeriodPassed(double period)
/* /*
* Return the FPGA system clock time in seconds. * Return the FPGA system clock time in seconds.
* *
* Return the time from the FPGA hardware clock in seconds since the FPGA * Return the time from the FPGA hardware clock in seconds since the FPGA
* started. * started.
* Rolls over after 71 minutes. * Rolls over after 71 minutes.

View File

@@ -230,9 +230,7 @@ void Ultrasonic::SetAutomaticMode(bool enabling)
*/ */
void Ultrasonic::Ping() void Ultrasonic::Ping()
{ {
// TODO: Either assert or disable, not both.
wpi_assert(!m_automaticEnabled); wpi_assert(!m_automaticEnabled);
SetAutomaticMode(false); // turn off automatic round robin if pinging single sensor
m_counter->Reset(); // reset the counter to zero (invalid data now) m_counter->Reset(); // reset the counter to zero (invalid data now)
m_pingChannel->Pulse(kPingTime); // do the ping to start getting a single range m_pingChannel->Pulse(kPingTime); // do the ping to start getting a single range
} }