Skip to content

Commit

Permalink
Add names to simulator components (wpilibsuite#1268)
Browse files Browse the repository at this point in the history
Makes configuration easier when you can associate the items with a name
instead of just a port number. Important if there is a GUI added at some
point.
  • Loading branch information
pjreiniger authored and PeterJohnson committed Aug 24, 2018
1 parent 8d8f120 commit de212a9
Show file tree
Hide file tree
Showing 30 changed files with 207 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ ADXL345_I2CData::~ADXL345_I2CData() {
HALSIM_CancelI2CWriteCallback(m_port, m_writeCallbackId);
}

bool ADXL345_I2CData::GetInitialized() const {
return HALSIM_GetI2CInitialized(m_port);
}

void ADXL345_I2CData::ADXL345_I2CData::HandleWrite(const uint8_t* buffer,
uint32_t count) {
m_lastWriteAddress = buffer[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ ADXL345_SpiAccelerometer::~ADXL345_SpiAccelerometer() {
HALSIM_CancelSPIWriteCallback(m_port, m_writeCallbackId);
}

bool ADXL345_SpiAccelerometer::GetInitialized() const {
return HALSIM_GetSPIInitialized(m_port);
}

void ADXL345_SpiAccelerometer::HandleWrite(const uint8_t* buffer,
uint32_t count) {
m_lastWriteAddress = buffer[0] & 0xF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ ADXL362_SpiAccelerometer::~ADXL362_SpiAccelerometer() {
HALSIM_CancelSPIReadCallback(m_port, m_readCallbackId);
HALSIM_CancelSPIWriteCallback(m_port, m_writeCallbackId);
}
bool ADXL362_SpiAccelerometer::GetInitialized() const {
return HALSIM_GetSPIInitialized(m_port);
}

void ADXL362_SpiAccelerometer::HandleWrite(const uint8_t* buffer,
uint32_t count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ ADXRS450_SpiGyroWrapper::~ADXRS450_SpiGyroWrapper() {
HALSIM_CancelSPIReadAutoReceivedDataCallback(m_port,
m_autoReceiveReadCallbackId);
}
bool ADXRS450_SpiGyroWrapper::GetInitialized() const {
return HALSIM_GetSPIInitialized(m_port);
}

void ADXRS450_SpiGyroWrapper::ResetData() {
std::lock_guard<wpi::mutex> lock(m_dataMutex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ADXL345_I2CData : public ThreeAxisAccelerometerData {
explicit ADXL345_I2CData(int port);
virtual ~ADXL345_I2CData();

bool GetInitialized() const override;

void HandleWrite(const uint8_t* buffer, uint32_t count);
void HandleRead(uint8_t* buffer, uint32_t count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ADXL345_SpiAccelerometer : public ThreeAxisAccelerometerData {
explicit ADXL345_SpiAccelerometer(int port);
virtual ~ADXL345_SpiAccelerometer();

bool GetInitialized() const override;

void HandleWrite(const uint8_t* buffer, uint32_t count);
void HandleRead(uint8_t* buffer, uint32_t count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ADXL362_SpiAccelerometer : public ThreeAxisAccelerometerData {
explicit ADXL362_SpiAccelerometer(int port);
virtual ~ADXL362_SpiAccelerometer();

bool GetInitialized() const override;

void HandleWrite(const uint8_t* buffer, uint32_t count);
void HandleRead(uint8_t* buffer, uint32_t count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ADXRS450_SpiGyroWrapper {
explicit ADXRS450_SpiGyroWrapper(int port);
virtual ~ADXRS450_SpiGyroWrapper();

bool GetInitialized() const;

void HandleRead(uint8_t* buffer, uint32_t count);
void HandleAutoReceiveData(uint8_t* buffer, int32_t numToRead,
int32_t& outputCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ThreeAxisAccelerometerData {
ThreeAxisAccelerometerData();
virtual ~ThreeAxisAccelerometerData();

virtual bool GetInitialized() const = 0;

int32_t RegisterXCallback(HAL_NotifyCallback callback, void* param,
HAL_Bool initialNotify);
void CancelXCallback(int32_t uid);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. 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 "lowfisim/SimulatorComponentBase.h"

namespace frc {
namespace sim {
namespace lowfi {

const std::string& SimulatorComponentBase::GetDisplayName() const {
return m_name;
}

void SimulatorComponentBase::SetDisplayName(const std::string& displayName) {
m_name = displayName;
}

} // namespace lowfi
} // namespace sim
} // namespace frc
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,30 @@ namespace lowfi {
ADXLThreeAxisAccelerometerSim::ADXLThreeAxisAccelerometerSim(
hal::ThreeAxisAccelerometerData& accelerometerWrapper)
: m_accelerometerWrapper(accelerometerWrapper),
m_xWrapper(std::function<void(double)>(
m_xWrapper(std::function<bool(void)>(
std::bind(&hal::ThreeAxisAccelerometerData::GetInitialized,
&m_accelerometerWrapper)),
std::function<void(double)>(
std::bind(&hal::ThreeAxisAccelerometerData::SetX,
&m_accelerometerWrapper, std::placeholders::_1)),
std::function<double(void)>(
std::bind(&hal::ThreeAxisAccelerometerData::GetX,
&m_accelerometerWrapper))),

m_yWrapper(std::function<void(double)>(
m_yWrapper(std::function<bool(void)>(
std::bind(&hal::ThreeAxisAccelerometerData::GetInitialized,
&m_accelerometerWrapper)),
std::function<void(double)>(
std::bind(&hal::ThreeAxisAccelerometerData::SetY,
&m_accelerometerWrapper, std::placeholders::_1)),
std::function<double(void)>(
std::bind(&hal::ThreeAxisAccelerometerData::GetY,
&m_accelerometerWrapper))),

m_zWrapper(std::function<void(double)>(
m_zWrapper(std::function<bool(void)>(
std::bind(&hal::ThreeAxisAccelerometerData::GetInitialized,
&m_accelerometerWrapper)),
std::function<void(double)>(
std::bind(&hal::ThreeAxisAccelerometerData::SetZ,
&m_accelerometerWrapper, std::placeholders::_1)),
std::function<double(void)>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ namespace lowfi {
ADXRS450_SpiGyroSim::ADXRS450_SpiGyroSim(int spiPort)
: m_gyroWrapper(spiPort) {}

bool ADXRS450_SpiGyroSim::IsWrapperInitialized() const {
return m_gyroWrapper.GetInitialized();
}

void ADXRS450_SpiGyroSim::SetAngle(double angle) {
m_gyroWrapper.SetAngle(angle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ namespace lowfi {

WpiAnalogGyroSim::WpiAnalogGyroSim(int index) : m_gyroSimulator(index) {}

bool WpiAnalogGyroSim::IsWrapperInitialized() const {
return m_gyroSimulator.GetInitialized();
}

void WpiAnalogGyroSim::SetAngle(double angle) {
m_gyroSimulator.SetAngle(angle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ namespace lowfi {

WpiEncoderSim::WpiEncoderSim(int index) : m_encoderSimulator(index) {}

bool WpiEncoderSim::IsWrapperInitialized() const {
return m_encoderSimulator.GetInitialized();
}

void WpiEncoderSim::SetPosition(double position) {
m_encoderSimulator.SetCount(
static_cast<int>(position / m_encoderSimulator.GetDistancePerPulse()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ void WpiMotorSim::Update(double elapsedTime) {
m_motorModelSimulation.Update(elapsedTime);
}

bool WpiMotorSim::IsWrapperInitialized() const {
return m_pwmSimulator.GetInitialized();
}

double WpiMotorSim::GetPosition() const {
return m_motorModelSimulation.GetPosition();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

#pragma once

#include "lowfisim/SimulatorComponent.h"

namespace frc {
namespace sim {
namespace lowfi {

class AccelerometerSim {
class AccelerometerSim : public virtual SimulatorComponent {
public:
virtual double GetAcceleration() = 0;
virtual void SetAcceleration(double acceleration) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

#pragma once

#include "lowfisim/SimulatorComponent.h"

namespace frc {
namespace sim {
namespace lowfi {

class EncoderSim {
class EncoderSim : public virtual SimulatorComponent {
public:
virtual void SetPosition(double position) = 0;
virtual void SetVelocity(double velocity) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

#pragma once

#include "lowfisim/SimulatorComponent.h"

namespace frc {
namespace sim {
namespace lowfi {

class GyroSim {
class GyroSim : public virtual SimulatorComponent {
public:
virtual void SetAngle(double angle) = 0;
virtual double GetAngle() = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

#pragma once

#include "lowfisim/SimulatorComponent.h"

namespace frc {
namespace sim {
namespace lowfi {

class MotorSim {
class MotorSim : public virtual SimulatorComponent {
public:
virtual double GetPosition() const = 0;
virtual double GetVelocity() const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,35 @@
#include <functional>

#include "lowfisim/AccelerometerSim.h"
#include "lowfisim/SimulatorComponentBase.h"

namespace frc {
namespace sim {
namespace lowfi {

class SimpleAccelerometerSim : public AccelerometerSim {
class SimpleAccelerometerSim : public SimulatorComponentBase,
public AccelerometerSim {
public:
SimpleAccelerometerSim(const std::function<void(double)>& setterFunction,
using SimulatorComponentBase::GetDisplayName;

SimpleAccelerometerSim(const std::function<bool(void)>& initializedFunction,
const std::function<void(double)>& setterFunction,
const std::function<double(void)>& getterFunction)
: m_setAccelerationFunction(setterFunction),
: m_isInitializedFunction(initializedFunction),
m_setAccelerationFunction(setterFunction),
m_getAccelerationFunction(getterFunction) {}
double GetAcceleration() override { return m_getAccelerationFunction(); }

void SetAcceleration(double acceleration) override {
m_setAccelerationFunction(acceleration);
}

bool IsWrapperInitialized() const override {
return m_isInitializedFunction();
}

private:
std::function<bool(void)> m_isInitializedFunction;
std::function<void(double)> m_setAccelerationFunction;
std::function<double(void)> m_getAccelerationFunction;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. 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 <string>

namespace frc {
namespace sim {
namespace lowfi {

class SimulatorComponent {
public:
virtual ~SimulatorComponent() = default;

virtual bool IsWrapperInitialized() const = 0;

virtual const std::string& GetDisplayName() const = 0;

virtual void SetDisplayName(const std::string& displayName) = 0;
};

} // namespace lowfi
} // namespace sim
} // namespace frc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. 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 <string>

#include "lowfisim/SimulatorComponent.h"

namespace frc {
namespace sim {
namespace lowfi {

class SimulatorComponentBase : public virtual SimulatorComponent {
public:
SimulatorComponentBase() = default;
virtual ~SimulatorComponentBase() = default;

const std::string& GetDisplayName() const override;

void SetDisplayName(const std::string& displayName) override;

private:
std::string m_name;
};

} // namespace lowfi
} // namespace sim
} // namespace frc
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@

#include "ADXRS450_SpiGyroWrapperData.h"
#include "lowfisim/GyroSim.h"
#include "lowfisim/SimulatorComponentBase.h"

namespace frc {
namespace sim {
namespace lowfi {

class ADXRS450_SpiGyroSim : public GyroSim {
class ADXRS450_SpiGyroSim : public SimulatorComponentBase, public GyroSim {
public:
explicit ADXRS450_SpiGyroSim(int spiPort);

bool IsWrapperInitialized() const override;

void SetAngle(double angle) override;
double GetAngle() override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
#pragma once

#include "lowfisim/GyroSim.h"
#include "lowfisim/SimulatorComponentBase.h"
#include "simulation/AnalogGyroSim.h"

namespace frc {
namespace sim {
namespace lowfi {

class WpiAnalogGyroSim : public GyroSim {
class WpiAnalogGyroSim : public SimulatorComponentBase, public GyroSim {
public:
explicit WpiAnalogGyroSim(int index);

bool IsWrapperInitialized() const override;

void SetAngle(double angle) override;
double GetAngle() override;

Expand Down
Loading

0 comments on commit de212a9

Please sign in to comment.