mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
Merge "Fixed Simulation C++ API"
This commit is contained in:
@@ -7,9 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "SensorBase.h"
|
||||
#include "PIDSource.h"
|
||||
#include "LiveWindow/LiveWindowSendable.h"
|
||||
#include "GyroBase.h"
|
||||
#include "simulation/SimGyro.h"
|
||||
|
||||
#include <memory>
|
||||
@@ -19,7 +17,7 @@ class AnalogModule;
|
||||
|
||||
/**
|
||||
* Use a rate gyro to return the robots heading relative to a starting position.
|
||||
* The Gyro class tracks the robots heading based on the starting position. As the robot
|
||||
* The AnalogGyro class tracks the robots heading based on the starting position. As the robot
|
||||
* rotates the new heading is computed by integrating the rate of rotation returned
|
||||
* by the sensor. When the class is instantiated, it does a short calibration routine
|
||||
* where it samples the gyro while at rest to determine the default offset. This is
|
||||
@@ -27,7 +25,7 @@ class AnalogModule;
|
||||
* with a channel that is assigned one of the Analog accumulators from the FPGA. See
|
||||
* AnalogInput for the current accumulator assignments.
|
||||
*/
|
||||
class Gyro : public SensorBase, public PIDSource, public LiveWindowSendable
|
||||
class AnalogGyro : public GyroBase
|
||||
{
|
||||
public:
|
||||
static const uint32_t kOversampleBits;
|
||||
@@ -36,25 +34,15 @@ public:
|
||||
static const float kCalibrationSampleTime;
|
||||
static const float kDefaultVoltsPerDegreePerSecond;
|
||||
|
||||
explicit Gyro(uint32_t channel);
|
||||
virtual ~Gyro() = default;
|
||||
virtual float GetAngle() const;
|
||||
virtual double GetRate() const;
|
||||
virtual void Reset();
|
||||
|
||||
// PIDSource interface
|
||||
void SetPIDSourceType(PIDSourceType pidSource) override;
|
||||
double PIDGet() override;
|
||||
|
||||
void UpdateTable() override;
|
||||
void StartLiveWindowMode() override;
|
||||
void StopLiveWindowMode() override;
|
||||
std::string GetSmartDashboardType() const override;
|
||||
void InitTable(std::shared_ptr<ITable> subTable) override;
|
||||
std::shared_ptr<ITable> GetTable() const override;
|
||||
explicit AnalogGyro(uint32_t channel);
|
||||
virtual ~AnalogGyro() = default;
|
||||
float GetAngle() const;
|
||||
void Calibrate() override;
|
||||
double GetRate() const;
|
||||
void Reset();
|
||||
|
||||
private:
|
||||
void InitGyro(int channel);
|
||||
void InitAnalogGyro(int channel);
|
||||
|
||||
SimGyro* impl;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "ErrorBase.h"
|
||||
#include <stdlib.h>
|
||||
#include <memory>
|
||||
#include "MotorSafety.h"
|
||||
#include "MotorSafetyHelper.h"
|
||||
|
||||
@@ -39,11 +40,17 @@ public:
|
||||
uint32_t frontRightMotorChannel, uint32_t rearRightMotorChannel);
|
||||
RobotDrive(SpeedController *leftMotor, SpeedController *rightMotor);
|
||||
RobotDrive(SpeedController &leftMotor, SpeedController &rightMotor);
|
||||
RobotDrive(std::shared_ptr<SpeedController> leftMotor,
|
||||
std::shared_ptr<SpeedController> rightMotor);
|
||||
RobotDrive(SpeedController *frontLeftMotor, SpeedController *rearLeftMotor,
|
||||
SpeedController *frontRightMotor, SpeedController *rearRightMotor);
|
||||
RobotDrive(SpeedController &frontLeftMotor, SpeedController &rearLeftMotor,
|
||||
SpeedController &frontRightMotor, SpeedController &rearRightMotor);
|
||||
virtual ~RobotDrive();
|
||||
RobotDrive(std::shared_ptr<SpeedController> frontLeftMotor,
|
||||
std::shared_ptr<SpeedController> rearLeftMotor,
|
||||
std::shared_ptr<SpeedController> frontRightMotor,
|
||||
std::shared_ptr<SpeedController> rearRightMotor);
|
||||
virtual ~RobotDrive() = default;
|
||||
|
||||
RobotDrive(const RobotDrive&) = delete;
|
||||
RobotDrive& operator=(const RobotDrive&) = delete;
|
||||
@@ -91,10 +98,10 @@ protected:
|
||||
float m_sensitivity = 0.5;
|
||||
double m_maxOutput = 1.0;
|
||||
bool m_deleteSpeedControllers;
|
||||
SpeedController *m_frontLeftMotor = nullptr;
|
||||
SpeedController *m_frontRightMotor = nullptr;
|
||||
SpeedController *m_rearLeftMotor = nullptr;
|
||||
SpeedController *m_rearRightMotor = nullptr;
|
||||
std::shared_ptr<SpeedController> m_frontLeftMotor;
|
||||
std::shared_ptr<SpeedController> m_frontRightMotor;
|
||||
std::shared_ptr<SpeedController> m_rearLeftMotor;
|
||||
std::shared_ptr<SpeedController> m_rearRightMotor;
|
||||
// FIXME: MotorSafetyHelper *m_safetyHelper;
|
||||
|
||||
private:
|
||||
|
||||
@@ -47,7 +47,7 @@
|
||||
#include "Counter.h"
|
||||
#include "DigitalInput.h"
|
||||
#include "Encoder.h"
|
||||
#include "Gyro.h"
|
||||
#include "AnalogGyro.h"
|
||||
#include "GenericHID.h"
|
||||
#include "Joystick.h"
|
||||
#include "PIDController.h"
|
||||
|
||||
@@ -5,16 +5,16 @@
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "Gyro.h"
|
||||
#include "AnalogGyro.h"
|
||||
#include "Timer.h"
|
||||
#include "WPIErrors.h"
|
||||
#include "LiveWindow/LiveWindow.h"
|
||||
|
||||
const uint32_t Gyro::kOversampleBits = 10;
|
||||
const uint32_t Gyro::kAverageBits = 0;
|
||||
const float Gyro::kSamplesPerSecond = 50.0;
|
||||
const float Gyro::kCalibrationSampleTime = 5.0;
|
||||
const float Gyro::kDefaultVoltsPerDegreePerSecond = 0.007;
|
||||
const uint32_t AnalogGyro::kOversampleBits = 10;
|
||||
const uint32_t AnalogGyro::kAverageBits = 0;
|
||||
const float AnalogGyro::kSamplesPerSecond = 50.0;
|
||||
const float AnalogGyro::kCalibrationSampleTime = 5.0;
|
||||
const float AnalogGyro::kDefaultVoltsPerDegreePerSecond = 0.007;
|
||||
|
||||
/**
|
||||
* Initialize the gyro.
|
||||
@@ -24,7 +24,7 @@ const float Gyro::kDefaultVoltsPerDegreePerSecond = 0.007;
|
||||
* in progress, this is typically done when the robot is first turned on while it's sitting at
|
||||
* rest before the competition starts.
|
||||
*/
|
||||
void Gyro::InitGyro(int channel)
|
||||
void AnalogGyro::InitAnalogGyro(int channel)
|
||||
{
|
||||
SetPIDSourceType(PIDSourceType::kDisplacement);
|
||||
|
||||
@@ -32,17 +32,17 @@ void Gyro::InitGyro(int channel)
|
||||
int n = sprintf(buffer, "analog/%d", channel);
|
||||
impl = new SimGyro(buffer);
|
||||
|
||||
LiveWindow::GetInstance()->AddSensor("Gyro", channel, this);
|
||||
LiveWindow::GetInstance()->AddSensor("AnalogGyro", channel, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gyro constructor with only a channel..
|
||||
* AnalogGyro constructor with only a channel..
|
||||
*
|
||||
* @param channel The analog channel the gyro is connected to.
|
||||
*/
|
||||
Gyro::Gyro(uint32_t channel)
|
||||
AnalogGyro::AnalogGyro(uint32_t channel)
|
||||
{
|
||||
InitGyro(channel);
|
||||
InitAnalogGyro(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,11 +50,15 @@ Gyro::Gyro(uint32_t channel)
|
||||
* Resets the gyro to a heading of zero. This can be used if there is significant
|
||||
* drift in the gyro and it needs to be recalibrated after it has been running.
|
||||
*/
|
||||
void Gyro::Reset()
|
||||
void AnalogGyro::Reset()
|
||||
{
|
||||
impl->Reset();
|
||||
}
|
||||
|
||||
void AnalogGyro::Calibrate(){
|
||||
Reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the actual angle in degrees that the robot is currently facing.
|
||||
*
|
||||
@@ -66,7 +70,7 @@ void Gyro::Reset()
|
||||
* @return the current heading of the robot in degrees. This heading is based on integration
|
||||
* of the returned rate from the gyro.
|
||||
*/
|
||||
float Gyro::GetAngle() const
|
||||
float AnalogGyro::GetAngle() const
|
||||
{
|
||||
return impl->GetAngle();
|
||||
}
|
||||
@@ -79,56 +83,7 @@ float Gyro::GetAngle() const
|
||||
*
|
||||
* @return the current rate in degrees per second
|
||||
*/
|
||||
double Gyro::GetRate() const
|
||||
double AnalogGyro::GetRate() const
|
||||
{
|
||||
return impl->GetVelocity();
|
||||
}
|
||||
|
||||
void Gyro::SetPIDSourceType(PIDSourceType pidSource)
|
||||
{
|
||||
m_pidSource = pidSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the angle in degrees for the PIDSource base object.
|
||||
*
|
||||
* @return The angle in degrees.
|
||||
*/
|
||||
double Gyro::PIDGet()
|
||||
{
|
||||
switch(GetPIDSourceType()){
|
||||
case PIDSourceType::kRate:
|
||||
return GetRate();
|
||||
case PIDSourceType::kDisplacement:
|
||||
return GetAngle();
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Gyro::UpdateTable() {
|
||||
if (m_table != nullptr) {
|
||||
m_table->PutNumber("Value", GetAngle());
|
||||
}
|
||||
}
|
||||
|
||||
void Gyro::StartLiveWindowMode() {
|
||||
|
||||
}
|
||||
|
||||
void Gyro::StopLiveWindowMode() {
|
||||
|
||||
}
|
||||
|
||||
std::string Gyro::GetSmartDashboardType() const {
|
||||
return "Gyro";
|
||||
}
|
||||
|
||||
void Gyro::InitTable(std::shared_ptr<ITable> subTable) {
|
||||
m_table = subTable;
|
||||
UpdateTable();
|
||||
}
|
||||
|
||||
std::shared_ptr<ITable> Gyro::GetTable() const {
|
||||
return m_table;
|
||||
}
|
||||
@@ -6,11 +6,10 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "RobotDrive.h"
|
||||
|
||||
//#include "CANJaguar.h"
|
||||
#include "GenericHID.h"
|
||||
#include "Joystick.h"
|
||||
#include "Jaguar.h"
|
||||
#include "Talon.h"
|
||||
#include "Utility.h"
|
||||
#include "WPIErrors.h"
|
||||
#include <math.h>
|
||||
@@ -20,6 +19,10 @@
|
||||
|
||||
const int32_t RobotDrive::kMaxNumberOfMotors;
|
||||
|
||||
static auto make_shared_nodelete(SpeedController *ptr) {
|
||||
return std::shared_ptr<SpeedController>(ptr, NullDeleter<SpeedController>());
|
||||
}
|
||||
|
||||
/*
|
||||
* Driving functions
|
||||
* These functions provide an interface to multiple motors that is used for C programming
|
||||
@@ -40,15 +43,16 @@ void RobotDrive::InitRobotDrive() {
|
||||
/** Constructor for RobotDrive with 2 motors specified with channel numbers.
|
||||
* Set up parameters for a two wheel drive system where the
|
||||
* left and right motor pwm channels are specified in the call.
|
||||
* This call assumes Jaguars for controlling the motors.
|
||||
* This call assumes Talosn for controlling the motors.
|
||||
* @param leftMotorChannel The PWM channel number that drives the left motor.
|
||||
* @param rightMotorChannel The PWM channel number that drives the right motor.
|
||||
*/
|
||||
RobotDrive::RobotDrive(uint32_t leftMotorChannel, uint32_t rightMotorChannel)
|
||||
{
|
||||
InitRobotDrive();
|
||||
m_rearLeftMotor = new Jaguar(leftMotorChannel);
|
||||
m_rearRightMotor = new Jaguar(rightMotorChannel);
|
||||
m_rearLeftMotor = std::make_shared<Talon>(leftMotorChannel);
|
||||
m_rearRightMotor = std::make_shared<Talon>(rightMotorChannel);
|
||||
|
||||
for (int32_t i=0; i < kMaxNumberOfMotors; i++)
|
||||
{
|
||||
m_invertedMotors[i] = 1;
|
||||
@@ -61,7 +65,7 @@ RobotDrive::RobotDrive(uint32_t leftMotorChannel, uint32_t rightMotorChannel)
|
||||
* Constructor for RobotDrive with 4 motors specified with channel numbers.
|
||||
* Set up parameters for a four wheel drive system where all four motor
|
||||
* pwm channels are specified in the call.
|
||||
* This call assumes Jaguars for controlling the motors.
|
||||
* This call assumes Talons for controlling the motors.
|
||||
* @param frontLeftMotor Front left motor channel number
|
||||
* @param rearLeftMotor Rear Left motor channel number
|
||||
* @param frontRightMotor Front right motor channel number
|
||||
@@ -71,10 +75,10 @@ RobotDrive::RobotDrive(uint32_t frontLeftMotor, uint32_t rearLeftMotor,
|
||||
uint32_t frontRightMotor, uint32_t rearRightMotor)
|
||||
{
|
||||
InitRobotDrive();
|
||||
m_rearLeftMotor = new Jaguar(rearLeftMotor);
|
||||
m_rearRightMotor = new Jaguar(rearRightMotor);
|
||||
m_frontLeftMotor = new Jaguar(frontLeftMotor);
|
||||
m_frontRightMotor = new Jaguar(frontRightMotor);
|
||||
m_rearLeftMotor = std::make_shared<Talon>(rearLeftMotor);
|
||||
m_rearRightMotor = std::make_shared<Talon>(rearRightMotor);
|
||||
m_frontLeftMotor = std::make_shared<Talon>(frontLeftMotor);
|
||||
m_frontRightMotor = std::make_shared<Talon>(frontRightMotor);
|
||||
for (int32_t i=0; i < kMaxNumberOfMotors; i++)
|
||||
{
|
||||
m_invertedMotors[i] = 1;
|
||||
@@ -91,34 +95,36 @@ RobotDrive::RobotDrive(uint32_t frontLeftMotor, uint32_t rearLeftMotor,
|
||||
* @param leftMotor The left SpeedController object used to drive the robot.
|
||||
* @param rightMotor the right SpeedController object used to drive the robot.
|
||||
*/
|
||||
RobotDrive::RobotDrive(SpeedController *leftMotor, SpeedController *rightMotor)
|
||||
{
|
||||
RobotDrive::RobotDrive(SpeedController *leftMotor,
|
||||
SpeedController *rightMotor) {
|
||||
InitRobotDrive();
|
||||
if (leftMotor == nullptr || rightMotor == nullptr)
|
||||
{
|
||||
if (leftMotor == nullptr || rightMotor == nullptr) {
|
||||
wpi_setWPIError(NullParameter);
|
||||
m_rearLeftMotor = m_rearRightMotor = nullptr;
|
||||
return;
|
||||
}
|
||||
m_rearLeftMotor = leftMotor;
|
||||
m_rearRightMotor = rightMotor;
|
||||
for (int32_t i=0; i < kMaxNumberOfMotors; i++)
|
||||
{
|
||||
m_invertedMotors[i] = 1;
|
||||
}
|
||||
m_deleteSpeedControllers = false;
|
||||
m_rearLeftMotor = make_shared_nodelete(leftMotor);
|
||||
m_rearRightMotor = make_shared_nodelete(rightMotor);
|
||||
}
|
||||
|
||||
RobotDrive::RobotDrive(SpeedController &leftMotor, SpeedController &rightMotor)
|
||||
{
|
||||
//TODO: Change to rvalue references & move syntax.
|
||||
RobotDrive::RobotDrive(SpeedController &leftMotor,
|
||||
SpeedController &rightMotor) {
|
||||
InitRobotDrive();
|
||||
m_rearLeftMotor = &leftMotor;
|
||||
m_rearRightMotor = &rightMotor;
|
||||
for (int32_t i=0; i < kMaxNumberOfMotors; i++)
|
||||
{
|
||||
m_invertedMotors[i] = 1;
|
||||
m_rearLeftMotor = make_shared_nodelete(&leftMotor);
|
||||
m_rearRightMotor = make_shared_nodelete(&rightMotor);
|
||||
}
|
||||
|
||||
RobotDrive::RobotDrive(std::shared_ptr<SpeedController> leftMotor,
|
||||
std::shared_ptr<SpeedController> rightMotor) {
|
||||
InitRobotDrive();
|
||||
if (leftMotor == nullptr || rightMotor == nullptr) {
|
||||
wpi_setWPIError(NullParameter);
|
||||
m_rearLeftMotor = m_rearRightMotor = nullptr;
|
||||
return;
|
||||
}
|
||||
m_deleteSpeedControllers = false;
|
||||
m_rearLeftMotor = leftMotor;
|
||||
m_rearRightMotor = rightMotor;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,12 +135,40 @@ RobotDrive::RobotDrive(SpeedController &leftMotor, SpeedController &rightMotor)
|
||||
* @param rearRightMotor The back right SpeedController object used to drive the robot.
|
||||
* @param frontRightMotor The front right SpeedController object used to drive the robot.
|
||||
*/
|
||||
RobotDrive::RobotDrive(SpeedController *frontLeftMotor, SpeedController *rearLeftMotor,
|
||||
SpeedController *frontRightMotor, SpeedController *rearRightMotor)
|
||||
{
|
||||
RobotDrive::RobotDrive(SpeedController *frontLeftMotor,
|
||||
SpeedController *rearLeftMotor,
|
||||
SpeedController *frontRightMotor,
|
||||
SpeedController *rearRightMotor) {
|
||||
InitRobotDrive();
|
||||
if (frontLeftMotor == nullptr || rearLeftMotor == nullptr || frontRightMotor == nullptr || rearRightMotor == nullptr)
|
||||
{
|
||||
if (frontLeftMotor == nullptr || rearLeftMotor == nullptr ||
|
||||
frontRightMotor == nullptr || rearRightMotor == nullptr) {
|
||||
wpi_setWPIError(NullParameter);
|
||||
return;
|
||||
}
|
||||
m_frontLeftMotor = make_shared_nodelete(frontLeftMotor);
|
||||
m_rearLeftMotor = make_shared_nodelete(rearLeftMotor);
|
||||
m_frontRightMotor = make_shared_nodelete(frontRightMotor);
|
||||
m_rearRightMotor = make_shared_nodelete(rearRightMotor);
|
||||
}
|
||||
|
||||
RobotDrive::RobotDrive(SpeedController &frontLeftMotor,
|
||||
SpeedController &rearLeftMotor,
|
||||
SpeedController &frontRightMotor,
|
||||
SpeedController &rearRightMotor) {
|
||||
InitRobotDrive();
|
||||
m_frontLeftMotor = make_shared_nodelete(&frontLeftMotor);
|
||||
m_rearLeftMotor = make_shared_nodelete(&rearLeftMotor);
|
||||
m_frontRightMotor = make_shared_nodelete(&frontRightMotor);
|
||||
m_rearRightMotor = make_shared_nodelete(&rearRightMotor);
|
||||
}
|
||||
|
||||
RobotDrive::RobotDrive(std::shared_ptr<SpeedController> frontLeftMotor,
|
||||
std::shared_ptr<SpeedController> rearLeftMotor,
|
||||
std::shared_ptr<SpeedController> frontRightMotor,
|
||||
std::shared_ptr<SpeedController> rearRightMotor) {
|
||||
InitRobotDrive();
|
||||
if (frontLeftMotor == nullptr || rearLeftMotor == nullptr ||
|
||||
frontRightMotor == nullptr || rearRightMotor == nullptr) {
|
||||
wpi_setWPIError(NullParameter);
|
||||
return;
|
||||
}
|
||||
@@ -142,42 +176,6 @@ RobotDrive::RobotDrive(SpeedController *frontLeftMotor, SpeedController *rearLef
|
||||
m_rearLeftMotor = rearLeftMotor;
|
||||
m_frontRightMotor = frontRightMotor;
|
||||
m_rearRightMotor = rearRightMotor;
|
||||
for (int32_t i=0; i < kMaxNumberOfMotors; i++)
|
||||
{
|
||||
m_invertedMotors[i] = 1;
|
||||
}
|
||||
m_deleteSpeedControllers = false;
|
||||
}
|
||||
|
||||
RobotDrive::RobotDrive(SpeedController &frontLeftMotor, SpeedController &rearLeftMotor,
|
||||
SpeedController &frontRightMotor, SpeedController &rearRightMotor)
|
||||
{
|
||||
InitRobotDrive();
|
||||
m_frontLeftMotor = &frontLeftMotor;
|
||||
m_rearLeftMotor = &rearLeftMotor;
|
||||
m_frontRightMotor = &frontRightMotor;
|
||||
m_rearRightMotor = &rearRightMotor;
|
||||
for (int32_t i=0; i < kMaxNumberOfMotors; i++)
|
||||
{
|
||||
m_invertedMotors[i] = 1;
|
||||
}
|
||||
m_deleteSpeedControllers = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* RobotDrive destructor.
|
||||
* Deletes motor objects that were not passed in and created internally only.
|
||||
**/
|
||||
RobotDrive::~RobotDrive()
|
||||
{
|
||||
if (m_deleteSpeedControllers)
|
||||
{
|
||||
delete m_frontLeftMotor;
|
||||
delete m_rearLeftMotor;
|
||||
delete m_frontRightMotor;
|
||||
delete m_rearRightMotor;
|
||||
}
|
||||
// FIXME: delete m_safetyHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user