Remove obsolete timer functions and replace with std::chrono (#64)

Removed delayTicks(), delayMillis(), delaySeconds(), HAL_NO_WAIT, HAL_WAIT_FOREVER,
niTimestamp32(), and niTimestamp64().

Replaced clock_gettime() and usleep() with std::chrono.
This commit is contained in:
Tyler Veness
2016-05-26 20:19:23 -07:00
committed by Peter Johnson
parent 4af0bbddee
commit fa8bb3fa91
14 changed files with 37 additions and 119 deletions

View File

@@ -32,7 +32,6 @@
#include "SerialPort.h"
#include "Solenoid.h"
#include "Task.h"
#include "Utilities.h"
#define HAL_IO_CONFIG_DATA_SIZE 32
#define HAL_SYS_STATUS_DATA_SIZE 44

View File

@@ -1,19 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <stdint.h>
extern "C" {
extern const int32_t HAL_NO_WAIT;
extern const int32_t HAL_WAIT_FOREVER;
void delayTicks(int32_t ticks);
void delayMillis(double ms);
void delaySeconds(double s);
}

View File

@@ -11,6 +11,7 @@
#include <stdio.h>
#include <mutex>
#include <thread>
#include "ChipObject.h"
#include "FRC_NetworkCommunication/LoadOut.h"
@@ -61,7 +62,7 @@ void initializeDigital(int32_t* status) {
// Make sure that the 9403 IONode has had a chance to initialize before
// continuing.
while (pwmSystem->readLoopTiming(status) == 0) delayTicks(1);
while (pwmSystem->readLoopTiming(status) == 0) std::this_thread::yield();
if (pwmSystem->readLoopTiming(status) != kExpectedLoopTiming) {
// TODO: char err[128];

View File

@@ -6,6 +6,7 @@
/*----------------------------------------------------------------------------*/
#include "HAL/HAL.h"
#include "HAL/cpp/priority_mutex.h"
#include <signal.h> // linux for kill
#include <stdlib.h>
@@ -15,6 +16,7 @@
#include <fstream>
#include <iostream>
#include <mutex>
#include <thread>
#include "ChipObject.h"
#include "FRC_NetworkCommunication/CANSessionMux.h"
@@ -343,7 +345,7 @@ int HALInitialize(int mode) {
if (pid >= 2 && kill(pid, 0) == 0 && pid != getpid()) {
std::cout << "Killing previously running FRC program..." << std::endl;
kill(pid, SIGTERM); // try to kill it
delayMillis(100);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (kill(pid, 0) == 0) {
// still not successfull
if (mode == 0) {
@@ -388,6 +390,5 @@ void Occur() {}
void imaqGetErrorText() {}
void imaqGetLastError() {}
void niTimestamp64() {}
} // extern "C"

View File

@@ -1,53 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "HAL/Utilities.h"
#include <time.h>
const int32_t HAL_NO_WAIT = 0;
const int32_t HAL_WAIT_FOREVER = -1;
extern "C" {
void delayTicks(int32_t ticks) {
struct timespec test, remaining;
test.tv_sec = 0;
test.tv_nsec = ticks * 3;
/* Sleep until the requested number of ticks has passed, with additional
time added if nanosleep is interrupted. */
while (nanosleep(&test, &remaining) == -1) {
test = remaining;
}
}
void delayMillis(double ms) {
struct timespec test, remaining;
test.tv_sec = ms / 1000;
test.tv_nsec = 1000 * (((uint64_t)ms) % 1000000);
/* Sleep until the requested number of milliseconds has passed, with
additional time added if nanosleep is interrupted. */
while (nanosleep(&test, &remaining) == -1) {
test = remaining;
}
}
void delaySeconds(double s) {
struct timespec test, remaining;
test.tv_sec = (int)s;
test.tv_nsec = (s - (int)s) * 1000000000.0;
/* Sleep until the requested number of seconds has passed, with additional
time added if nanosleep is interrupted. */
while (nanosleep(&test, &remaining) == -1) {
test = remaining;
}
}
} // extern "C"

View File

@@ -107,7 +107,6 @@
#include "HAL/CanTalonSRX.h"
#include "FRC_NetworkCommunication/CANSessionMux.h" //CAN Comm
#include <string.h> // memset
#include <unistd.h> // usleep
#define STATUS_1 0x02041400
#define STATUS_2 0x02041440

View File

@@ -3,7 +3,6 @@
#include "ctre/CtreCanNode.h"
#include "FRC_NetworkCommunication/CANSessionMux.h"
#include <string.h> // memset
#include <unistd.h> // usleep
static const UINT32 kFullMessageIDMask = 0x1fffffff;

View File

@@ -3,7 +3,6 @@
#include "ctre/PCM.h"
#include "FRC_NetworkCommunication/CANSessionMux.h"
#include <string.h> // memset
#include <unistd.h> // usleep
/* This can be a constant, as long as nobody needs to update solenoids within
1/50 of a second. */
static const INT32 kCANPeriod = 20;

View File

@@ -1,7 +1,6 @@
#include "ctre/PDP.h"
#include "FRC_NetworkCommunication/CANSessionMux.h" //CAN Comm
#include <string.h> // memset
#include <unistd.h> // usleep
#define STATUS_1 0x8041400
#define STATUS_2 0x8041440

View File

@@ -500,7 +500,7 @@ class CANTalon : public MotorSafety,
*/
FeedbackDevice m_feedbackDevice = QuadEncoder;
static const unsigned int kDelayForSolicitedSignalsUs = 4000;
static constexpr unsigned int kDelayForSolicitedSignalsUs = 4000;
/**
* @param devToLookup FeedbackDevice to lookup the scalar for. Because Talon
* allows multiple sensors to be attached simultaneously,

View File

@@ -7,9 +7,9 @@
#include "CANTalon.h"
#include <unistd.h> // usleep
#include <chrono>
#include <sstream>
#include <thread>
#include "HAL/HAL.h"
#include "LiveWindow/LiveWindow.h"
@@ -31,6 +31,8 @@ const double kNativePwdUnitsPerRotation = 4096.0;
*/
const double kMinutesPer100msUnit = 1.0 / 600.0;
constexpr unsigned int CANTalon::kDelayForSolicitedSignalsUs;
/**
* Constructor for the CANTalon device.
*
@@ -386,7 +388,9 @@ double CANTalon::GetP() const {
if (status != CTR_OKAY) {
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
usleep(kDelayForSolicitedSignalsUs); /* small yield for getting response */
// small yield for getting response
std::this_thread::sleep_for(
std::chrono::microseconds(kDelayForSolicitedSignalsUs));
double p;
status = m_impl->GetPgain(m_profile, p);
if (status != CTR_OKAY) {
@@ -407,7 +411,9 @@ double CANTalon::GetI() const {
if (status != CTR_OKAY) {
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
usleep(kDelayForSolicitedSignalsUs); /* small yield for getting response */
// small yield for getting response
std::this_thread::sleep_for(
std::chrono::microseconds(kDelayForSolicitedSignalsUs));
double i;
status = m_impl->GetIgain(m_profile, i);
@@ -429,8 +435,9 @@ double CANTalon::GetD() const {
if (status != CTR_OKAY) {
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
usleep(kDelayForSolicitedSignalsUs); /* small yield for getting response */
// small yield for getting response
std::this_thread::sleep_for(
std::chrono::microseconds(kDelayForSolicitedSignalsUs));
double d;
status = m_impl->GetDgain(m_profile, d);
if (status != CTR_OKAY) {
@@ -452,7 +459,9 @@ double CANTalon::GetF() const {
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
usleep(kDelayForSolicitedSignalsUs); /* small yield for getting response */
// small yield for getting response
std::this_thread::sleep_for(
std::chrono::microseconds(kDelayForSolicitedSignalsUs));
double f;
status = m_impl->GetFgain(m_profile, f);
if (status != CTR_OKAY) {
@@ -473,7 +482,8 @@ int CANTalon::GetIzone() const {
if (status != CTR_OKAY) {
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
usleep(kDelayForSolicitedSignalsUs);
std::this_thread::sleep_for(
std::chrono::microseconds(kDelayForSolicitedSignalsUs));
int iz;
status = m_impl->GetIzone(m_profile, iz);
@@ -1117,7 +1127,8 @@ uint32_t CANTalon::GetFirmwareVersion() const {
if (status != CTR_OKAY) {
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
usleep(kDelayForSolicitedSignalsUs);
std::this_thread::sleep_for(
std::chrono::microseconds(kDelayForSolicitedSignalsUs));
status =
m_impl->GetParamResponseInt32(CanTalonSRX::eFirmVers, firmwareVersion);
if (status != CTR_OKAY) {
@@ -1141,7 +1152,9 @@ int CANTalon::GetIaccum() const {
if (status != CTR_OKAY) {
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
usleep(kDelayForSolicitedSignalsUs); /* small yield for getting response */
// small yield for getting response
std::this_thread::sleep_for(
std::chrono::microseconds(kDelayForSolicitedSignalsUs));
int iaccum;
status = m_impl->GetParamResponseInt32(CanTalonSRX::ePidIaccum, iaccum);
if (status != CTR_OKAY) {
@@ -1519,7 +1532,8 @@ bool CANTalon::GetParameter(uint32_t paramEnum, double& dvalue) const {
retval = false;
}
/* small yield for getting response */
usleep(kDelayForSolicitedSignalsUs);
std::this_thread::sleep_for(
std::chrono::microseconds(kDelayForSolicitedSignalsUs));
/* get the last received update */
status = m_impl->GetParamResponse((CanTalonSRX::param_t)paramEnum, dvalue);
if (status != CTR_OKAY) {

View File

@@ -172,7 +172,6 @@ void IterativeRobot::DisabledPeriodic() {
printf("Default %s() method... Overload me!\n", __FUNCTION__);
firstRun = false;
}
delayTicks(1);
}
/**
@@ -187,7 +186,6 @@ void IterativeRobot::AutonomousPeriodic() {
printf("Default %s() method... Overload me!\n", __FUNCTION__);
firstRun = false;
}
delayTicks(1);
}
/**
@@ -202,7 +200,6 @@ void IterativeRobot::TeleopPeriodic() {
printf("Default %s() method... Overload me!\n", __FUNCTION__);
firstRun = false;
}
delayTicks(1);
}
/**
@@ -217,5 +214,4 @@ void IterativeRobot::TestPeriodic() {
printf("Default %s() method... Overload me!\n", __FUNCTION__);
firstRun = false;
}
delayTicks(1);
}

View File

@@ -7,9 +7,9 @@
#include "Timer.h"
#include <time.h>
#include <chrono>
#include <iostream>
#include <thread>
#include "HAL/HAL.h"
#include "Utility.h"
@@ -25,7 +25,7 @@
*/
void Wait(double seconds) {
if (seconds < 0.0) return;
delaySeconds(seconds);
std::this_thread::sleep_for(std::chrono::duration<double>(seconds));
}
/**
@@ -41,12 +41,9 @@ double GetClock() { return Timer::GetFPGATimestamp(); }
* on Saturday.
*/
double GetTime() {
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
double realTime = (double)tp.tv_sec + (double)((double)tp.tv_nsec * 1e-9);
return (realTime);
using namespace std::chrono;
return duration_cast<duration<double>>(system_clock::now().time_since_epoch())
.count();
}
// for compatibility with msvc12--see C2864
@@ -167,9 +164,3 @@ double Timer::GetFPGATimestamp() {
// Call the helper GetFPGATime() in Utility.cpp
return GetFPGATime() * 1.0e-6;
}
// Internal function that reads the PPC timestamp counter.
extern "C" {
uint32_t niTimestamp32(void);
uint64_t niTimestamp64(void);
}

View File

@@ -7,8 +7,6 @@
#include "Timer.h"
#include <time.h>
#include "Utility.h"
#include "simulation/simTime.h"
@@ -186,9 +184,3 @@ double Timer::GetFPGATimestamp() {
* Not in a match.
*/
double Timer::GetMatchTime() { return Timer::GetFPGATimestamp(); }
// Internal function that reads the PPC timestamp counter.
extern "C" {
uint32_t niTimestamp32(void);
uint64_t niTimestamp64(void);
}